-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSujalSamai.py
210 lines (170 loc) · 6.86 KB
/
SujalSamai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#Author: Sujal Samai
#Date Created:
#Date Created: 1 Aug,2021
#Title: UNO CARD GAME
# Importing libraries
import random
'''
Generate the UNO deck of 108 cards.
Parameter: None
Return value: deck->list
'''
def buildDeck():
deck = []
colors = ["Red","Green","Yellow","Blue"]
values = [0,1,2,3,4,5,6,7,8,9,"Draw Two","Skip","Reverse"]
# appending combination of all this 3 list into the deck using for loops
for color in colors:
for value in values:
card="{} {}".format(color, value)
=======
card="{} {}".format(color, value) # formate for this -> (color,values)
deck.append(card)
if value !=0:
deck.append(card)
wilds = ["Wild","Wild draw four"]
for wild in range(4):
deck.append(wilds[0])
deck.append(wilds[1])
#print(deck)
return deck
def shuffleDeck(deck):
for card_position in range(len(deck)):
random_position = random.randint(0, 107)
deck[card_position],deck[random_position]= deck[random_position],deck[card_position]
return deck
def drawCards(numCards):
cardDrawn = []
for cards in range(numCards):
cardDrawn.append(unoDeck.pop(0))
return cardDrawn
def showHand(player, playerHand):
# printing whose turn it is with +1 for index
print("Player {}'s Turn".format(player+1))
print("Your Hand")
print("*****************************")
x = 1 # index for the cards
for card in playerHand:
print("{}. {}".format(x,card))
x+=1
print("")
def canPlay(color, value, playerHand):
# checking cards in playerHand returning true if color or value is present or the card is wild else returning false
for card in playerHand:
if "Wild" in card:
return True
elif color in card or value in card:
return True
return False
unoDeck = buildDeck() # storing the cards in a variable
unoDeck = shuffleDeck(unoDeck) # storing shuffled deck in a variable
# print(unoDeck)
discards = [] # list for our discard pile
players = []
colors = ["Red", "Green", "Yellow", "Blue"]
# taking number of players as input we will be having 2 to 4 players
print("")
numPlayers = int(input("Enter the number of players (2/3/4): "))
if numPlayers<2 or numPlayers>4:
numPlayers = int(input("Invalid input... Please Enter the number of players between 2-4: "))
num=int(input("Enter How many cards you want to draw for each player (4-7): "))
print("")
print("Let's begin the Game!!")
print("")
for player in range(numPlayers):
players.append(drawCards(num))
# print(players)
playerTurn = 0
playDirection = 1
playing = True
discards.append(unoDeck.pop(0)) # appending top card from unodeck to our discard pile
splitCard= discards[0].split(" ",1)
currentcolor= splitCard[0]
if currentcolor!= "Wild":
cardValue= splitCard[1]
else:
cardValue="Any"
while playing:
showHand(playerTurn,players[playerTurn])
print("Card available on top of discard pile is {}".format(discards[-1]))
if canPlay(currentcolor,cardValue,players[playerTurn]):
cardChoice= int(input("Which Card do you want to play? "))
while not canPlay(currentcolor,cardValue,[players[playerTurn][cardChoice-1]]):
cardChoice= int(input("Invalid Card... Which Card do you want to play? "))
print("Player {} played: {}".format(playerTurn+1,players[playerTurn][cardChoice-1]))
discards.append(players[playerTurn].pop(cardChoice-1))
#check winner
if len(players[playerTurn])==0:
playing= False
winner="Player {}".format(playerTurn+1)
#special cards
else:
splitCard= discards[-1].split(" ",1)
currentcolor= splitCard[0]
if len(splitCard)==1:
cardValue="Any"
else:
cardValue=splitCard[1]
if currentcolor=="Wild":
if cardValue=="draw four":
for x in range(len(colors)):
print("{}. {}".format(x+1,colors[x]))
userChoice= int(input("Which color would you like to choose? "))
currentcolor=colors[userChoice-1]
print("Choosen color is {}".format(currentcolor))
while userChoice<1 or userChoice>4:
userChoice= int(input("Sorry, Invalid Option!! Which color would you like to choose? "))
currentcolor=colors[userChoice-1] #colors = ["Red", "Green", "Yellow", "Blue"]
playerDraw = playerTurn+playDirection
if playerDraw>=numPlayers:
playerDraw= 0
elif playerDraw<0:
playerDraw=numPlayers-1
players[playerDraw].extend(drawCards(4))
else:
for x in range(len(colors)):
print("{}. {}".format(x+1,colors[x]))
userChoice= int(input("Which color would you like to choose? "))
currentcolor=colors[userChoice-1]
print("Choosen color is {}".format(currentcolor))
while userChoice<1 or userChoice>4:
userChoice= int(input("Sorry, Invalid Option!! Which color would you like to choose? "))
currentcolor=colors[userChoice-1]
elif cardValue=="Reverse":
playDirection= playDirection* -1
elif cardValue=="Skip":
playerTurn= playerTurn+playDirection
if playerTurn>=numPlayers:
playerTurn= 0
elif playerTurn<0:
playerTurn=numPlayers-1
elif cardValue=="Draw Two":
playerDraw = playerTurn+playDirection
if playerDraw>=numPlayers:
playerDraw= 0
elif playerDraw<0:
playerDraw=numPlayers-1
players[playerDraw].extend(drawCards(2))
elif cardValue=="Draw Four":
playerDraw = playerTurn+playDirection
if playerDraw>=numPlayers:
playerDraw= 0
elif playerDraw<0:
playerDraw=numPlayers-1
players[playerDraw].extend(drawCards(4))
print("")
else:
print("Sorry! You can't play. Please draw a card.")
players[playerTurn].extend(drawCards(1))
print("")
playerTurn+= playDirection
if playerTurn>=numPlayers:
playerTurn= 0
elif playerTurn<0:
playerTurn=numPlayers-1
print("Game Over...")
print("COngratulations! {}...".format(winner))
print("You are the winner!!")
print("")
=======
uno_deck= buildDeck()