Skip to content

Commit d145c84

Browse files
Merge pull request #1513 from Harivansh-coder/master
Added some cli board games
2 parents d244266 + 0384815 commit d145c84

File tree

2 files changed

+354
-0
lines changed

2 files changed

+354
-0
lines changed

BoardGame-CLI/snakeLadder.py

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import random
2+
3+
# Taking players data
4+
players = {} # stores players name their locations
5+
isReady = {}
6+
current_loc = 1 # vaiable for iterating location
7+
8+
imp = True
9+
10+
11+
# players input function
12+
def player_input():
13+
global players
14+
global current_loc
15+
global isReady
16+
17+
y = True
18+
while y:
19+
player_num = int(input("Enter the number of players: "))
20+
if player_num > 0:
21+
for i in range(player_num):
22+
name = input(f"Enter player {i+1} name: ")
23+
players[name] = current_loc
24+
isReady[name] = False
25+
y = False
26+
play() # play funtion call
27+
28+
else:
29+
print("Number of player cannot be zero")
30+
print()
31+
32+
33+
# Dice roll method
34+
def roll():
35+
# print(players)
36+
return random.randrange(1, 7)
37+
38+
39+
# play method
40+
def play():
41+
global players
42+
global isReady
43+
global imp
44+
45+
while imp:
46+
print("/"*20)
47+
print("1 -> roll the dice (or enter)")
48+
print("2 -> start new game")
49+
print("3 -> exit the game")
50+
print("/"*20)
51+
52+
for i in players:
53+
n = input("{}'s turn: ".format(i)) or 1
54+
n = int(n)
55+
56+
if players[i] < 100:
57+
if n == 1:
58+
temp1 = roll()
59+
print(f"you got {temp1}")
60+
print("")
61+
62+
if isReady[i] == False and temp1 == 6:
63+
isReady[i] = True
64+
65+
if isReady[i]:
66+
looproll = temp1
67+
while looproll == 6:
68+
looproll = roll()
69+
temp1 += looproll
70+
print(f"you got {looproll} ")
71+
print("")
72+
# print(temp1)
73+
if (players[i] + temp1) > 100:
74+
pass
75+
elif (players[i] + temp1) < 100:
76+
players[i] += temp1
77+
players[i] = move(players[i], i)
78+
elif (players[i] + temp1) == 100:
79+
print(f"congrats {i} you won !!!")
80+
imp = False
81+
return
82+
83+
print(f"you are at position {players[i]}")
84+
85+
elif n == 2:
86+
players = {} # stores player ans their locations
87+
isReady = {}
88+
current_loc = 0 # vaiable for iterating location
89+
player_input()
90+
91+
elif n == 3:
92+
print("Bye Bye")
93+
imp = False
94+
95+
else:
96+
print("pls enter a valid input")
97+
98+
99+
# Move method
100+
def move(a, i):
101+
global players
102+
global imp
103+
temp_loc = players[i]
104+
105+
if (temp_loc) < 100:
106+
temp_loc = ladder(temp_loc, i)
107+
temp_loc = snake(temp_loc, i)
108+
109+
return temp_loc
110+
111+
112+
# snake bite code
113+
def snake(c, i):
114+
if (c == 32):
115+
players[i] = 10
116+
elif (c == 36):
117+
players[i] = 6
118+
elif (c == 48):
119+
players[i] = 26
120+
elif (c == 63):
121+
players[i] = 18
122+
elif (c == 88):
123+
players[i] = 24
124+
elif (c == 95):
125+
players[i] = 56
126+
elif (c == 97):
127+
players[i] = 78
128+
else:
129+
return players[i]
130+
print(f"You got bitten by a snake now you are at {players[i]}")
131+
132+
return players[i]
133+
134+
135+
# ladder code
136+
def ladder(a, i):
137+
global players
138+
139+
if (a == 4):
140+
players[i] = 14
141+
elif (a == 8):
142+
players[i] = 30
143+
elif (a == 20):
144+
players[i] = 38
145+
elif (a == 40):
146+
players[i] = 42
147+
elif (a == 28):
148+
players[i] = 76
149+
elif (a == 50):
150+
players[i] = 67
151+
elif (a == 71):
152+
players[i] = 92
153+
elif (a == 88):
154+
players[i] = 99
155+
else:
156+
return players[i]
157+
print(f"You got a ladder now you are at {players[i]}")
158+
159+
return players[i]
160+
161+
162+
# while run:
163+
print("/"*40)
164+
print("Welcome to the snake ladder game !!!!!!!")
165+
print("/"*40)
166+
167+
168+
player_input()

BoardGame-CLI/uno.py

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# uno game #
2+
3+
import random
4+
"""
5+
Generate the UNO deck of 108 cards.
6+
Parameters: None
7+
Return values: deck=>list
8+
"""
9+
10+
11+
def buildDeck():
12+
deck = []
13+
# example card:Red 7,Green 8, Blue skip
14+
colours = ["Red", "Green", "Yellow", "Blue"]
15+
values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "Draw Two", "Skip", "Reverse"]
16+
wilds = ["Wild", "Wild Draw Four"]
17+
for colour in colours:
18+
for value in values:
19+
cardVal = "{} {}".format(colour, value)
20+
deck.append(cardVal)
21+
if value != 0:
22+
deck.append(cardVal)
23+
for i in range(4):
24+
deck.append(wilds[0])
25+
deck.append(wilds[1])
26+
print(deck)
27+
return deck
28+
29+
30+
"""
31+
Shuffles a list of items passed into it
32+
Parameters: deck=>list
33+
Return values: deck=>list
34+
"""
35+
36+
37+
def shuffleDeck(deck):
38+
for cardPos in range(len(deck)):
39+
randPos = random.randint(0, 107)
40+
deck[cardPos], deck[randPos] = deck[randPos], deck[cardPos]
41+
return deck
42+
43+
44+
"""Draw card function that draws a specified number of cards off the top of the deck
45+
Parameters: numCards -> integer
46+
Return: cardsDrawn -> list
47+
"""
48+
49+
50+
def drawCards(numCards):
51+
cardsDrawn = []
52+
for x in range(numCards):
53+
cardsDrawn.append(unoDeck.pop(0))
54+
return cardsDrawn
55+
56+
57+
"""
58+
Print formatted list of player's hand
59+
Parameter: player->integer , playerHand->list
60+
Return: None
61+
"""
62+
63+
64+
def showHand(player, playerHand):
65+
print("Player {}'s Turn".format(players_name[player]))
66+
print("Your Hand")
67+
print("------------------")
68+
y = 1
69+
for card in playerHand:
70+
print("{}) {}".format(y, card))
71+
y += 1
72+
print("")
73+
74+
75+
"""
76+
Check whether a player is able to play a card, or not
77+
Parameters: discardCard->string,value->string, playerHand->list
78+
Return: boolean
79+
"""
80+
81+
82+
def canPlay(colour, value, playerHand):
83+
for card in playerHand:
84+
if "Wild" in card:
85+
return True
86+
elif colour in card or value in card:
87+
return True
88+
return False
89+
90+
91+
unoDeck = buildDeck()
92+
unoDeck = shuffleDeck(unoDeck)
93+
unoDeck = shuffleDeck(unoDeck)
94+
discards = []
95+
96+
players_name = []
97+
players = []
98+
colours = ["Red", "Green", "Yellow", "Blue"]
99+
numPlayers = int(input("How many players?"))
100+
while numPlayers < 2 or numPlayers > 4:
101+
numPlayers = int(
102+
input("Invalid. Please enter a number between 2-4.\nHow many players?"))
103+
for player in range(numPlayers):
104+
players_name.append(input("Enter player {} name: ".format(player+1)))
105+
players.append(drawCards(5))
106+
107+
108+
playerTurn = 0
109+
playDirection = 1
110+
playing = True
111+
discards.append(unoDeck.pop(0))
112+
splitCard = discards[0].split(" ", 1)
113+
currentColour = splitCard[0]
114+
if currentColour != "Wild":
115+
cardVal = splitCard[1]
116+
else:
117+
cardVal = "Any"
118+
119+
while playing:
120+
showHand(playerTurn, players[playerTurn])
121+
print("Card on top of discard pile: {}".format(discards[-1]))
122+
if canPlay(currentColour, cardVal, players[playerTurn]):
123+
cardChosen = int(input("Which card do you want to play?"))
124+
while not canPlay(currentColour, cardVal, [players[playerTurn][cardChosen-1]]):
125+
cardChosen = int(
126+
input("Not a valid card. Which card do you want to play?"))
127+
print("You played {}".format(players[playerTurn][cardChosen-1]))
128+
discards.append(players[playerTurn].pop(cardChosen-1))
129+
130+
# cheak if player won
131+
if len(players[playerTurn]) == 0:
132+
playing = False
133+
# winner = "Player {}".format(playerTurn+1)
134+
winner = players_name[playerTurn]
135+
else:
136+
# cheak for special cards
137+
splitCard = discards[-1].split(" ", 1)
138+
currentColour = splitCard[0]
139+
if len(splitCard) == 1:
140+
cardVal = "Any"
141+
else:
142+
cardVal = splitCard[1]
143+
if currentColour == "Wild":
144+
for x in range(len(colours)):
145+
print("{}) {}".format(x+1, colours[x]))
146+
newColour = int(
147+
input("What colour would you like to choose? "))
148+
while newColour < 1 or newColour > 4:
149+
newColour = int(
150+
input("Invalid option. What colour would you like to choose"))
151+
currentColour = colours[newColour-1]
152+
if cardVal == "Reverse":
153+
playDirection = playDirection * -1
154+
elif cardVal == "Skip":
155+
playerTurn += playDirection
156+
if playerTurn >= numPlayers:
157+
playerTurn = 0
158+
elif playerTurn < 0:
159+
playerTurn = numPlayers-1
160+
elif cardVal == "Draw Two":
161+
playerDraw = playerTurn+playDirection
162+
if playerDraw == numPlayers:
163+
playerDraw = 0
164+
elif playerDraw < 0:
165+
playerDraw = numPlayers-1
166+
players[playerDraw].extend(drawCards(2))
167+
elif cardVal == "Draw Four":
168+
playerDraw = playerTurn+playDirection
169+
if playerDraw == numPlayers:
170+
playerDraw = 0
171+
elif playerDraw < 0:
172+
playerDraw = numPlayers-1
173+
players[playerDraw].extend(drawCards(4))
174+
print("")
175+
else:
176+
print("You can't play. You have to draw a card.")
177+
players[playerTurn].extend(drawCards(1))
178+
179+
playerTurn += playDirection
180+
if playerTurn >= numPlayers:
181+
playerTurn = 0
182+
elif playerTurn < 0:
183+
playerTurn = numPlayers-1
184+
185+
print("Game Over")
186+
print("{} is the Winner!".format(winner))

0 commit comments

Comments
 (0)