Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Data translation: We will 5 different colors: red(r), yellow(y), blue(b), green(g) and the last
- # one is my creation: no_color(n)
- # Each card will be a string with 1st component being the color and the next one the value of the card
- # Examples: red_12 = r12, yellow_5 = y5, wizard = nW, paso = nN
- from random import shuffle
- colors = ["r", "y", "b", "g", "n"]
- values = [str(i) for i in range(1, 14)]
- valuesNoColor = ["W", "N"]
- # Function 1 - Create the cards (4 times shuffled deck)
- def createCards():
- cards = list()
- for i in range(len(colors)-1):
- color = colors[i]
- for j in range(len(values)):
- value = values[j]
- cards.append(color + value)
- # No_color cards
- color = colors[len(colors)-1]
- for i in range(4):
- cards.append(color + valuesNoColor[0])
- cards.append(color + valuesNoColor[1])
- shuffle(cards)
- shuffle(cards)
- shuffle(cards)
- shuffle(cards)
- return cards
- # FUNCTION 2 - Find driver color
- def findDriverColor(card1, card2, card3, card4):
- # All depending on card1
- if card1[0] != "n":
- return card1[0]
- else:
- # Card1 is wizard or paso
- if card1[1] == "W":
- return "NONE"
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- # PLAYER 1 HAS PASO
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- else:
- # 1st player has paso and we check the second one
- if card2[0] != "n":
- return card2[0]
- else:
- # Card2 is wizard or paso
- if card2[1] == "W":
- return "NONE"
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- # PLAYER 1 AND 2 HAVE PASO
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- else:
- if card3[0] != "n":
- return card3[0]
- else:
- # Card3 is wizard or paso
- if card3[1] == "W":
- return "NONE"
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- # PLAYER 1, 2 AND 3 HAVE PASO
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- else:
- if card4[0] != "n":
- return card4[0]
- else:
- # Card4 is wizard or paso
- if card4[1] == "W":
- return "NONE"
- else:
- # ALL PLAYERS HAVE PASO, 1ST ONE WINS
- return "NONE, P1 WINS"
- # Function 3 - Select atou color
- def findAtouColor(card1, atou):
- # 1. ATOU - I have to check some cases
- atouColor = "NONE"
- if atou[0] != "n":
- atouColor = atou[0]
- else:
- # If atou[1] == "N", remains the "NONE" choice for atoucolor
- if atou[1] == "W":
- # The wizard gives me the choice to select color
- # I will watch my card color
- if card1[0] != "n":
- atouColor = card1[0]
- else:
- # I have wizard or paso. In both cases, we will make the color red ("r")
- # In the 1st one I will all the times, in the next one I lose
- atouColor = "r"
- return atouColor
- # FUNCTION 4 - SHOW STATUS
- def showStatus(card1, card2, card3, card4, atou, driverColor, atouColor):
- print("*************************************************************************")
- print("Card1 = " + card1 + ", card2 = " + card2 + ", card3 = " + card3 + ", card4 = " + card4 + " |||| ATOU = " + atou)
- print("DriverColor = " + driverColor + ", atouColor = " + atouColor)
- # Function 5 - Give 1 card in n players and determine winner
- def determineWinner(cards):
- # First I give 4 cards and the 5th is the atou
- card1 = cards[0]
- card2 = cards[1]
- card3 = cards[2]
- card4 = cards[3]
- atou = cards[4]
- driverColor = findDriverColor(card1, card2, card3, card4)
- atouColor = findAtouColor(card1, atou)
- showStatus(card1, card2, card3, card4, atou, driverColor, atouColor)
- # Cases to determine the winner
- # 1. I check if there are WIZARDS
- cardsDown = [card1, card2, card3, card4]
- for i in range(len(cardsDown)):
- card = cards[i]
- if card == "nW":
- winner = i + 1
- print("1. Player " + str(winner) + " wins, because he is the 1st one with wizard in his hand.")
- print("*************************************************************************")
- return winner
- print("1. No wizard in the round")
- # 2. I check if there are cards in atouColor
- atouColorCards = list()
- indexPlayers = list()
- for i in range(len(cardsDown)):
- card = cardsDown[i]
- if card[0] == atouColor:
- # I will append a tuple in the form (card, player)
- atouColorCards.append(card)
- indexPlayers.append(i)
- # My list is completed and I scan it to find the winner
- atouBiggestValue = -1000
- atouWinner = -1
- if len(atouColorCards) > 0:
- print("2. Win will be decided by the highest card in the atouColor")
- for j in range(len(atouColorCards)):
- atouColorCard = atouColorCards[j]
- if int(atouColorCard[1:]) > atouBiggestValue:
- atouBiggestValue = int(atouColorCard[1:])
- atouWinner = indexPlayers[j]
- # After this process, we have the winner
- print("Player " + str(atouWinner + 1) + " wins, because he has the highest card in the atouColor")
- print("*************************************************************************")
- return atouWinner
- # 3. The last case: Win by the biggest card in the driverColor
- print("2. Noone has a card in the atouColor")
- driverColorCards = list()
- indexPlayers = list()
- for i in range(len(cardsDown)):
- card = cardsDown[i]
- if card[0] == driverColor:
- # I will append a tuple in the form (card, player)
- driverColorCards.append(card)
- indexPlayers.append(i)
- # My list is completed and I scan it to find the winner
- driverBiggestValue = -1000
- driverWinner = -1
- if len(driverColorCards) > 0:
- print("3. Win will be decided by the highest card in the driverColor")
- for j in range(len(driverColorCards)):
- driverColorCard = driverColorCards[j]
- if int(driverColorCard[1:]) > driverBiggestValue:
- driverBiggestValue = int(driverColorCard[1:])
- driverWinner = indexPlayers[j]
- # After this process, we have the winner
- print("Player " + str(driverWinner + 1) + " wins, because he has the highest card in the driverColor")
- print("*************************************************************************")
- return driverWinner
- # MAIN FUNCTION - Here I will find how many times does the player 1 win
- LIMIT = 5 * 10**3
- gamesWithNoPaso = 0
- gamesWon = 0
- for game in range(1, LIMIT+1):
- cards = createCards()
- card1 = cards[0]
- if card1 != "nN":
- gamesWithNoPaso += 1
- print("Game " + str(game))
- winner = determineWinner(cards)
- if winner == 1:
- gamesWon += 1
- print()
- else:
- print("Game " + str(game) + " started with player 1 having paso.")
- print("We don't check that case.")
- print()
- print("~~~~~~~~~~~~~~~~ Statistics ~~~~~~~~~~~~~~~~")
- print("Games not played: " + str(LIMIT - gamesWithNoPaso))
- print("Games played: " + str(gamesWithNoPaso))
- print("Games won: " + str(gamesWon))
- percentage = gamesWon / gamesWithNoPaso
- percentage100 = round(100 * percentage, 2)
- print("Player 1 won: " + str(percentage100) + "% of the games played.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement