From b0bee0c420b66c7572f15945065db106bf879a94 Mon Sep 17 00:00:00 2001 From: troyscheet2 Date: Mon, 2 Mar 2015 07:39:23 -0600 Subject: [PATCH] Chapter 9 Challenge --- OneCardWar.py | 43 ++++++ __pycache__/cards.cpython-32.pyc | Bin 0 -> 3872 bytes __pycache__/games.cpython-32.pyc | Bin 0 -> 1650 bytes adventure.py | 38 ++++++ blackjack(edited).py | 216 +++++++++++++++++++++++++++++++ 5 files changed, 297 insertions(+) create mode 100644 OneCardWar.py create mode 100644 __pycache__/cards.cpython-32.pyc create mode 100644 __pycache__/games.cpython-32.pyc create mode 100644 adventure.py create mode 100644 blackjack(edited).py diff --git a/OneCardWar.py b/OneCardWar.py new file mode 100644 index 0000000..ac534fa --- /dev/null +++ b/OneCardWar.py @@ -0,0 +1,43 @@ +import itertools +import random + +class Cards: + def __init__(self): + self.suits = ['Spades', 'Hearts', 'Diamonds', 'Clubs'] + self.values = range(1,14) + self.RealCards = [] + for Card in itertools.product(self.suits,self.values): + self.RealCards,append(Card) + def GetRandomCard(self): + RandomNumber = random.randint(0,51) + ReturnedCard = self.RealCards[RandomNumber] + return(ReturnedCard) + +class Player: + def __init__(self, ID, Card): + self.name = NameofGame + +class War(Game): + def __init__(self, NumberofPlayers): + self.NumberofPlayers = NumberofPlayers + self.PlayerList = [] + def GameStart(self): + CarDeck = Cards() + for playerID in range(0, self.NumberofPlayers): + PlayersCard = CarDeck.GetRandomCard() + NewPlayer = Player(playerID, PlayersCard) + self.PlayerList.append(NewPlayer) + self.Winner() + def Winner(self): + WinningID = self.PlayerList[0] + for playerID in self.PlayerList: + if(playerID.PlayersCard[1] > WinningID.PlayersCard[1]): + WinningID = playerID + print("And the winner is... " + str(WinningID.playerID) + print("The winning card was... " + str(WinningID.PlayersCard[1]) + " of " + str(WinningID.PlayersCard[0]) + +if __name__=="__main__": + NewGame = SimpleWar(2) + NewGame.StartGame() + + diff --git a/__pycache__/cards.cpython-32.pyc b/__pycache__/cards.cpython-32.pyc new file mode 100644 index 0000000000000000000000000000000000000000..00a3d397bf79860c24fc1c639153bc6ff9b91870 GIT binary patch literal 3872 zcmcgv`ET4r6n@?#=`KmH^lk+%NHjts=>fEbsv=Dgw3U`4L9HSpV?2|rla1~8C@E@w zYX4mR2)^&x>rIvd?W)4bJii&w%)7st9n8#j|A_xw3*^w#0{=By_AAJ@M&y-uOQJ8xk1!q$r;&kv= z$C{?q=6#F~Y}x^Sas&(+=ne&n6UW1%V~kCPuuPyKmu?Qi zv<~H^LArX4A?CVGjRaEjmbYP^&YP(xBBzLwN>@qT3Xm6 zfvr}lJ9GvGYZ6x558}WdWS%#n9m~ijb;WUL)ODtuv(5$QbYrI7nO1T9US|^>$Myg% zTLOW*$C2X|${nbn=tVGSYTW}x6ubxtZ5;t5Od-+el;RSIyX5x$$hh(55k$xq+#}-a zpcBEiR0Gti8C9kRm+sX%DLthIP8Mrs->}FogRkOU>%=_g_RIXX0#X&&msK_GFH|Ed z8<*q&km5*=8UuhU$ezEAirc#=Pgs7k~)sHr#->oe5Hg;ZEYD82Gup1-`neoJ8W( zo~D=C)N&ULZ=oUssX&p%3tLP3)Gp5Mg&i$yh;;rcYoI zv9S_xhb)~V&WsZ>VFG%R4@Jqr*?(Gv3e~)_@kkDuvcJT*t!&(tgO*z8Ncym1 z;DU<>?aJ;KU|I|?qnqmyOH9yE&sL$P;L;_l*717ToWPoEfNa zceT8tQb%}h>yWeRx>NrpX&B{YBGXbmVYrA8l4;c^6`&}9rg6b>PUQ@pkjIHPA%1cn z>VA)+%Ts91nZ-#oi%VgzRI5ak|Gsz~PQ@c>G8FO-ab^X@euFwwsNzh!(^9YSO9hs@ zei-3GcpX#L`eEkc-*?M9qx*Z9_uXLNXPM31%{U#q85KO1yWJmBn`JH_-L(;ZBDg#D zxtqtX-4FAHUU|V{BF4XM+aMo701YPAjVo07km1#^YXXE(QskpLQSPmNsN5?}x=*PY Re@o1rox3o1ZpP_4e*tc{??nIr literal 0 HcmV?d00001 diff --git a/__pycache__/games.cpython-32.pyc b/__pycache__/games.cpython-32.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7c1bcd5078e47db1d49b17262b5511f8cf70e8ab GIT binary patch literal 1650 zcmb_cOK%e~5FYQQNfQ+~AX?ynEI4!vQgYzHLkLkS1P2sVDiWd>YrE?<+idKuA8Lcz zQ~A042xjawNu^#|v5F^tjOX#qH?G(2^?vKWKMGj-S~&lN!+uBcwFLMBP%@AJmnrNs zC==S*PoPZ0Bn8=mvIXM=%r}rLP_Dow118&0w!=)Jfe!3tt7x!bDCndA1jlC_b_>Nh zz!!8v9-{f^iot$*FNWD_#DT^b#{DFoUUqDM|*7EjqQmMLaQ zF$m#FL5PVieND@FwbTX9m(iFV1e>8Qvx~JZ^4bboH6!wmOXL5BU`@@VJhOV3A5l(!H`<05g1cajIHC#vsF$>p6)zRpG z0Tp9?gx2RkJw~8rOrGSGLR!DU<6qiSA{=wFJd&T6m63(3r##D5iK`OX;rM1XX|!>h zymDK8PaWUu?P5(W-j!S`d_nkFPPx-uo>dNkp%w}h)(6LEaEZ;KXBGvWFJsuVC}(|1 kd~M8iAaN@`Xn{+&deuI}BtM2$BVIl1bvxbFHP&PQ0HexS&;S4c literal 0 HcmV?d00001 diff --git a/adventure.py b/adventure.py new file mode 100644 index 0000000..a64e67b --- /dev/null +++ b/adventure.py @@ -0,0 +1,38 @@ +hp = 50 + +print("You enter a creepy, dark, underground pathway. You look in front of you, to see two alternative pathways. Do you choose into the left pathway for the right one?") +pathway = input('> ') + +if pathway == "Left" or "L": + print("Down the left pathway, you start seeing creepy hallucinations of red eyes staring at you.") + print("You see an evil witch casting a hallucenation spell on you. Do you #1. try to kill the witch or #2. get away from the witch?") + + witch = input('> ') + + if witch == "1": + print("You approach the witch quietly with a stake to slay it. However, the witch senses your prescence and casts a ball spell which turns you into a ball, and then she throws you into her brew. Poor you....") + + elif witch == "2": + print("You try to sneak past the witch, but her hallucinations take a toll on your body, which makes you fall down in unconsiousness. You don't know what happens to you after that.") + + else: + print("The witch gets bored of you,and flies away on her magic broomstick. Great Job!") + +elif pathway == "Right" or "R": + print("You enter a big, wet room and a huge man-eating slug is seen approaching you. This slug also happens to be faster than a normal slug is. What do you do?") + print("#1. Try to mount it to use it to escape.") + print("#2. Trick it into charging towards you, into a wall.") + print("#3. You attempt to become friends with it.") + slug = input('> ') + + if slug == "1" or "2": + print("You successfully slayed the slug, and now, you can escape.") + + else: + print("Being friends with a man-eating slug? Yeah, no. It gobbles you up with one slurp and now, you're stuck in its digestive track.") + +else: + print("You took too long of a descicion and you find yourself being bitten by a bunch of poisonous spiders. What an anticlimatic ending.") + + + diff --git a/blackjack(edited).py b/blackjack(edited).py new file mode 100644 index 0000000..6d32949 --- /dev/null +++ b/blackjack(edited).py @@ -0,0 +1,216 @@ +# Blackjack +# From 1 to 7 players compete against a dealer + +import cards, games + +class BJ_Card(cards.Card): + """ A Blackjack Card. """ + ACE_VALUE = 1 + + @property + def value(self): + if self.is_face_up: + value = BJ_Card.RANKS.index(self.rank) + 1 + if value > 10: + value = 10 + else: + value = None + return value + +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 + + def deal(self, hands, per_hand=1): + for rounds in range(per_hand): + if len(self.cards) <= 7 * len(hands): + print("Reshuffling the deck...") + self.cards = [] + self.populate() + self.shuffle() + for hand in hands: + top_card = self.cards[0] + self.give(top_card, hand) + + @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 betting(stash): + try: + if stash > 0: + wager = int(input("\nHow much would you like to wager?: ")) + if wager > bet.stash: + int(input("\n You can only wager what you have at hand. How much?: ")) + elif wager < 0: + int(input("You can only wager what you have at hand. How much?: ")) + except ValueError: + int(input("\n Entered an invalid amount. Choose a number: ")) + + 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() + + +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: ") + 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.") + + +