From d8030b11a85bd46d6892543462d0442792bae00d Mon Sep 17 00:00:00 2001 From: Ipman345 Date: Thu, 5 Mar 2015 06:46:53 -0600 Subject: [PATCH] Had Help from Tom and Tyler. 03/05/15 --- Challenge1.py | 199 +++++++++++++++++++++++++++++ Challenge2.py | 47 +++++++ Challenge3.py | 210 +++++++++++++++++++++++++++++++ __pycache__/cards.cpython-32.pyc | Bin 0 -> 3812 bytes __pycache__/games.cpython-32.pyc | Bin 0 -> 1626 bytes blackjack.py | 4 + 6 files changed, 460 insertions(+) create mode 100644 Challenge1.py create mode 100644 Challenge2.py create mode 100644 Challenge3.py create mode 100644 __pycache__/cards.cpython-32.pyc create mode 100644 __pycache__/games.cpython-32.pyc diff --git a/Challenge1.py b/Challenge1.py new file mode 100644 index 0000000..88b6de7 --- /dev/null +++ b/Challenge1.py @@ -0,0 +1,199 @@ +# 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: + 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(): + if self.deck == 0: + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() + 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.") + + + diff --git a/Challenge2.py b/Challenge2.py new file mode 100644 index 0000000..7f69aae --- /dev/null +++ b/Challenge2.py @@ -0,0 +1,47 @@ +import random +RANKS =["A", "2", "3", "4", "5", "6", "7", + "8", "9", "10", "J", "Q", "K"] +SUITS = ["c", "d", "h", "s"] + +Player1Counter=0 +Player2Counter=0 +print("Welcome to WAR!") +print("You and a friend will player war until one of the two of you get ten points!") +print("Before we begin, Can you please enter our names.") +Player1Name=input("Player 1 Name: ") +Player2Name=input("Player 2 Name: ") +while Player1Counter < 10 and Player2Counter < 10: + Player1CardIndex=random.randrange(len(RANKS)) + Player2CardIndex=random.randrange(len(RANKS)) + Player1Card=RANKS[Player1CardIndex] + Player2Card=RANKS[Player2CardIndex] + if Player1CardIndex > 8: + Player1CardIndex = 10 + if Player2CardIndex > 8: + Player2CardIndex = 10 + Player1Suit=random.choice(SUITS) + Player2Suit=random.choice(SUITS) + Player1Card =Player1Card + Player1Suit + Player2Card = Player2Card + Player2Suit + print(Player1Name,":",Player1Card) + print(Player2Name,":",Player2Card) + if Player1CardIndex > Player2CardIndex: + print(Player1Name,"has won this round and has gained one point.") + Player1Counter += 1 + elif Player1CardIndex < Player2CardIndex: + print(Player2Name,"has won this round and has gained one point.") + Player2Counter += 1 + else: + print("Oh No! No one has won this round1") + print(Player1Name,":",Player1Counter) + print(Player2Name,":",Player2Counter) + input("Please press the key.") +if Player1Counter < Player2Counter: + print(Player1Name,"has won and is the ultimate champion!") +else: + print(Player2Name,"has won and is the ulimate champion!") +print("Thanks for playing.") +input("Please press to exit.") + +#Tom helped with this. He is a good man. + diff --git a/Challenge3.py b/Challenge3.py new file mode 100644 index 0000000..ad2e289 --- /dev/null +++ b/Challenge3.py @@ -0,0 +1,210 @@ +# 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: + 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):bet = BJ_Player(name).betting(pari) + 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 is_betting(self): + response = games.ask_yes_no("\n" + self.name + ", do you want to bet? (Y/N): ") + if responce =="y": + bet = input("Enter how much you want to bet: ") + if bet > self.bankroll: + while bet > self.bankroll: + bet=input("Enter an amount that is equal too or less than "+self.bankroll+":") + else: + self.pot+=bet + + def bust(self): + print(self.name, "busts.") + self.lose() + + def lose(self): + print(self.name, "loses.") + + def win(self): + print(self.name, "wins the pot:",POT ) + + 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(): + if self.deck == 0: + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() + 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.") + + + diff --git a/__pycache__/cards.cpython-32.pyc b/__pycache__/cards.cpython-32.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4e3dbb6d5dcf4d5991ea4a0e7696aff8abb5b44b GIT binary patch literal 3812 zcmcgvYjfK~6ut5*wTb&m-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=V;*xMHX2S~91*q~ z7q}3JE$I(E$&cOZXgLcq3gEs zjHQ_~ZE5GaXhUlP^$Jt@hRaskQ(Xsg5@1&*8NFJ&${MHdi4N5ybrXewDq|nw%8_Y^ z+TxK|7mNNO>}c%ZaPLvzTW}G>*&8^I84n~Nf)O!7Z=s2xiHWQT68=z=5sK#&^%5-* zLZM-epB`fOo)*ohFo;^6nq%D`CghO2Y=H%z_!##K6EI!ad$^glof zaXvxI3y_{7NOSg1)50K?-{JAT^^}Cfe4Gq~mw8d^tSM(QNllKc99hxG&0<>Fy4jRP zv(@+1;l182*3#i!Ew#ZHLr(NeHdgAhqCrroZJ4_DaEu0*Sj>HLUChNYgguXP-iyT3 is=f&#ZiUB-Idtb2>{CeaGq)G<>2a^y>8`Gcp7;k}6-}7{ literal 0 HcmV?d00001 diff --git a/blackjack.py b/blackjack.py index b98c635..96144ec 100644 --- a/blackjack.py +++ b/blackjack.py @@ -123,6 +123,10 @@ def still_playing(self): def __additional_cards(self, player): while not player.is_busted() and player.is_hitting(): + if self.deck == 0: + self.deck = BJ_Deck() + self.deck.populate() + self.deck.shuffle() self.deck.deal([player]) print(player) if player.is_busted():