Advertisement
makispaiktis

Wizard - Player 1 percentage of win

Aug 25th, 2021 (edited)
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.06 KB | None | 0 0
  1. # Data translation: We will 5 different colors: red(r), yellow(y), blue(b), green(g) and the last
  2. # one is my creation: no_color(n)
  3. # Each card will be a string with 1st component being the color and the next one the value of the card
  4. # Examples: red_12 = r12, yellow_5 = y5, wizard = nW, paso = nN
  5. from random import shuffle
  6.  
  7. colors = ["r", "y", "b", "g", "n"]
  8. values = [str(i) for i in range(1, 14)]
  9. valuesNoColor = ["W", "N"]
  10.  
  11. # Function 1 - Create the cards (4 times shuffled deck)
  12. def createCards():
  13.     cards = list()
  14.     for i in range(len(colors)-1):
  15.         color = colors[i]
  16.         for j in range(len(values)):
  17.             value = values[j]
  18.             cards.append(color + value)
  19.     # No_color cards
  20.     color = colors[len(colors)-1]
  21.     for i in range(4):
  22.         cards.append(color + valuesNoColor[0])
  23.         cards.append(color + valuesNoColor[1])
  24.     shuffle(cards)
  25.     shuffle(cards)
  26.     shuffle(cards)
  27.     shuffle(cards)
  28.     return cards
  29.  
  30.  
  31. # FUNCTION 2 - Find driver color
  32. def findDriverColor(card1, card2, card3, card4):
  33.     # All depending on card1
  34.     if card1[0] != "n":
  35.         return card1[0]
  36.     else:
  37.         # Card1 is wizard or paso
  38.         if card1[1] == "W":
  39.             return "NONE"
  40.         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  41.         # PLAYER 1 HAS PASO
  42.         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  43.         else:
  44.             # 1st player has paso and we check the second one
  45.             if card2[0] != "n":
  46.                 return card2[0]
  47.             else:
  48.                 # Card2 is wizard or paso
  49.                 if card2[1] == "W":
  50.                     return "NONE"
  51.                 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  52.                 # PLAYER 1 AND 2 HAVE PASO
  53.                 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  54.                 else:
  55.                     if card3[0] != "n":
  56.                         return card3[0]
  57.                     else:
  58.                         # Card3 is wizard or paso
  59.                         if card3[1] == "W":
  60.                             return "NONE"
  61.                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  62.                         # PLAYER 1, 2 AND 3 HAVE PASO
  63.                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  64.                         else:
  65.                             if card4[0] != "n":
  66.                                 return card4[0]
  67.                             else:
  68.                                 # Card4 is wizard or paso
  69.                                 if card4[1] == "W":
  70.                                     return "NONE"
  71.                                 else:
  72.                                     # ALL PLAYERS HAVE PASO, 1ST ONE WINS
  73.                                     return "NONE, P1 WINS"
  74.  
  75.  
  76.  
  77. # Function 3 - Select atou color
  78. def findAtouColor(card1, atou):
  79.     # 1. ATOU - I have to check some cases
  80.     atouColor = "NONE"
  81.     if atou[0] != "n":
  82.         atouColor = atou[0]
  83.     else:
  84.         # If atou[1] == "N", remains the "NONE" choice for atoucolor
  85.         if atou[1] == "W":
  86.             # The wizard gives me the choice to select color
  87.             # I will watch my card color
  88.             if card1[0] != "n":
  89.                 atouColor = card1[0]
  90.             else:
  91.                 # I have wizard or paso. In both cases, we will make the color red ("r")
  92.                 # In the 1st one I will all the times, in the next one I lose
  93.                 atouColor = "r"
  94.     return atouColor
  95.  
  96.  
  97. # FUNCTION 4 - SHOW STATUS
  98. def showStatus(card1, card2, card3, card4, atou, driverColor, atouColor):
  99.     print("*************************************************************************")
  100.     print("Card1 = " + card1 + ", card2 = " + card2 + ", card3 = " + card3 + ", card4 = " + card4 + "   ||||    ATOU = " + atou)
  101.     print("DriverColor = " + driverColor + ", atouColor = " + atouColor)
  102.  
  103.  
  104. # Function 5 - Give 1 card in n players and determine winner
  105. def determineWinner(cards):
  106.     # First I give 4 cards and the 5th is the atou
  107.     card1 = cards[0]
  108.     card2 = cards[1]
  109.     card3 = cards[2]
  110.     card4 = cards[3]
  111.     atou = cards[4]
  112.     driverColor = findDriverColor(card1, card2, card3, card4)
  113.     atouColor = findAtouColor(card1, atou)
  114.     showStatus(card1, card2, card3, card4, atou, driverColor, atouColor)
  115.  
  116.     # Cases to determine the winner
  117.     # 1. I check if there are WIZARDS
  118.     cardsDown = [card1, card2, card3, card4]
  119.     for i in range(len(cardsDown)):
  120.         card = cards[i]
  121.         if card == "nW":
  122.             winner = i + 1
  123.             print("1. Player " + str(winner) + " wins, because he is the 1st one with wizard in his hand.")
  124.             print("*************************************************************************")
  125.             return winner
  126.     print("1. No wizard in the round")
  127.  
  128.     # 2. I check if there are cards in atouColor
  129.     atouColorCards = list()
  130.     indexPlayers = list()
  131.     for i in range(len(cardsDown)):
  132.         card = cardsDown[i]
  133.         if card[0] == atouColor:
  134.             # I will append a tuple in the form (card, player)
  135.             atouColorCards.append(card)
  136.             indexPlayers.append(i)
  137.     # My list is completed and I scan it to find the winner
  138.     atouBiggestValue = -1000
  139.     atouWinner = -1
  140.     if len(atouColorCards) > 0:
  141.         print("2. Win will be decided by the highest card in the atouColor")
  142.         for j in range(len(atouColorCards)):
  143.             atouColorCard = atouColorCards[j]
  144.             if int(atouColorCard[1:]) > atouBiggestValue:
  145.                 atouBiggestValue = int(atouColorCard[1:])
  146.                 atouWinner = indexPlayers[j]
  147.         # After this process, we have the winner
  148.         print("Player " + str(atouWinner + 1) + " wins, because he has the highest card in the atouColor")
  149.         print("*************************************************************************")
  150.         return atouWinner
  151.  
  152.     # 3. The last case: Win by the biggest card in the driverColor
  153.     print("2. Noone has a card in the atouColor")
  154.     driverColorCards = list()
  155.     indexPlayers = list()
  156.     for i in range(len(cardsDown)):
  157.         card = cardsDown[i]
  158.         if card[0] == driverColor:
  159.             # I will append a tuple in the form (card, player)
  160.             driverColorCards.append(card)
  161.             indexPlayers.append(i)
  162.     # My list is completed and I scan it to find the winner
  163.     driverBiggestValue = -1000
  164.     driverWinner = -1
  165.     if len(driverColorCards) > 0:
  166.         print("3. Win will be decided by the highest card in the driverColor")
  167.         for j in range(len(driverColorCards)):
  168.             driverColorCard = driverColorCards[j]
  169.             if int(driverColorCard[1:]) > driverBiggestValue:
  170.                 driverBiggestValue = int(driverColorCard[1:])
  171.                 driverWinner = indexPlayers[j]
  172.         # After this process, we have the winner
  173.         print("Player " + str(driverWinner + 1) + " wins, because he has the highest card in the driverColor")
  174.         print("*************************************************************************")
  175.         return driverWinner
  176.  
  177.  
  178.  
  179. # MAIN FUNCTION - Here I will find how many times does the player 1 win
  180. LIMIT = 5 * 10**3
  181. gamesWithNoPaso = 0
  182. gamesWon = 0
  183. for game in range(1, LIMIT+1):
  184.     cards = createCards()
  185.     card1 = cards[0]
  186.     if card1 != "nN":
  187.         gamesWithNoPaso += 1
  188.         print("Game " + str(game))
  189.         winner = determineWinner(cards)
  190.         if winner == 1:
  191.             gamesWon += 1
  192.         print()
  193.     else:
  194.         print("Game " + str(game) + " started with player 1 having paso.")
  195.         print("We don't check that case.")
  196.  
  197. print()
  198. print("~~~~~~~~~~~~~~~~ Statistics ~~~~~~~~~~~~~~~~")
  199. print("Games not played: " + str(LIMIT - gamesWithNoPaso))
  200. print("Games played: " + str(gamesWithNoPaso))
  201. print("Games won: " + str(gamesWon))
  202. percentage = gamesWon / gamesWithNoPaso
  203. percentage100 = round(100 * percentage, 2)
  204. print("Player 1 won: " + str(percentage100) + "% of the games played.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement