Acey Ducey (Python): Code Cleanup (#667)

This commit is contained in:
Martin Thoma
2022-03-21 11:03:21 +01:00
committed by GitHub
parent 1b1d50986b
commit 373e5a1868
3 changed files with 94 additions and 84 deletions

View File

@@ -1,15 +1,14 @@
#
# AceyDuchy
#
# From: BASIC Computer Games (1978)
# Edited by David Ahl
#
# "The original BASIC program author was Bill Palmby
# of Prairie View, Illinois."
#
# Python port by Aviyam Fischer, 2022
#
######################################################
"""
AceyDuchy
From: BASIC Computer Games (1978)
Edited by David Ahl
"The original BASIC program author was Bill Palmby
of Prairie View, Illinois."
Python port by Aviyam Fischer, 2022
"""
from typing import List, Literal, TypeAlias, get_args
@@ -107,7 +106,19 @@ class Game:
self.not_done = False
if __name__ == "__main__":
def game_loop() -> None:
game_over = False
while not game_over:
game = Game()
game.play()
print(f"You have ${game.money} left")
print("Would you like to play again? (y/n)")
if input() == "n":
game_over = True
def main():
print(
"""
Acey Ducey is a card game where you play against the computer.
@@ -117,14 +128,9 @@ if __name__ == "__main__":
If you do not want to bet input a 0
"""
)
GAME_OVER = False
while not GAME_OVER:
game = Game()
game.play()
print(f"You have ${game.money} left")
print("Would you like to play again? (y/n)")
if input() == "n":
GAME_OVER = True
game_loop()
print("\nThanks for playing!")
if __name__ == "__main__":
main()

View File

@@ -59,11 +59,11 @@ def get_card_name(number: int) -> str:
def display_bankroll(bank_roll: int) -> None:
"""Print current bankroll"""
if bank_roll > 0:
print("You now have %s dollars\n" % bank_roll)
print(f"You now have {bank_roll} dollars\n")
def main() -> None:
# Display initial title and instructions
"""Display initial title and instructions."""
print("\n Acey Ducey Card Game")
print("Creative Computing Morristown, New Jersey")
print("\n\n")
@@ -74,77 +74,79 @@ def main() -> None:
print("a value between the first two.")
print("If you do not want to bet, input a 0")
# Loop for series of multiple games
KEEP_PLAYING = True
while KEEP_PLAYING:
multiple_game_loop()
print("OK Hope you had fun\n")
def multiple_game_loop() -> None:
"""Loop for series of multiple games."""
keep_playing = True
while keep_playing:
# Initialize bankroll at start of each game
BANK_ROLL = DEFAULT_BANKROLL
display_bankroll(BANK_ROLL)
# Loop for a single round. Repeat until out of money.
while BANK_ROLL > 0:
# Deal out dealer cards
print("Here are your next two cards")
dealer1 = deal_card_num()
# If the cards match, we redeal 2nd card until they don't
dealer2 = dealer1
while dealer1 == dealer2:
dealer2 = deal_card_num()
# Organize the cards in order if they're not already
if dealer1 >= dealer2:
(dealer1, dealer2) = (dealer2, dealer1) # Ya gotta love Python!
# Show dealer cards to the player
# (use card name rather than internal number)
print(get_card_name(dealer1))
print(get_card_name(dealer2) + "\n")
# Get and handle player bet choice
BET_IS_VALID = False
while not BET_IS_VALID:
curr_bet_str = input("What is your bet? ")
try:
curr_bet = int(curr_bet_str)
except ValueError:
# Bad input? Just loop back up and ask again...
pass
else:
if curr_bet == 0:
BET_IS_VALID = True
print("Chicken!!\n")
elif curr_bet > BANK_ROLL:
print("Sorry, my friend but you bet too much")
print("You have only %s dollars to bet\n" % BANK_ROLL)
else:
# Deal player card
BET_IS_VALID = True
player = deal_card_num()
print(get_card_name(player))
# Did we win?
if dealer1 < player < dealer2:
print("You win!!!")
BANK_ROLL += curr_bet
else:
print("Sorry, you lose")
BANK_ROLL -= curr_bet
# Update player on new bankroll level
display_bankroll(BANK_ROLL)
# End of loop for a single round
single_round(BANK_ROLL)
print("\n\nSorry, friend but you blew your wad")
player_response = input("Try again (yes or no) ")
if player_response.lower() == "yes":
print()
else:
KEEP_PLAYING = False
keep_playing = False
# End of multiple game loop
print("OK Hope you had fun\n")
def single_round(BANK_ROLL: int) -> None:
"""Loop for a single round. Repeat until out of money."""
while BANK_ROLL > 0:
# Deal out dealer cards
print("Here are your next two cards")
dealer1 = deal_card_num()
# If the cards match, we redeal 2nd card until they don't
dealer2 = dealer1
while dealer1 == dealer2:
dealer2 = deal_card_num()
# Organize the cards in order if they're not already
if dealer1 >= dealer2:
(dealer1, dealer2) = (dealer2, dealer1) # Ya gotta love Python!
# Show dealer cards to the player
# (use card name rather than internal number)
print(get_card_name(dealer1))
print(get_card_name(dealer2) + "\n")
# Get and handle player bet choice
BET_IS_VALID = False
while not BET_IS_VALID:
curr_bet_str = input("What is your bet? ")
try:
curr_bet = int(curr_bet_str)
except ValueError:
# Bad input? Just loop back up and ask again...
pass
else:
if curr_bet == 0:
BET_IS_VALID = True
print("Chicken!!\n")
elif curr_bet > BANK_ROLL:
print("Sorry, my friend but you bet too much")
print(f"You have only {BANK_ROLL} dollars to bet\n")
else:
# Deal player card
BET_IS_VALID = True
player = deal_card_num()
print(get_card_name(player))
# Did we win?
if dealer1 < player < dealer2:
print("You win!!!")
BANK_ROLL += curr_bet
else:
print("Sorry, you lose")
BANK_ROLL -= curr_bet
# Update player on new bankroll level
display_bankroll(BANK_ROLL)
if __name__ == "__main__":

View File

@@ -1,5 +1,6 @@
import io
from unittest import mock
from typing import TypeVar
from acey_ducey import play_game
@@ -7,8 +8,9 @@ from acey_ducey import play_game
@mock.patch("random.shuffle")
def test_play_game_lose(mock_random_shuffle, monkeypatch, capsys) -> None:
monkeypatch.setattr("sys.stdin", io.StringIO("100\n100"))
T = TypeVar("T")
def identity(x):
def identity(x: T) -> T:
return x
mock_random_shuffle = identity # noqa: F841