|
| 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