Skip to content

Commit 0418e3e

Browse files
committed
Restore acey_ducey_oo.py
See #689 (review)
1 parent 6c32fc1 commit 0418e3e

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

01_Acey_Ducey/python/acey_ducey_oo.py

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""
2+
AceyDuchy
3+
From: BASIC Computer Games (1978)
4+
Edited by David Ahl
5+
"The original BASIC program author was Bill Palmby
6+
of Prairie View, Illinois."
7+
Python port by Aviyam Fischer, 2022
8+
"""
9+
10+
from typing import List, Literal, TypeAlias, get_args
11+
12+
Suit: TypeAlias = Literal["\u2665", "\u2666", "\u2663", "\u2660"]
13+
Rank: TypeAlias = Literal[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
14+
15+
16+
class Card:
17+
def __init__(self, suit: Suit, rank: Rank) -> None:
18+
self.suit = suit
19+
self.rank = rank
20+
21+
def __str__(self) -> str:
22+
r = str(self.rank)
23+
r = {"11": "J", "12": "Q", "13": "K", "14": "A"}.get(r, r)
24+
return f"{r}{self.suit}"
25+
26+
27+
class Deck:
28+
def __init__(self) -> None:
29+
self.cards: List[Card] = []
30+
self.build()
31+
32+
def build(self) -> None:
33+
for suit in get_args(Suit):
34+
for rank in get_args(Rank):
35+
self.cards.append(Card(suit, rank))
36+
37+
def shuffle(self) -> None:
38+
import random
39+
40+
random.shuffle(self.cards)
41+
42+
def deal(self) -> Card:
43+
return self.cards.pop()
44+
45+
46+
class Game:
47+
def __init__(self) -> None:
48+
self.deck = Deck()
49+
self.deck.shuffle()
50+
self.card_a = self.deck.deal()
51+
self.card_b = self.deck.deal()
52+
self.money = 100
53+
self.not_done = True
54+
55+
def play(self) -> None:
56+
while self.not_done:
57+
while self.money > 0:
58+
card_a = self.card_a
59+
card_b = self.card_b
60+
61+
if card_a.rank > card_b.rank:
62+
card_a, card_b = card_b, card_a
63+
64+
if card_a.rank == card_b.rank:
65+
self.card_b = self.deck.deal()
66+
card_b = self.card_b
67+
68+
print(f"You have:\t ${self.money} ")
69+
print(f"Your cards:\t {card_a} {card_b}")
70+
71+
bet = int(input("What is your bet? "))
72+
player_card = self.deck.deal()
73+
if 0 < bet <= self.money:
74+
75+
print(f"Your deal:\t {player_card}")
76+
if card_a.rank < player_card.rank < card_b.rank:
77+
print("You Win!")
78+
self.money += bet
79+
else:
80+
print("You Lose!")
81+
self.money -= bet
82+
self.not_done = False
83+
else:
84+
print("Chicken!")
85+
print(f"Your deal should have been: {player_card}")
86+
if card_a.rank < player_card.rank < card_b.rank:
87+
print("You could have won!")
88+
else:
89+
print("You would lose, so it was wise of you to chicken out!")
90+
91+
self.not_done = False
92+
break
93+
94+
if len(self.deck.cards) <= 3:
95+
print("You ran out of cards. Game over.")
96+
self.not_done = False
97+
break
98+
99+
self.card_a = self.deck.deal()
100+
self.card_b = self.deck.deal()
101+
102+
if self.money == 0:
103+
self.not_done = False
104+
105+
106+
def game_loop() -> None:
107+
game_over = False
108+
109+
while not game_over:
110+
game = Game()
111+
game.play()
112+
print(f"You have ${game.money} left")
113+
print("Would you like to play again? (y/n)")
114+
if input() == "n":
115+
game_over = True
116+
117+
118+
def main():
119+
print(
120+
"""
121+
Acey Ducey is a card game where you play against the computer.
122+
The Dealer(computer) will deal two cards facing up.
123+
You have an option to bet or not bet depending on whether or not you
124+
feel the card will have a value between the first two.
125+
If you do not want to bet input a 0
126+
"""
127+
)
128+
game_loop()
129+
print("\nThanks for playing!")
130+
131+
132+
if __name__ == "__main__":
133+
main()

0 commit comments

Comments
 (0)