Skip to content

Commit

Permalink
Create player class, implement some prompt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
creitz committed Jul 29, 2021
1 parent bf08a2e commit a607651
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
55 changes: 54 additions & 1 deletion blackjack.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
from decimal import Decimal

from player import Player
from utils import get_integer_input

class BlackJack():

# The starting amount of money for each player, in dollars
DEFAULT_START_DOLLARS = Decimal(1000)

def play(self):
pass
"""
The entrypoint for the Blackjack class. Play the game!
"""

# Get the number of players we want to play with
num_players = self.ask_num_players()
if num_players == 0:
# No players :) Game is over!
print('Cool, you decided to go touch grass instead. Game over!')
return

players = [Player(player_number=i+1, start_dollars=self.DEFAULT_START_DOLLARS)
for i in range(num_players)]
self.get_bet(player=players[0])

def get_bet(self, player: Player):
"""
Prompts the user to enter a bet amount as an integer, and returns the amount
once an integer is input.
Args:
player (Player): The player for which to get the bet
Returns:
int: The number of dollars input
"""
blurb = ('Place bet for player {}. Input must be an integer. ${} remaining'
.format(player.player_number, player.dollars_remaining))
prompt = '$: '
input_amt = get_integer_input(blurb=blurb, prompt=prompt)
while input_amt > player.dollars_remaining:
blurb = ('Insufficent balance. Enter an amount < ${} for player {}'
.format(player.dollars_remaining, player.player_number))
input_amt = get_integer_input(blurb=blurb, prompt=prompt)

return input_amt

def ask_num_players(self) -> int:
"""
Asks the user to input the number of players, and returns the result
once an integer is input.
Returns:
int: The number of players
"""
blurb = 'How many players do you want to play with? Input must be an integer.'
prompt = '#: '
return get_integer_input(blurb=blurb, prompt=prompt)

if __name__ == "__main__":
game = BlackJack()
Expand Down
11 changes: 11 additions & 0 deletions player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

from decimal import Decimal

class Player():

def __init__(self, player_number: int, start_dollars: Decimal):
"""
Create a player with the given starting amount of money,
"""
self.player_number = player_number
self.dollars_remaining = start_dollars
19 changes: 19 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

def get_integer_input(blurb: str, prompt: str):
"""
Prompts the user to enter a bet amount as an integer, and returns the amount
once an integer is input.
Args:
blurb (str): The string printed before input
prompt (str): The string printed on the same line as the input
Returns:
[int]: The integer the user input
"""
input_amt = None
print(blurb)
while input_amt is None or not input_amt.isnumeric():
input_amt = input(prompt)

return int(input_amt)

0 comments on commit a607651

Please sign in to comment.