diff --git a/Challenge2.py b/Challenge2.py new file mode 100644 index 0000000..f04929a --- /dev/null +++ b/Challenge2.py @@ -0,0 +1,45 @@ +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.") + diff --git a/__pycache__/cards.cpython-32.pyc b/__pycache__/cards.cpython-32.pyc new file mode 100644 index 0000000..4f60ecd Binary files /dev/null and b/__pycache__/cards.cpython-32.pyc differ diff --git a/__pycache__/games.cpython-32.pyc b/__pycache__/games.cpython-32.pyc new file mode 100644 index 0000000..953da5d Binary files /dev/null and b/__pycache__/games.cpython-32.pyc differ diff --git a/blackjack.py b/blackjack.py index b98c635..2f7f7d3 100644 --- a/blackjack.py +++ b/blackjack.py @@ -1,5 +1,7 @@ # Blackjack # From 1 to 7 players compete against a dealer +#Help from interweb tutor +#and tom import cards, games @@ -66,26 +68,60 @@ 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("\n How mush do you want to wager?: ")) + if wager > bet.stash: + int(input("\n You can only wager what you have. How much?: ")) + elif wager <0: + int(input("\nYou can only wager what you have. How much?: ")) + except ValuError: + int(input("\n Thats not valid 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): + def bust(self,stash,wager): print(self.name, "busts.") - self.lose() + self.lose(self,stash,wager) - def lose(self): + def lose(self,stash,wager): print(self.name, "loses.") + stash = stash - wager + print("Your stash is: ",stash) + return stash - def win(self): + def win(self,stash,wager): print(self.name, "wins.") + stash = stash + wager + print("Your stash is: ",stash) + return stash def push(self): print(self.name, "pushes.") - + def betting(bet,stash): + try: + if stash > 0: + wager = int(input("\nHow much do you want to wager?: ")) + if wager > stash: + int(input("\n You can only wager what you have. How much?: ")) + elif wager < 0: + int(input("\n You can only wager what you have. How much?: ")) + except ValueError: + int(input("\n That's not valid! Choose a number: ")) + + class BJ_Dealer(BJ_Hand): """ A Blackjack Dealer. """ def is_hitting(self): @@ -104,8 +140,24 @@ class BJ_Game(object): def __init__(self, names): self.players = [] for name in names: + stash=100 player = BJ_Player(name) + bet=BJ_Player(name).betting(stash) + + playerbet=bet(stash).betting(stash) self.players.append(player) + + + def betting(bet,stash): + try: + if stash > 0: + wager = int(input("\nHow much do you want to wager?: ")) + if wager > stash: + int(input("\n You can only wager what you have. How much?: ")) + elif wager < 0: + int(input("\n You can only wager what you have. How much?: ")) + except ValueError: + int(input("\n That's not valid! Choose a number: ")) self.dealer = BJ_Dealer("Dealer") @@ -113,6 +165,18 @@ def __init__(self, names): self.deck.populate() self.deck.shuffle() + def populate(self): + for suit in BJ_Card.SUITS: + for rank in BJ_Card.RANKS: + self.cards.append(BJ_Card(rank, suit)) + + def gamble(bet): + if bet.stash <= 0: + print("\nYou are out of money! You're out of the game!") + + def __init__(bet, money = 10): + stash = money + @property def still_playing(self): sp = [] @@ -121,14 +185,14 @@ def still_playing(self): sp.append(player) return sp - def __additional_cards(self, player): + def __additional_cards(self, player,stash,wager): while not player.is_busted() and player.is_hitting(): self.deck.deal([player]) print(player) if player.is_busted(): - player.bust() + player.bust(self,stash,wager) - def play(self): + def play(self,stash,wager): # 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 @@ -138,9 +202,9 @@ def play(self): # deal additional cards to players for player in self.players: - self.__additional_cards(player) + self.__additional_cards(player,stash,wager) - self.dealer.flip_first_card() # reveal dealer's first + 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 @@ -148,19 +212,19 @@ def play(self): else: # deal additional cards to dealer print(self.dealer) - self.__additional_cards(self.dealer) + self.__additional_cards(self.dealer,stash,wager) if self.dealer.is_busted(): # everyone still playing wins for player in self.still_playing: - player.win() + player.win(stash,wager) else: # compare each player still playing to dealer for player in self.still_playing: if player.total > self.dealer.total: - player.win() + player.win(stash,wager) elif player.total < self.dealer.total: - player.lose() + player.lose(stash,wager) else: player.push() @@ -168,23 +232,29 @@ def play(self): for player in self.players: player.clear() self.dealer.clear() - + def main(): print("\t\tWelcome to Blackjack!\n") - + stash = 0 + wager = 0 + pari=10 names = [] - number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8) + number = games.ask_number("How many players? (1 - 7): ", low = 1, high =8) + for i in range(number): name = input("Enter player name: ") + bet=BJ_Player(name).betting(pari) names.append(name) print() - + game = BJ_Game(names) + + again = None while again != "n": - game.play() + game.play(stash,wager) again = games.ask_yes_no("\nDo you want to play again?: ") @@ -192,4 +262,3 @@ def main(): input("\n\nPress the enter key to exit.") - diff --git a/challeng2.py b/challeng2.py new file mode 100644 index 0000000..47db231 --- /dev/null +++ b/challeng2.py @@ -0,0 +1,45 @@ +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 or Player2Counter < 10: + Player1CardIndex=random.randint(len(RANKS)) + Player2CardIndex=random.randint(len(RANKS)) + Player1Card=RANKS[Player1CardIndex] + Player2Card=RANKS[Player2CardIndex] + if Player1CardIndex < 9: + Player1CardIndex = 10 + if Player2CardIndex < 9: + 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(PlayerName,"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.") + \ No newline at end of file diff --git a/challeng2.py~ b/challeng2.py~ new file mode 100644 index 0000000..f0437b9 --- /dev/null +++ b/challeng2.py~ @@ -0,0 +1,13 @@ +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 or Player2Counter < 10: + \ No newline at end of file