Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.40 KB | None | 0 0
  1. import random
  2. import numpy as np
  3.  
  4. # *******************************************
  5. #tablica możliwych ruchów
  6.  
  7. MOVE_ARR = ['K', 'P', 'N']
  8.  
  9. MOVE_PLAYER1 = ['K', 'P', 'P', 'N', 'K', 'P', 'K', 'P', 'N', 'K', 'P', 'K', 'N', 'K', 'N', 'P', 'P', 'N', 'N', 'K', 'K', 'K', 'K', 'N', 'N', 'K', 'K', 'P', 'N', 'K']
  10. MOVE_PLAYER2 = ['N', 'K', 'N', 'N', 'N', 'K', 'N', 'N', 'P', 'K', 'N', 'P', 'P', 'N', 'K', 'P', 'K', 'P', 'N', 'K', 'N', 'K', 'P', 'K', 'K', 'P', 'K', 'N', 'N', 'P']
  11. PAYOFF = [1, 1, -1, 0, 1, 1, 1, -1, 1, 0, -1, -1, 1, 1, -1, 0, 1, 1, 0, 0, 1, 0, -1, -1, -1, -1, 0, -1, 0, -1]
  12.  
  13.  
  14.  
  15.  
  16. #Metoda symulacja gre dla podanej liczby gier
  17. def simulateGame(numer_of_moves):
  18.     #inicjalne wartości tablic
  19.     player1_moves_array = []
  20.     player2_moves_array = []
  21.     payoff_array = []
  22.     count_movement = 0
  23.  
  24.     while count_movement < numer_of_moves:
  25.         player1_move = generateRandomValue()
  26.         player2_move = generateRandomValue()
  27.         payoff_result = getPayOff(player1_move,player2_move)
  28.  
  29.         player1_moves_array.append(player1_move)
  30.         player2_moves_array.append(player2_move)
  31.         payoff_array.append(payoff_result)
  32.  
  33.         count_movement = count_movement + 1
  34.  
  35.     return player1_moves_array, player2_moves_array, payoff_array
  36.  
  37. def generateRandomValue():
  38.     return random.choice(MOVE_ARR)
  39.  
  40. def getPayOff(mov1,mov2):
  41.     if mov1 == 'K':
  42.         if mov2 == 'K':
  43.             return 0
  44.         elif mov2 == 'P':
  45.             return -1
  46.         else:
  47.             return 1
  48.     elif mov1 == 'P':
  49.         if mov2 == 'K':
  50.             return 1
  51.         elif mov2 == 'P':
  52.             return 0
  53.         else:
  54.             return -1
  55.     else:
  56.         if mov2 == 'K':
  57.             return -1
  58.         elif mov2 == 'P':
  59.             return 1
  60.         else:
  61.             return 0
  62.  
  63.  
  64. def countWinDrawLoose(target,movements_array,payoff_array):
  65.     win = 0;
  66.     draw = 0;
  67.     loose = 0;
  68.     for i, move in enumerate(movements_array):
  69.         if move == target:
  70.             payoff = payoff_array[i]
  71.  
  72.             if payoff == 0:
  73.                 draw = draw + 1
  74.             elif payoff == 1:
  75.                 win = win + 1
  76.             else:
  77.                 loose = loose + 1
  78.  
  79.     return win, draw, loose
  80.  
  81. def getProbability(result,count):
  82.     return round(result/count,3)
  83.  
  84. # Główna metoda
  85. def main():
  86.     #Wybór trybu: nowy/z tablicy
  87.     print("*** START ***")
  88.     print("* WYBIERZ OPCJE *")
  89.     print("1 - nowa symulacja rozgrywki")
  90.     print("2 - liczy z tablic statystycznych")
  91.     numerOfGame = 30
  92.  
  93.     user_choice1 = int(input("Wybierz opcje: "))
  94.     print("* Wybrales opcje " + str(user_choice1) + " *")
  95.  
  96.     if user_choice1 == 1:
  97.         #Generowanie gry
  98.         player1_moves_array, player2_moves_array, payoff_araa = simulateGame(numerOfGame);
  99.         # Macierze
  100.         generateMacierzWyjsc(numerOfGame, player1_moves_array, player2_moves_array, payoff_araa)
  101.         generateMacierzPrzejsc(numerOfGame, player1_moves_array)
  102.  
  103.         print("* PIERWSZY WYBOR PLAYERA *")
  104.         r, p, s = generateGeneralPro(player1_moves_array, numerOfGame);
  105.         print("Kamien: " + str(r) + " Papier: " + str(p) + " Nozyczki: " + str(s))
  106.  
  107.     elif user_choice1 == 2:
  108.         #TODO
  109.         print("*** DZIALANIE DLA TABLIC STATYCZNYCH")
  110.         generateMacierzWyjsc(numerOfGame, MOVE_PLAYER1, MOVE_PLAYER2, PAYOFF)
  111.         generateMacierzPrzejsc(numerOfGame, MOVE_PLAYER1)
  112.  
  113.         print("* PIERWSZY WYBOR PLAYERA *")
  114.         r, p, s = generateGeneralPro(MOVE_PLAYER1, numerOfGame);
  115.         print("Kamien: " + str(r) + " Papier: " + str(p) + " Nozyczki: " + str(s))
  116.     else:
  117.         print("*** WYBRALES ZLA OPCJE ***")
  118.  
  119.  
  120. def generateMacierzWyjsc(numerOfGame,player1_moves_array, player2_moves_array, payoff_araa):
  121.     # player1_moves_array, player2_moves_array, payoff_araa = simulateGame(numerOfGame);
  122.  
  123.     # Prezentacja wyników
  124.     print("*** WYNIKI ***")
  125.     print("! Player 1 !")
  126.     print(player1_moves_array)
  127.  
  128.     print("*** WYNIKI ***")
  129.     print("! Player 2 !")
  130.     print(player2_moves_array)
  131.  
  132.     print("*** WYNIKI ***")
  133.     print("! Payoff !")
  134.     print(payoff_araa)
  135.  
  136.     rock_win, rock_draw, rock_loose = countWinDrawLoose('K', player1_moves_array, payoff_araa)
  137.     paper_win, paper_draw, paper_loose = countWinDrawLoose('P', player1_moves_array, payoff_araa)
  138.     scizor_win, scizor_draw, scizor_loose = countWinDrawLoose('N', player1_moves_array, payoff_araa)
  139.  
  140.     print('** EMISSION W LICZBACH **')
  141.     print('  |    W    |    D    |    P    ')
  142.     print('--|---------|---------|---------')
  143.     print('K |    {}    |    {}    |    {}    '.format(rock_win, rock_draw, rock_loose))
  144.     print('P |    {}    |    {}    |    {}    '.format(paper_win, paper_draw, paper_loose))
  145.     print('N |    {}    |    {}    |    {}    '.format(scizor_win, scizor_draw, scizor_loose))
  146.  
  147.     rock_win_pro = getProbability(rock_win, numerOfGame)
  148.     rock_draw_pro = getProbability(rock_draw, numerOfGame)
  149.     rock_loose_pro = getProbability(rock_loose, numerOfGame)
  150.     paper_win_pro = getProbability(paper_win, numerOfGame)
  151.     paper_draw_pro = getProbability(paper_draw, numerOfGame)
  152.     paper_loose_pro = getProbability(paper_loose, numerOfGame)
  153.     scizor_win_pro = getProbability(scizor_win, numerOfGame)
  154.     scizor_draw_pro = getProbability(scizor_draw, numerOfGame)
  155.     scizor_loose_pro = getProbability(scizor_loose, numerOfGame)
  156.  
  157.     print('** EMISSION **')
  158.     print('  |    W    |    D    |    P    ')
  159.     print('--|---------|---------|---------')
  160.     print('K |    {}    |    {}    |    {}    '.format(rock_win_pro, rock_draw_pro, rock_loose_pro))
  161.     print('P |    {}    |    {}    |    {}    '.format(paper_win_pro, paper_draw_pro, paper_loose_pro))
  162.     print('N |    {}    |    {}    |    {}    '.format(scizor_win_pro, scizor_draw_pro, scizor_loose_pro))
  163.  
  164.     matrix_output = []
  165.     matrix_output.insert(0, [rock_win_pro, rock_draw_pro, rock_loose_pro])
  166.     matrix_output.insert(1, [paper_win_pro, paper_draw_pro, paper_loose_pro])
  167.     matrix_output.insert(2, [scizor_win_pro, scizor_draw_pro, scizor_loose_pro])
  168.     print('** MACIERZ WYJSC **')
  169.     print(matrix_output)
  170.  
  171. def generateMacierzPrzejsc(numberOfGame, movements_array):
  172.     rock_after_rock, paper_after_rock, scizor_after_rock, rock_count = calculateChooicesPlayerForOneTarget('K',
  173.                                                                                                            movements_array)
  174.     rock_after_paper, paper_after_paper, scizor_after_paper, paper_count = calculateChooicesPlayerForOneTarget('P',
  175.                                                                                                            movements_array)
  176.     rock_after_scizor, paper_after_scizor, scizor_after_scizor, scizor_count = calculateChooicesPlayerForOneTarget('N',
  177.                                                                                                            movements_array)
  178.     print('** TRANSITION W LICZBACH **')
  179.     print("X PO Y")
  180.     print('  |    K    |    P    |    N    ')
  181.     print('--|---------|---------|---------')
  182.     print('K |    {}    |    {}    |    {}    '.format(rock_after_rock, paper_after_rock, scizor_after_rock))
  183.     print('P |    {}    |    {}    |    {}    '.format(rock_after_paper, paper_after_paper, scizor_after_paper))
  184.     print('N |    {}    |    {}    |    {}    '.format(rock_after_scizor, paper_after_scizor, scizor_after_scizor))
  185.  
  186.     rock_after_rock_pro, paper_after_rock_pro, scizor_after_rock_pro = 0,0,0
  187.     if rock_count != 0:
  188.         rock_after_rock_pro = getProbability(rock_after_rock, rock_count)
  189.         paper_after_rock_pro = getProbability(paper_after_rock, rock_count)
  190.         scizor_after_rock_pro = getProbability(scizor_after_rock, rock_count)
  191.  
  192.     rock_after_paper_pro, paper_after_paper_pro , scizor_after_paper_pro = 0,0,0
  193.     if paper_count != 0:
  194.         rock_after_paper_pro = getProbability(rock_after_paper, paper_count)
  195.         paper_after_paper_pro = getProbability(paper_after_paper, paper_count)
  196.         scizor_after_paper_pro = getProbability(scizor_after_paper, paper_count)
  197.  
  198.     rock_after_scizor_pro, paper_after_scizor_pro, scizor_after_scizor_pro = 0,0,0
  199.     if scizor_count != 0:
  200.         rock_after_scizor_pro = getProbability(rock_after_scizor, scizor_count)
  201.         paper_after_scizor_pro = getProbability(paper_after_scizor, scizor_count)
  202.         scizor_after_scizor_pro = getProbability(scizor_after_scizor, scizor_count)
  203.  
  204.     print('** TRANSITION  **')
  205.     print("X PO Y")
  206.     print('  |    K    |    P    |    N    ')
  207.     print('--|---------|---------|---------')
  208.     print('K |    {}    |    {}    |    {}    '.format(rock_after_rock_pro, paper_after_rock_pro, scizor_after_rock_pro))
  209.     print('P |    {}    |    {}    |    {}    '.format(rock_after_paper_pro, paper_after_paper_pro, scizor_after_paper_pro))
  210.     print('N |    {}    |    {}    |    {}    '.format(rock_after_scizor_pro, paper_after_scizor_pro, scizor_after_scizor_pro))
  211.  
  212.     matrix_output = []
  213.     matrix_output.insert(0, [rock_after_rock_pro, paper_after_rock_pro, scizor_after_rock_pro])
  214.     matrix_output.insert(1, [rock_after_paper_pro, paper_after_paper_pro, scizor_after_paper_pro])
  215.     matrix_output.insert(2, [rock_after_scizor_pro, paper_after_scizor_pro, scizor_after_scizor_pro])
  216.     print('** MACIERZ PRZEJSC **')
  217.     print(matrix_output)
  218.  
  219. def generateGeneralPro(array, numerOfGame):
  220.     rock = 0;
  221.     paper = 0;
  222.     scizor = 0;
  223.  
  224.     #Zliczenie poszegolnych wartrosci
  225.     for move in array:
  226.         if move == 'K':
  227.             rock = rock + 1
  228.         elif move == 'P':
  229.             paper = paper + 1
  230.         else:
  231.             scizor = scizor + 1
  232.  
  233.     return round((rock/numerOfGame),3), round((paper/numerOfGame),3), round((scizor/numerOfGame),3)
  234.  
  235. def calculateChooicesPlayerForOneTarget(target,array):
  236.     count = 0;
  237.     rock = 0;
  238.     paper = 0;
  239.     scizor = 0;
  240.     for i,move in enumerate(array):
  241.         if (len(array) - 1) != i:
  242.             if move == target:
  243.                 count = count + 1;
  244.                 #Kamien po targecie
  245.                 if array[i+1] == 'K':
  246.                     rock = rock + 1
  247.                 #Papier po targecie
  248.                 if array[i + 1] == 'P':
  249.                     paper = paper + 1
  250.                 #Nozyce po targecie
  251.                 if array[i + 1] == 'N':
  252.                     scizor = scizor + 1
  253.  
  254.     return rock, paper, scizor, count
  255.  
  256.  
  257. if __name__ == '__main__':
  258.     main()
  259. # *******************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement