-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1513 from Harivansh-coder/master
Added some cli board games
- Loading branch information
Showing
2 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import random | ||
|
||
# Taking players data | ||
players = {} # stores players name their locations | ||
isReady = {} | ||
current_loc = 1 # vaiable for iterating location | ||
|
||
imp = True | ||
|
||
|
||
# players input function | ||
def player_input(): | ||
global players | ||
global current_loc | ||
global isReady | ||
|
||
y = True | ||
while y: | ||
player_num = int(input("Enter the number of players: ")) | ||
if player_num > 0: | ||
for i in range(player_num): | ||
name = input(f"Enter player {i+1} name: ") | ||
players[name] = current_loc | ||
isReady[name] = False | ||
y = False | ||
play() # play funtion call | ||
|
||
else: | ||
print("Number of player cannot be zero") | ||
print() | ||
|
||
|
||
# Dice roll method | ||
def roll(): | ||
# print(players) | ||
return random.randrange(1, 7) | ||
|
||
|
||
# play method | ||
def play(): | ||
global players | ||
global isReady | ||
global imp | ||
|
||
while imp: | ||
print("/"*20) | ||
print("1 -> roll the dice (or enter)") | ||
print("2 -> start new game") | ||
print("3 -> exit the game") | ||
print("/"*20) | ||
|
||
for i in players: | ||
n = input("{}'s turn: ".format(i)) or 1 | ||
n = int(n) | ||
|
||
if players[i] < 100: | ||
if n == 1: | ||
temp1 = roll() | ||
print(f"you got {temp1}") | ||
print("") | ||
|
||
if isReady[i] == False and temp1 == 6: | ||
isReady[i] = True | ||
|
||
if isReady[i]: | ||
looproll = temp1 | ||
while looproll == 6: | ||
looproll = roll() | ||
temp1 += looproll | ||
print(f"you got {looproll} ") | ||
print("") | ||
# print(temp1) | ||
if (players[i] + temp1) > 100: | ||
pass | ||
elif (players[i] + temp1) < 100: | ||
players[i] += temp1 | ||
players[i] = move(players[i], i) | ||
elif (players[i] + temp1) == 100: | ||
print(f"congrats {i} you won !!!") | ||
imp = False | ||
return | ||
|
||
print(f"you are at position {players[i]}") | ||
|
||
elif n == 2: | ||
players = {} # stores player ans their locations | ||
isReady = {} | ||
current_loc = 0 # vaiable for iterating location | ||
player_input() | ||
|
||
elif n == 3: | ||
print("Bye Bye") | ||
imp = False | ||
|
||
else: | ||
print("pls enter a valid input") | ||
|
||
|
||
# Move method | ||
def move(a, i): | ||
global players | ||
global imp | ||
temp_loc = players[i] | ||
|
||
if (temp_loc) < 100: | ||
temp_loc = ladder(temp_loc, i) | ||
temp_loc = snake(temp_loc, i) | ||
|
||
return temp_loc | ||
|
||
|
||
# snake bite code | ||
def snake(c, i): | ||
if (c == 32): | ||
players[i] = 10 | ||
elif (c == 36): | ||
players[i] = 6 | ||
elif (c == 48): | ||
players[i] = 26 | ||
elif (c == 63): | ||
players[i] = 18 | ||
elif (c == 88): | ||
players[i] = 24 | ||
elif (c == 95): | ||
players[i] = 56 | ||
elif (c == 97): | ||
players[i] = 78 | ||
else: | ||
return players[i] | ||
print(f"You got bitten by a snake now you are at {players[i]}") | ||
|
||
return players[i] | ||
|
||
|
||
# ladder code | ||
def ladder(a, i): | ||
global players | ||
|
||
if (a == 4): | ||
players[i] = 14 | ||
elif (a == 8): | ||
players[i] = 30 | ||
elif (a == 20): | ||
players[i] = 38 | ||
elif (a == 40): | ||
players[i] = 42 | ||
elif (a == 28): | ||
players[i] = 76 | ||
elif (a == 50): | ||
players[i] = 67 | ||
elif (a == 71): | ||
players[i] = 92 | ||
elif (a == 88): | ||
players[i] = 99 | ||
else: | ||
return players[i] | ||
print(f"You got a ladder now you are at {players[i]}") | ||
|
||
return players[i] | ||
|
||
|
||
# while run: | ||
print("/"*40) | ||
print("Welcome to the snake ladder game !!!!!!!") | ||
print("/"*40) | ||
|
||
|
||
player_input() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
# uno game # | ||
|
||
import random | ||
""" | ||
Generate the UNO deck of 108 cards. | ||
Parameters: None | ||
Return values: deck=>list | ||
""" | ||
|
||
|
||
def buildDeck(): | ||
deck = [] | ||
# example card:Red 7,Green 8, Blue skip | ||
colours = ["Red", "Green", "Yellow", "Blue"] | ||
values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "Draw Two", "Skip", "Reverse"] | ||
wilds = ["Wild", "Wild Draw Four"] | ||
for colour in colours: | ||
for value in values: | ||
cardVal = "{} {}".format(colour, value) | ||
deck.append(cardVal) | ||
if value != 0: | ||
deck.append(cardVal) | ||
for i in range(4): | ||
deck.append(wilds[0]) | ||
deck.append(wilds[1]) | ||
print(deck) | ||
return deck | ||
|
||
|
||
""" | ||
Shuffles a list of items passed into it | ||
Parameters: deck=>list | ||
Return values: deck=>list | ||
""" | ||
|
||
|
||
def shuffleDeck(deck): | ||
for cardPos in range(len(deck)): | ||
randPos = random.randint(0, 107) | ||
deck[cardPos], deck[randPos] = deck[randPos], deck[cardPos] | ||
return deck | ||
|
||
|
||
"""Draw card function that draws a specified number of cards off the top of the deck | ||
Parameters: numCards -> integer | ||
Return: cardsDrawn -> list | ||
""" | ||
|
||
|
||
def drawCards(numCards): | ||
cardsDrawn = [] | ||
for x in range(numCards): | ||
cardsDrawn.append(unoDeck.pop(0)) | ||
return cardsDrawn | ||
|
||
|
||
""" | ||
Print formatted list of player's hand | ||
Parameter: player->integer , playerHand->list | ||
Return: None | ||
""" | ||
|
||
|
||
def showHand(player, playerHand): | ||
print("Player {}'s Turn".format(players_name[player])) | ||
print("Your Hand") | ||
print("------------------") | ||
y = 1 | ||
for card in playerHand: | ||
print("{}) {}".format(y, card)) | ||
y += 1 | ||
print("") | ||
|
||
|
||
""" | ||
Check whether a player is able to play a card, or not | ||
Parameters: discardCard->string,value->string, playerHand->list | ||
Return: boolean | ||
""" | ||
|
||
|
||
def canPlay(colour, value, playerHand): | ||
for card in playerHand: | ||
if "Wild" in card: | ||
return True | ||
elif colour in card or value in card: | ||
return True | ||
return False | ||
|
||
|
||
unoDeck = buildDeck() | ||
unoDeck = shuffleDeck(unoDeck) | ||
unoDeck = shuffleDeck(unoDeck) | ||
discards = [] | ||
|
||
players_name = [] | ||
players = [] | ||
colours = ["Red", "Green", "Yellow", "Blue"] | ||
numPlayers = int(input("How many players?")) | ||
while numPlayers < 2 or numPlayers > 4: | ||
numPlayers = int( | ||
input("Invalid. Please enter a number between 2-4.\nHow many players?")) | ||
for player in range(numPlayers): | ||
players_name.append(input("Enter player {} name: ".format(player+1))) | ||
players.append(drawCards(5)) | ||
|
||
|
||
playerTurn = 0 | ||
playDirection = 1 | ||
playing = True | ||
discards.append(unoDeck.pop(0)) | ||
splitCard = discards[0].split(" ", 1) | ||
currentColour = splitCard[0] | ||
if currentColour != "Wild": | ||
cardVal = splitCard[1] | ||
else: | ||
cardVal = "Any" | ||
|
||
while playing: | ||
showHand(playerTurn, players[playerTurn]) | ||
print("Card on top of discard pile: {}".format(discards[-1])) | ||
if canPlay(currentColour, cardVal, players[playerTurn]): | ||
cardChosen = int(input("Which card do you want to play?")) | ||
while not canPlay(currentColour, cardVal, [players[playerTurn][cardChosen-1]]): | ||
cardChosen = int( | ||
input("Not a valid card. Which card do you want to play?")) | ||
print("You played {}".format(players[playerTurn][cardChosen-1])) | ||
discards.append(players[playerTurn].pop(cardChosen-1)) | ||
|
||
# cheak if player won | ||
if len(players[playerTurn]) == 0: | ||
playing = False | ||
# winner = "Player {}".format(playerTurn+1) | ||
winner = players_name[playerTurn] | ||
else: | ||
# cheak for special cards | ||
splitCard = discards[-1].split(" ", 1) | ||
currentColour = splitCard[0] | ||
if len(splitCard) == 1: | ||
cardVal = "Any" | ||
else: | ||
cardVal = splitCard[1] | ||
if currentColour == "Wild": | ||
for x in range(len(colours)): | ||
print("{}) {}".format(x+1, colours[x])) | ||
newColour = int( | ||
input("What colour would you like to choose? ")) | ||
while newColour < 1 or newColour > 4: | ||
newColour = int( | ||
input("Invalid option. What colour would you like to choose")) | ||
currentColour = colours[newColour-1] | ||
if cardVal == "Reverse": | ||
playDirection = playDirection * -1 | ||
elif cardVal == "Skip": | ||
playerTurn += playDirection | ||
if playerTurn >= numPlayers: | ||
playerTurn = 0 | ||
elif playerTurn < 0: | ||
playerTurn = numPlayers-1 | ||
elif cardVal == "Draw Two": | ||
playerDraw = playerTurn+playDirection | ||
if playerDraw == numPlayers: | ||
playerDraw = 0 | ||
elif playerDraw < 0: | ||
playerDraw = numPlayers-1 | ||
players[playerDraw].extend(drawCards(2)) | ||
elif cardVal == "Draw Four": | ||
playerDraw = playerTurn+playDirection | ||
if playerDraw == numPlayers: | ||
playerDraw = 0 | ||
elif playerDraw < 0: | ||
playerDraw = numPlayers-1 | ||
players[playerDraw].extend(drawCards(4)) | ||
print("") | ||
else: | ||
print("You can't play. You have to draw a card.") | ||
players[playerTurn].extend(drawCards(1)) | ||
|
||
playerTurn += playDirection | ||
if playerTurn >= numPlayers: | ||
playerTurn = 0 | ||
elif playerTurn < 0: | ||
playerTurn = numPlayers-1 | ||
|
||
print("Game Over") | ||
print("{} is the Winner!".format(winner)) |