From 0cc787e8f930b3cc044ad8cde04539d5e3356e38 Mon Sep 17 00:00:00 2001 From: astillwell Date: Tue, 10 Mar 2015 03:54:06 -0500 Subject: [PATCH] new file: Exercise1.py new file: Exercise2.py new file: Exercise3.py new file: __pycache__/cards.cpython-32.pyc new file: __pycache__/games.cpython-32.pyc --- Exercise1.py | 201 +++++++++++++++++++++++++++++ Exercise2.py | 106 +++++++++++++++ Exercise3.py | 214 +++++++++++++++++++++++++++++++ __pycache__/cards.cpython-32.pyc | Bin 0 -> 3872 bytes __pycache__/games.cpython-32.pyc | Bin 0 -> 1650 bytes 5 files changed, 521 insertions(+) create mode 100644 Exercise1.py create mode 100644 Exercise2.py create mode 100644 Exercise3.py create mode 100644 __pycache__/cards.cpython-32.pyc create mode 100644 __pycache__/games.cpython-32.pyc diff --git a/Exercise1.py b/Exercise1.py new file mode 100644 index 0000000..27f3045 --- /dev/null +++ b/Exercise1.py @@ -0,0 +1,201 @@ +# Exercise1 Chapter 9 +# Author: Alton Stillwell +# Date: 3/4/15 +############### +# Blackjack v1.1 +# From 1 to 7 players compete against a dealer +# New Update added error checking and deck repopulating +##################################### +import cards, games +########################################## +class BJ_Card(cards.Card): + """ A Blackjack Card. """ + ACE_VALUE = 1 + + @property + def value(self): + if self.is_face_up: + v = BJ_Card.RANKS.index(self.rank) + 1 + if v > 10: + v = 10 + else: + v = None + return v +########################################## +class BJ_Deck(cards.Deck): + """ A Blackjack Deck. """ + def populate(self): + for suit in BJ_Card.SUITS: + for rank in BJ_Card.RANKS: + self.cards.append(BJ_Card(rank, suit)) +########################################## +class BJ_Hand(cards.Hand): + """ A Blackjack Hand. """ + def __init__(self, name): + super(BJ_Hand, self).__init__() + self.name = name + + def __str__(self): + rep = self.name + ":\t" + super(BJ_Hand, self).__str__() + if self.total: + rep += "(" + str(self.total) + ")" + return rep + + @property + def total(self): + # if a card in the hand has value of None, then total is None + for card in self.cards: + if not card.value: + return None + + # add up card values, treat each Ace as 1 + t = 0 + for card in self.cards: + t += card.value + + # determine if hand contains an Ace + contains_ace = False + for card in self.cards: + if card.value == BJ_Card.ACE_VALUE: + contains_ace = True + + # if hand contains Ace and total is low enough, treat Ace as 11 + if contains_ace and t <= 11: + # add only 10 since we've already added 1 for the Ace + t += 10 + + return t + + def is_busted(self): + return self.total > 21 +################################################## +class BJ_Player(BJ_Hand): + """ A Blackjack Player. """ + def is_hitting(self): + response = games.ask_yes_no("\n" + self.name + ", do you want a hit? (Y/N): ") + return response == "y" + + def bust(self): + print(self.name, "busts.") + self.lose() + + def lose(self): + print(self.name, "loses.") + + def win(self): + print(self.name, "wins.") + + def push(self): + print(self.name, "pushes.") +############################################# +class BJ_Dealer(BJ_Hand): + """ A Blackjack Dealer. """ + def is_hitting(self): + return self.total < 17 + + def bust(self): + print(self.name, "busts.") + + def flip_first_card(self): + first_card = self.cards[0] + first_card.flip() +######################################### +class BJ_Game(object): + """ A Blackjack Game. """ + def __init__(self, names): + self.players = [] + for name in names: + player = BJ_Player(name) + self.players.append(player) + + self.dealer = BJ_Dealer("Dealer") + + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() + + @property + def still_playing(self): + sp = [] + for player in self.players: + if not player.is_busted(): + sp.append(player) + return sp + + def __additional_cards(self, player): + while not player.is_busted() and player.is_hitting(): + self.deck.deal([player]) + print(player) + if player.is_busted(): + player.bust() + + def play(self): + # deal initial 2 cards to everyone + self.deck.deal(self.players + [self.dealer], per_hand = 2) + self.dealer.flip_first_card() # hide dealer's first card + for player in self.players: + print(player) + print(self.dealer) + + # deal additional cards to players + for player in self.players: + self.__additional_cards(player) + + self.dealer.flip_first_card() # reveal dealer's first + + if not self.still_playing: + # since all players have busted, just show the dealer's hand + print(self.dealer) + else: + # deal additional cards to dealer + print(self.dealer) + self.__additional_cards(self.dealer) + + if self.dealer.is_busted(): + # everyone still playing wins + for player in self.still_playing: + player.win() + else: + # compare each player still playing to dealer + for player in self.still_playing: + if player.total > self.dealer.total: + player.win() + elif player.total < self.dealer.total: + player.lose() + else: + player.push() + + # remove everyone's cards + for player in self.players: + player.clear() + self.dealer.clear() + + # resets the deck + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() +############################################# +def main(): + print("\t\tWelcome to Blackjack!\n") + + names = [] + number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8) + for i in range(number): + name = input("Enter player name: ") + while name == "": + name = input("Enter different player name: ") + names.append(name) + print() + + game = BJ_Game(names) + + again = None + while again != "n": + game.play() + again = games.ask_yes_no("\nDo you want to play again?: ") +############################################# +main() +input("\n\nPress the enter key to exit.") + + + diff --git a/Exercise2.py b/Exercise2.py new file mode 100644 index 0000000..1182afa --- /dev/null +++ b/Exercise2.py @@ -0,0 +1,106 @@ +# Exercise2 Chapter 9 +# Author: Alton Stillwell +# Date: 3/4/15 +####################### +# One-Card War +# Each player will get a single card, +# player with highest value card wins +####################### +import cards, games +####################### +class CW_Card(cards.Card): + ACE_VALUE = 1 + @property + def value(self): + v = CW_Card.RANKS.index(self.rank) + 1 + if v > 10: + v = 10 + return v +####################### +class CW_Hand(cards.Hand): + def __init__(self,name): + super(CW_Hand,self).__init__() + self.name = name + def __str__(self): + rep = self.name + ":\t" + super(CW_Hand,self).__str__() + if self.total: + rep += "(" + str(self.total) + ")" + return rep + @property + def total(self): + for card in self.cards: + if not card.value: + return None + t = 0 + for card in self.cards: + t += card.value + contains_ace = False + for card in self.cards: + if card.value == CW_Card.ACE_VALUE: + contains_ace = True + if contains_ace: + t = 11 + return t +####################### +class CW_Deck(cards.Deck): + def populate(self): + for suit in CW_Card.SUITS: + for rank in CW_Card.RANKS: + self.cards.append(CW_Card(rank,suit)) +####################### +class CW_Player(CW_Hand): + def lose(self): + print(self.name,"loses.") + def win(self): + print(self.name,"wins.") + def push(self): + print(self.name,"pushes.") +#################### +class CW_Game(object): + def __init__(self,names): + self.players = [] + for name in names: + player = CW_Player(name) + self.players.append(player) + self.deck = CW_Deck() + self.deck.populate() + self.deck.shuffle() + def play(self): + self.deck.deal(self.players, per_hand = 1) + for player in self.players: + print(player) + # finds top score + topPlayerScore = 0 + for player in self.players: + v = player.total + if v > topPlayerScore: + topPlayerScore = v + topPlayer = player + print("~~~~~~~~~~~~~~~") + print(topPlayer,"is the winner!") + print("With a final score of:",topPlayerScore) + # resets the game + for player in self.players: + player.clear() + self.deck = CW_Deck() + self.deck.populate() + self.deck.shuffle() +####################### +def main(): + print("\t\tWelcome to One-Card War!\n") + names = [] + number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8) + for i in range(number): + name = input("Enter player name: ") + while name == "": + name = input("Enter different player name: ") + names.append(name) + print() + game = CW_Game(names) + again = None + while again != "n": + game.play() + again = games.ask_yes_no("\nDo you want to play again?: ") +######################################### +main() +input("Press to exit.") diff --git a/Exercise3.py b/Exercise3.py new file mode 100644 index 0000000..576a63d --- /dev/null +++ b/Exercise3.py @@ -0,0 +1,214 @@ +# Exercise3, Chapter 9 +# Author: Alton Stillwell +# Date: 3/4/15 +############### +# Blackjack v1.2 +# From 1 to 7 players compete against a dealer +# New Update added the ability to bet +##################################### +import cards, games +########################################## +class BJ_Card(cards.Card): + """ A Blackjack Card. """ + ACE_VALUE = 1 + + @property + def value(self): + if self.is_face_up: + v = BJ_Card.RANKS.index(self.rank) + 1 + if v > 10: + v = 10 + else: + v = None + return v +########################################## +class BJ_Deck(cards.Deck): + """ A Blackjack Deck. """ + def populate(self): + for suit in BJ_Card.SUITS: + for rank in BJ_Card.RANKS: + self.cards.append(BJ_Card(rank, suit)) +########################################## +class BJ_Hand(cards.Hand): + """ A Blackjack Hand. """ + def __init__(self, name): + super(BJ_Hand, self).__init__() + self.name = name + + def __str__(self): + rep = self.name + ":\t" + super(BJ_Hand, self).__str__() + if self.total: + rep += "(" + str(self.total) + ")" + return rep + + @property + def total(self): + # if a card in the hand has value of None, then total is None + for card in self.cards: + if not card.value: + return None + + # add up card values, treat each Ace as 1 + t = 0 + for card in self.cards: + t += card.value + + # determine if hand contains an Ace + contains_ace = False + for card in self.cards: + if card.value == BJ_Card.ACE_VALUE: + contains_ace = True + + # if hand contains Ace and total is low enough, treat Ace as 11 + if contains_ace and t <= 11: + # add only 10 since we've already added 1 for the Ace + t += 10 + + return t + + def is_busted(self): + return self.total > 21 +################################################## +class BJ_Player(BJ_Hand): + """ A Blackjack Player. """ + def is_hitting(self): + response = games.ask_yes_no("\n" + self.name + ", do you want a hit? (Y/N): ") + return response == "y" + + def bust(self): + print(self.name, "busts.") + self.lose() + + def lose(self): + print(self.name, "loses.") + + def win(self): + print(self.name, "wins.") + self.bankroll += (self.bet * 2) + def push(self): + print(self.name, "pushes.") + self.bankroll += (self.bet) +############################################# +class BJ_Dealer(BJ_Hand): + """ A Blackjack Dealer. """ + def is_hitting(self): + return self.total < 17 + + def bust(self): + print(self.name, "busts.") + + def flip_first_card(self): + first_card = self.cards[0] + first_card.flip() +######################################### +class BJ_Game(object): + """ A Blackjack Game. """ + def __init__(self, names, startingBankroll): + self.players = [] + for name in names: + player = BJ_Player(name) + self.players.append(player) + player.bankroll = startingBankroll + self.dealer = BJ_Dealer("Dealer") + + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() + + @property + def still_playing(self): + sp = [] + for player in self.players: + if not player.is_busted(): + sp.append(player) + return sp + + def __additional_cards(self, player): + while not player.is_busted() and player.is_hitting(): + self.deck.deal([player]) + print(player) + if player.is_busted(): + player.bust() + + def play(self): + # place bets + for player in self.players: + print("Player", player,", has",player.bankroll,"chips left!") + bet = games.ask_number("How much do you want to bet? (1 - 40): ", low = 1, high = 41) + temp = player.bankroll - bet + while temp < 0: + print("Invalid amount!") + bet = games.ask_number("How much do you want to bet? (1 - 40): ", low = 1, high = 41) + temp = player.bankroll - bet + player.bet = bet + player.bankroll = temp + # deal initial 2 cards to everyone + self.deck.deal(self.players + [self.dealer], per_hand = 2) + self.dealer.flip_first_card() # hide dealer's first card + for player in self.players: + print(player) + print(self.dealer) + + # deal additional cards to players + for player in self.players: + self.__additional_cards(player) + + self.dealer.flip_first_card() # reveal dealer's first + + if not self.still_playing: + # since all players have busted, just show the dealer's hand + print(self.dealer) + else: + # deal additional cards to dealer + print(self.dealer) + self.__additional_cards(self.dealer) + + if self.dealer.is_busted(): + # everyone still playing wins + for player in self.still_playing: + player.win() + else: + # compare each player still playing to dealer + for player in self.still_playing: + if player.total > self.dealer.total: + player.win() + elif player.total < self.dealer.total: + player.lose() + else: + player.push() + + # remove everyone's cards + for player in self.players: + player.clear() + self.dealer.clear() + # remove players if they have no chips + for player in self.players: + if player.bankroll == 0: + self.players.remove(player) + # resets the deck + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() +############################################# +def main(): + print("\t\tWelcome to Blackjack!\n") + + names = [] + number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8) + startingBankroll = games.ask_number("What is the player's starting bankroll? (10 - 1000): ", low = 10, high = 1001) + for i in range(number): + name = input("Enter player name: ") + while name == "": + name = input("Enter different player name: ") + names.append(name) + print() + + game = BJ_Game(names, startingBankroll) + + again = None + while again != "n": + game.play() + again = games.ask_yes_no("\nDo you want to play again?: ") +############################################# +main() +input("\n\nPress the enter key to exit.") diff --git a/__pycache__/cards.cpython-32.pyc b/__pycache__/cards.cpython-32.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d68cf49c93d4a802d50cccf79a180901654cf468 GIT binary patch literal 3872 zcmcgvYj4~{6usU@(p{3i(swIxL81{7NgqI4s4CI~(TB7=64WXpGG=Fzb>i60+HMo6 z`PBY0eiWZM=i2MtECt$Cg_F6yGoG3IJ~JE6%(h?s@#k7sMxF-vZ_(1;+VW!hps5HllYR<>qk9dnM0 zIU(kxm{YRVkaa9MC+4)gc4Qq3=cw{JmY-1uo1BF{=OHye1>!EbN$4L0(U#lw`)0w- zsoj#&n@Vpf{X*%NO21NiTj?F8ck>!n-MFru$4Y-v`b240sZrWfnsyslz=v-xUPenl z0(t4sA0C*RGU(Qofky|Kz8~!v)zJ+%glgQt-o~L3xZ#Oo1$ZlbniM!yNnEdln=gF#atG_&2TUFn! z^>ce9u;nUsht8m2O~P{fFz)(c>Uk5|v5ag|R~&~%ZD-0k>s)Y7*JhfnX%)xsRW{La zY!A@VB@noK964U2+<^*;UIc@>);&-}!Hba4)Db|!6cUY2$uEJpOK#7Pj2mwrL40Ygq!c%JCWU*%U4T|hC_%hy=PRw&|zszqdAZ3AlT~^cn zLOHUsaY+sVDUM{QF#x!N?D^ZMxV?+=l;u~i%8xsCau89tYH1eSv%5)laK9+D%EFu) zAU%eV#H`j))St$Xo|qs#Mhfvm%9x=(D8wn}q;vTni}8Poi`pMrzkiLY6-C(97y5ix ziE;*=A}HGmZg9L~hWWyIT7;S}IWg2v5*wK@Kx0TIbR;9>8}k`n15G)^hwn<$tLTht zicA4OBWuRg=E5|K(ldu@4YhI!6NRfs-v>d@oGNkrLiP8_}4a0A|UCP*2EJBgD#^fP-4e0ftj ziNvWqO)s;lYpfqGxIg3> zGf?5~YH>xSj_};ph_kA?Q~xFXAj*nFrloqqa1kLS)v8ZQKv4kg$2rG2l{0ig7AM|> z_{n{!`#p*-PN6wx7AMgxE`^;!tpZX0`{Fg4ibv99Amlyb%nFMA26d)T#hGTSp z;O^K1H;Y}nA7l%i;)2CQjDO#@-E0T}G?-X7u2AVihF8O`2@pg{o(=0nxp(@Za<4Gy UKBcDoEird??!w%;8K>?11+0+pBLDyZ literal 0 HcmV?d00001 diff --git a/__pycache__/games.cpython-32.pyc b/__pycache__/games.cpython-32.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b71fd89ee364b6672676c13d43aac4d58c5f8366 GIT binary patch literal 1650 zcmb_cOK%e~5FYQQNfQ+~AX?ynEI2d;DLHW9A%rLuf&+>w6$#OcwavQCHXHADy;d8k zJ(XX^kK)WXcABJ8FRfU`6FrfBya$($d$!{1b-#4)HZ5`c0Gy(nOar z?F&jH-r0{Rjr}Ah-JrBVi%KT)D4FP<%y2@(m?b{`XFod_ zmM@(PcBL?h{KWg)>vBAUE^E35gn=o?j_k0gbeFC8eXH}sn-p+PT^8;IM1NFFbU(3m zmglEB&-**0q^xzN-t{>~+bgGz(NHQgS*=tPbIWc?w8b1E4u&23(4S#~xQ~4?Hfo>5 zam4s;Two!9FY6B+%lGX?#6A-?W9w=uqj{!2BR;Qm=`*-p8ZF#Z3bvK%u#=F*Q!xl- z3R!XtN;y_gYEq;w=ViE>7DLRJ(U={0o4ziyi+M3ja;wxGqGW(~Jyke9%gc^vinWGY zoZO6iAF5zFFg| zn4V+0O}#WT)-CNUiZ>`spkZc8SNrni#i_0WQ5m!=lODlJ+p;iL-vJ@6O=8Ci!75YS z$I7yKikjkqSQqntV*GgQVA!`1cpENaI(tRuvBv~t1R#+|={406)iDznK;Z{Xk5W9N ztCO(6D1`<#ZgPNbKF#V;W`JCkm?PaAblIWrx;Y;H#KqXpOu)q>6g02l!aBgrUms;h zqyGg|i1iVy&w+Xjpk_>-B$)xN-{AOH;W!bAoU9DwmuXh%VVzH9H!&$zDcDiV&1_N@ zRlO;*daLKS<2#*Q)YRgxmfGM2kz+lTbs_axRs$GrAy47r;1~v%*z9{|QPBA^gguLL m)|brJvbqi=w!(wvxpb>n?L$cNqi;3l)x%D^)m~i_9q|vl7FlKh literal 0 HcmV?d00001