just like tiger woods
This commit is contained in:
parent
a31c7f985c
commit
2875f08ed7
5 changed files with 73 additions and 71 deletions
28
blackjack.py
28
blackjack.py
|
@ -1,28 +0,0 @@
|
||||||
from src.deck import Deck
|
|
||||||
from src.io import CLI
|
|
||||||
from src.gamemaster import GameMaster
|
|
||||||
|
|
||||||
kill = False
|
|
||||||
ui = CLI()
|
|
||||||
print(ui.intro)
|
|
||||||
num_decks = ui.get_decks()
|
|
||||||
if num_decks == 'q':
|
|
||||||
kill = True
|
|
||||||
deck = Deck(num_decks)
|
|
||||||
|
|
||||||
while not kill:
|
|
||||||
game = GameMaster(deck)
|
|
||||||
game.active = True
|
|
||||||
|
|
||||||
game.deal()
|
|
||||||
|
|
||||||
while game.active:
|
|
||||||
print(ui.update(game))
|
|
||||||
if not game.blackjack():
|
|
||||||
game.handle_player_input(ui.player_move(deck.count()), kill)
|
|
||||||
|
|
||||||
print(ui.update(game))
|
|
||||||
|
|
||||||
if not ui.play_again():
|
|
||||||
kill = True
|
|
||||||
|
|
28
main.py
Normal file
28
main.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
from src.deck import Deck
|
||||||
|
from src.io import CLI
|
||||||
|
from src.blackjack import Blackjack
|
||||||
|
|
||||||
|
def main():
|
||||||
|
kill = False
|
||||||
|
ui = CLI()
|
||||||
|
print(ui.intro)
|
||||||
|
num_decks = ui.get_decks()
|
||||||
|
if num_decks in 'qx':
|
||||||
|
kill = True
|
||||||
|
deck = Deck(num_decks)
|
||||||
|
|
||||||
|
while not kill:
|
||||||
|
game = Blackjack(deck)
|
||||||
|
|
||||||
|
game.deal()
|
||||||
|
|
||||||
|
while game.active:
|
||||||
|
print(ui.update(game))
|
||||||
|
if not game.blackjack():
|
||||||
|
game.handle_player_input(ui.player_move(deck.count()), kill)
|
||||||
|
|
||||||
|
print(ui.update(game))
|
||||||
|
|
||||||
|
if not ui.play_again():
|
||||||
|
kill = True
|
||||||
|
main()
|
|
@ -1,44 +1,12 @@
|
||||||
from src.player import Player
|
from src.player import Player
|
||||||
|
|
||||||
class GameMaster:
|
class Blackjack:
|
||||||
def __init__(self, deck):
|
def __init__(self, deck):
|
||||||
|
self.deck = deck
|
||||||
self.active = True
|
self.active = True
|
||||||
self.players = [Player('Dealer'), Player('Player')] # 5-9 seats
|
self.players = [Player('Dealer'), Player('Player')] # 5-9 seats
|
||||||
self.dealer = self.players[0]
|
self.dealer = self.players[0] # Mickey
|
||||||
self.player = self.players[1]
|
self.player = self.players[1] # Mouse
|
||||||
self.deck = deck
|
|
||||||
|
|
||||||
def deal(self):
|
|
||||||
for player in self.players:
|
|
||||||
for _ in range(2):
|
|
||||||
player.hand.append(self.deck.draw())
|
|
||||||
|
|
||||||
def blackjack(self):
|
|
||||||
for player in self.players:
|
|
||||||
if player.blackjack(self):
|
|
||||||
self.active = False
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def handle_player_input(self, input, kill):
|
|
||||||
if self.active:
|
|
||||||
match input:
|
|
||||||
case 'y':
|
|
||||||
self.active = False
|
|
||||||
case 'q':
|
|
||||||
self.active = False
|
|
||||||
kill = True
|
|
||||||
case 'n':
|
|
||||||
self.active = False
|
|
||||||
case 'h':
|
|
||||||
self.player.hand.append(self.deck.draw())
|
|
||||||
if self.player.bust(self):
|
|
||||||
self.active = False
|
|
||||||
case 's':
|
|
||||||
self.active = False
|
|
||||||
while self.dealer.score(self) < 17:
|
|
||||||
self.dealer.hand.append(self.deck.draw())
|
|
||||||
|
|
||||||
def score(self):
|
def score(self):
|
||||||
status = 'Error'
|
status = 'Error'
|
||||||
|
@ -62,3 +30,34 @@ class GameMaster:
|
||||||
status = player.name + ' has Blackjack!'
|
status = player.name + ' has Blackjack!'
|
||||||
|
|
||||||
return status
|
return status
|
||||||
|
def deal(self):
|
||||||
|
for player in self.players:
|
||||||
|
for _ in range(2):
|
||||||
|
player.hand.append(self.deck.draw())
|
||||||
|
|
||||||
|
def blackjack(self):
|
||||||
|
for player in self.players:
|
||||||
|
if player.blackjack(self):
|
||||||
|
self.active = False
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def handle_player_input(self, input, kill):
|
||||||
|
match input:
|
||||||
|
case 'y':
|
||||||
|
self.active = False
|
||||||
|
case 'q':
|
||||||
|
self.active = False
|
||||||
|
kill = True
|
||||||
|
case 'n':
|
||||||
|
self.active = False
|
||||||
|
case 'h':
|
||||||
|
self.player.hand.append(self.deck.draw())
|
||||||
|
if self.player.bust(self):
|
||||||
|
self.active = False
|
||||||
|
case 's':
|
||||||
|
self.active = False
|
||||||
|
while self.dealer.score(self) < 17:
|
||||||
|
self.dealer.hand.append(self.deck.draw())
|
||||||
|
|
12
src/deck.py
12
src/deck.py
|
@ -8,19 +8,19 @@ class Deck:
|
||||||
def count(self):
|
def count(self):
|
||||||
return len(self.cards)
|
return len(self.cards)
|
||||||
|
|
||||||
def shuffle(self, n_decks):
|
def shuffle(self):
|
||||||
self.cards = []
|
|
||||||
n_decks = 1 if not n_decks or n_decks not in '12345678' else int(n_decks)
|
|
||||||
suits = '♠♥♦♣'
|
suits = '♠♥♦♣'
|
||||||
cards = 'A234567890JQK'
|
cards = 'A234567890JQK'
|
||||||
|
|
||||||
while n_decks > 0:
|
decks_to_shuffle = int(self.n_decks)
|
||||||
|
|
||||||
|
while decks_to_shuffle > 0:
|
||||||
self.cards += [card + suit for card in cards for suit in suits]
|
self.cards += [card + suit for card in cards for suit in suits]
|
||||||
n_decks -= 1
|
decks_to_shuffle -= 1
|
||||||
|
|
||||||
def check(self):
|
def check(self):
|
||||||
if self.count() < 1:
|
if self.count() < 1:
|
||||||
self.shuffle(self.n_decks)
|
self.shuffle()
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
self.check()
|
self.check()
|
||||||
|
|
|
@ -98,7 +98,10 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
def get_decks(self):
|
def get_decks(self):
|
||||||
return input('How many decks? (1-8): ')
|
user_input = input('How many decks? (1-8): ')
|
||||||
|
if not user_input:
|
||||||
|
return '1'
|
||||||
|
return user_input
|
||||||
|
|
||||||
def play_again(self):
|
def play_again(self):
|
||||||
match input('Play again? [Y/n] ').lower():
|
match input('Play again? [Y/n] ').lower():
|
||||||
|
|
Loading…
Add table
Reference in a new issue