Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.62 KB | None | 0 0
  1. # Napisz funkcje, ktora wyswietla plansze
  2. # Napisz funkcje, ktora wczyta ruchy graczy i wyswietli je na ekranie komputera
  3. # Napisz funkcje, ktora po kazdym ruchu sprawdzi czy rozgrywka sie zakonczyla i wyswietli wynik
  4. import random
  5.  
  6. board = ["-", "-", "-",
  7.          "-", "-", "-",
  8.          "-", "-", "-"]
  9.  
  10. game_is_not_over = True
  11. current_player = "X"
  12. winner = None
  13. pos = None
  14.  
  15.  
  16. def display_board():
  17.     print(" -------------")
  18.     print(" | " + board[0] + " | " + board[1] + " | " + board[2] + " | ")
  19.     print(" | " + board[3] + " | " + board[4] + " | " + board[5] + " | ")
  20.     print(" | " + board[6] + " | " + board[7] + " | " + board[8] + " | ")
  21.     print(" -------------")
  22.  
  23. def choose_position(player):
  24.     print("Ruch Gracza: ", player)
  25.     position = input("Wybierz wolna pozycje 1-9: ")
  26.  
  27.     correct = False
  28.     while not correct:
  29.  
  30.         while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  31.             position = input("Wybierz wolna pozycje 1-9: ")
  32.  
  33.         position = int(position) - 1
  34.  
  35.         if board[position] == "-":
  36.             correct = True
  37.         else:
  38.             print("Wybrana pozycja jest juz zajeta")
  39.  
  40.     board[position] = player
  41.     display_board()
  42.  
  43. def play_ai():
  44.     display_board()
  45.  
  46.     while game_is_not_over:
  47.         choose_position_ai(current_player)
  48.  
  49.         end_of_the_game()
  50.         player_change()
  51.  
  52.     if winner == "X" or winner == "O":
  53.         print(winner, " wygrywa.")
  54.     elif winner == None:
  55.         print("Remis.")
  56.  
  57. def play():
  58.     display_board()
  59.  
  60.     while game_is_not_over:
  61.         choose_position(current_player)
  62.  
  63.         end_of_the_game()
  64.         player_change()
  65.  
  66.     if winner == "X" or winner == "O":
  67.         print(winner, " wygrywa.")
  68.     elif winner == None:
  69.         print("Remis.")
  70.  
  71.  
  72. # Plan dzialania:
  73. # 1. Wyswietl tablice
  74. # 2. Pozwol graczowi 1 wybrac pole 1-9
  75. # 3. Sprawdz czy to pole nie jest juz zajete
  76. # 4. Sprawdz czy nastapila wygrana/remis
  77. # 5. Zmien gracza
  78. # 6. ... i tak do skutku
  79.  
  80. # Wybierz pole
  81.  
  82. def choose_position_ai(player):
  83.     print("Ruch Gracza: ", player)
  84.     if player == "O":
  85.  
  86.         correct = False
  87.         while not correct:
  88.             position = random.randint(0, 8)
  89.             print(random.randint(0, 8))
  90.             position = int(position)
  91.  
  92.             if board[position] == "-":
  93.                 correct = True
  94.             else:
  95.                 print("Wybrana pozycja jest juz zajeta")
  96.  
  97.         board[position] = player
  98.         display_board()
  99.     elif player == "X":
  100.         position = input("Wybierz wolna pozycje 1-9: ")
  101.  
  102.         correct = False
  103.         while not correct:
  104.  
  105.             while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  106.                 position = input("Wybierz wolna pozycje 1-9: ")
  107.  
  108.             position = int(position) - 1
  109.  
  110.             if board[position] == "-":
  111.                 correct = True
  112.             else:
  113.                 print("Wybrana pozycja jest juz zajeta")
  114.  
  115.         board[position] = player
  116.         display_board()
  117.  
  118.  
  119.  
  120.  
  121.  
  122. # Zmien gracza
  123.  
  124. def player_change():
  125.     global current_player
  126.     if current_player == "X":
  127.         current_player = "O"
  128.     elif current_player == "O":
  129.         current_player = "X"
  130.  
  131.  
  132. def end_of_the_game():
  133.     check_winner()
  134.     check_tie()
  135.  
  136.  
  137. def check_columns():
  138.     global game_is_not_over
  139.  
  140.     column_1 = board[0] == board[3] == board[6] != "-"
  141.     column_2 = board[1] == board[4] == board[7] != "-"
  142.     column_3 = board[2] == board[5] == board[8] != "-"
  143.  
  144.     if column_1 or column_2 or column_3:
  145.         game_is_not_over = False
  146.  
  147.     if column_1:
  148.         return board[0]
  149.     elif column_2:
  150.         return board[1]
  151.     elif column_3:
  152.         return board[2]
  153.     else:
  154.         return None
  155.  
  156.  
  157. def check_rows():
  158.     global game_is_not_over
  159.  
  160.     row_1 = board[0] == board[1] == board[2] != "-"
  161.     row_2 = board[3] == board[4] == board[5] != "-"
  162.     row_3 = board[6] == board[7] == board[8] != "-"
  163.  
  164.     if row_1 or row_2 or row_3:
  165.         game_is_not_over = False
  166.  
  167.     if row_1:
  168.         return board[0]
  169.     elif row_2:
  170.         return board[3]
  171.     elif row_3:
  172.         return board[6]
  173.     else:
  174.         return None
  175.  
  176.  
  177. def check_diagonals():
  178.     global game_is_not_over
  179.  
  180.     diagonal_1 = board[0] == board[4] == board[8] != "-"
  181.     diagonal_2 = board[2] == board[4] == board[6] != "-"
  182.  
  183.     if diagonal_1 or diagonal_2:
  184.         game_is_not_over = False
  185.  
  186.     if diagonal_1:
  187.         return board[0]
  188.     elif diagonal_2:
  189.         return board[2]
  190.     else:
  191.         return None
  192.  
  193.  
  194. def check_winner():
  195.     global winner
  196.  
  197.     column_winner = check_columns()
  198.     row_winner = check_rows()
  199.     diagonal_winner = check_diagonals()
  200.  
  201.     if column_winner:
  202.         winner = column_winner
  203.     elif row_winner:
  204.         winner = row_winner
  205.     elif diagonal_winner:
  206.         winner = diagonal_winner
  207.     else:
  208.         winner = None
  209.  
  210.  
  211. def check_tie():
  212.     global game_is_not_over
  213.  
  214.     if "-" not in board:
  215.         game_is_not_over = False
  216.         return True
  217.     else:
  218.         return False
  219.  
  220. def losuj():
  221.     x = random.randint(0,9)
  222.     return x
  223.  
  224. def menu():
  225.     print("Wybierz tryb gry: ")
  226.     print("1. Gra dwuosobowa")
  227.     print("2. Gra przeciwko komputerowi")
  228.     wybor = input()
  229.     wybor = int(wybor)
  230.     decision = False
  231.     while not decision:
  232.         if wybor == 1:
  233.             play()
  234.             decision = True
  235.         elif wybor == 2:
  236.             play_ai()
  237.             decision = True
  238.         else:
  239.             print("Dokonano niewlasciwego wyboru. Sprobuj ponownie")
  240.             wybor = input()
  241.             wybor = int(wybor)
  242.  
  243.  
  244.  
  245. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement