gamemaster is the boss
This commit is contained in:
parent
b666aa803b
commit
7572833e60
3 changed files with 47 additions and 35 deletions
32
blackjack.py
32
blackjack.py
|
@ -7,36 +7,16 @@ def main():
|
||||||
global ui
|
global ui
|
||||||
global num_decks
|
global num_decks
|
||||||
global deck
|
global deck
|
||||||
game = GameMaster()
|
game = GameMaster(deck)
|
||||||
|
|
||||||
for player in game.players:
|
game.deal()
|
||||||
for _ in range(2):
|
|
||||||
player.hand.append(deck.draw())
|
|
||||||
|
|
||||||
dealer = game.dealer
|
|
||||||
player = game.player
|
|
||||||
|
|
||||||
while game.active:
|
while game.active:
|
||||||
ui.update(game)
|
print(ui.update(game))
|
||||||
|
if not game.blackjack():
|
||||||
|
game.handle_player_input(ui.player_move(deck.count()), kill)
|
||||||
|
|
||||||
if game.active:
|
print(ui.update(game))
|
||||||
match ui.player_move(deck).lower():
|
|
||||||
case 'y':
|
|
||||||
game.active = False
|
|
||||||
case 'q':
|
|
||||||
game.active = False
|
|
||||||
kill = True
|
|
||||||
case 'n':
|
|
||||||
game.active = False
|
|
||||||
case 'h':
|
|
||||||
player.hand.append(deck.draw())
|
|
||||||
if player.bust(game):
|
|
||||||
game.active = False
|
|
||||||
case 's':
|
|
||||||
game.active = False
|
|
||||||
while dealer.score(game) < 17:
|
|
||||||
dealer.hand.append(deck.draw())
|
|
||||||
ui.update(game)
|
|
||||||
|
|
||||||
kill = False
|
kill = False
|
||||||
ui = CLI()
|
ui = CLI()
|
||||||
|
|
|
@ -1,11 +1,44 @@
|
||||||
from src.player import Player
|
from src.player import Player
|
||||||
|
|
||||||
class GameMaster:
|
class GameMaster:
|
||||||
def __init__(self):
|
def __init__(self, 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]
|
||||||
self.player = self.players[1]
|
self.player = self.players[1]
|
||||||
|
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'
|
||||||
|
|
15
src/io.py
15
src/io.py
|
@ -91,13 +91,12 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
def update(self, game):
|
def update(self, game):
|
||||||
print(self.clear + self.title +
|
buffer = f'{self.clear}{self.title}{self.show_players(game)}'
|
||||||
self.show_players(game))
|
|
||||||
for player in game.players:
|
|
||||||
if player.blackjack(game):
|
|
||||||
game.active = False
|
|
||||||
if not game.active:
|
if not game.active:
|
||||||
print(game.score())
|
buffer += '\n' + game.score()
|
||||||
|
|
||||||
|
return buffer
|
||||||
|
|
||||||
def get_decks(self):
|
def get_decks(self):
|
||||||
return input('How many decks? (1-8): ')
|
return input('How many decks? (1-8): ')
|
||||||
|
|
||||||
|
@ -110,6 +109,6 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o
|
||||||
case _:
|
case _:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def player_move(self, deck):
|
def player_move(self, count):
|
||||||
return input(str(deck.count()) + ' cards left in deck.\n[H]it or [S]tand? ')
|
return input(f'{count} cards left in deck.\n[H]it or [S]tand? ')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue