Skip to content

Commit 957533b

Browse files
committed
change game
1 parent b161cba commit 957533b

File tree

4 files changed

+147
-111
lines changed

4 files changed

+147
-111
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ for i in string:
1616
odd+=i
1717
print(lower+upper+odd+even)
1818

19+
.vscode
20+
__pycache__/
1921
.venv
20-
# operating system-related files
2122

22-
# file properties cache/storage on macOS
2323
*.DS_Store
24-
25-
# thumbnail cache on Windows
2624
Thumbs.db
2725
bankmanaging.db

BoardGame-CLI/python.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ def play_snakes_and_ladders():
8787

8888

8989
# Start the game
90-
play_snakes_and_ladders()
90+
if __name__ == "__main__":
91+
play_snakes_and_ladders()

BoardGame-CLI/snakeLadder.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def play():
9090
elif n == 2:
9191
players = {} # stores player ans their locations
9292
isReady = {}
93-
current_loc = 0 # vaiable for iterating location
93+
current_loc = 1 # reset starting location to 1
9494
player_input()
9595

9696
elif n == 3:
@@ -170,4 +170,5 @@ def ladder(a, i):
170170
print("/" * 40)
171171

172172

173-
player_input()
173+
if __name__ == "__main__":
174+
player_input()

BoardGame-CLI/uno.py

Lines changed: 140 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# uno game #
22

33
import random
4+
from typing import List
45

56
"""
67
Generate the UNO deck of 108 cards.
7-
Parameters: None
8-
Return values: deck=>list
8+
9+
Doctest examples:
10+
11+
>>> deck = buildDeck()
12+
>>> len(deck)
13+
108
14+
>>> sum(1 for c in deck if 'Wild' in c)
15+
8
16+
17+
Return: list of card strings (e.g. 'Red 7', 'Wild Draw Four')
918
"""
1019

1120

12-
def buildDeck():
13-
deck = []
21+
def buildDeck() -> List[str]:
22+
deck: List[str] = []
1423
# example card:Red 7,Green 8, Blue skip
1524
colours = ["Red", "Green", "Yellow", "Blue"]
1625
values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "Draw Two", "Skip", "Reverse"]
@@ -24,7 +33,6 @@ def buildDeck():
2433
for i in range(4):
2534
deck.append(wilds[0])
2635
deck.append(wilds[1])
27-
print(deck)
2836
return deck
2937

3038

@@ -35,10 +43,9 @@ def buildDeck():
3543
"""
3644

3745

38-
def shuffleDeck(deck):
39-
for cardPos in range(len(deck)):
40-
randPos = random.randint(0, 107)
41-
deck[cardPos], deck[randPos] = deck[randPos], deck[cardPos]
46+
def shuffleDeck(deck: List[str]) -> List[str]:
47+
# use Python's built-in shuffle which is efficient and correct
48+
random.shuffle(deck)
4249
return deck
4350

4451

@@ -48,10 +55,18 @@ def shuffleDeck(deck):
4855
"""
4956

5057

51-
def drawCards(numCards):
52-
cardsDrawn = []
58+
def drawCards(numCards: int) -> List[str]:
59+
"""
60+
Draw a number of cards from the top of the global `unoDeck`.
61+
62+
Raises ValueError if the deck runs out of cards.
63+
"""
64+
cardsDrawn: List[str] = []
5365
for x in range(numCards):
54-
cardsDrawn.append(unoDeck.pop(0))
66+
try:
67+
cardsDrawn.append(unoDeck.pop(0))
68+
except IndexError:
69+
raise ValueError("The deck is empty; cannot draw more cards")
5570
return cardsDrawn
5671

5772

@@ -62,7 +77,7 @@ def drawCards(numCards):
6277
"""
6378

6479

65-
def showHand(player, playerHand):
80+
def showHand(player: int, playerHand: List[str]) -> None:
6681
print("Player {}'s Turn".format(players_name[player]))
6782
print("Your Hand")
6883
print("------------------")
@@ -80,7 +95,15 @@ def showHand(player, playerHand):
8095
"""
8196

8297

83-
def canPlay(colour, value, playerHand):
98+
def canPlay(colour: str, value: str, playerHand: List[str]) -> bool:
99+
"""
100+
Return True if any card in playerHand is playable on a discard with given colour and value.
101+
102+
>>> canPlay('Red','5',['Red 3','Green 5'])
103+
True
104+
>>> canPlay('Blue','7',['Green 1'])
105+
False
106+
"""
84107
for card in playerHand:
85108
if "Wild" in card:
86109
return True
@@ -89,101 +112,114 @@ def canPlay(colour, value, playerHand):
89112
return False
90113

91114

115+
# --- Global deck and initial setup ---
92116
unoDeck = buildDeck()
93117
unoDeck = shuffleDeck(unoDeck)
94118
unoDeck = shuffleDeck(unoDeck)
95-
discards = []
119+
discards: List[str] = []
96120

97-
players_name = []
98-
players = []
121+
players_name: List[str] = []
122+
players: List[List[str]] = []
99123
colours = ["Red", "Green", "Yellow", "Blue"]
100-
numPlayers = int(input("How many players?"))
101-
while numPlayers < 2 or numPlayers > 4:
102-
numPlayers = int(
103-
input("Invalid. Please enter a number between 2-4.\nHow many players?")
104-
)
105-
for player in range(numPlayers):
106-
players_name.append(input("Enter player {} name: ".format(player + 1)))
107-
players.append(drawCards(5))
108-
109-
110-
playerTurn = 0
111-
playDirection = 1
112-
playing = True
113-
discards.append(unoDeck.pop(0))
114-
splitCard = discards[0].split(" ", 1)
115-
currentColour = splitCard[0]
116-
if currentColour != "Wild":
117-
cardVal = splitCard[1]
118-
else:
119-
cardVal = "Any"
120-
121-
while playing:
122-
showHand(playerTurn, players[playerTurn])
123-
print("Card on top of discard pile: {}".format(discards[-1]))
124-
if canPlay(currentColour, cardVal, players[playerTurn]):
125-
cardChosen = int(input("Which card do you want to play?"))
126-
while not canPlay(
127-
currentColour, cardVal, [players[playerTurn][cardChosen - 1]]
128-
):
129-
cardChosen = int(input("Not a valid card. Which card do you want to play?"))
130-
print("You played {}".format(players[playerTurn][cardChosen - 1]))
131-
discards.append(players[playerTurn].pop(cardChosen - 1))
132-
133-
# cheak if player won
134-
if len(players[playerTurn]) == 0:
135-
playing = False
136-
# winner = "Player {}".format(playerTurn+1)
137-
winner = players_name[playerTurn]
138-
else:
139-
# cheak for special cards
140-
splitCard = discards[-1].split(" ", 1)
141-
currentColour = splitCard[0]
142-
if len(splitCard) == 1:
143-
cardVal = "Any"
144-
else:
145-
cardVal = splitCard[1]
146-
if currentColour == "Wild":
147-
for x in range(len(colours)):
148-
print("{}) {}".format(x + 1, colours[x]))
149-
newColour = int(input("What colour would you like to choose? "))
150-
while newColour < 1 or newColour > 4:
151-
newColour = int(
152-
input("Invalid option. What colour would you like to choose")
153-
)
154-
currentColour = colours[newColour - 1]
155-
if cardVal == "Reverse":
156-
playDirection = playDirection * -1
157-
elif cardVal == "Skip":
158-
playerTurn += playDirection
159-
if playerTurn >= numPlayers:
160-
playerTurn = 0
161-
elif playerTurn < 0:
162-
playerTurn = numPlayers - 1
163-
elif cardVal == "Draw Two":
164-
playerDraw = playerTurn + playDirection
165-
if playerDraw == numPlayers:
166-
playerDraw = 0
167-
elif playerDraw < 0:
168-
playerDraw = numPlayers - 1
169-
players[playerDraw].extend(drawCards(2))
170-
elif cardVal == "Draw Four":
171-
playerDraw = playerTurn + playDirection
172-
if playerDraw == numPlayers:
173-
playerDraw = 0
174-
elif playerDraw < 0:
175-
playerDraw = numPlayers - 1
176-
players[playerDraw].extend(drawCards(4))
177-
print("")
124+
125+
126+
def main() -> None:
127+
"""Run interactive UNO game (keeps original behavior).
128+
129+
Note: main() is interactive and not exercised by doctest.
130+
"""
131+
global players_name, players, discards
132+
133+
numPlayers = int(input("How many players?"))
134+
while numPlayers < 2 or numPlayers > 4:
135+
numPlayers = int(
136+
input("Invalid. Please enter a number between 2-4.\nHow many players?")
137+
)
138+
for player in range(numPlayers):
139+
players_name.append(input("Enter player {} name: ".format(player + 1)))
140+
players.append(drawCards(5))
141+
142+
playerTurn = 0
143+
playDirection = 1
144+
playing = True
145+
discards.append(unoDeck.pop(0))
146+
splitCard = discards[0].split(" ", 1)
147+
currentColour = splitCard[0]
148+
if currentColour != "Wild":
149+
cardVal = splitCard[1]
178150
else:
179-
print("You can't play. You have to draw a card.")
180-
players[playerTurn].extend(drawCards(1))
151+
cardVal = "Any"
152+
153+
while playing:
154+
showHand(playerTurn, players[playerTurn])
155+
print("Card on top of discard pile: {}".format(discards[-1]))
156+
if canPlay(currentColour, cardVal, players[playerTurn]):
157+
cardChosen = int(input("Which card do you want to play?"))
158+
while not canPlay(
159+
currentColour, cardVal, [players[playerTurn][cardChosen - 1]]
160+
):
161+
cardChosen = int(input("Not a valid card. Which card do you want to play?"))
162+
print("You played {}".format(players[playerTurn][cardChosen - 1]))
163+
discards.append(players[playerTurn].pop(cardChosen - 1))
164+
165+
# cheak if player won
166+
if len(players[playerTurn]) == 0:
167+
playing = False
168+
# winner = "Player {}".format(playerTurn+1)
169+
winner = players_name[playerTurn]
170+
else:
171+
# cheak for special cards
172+
splitCard = discards[-1].split(" ", 1)
173+
currentColour = splitCard[0]
174+
if len(splitCard) == 1:
175+
cardVal = "Any"
176+
else:
177+
cardVal = splitCard[1]
178+
if currentColour == "Wild":
179+
for x in range(len(colours)):
180+
print("{}) {}".format(x + 1, colours[x]))
181+
newColour = int(input("What colour would you like to choose? "))
182+
while newColour < 1 or newColour > 4:
183+
newColour = int(
184+
input("Invalid option. What colour would you like to choose")
185+
)
186+
currentColour = colours[newColour - 1]
187+
if cardVal == "Reverse":
188+
playDirection = playDirection * -1
189+
elif cardVal == "Skip":
190+
playerTurn += playDirection
191+
if playerTurn >= numPlayers:
192+
playerTurn = 0
193+
elif playerTurn < 0:
194+
playerTurn = numPlayers - 1
195+
elif cardVal == "Draw Two":
196+
playerDraw = playerTurn + playDirection
197+
if playerDraw == numPlayers:
198+
playerDraw = 0
199+
elif playerDraw < 0:
200+
playerDraw = numPlayers - 1
201+
players[playerDraw].extend(drawCards(2))
202+
elif cardVal == "Draw Four":
203+
playerDraw = playerTurn + playDirection
204+
if playerDraw == numPlayers:
205+
playerDraw = 0
206+
elif playerDraw < 0:
207+
playerDraw = numPlayers - 1
208+
players[playerDraw].extend(drawCards(4))
209+
print("")
210+
else:
211+
print("You can't play. You have to draw a card.")
212+
players[playerTurn].extend(drawCards(1))
213+
214+
playerTurn += playDirection
215+
if playerTurn >= numPlayers:
216+
playerTurn = 0
217+
elif playerTurn < 0:
218+
playerTurn = numPlayers - 1
219+
220+
print("Game Over")
221+
print("{} is the Winner!".format(winner))
181222

182-
playerTurn += playDirection
183-
if playerTurn >= numPlayers:
184-
playerTurn = 0
185-
elif playerTurn < 0:
186-
playerTurn = numPlayers - 1
187223

188-
print("Game Over")
189-
print("{} is the Winner!".format(winner))
224+
if __name__ == "__main__":
225+
main()

0 commit comments

Comments
 (0)