pawlack

Untitled

Jan 22nd, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.98 KB | None | 0 0
  1.  
  2. #another tic tac toe game
  3. import random
  4. from random import randint
  5. #gameboard interface
  6.  
  7. gameboard = [[" "] * 3, [" "] * 3, [" "] * 3] # 2d Array
  8. aigameboard = [[" "] * 3, [" "] * 3, [" "] * 3] # 2d Array for ai minmax sim
  9. input_var1 = [["a1", "b1", "c1"], ["a2", "b2", "c2"], ["a3", "b3", "c3"]]
  10. available_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]
  11. potential_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"] #for minmax use
  12. rows = 3
  13. columns = 3
  14. player_symbols = ["_", "X", "O"]
  15. current_player = 1
  16.  
  17. def end_game(is_it_tie):
  18.  
  19.     global gameboard
  20.     global aigameboard
  21.     global potential_moves
  22.     global available_moves
  23.     global current_player
  24.  
  25.     if is_it_tie == False:
  26.         print("We have a winner ", "Player ", current_player, " has won!")
  27.         draw_gameboard()
  28.     else:
  29.         print("It's a TIE")
  30.         draw_gameboard()
  31.  
  32.     replay = input("Do you want to play again? Y/N: ")
  33.  
  34.     if replay == "Y" or replay == "y":
  35.         gameboard = [[" "] * 3, [" "] * 3, [" "] * 3] # 2d Array
  36.         aigameboard = [[" "] * 3, [" "] * 3, [" "] * 3]
  37.         available_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]
  38.         potential_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]
  39.         current_player = 1
  40.         main_game_loop(False)
  41.    
  42.  
  43.     else:
  44.         quit()
  45.  
  46. def draw_gameboard(): #this is our game output
  47.     print("    A    B    C")
  48.     for i in range(len(gameboard)):
  49.         print(i+1, (gameboard[i]))
  50.  
  51. def draw_fake_gameboard(board): #this is debug tool
  52.     print("    A    B    C <- fake ai gameboard")
  53.     for i in range(len(board)):
  54.         print(i+1, (board[i]))
  55.  
  56. def get_player_input():
  57.     print("It's your turn, proper input example is 'a3', or 'b1'. Enter 'x' to quit")
  58.     pos = input("Enter position you want to take:")
  59.     if pos == "x":
  60.         quit()
  61.     return pos
  62.  
  63. def get_ai_input():
  64.     global aigameboard
  65.     global gameboard
  66.     global potential_moves
  67.     global available_moves
  68.     score = []
  69.  
  70.     for i in range(len(available_moves)):
  71.         score.append(mini_max(available_moves[i], aigameboard, True))
  72.         aigameboard = gameboard
  73.        
  74.    
  75.     print(score)
  76.     pos = available_moves[score.index(max(score))]
  77.  
  78.  
  79.     #pos = available_moves[randint(0, (len(available_moves))-1)]
  80.     return pos
  81.  
  82. def mini_max(position, board, isMaximizing):
  83.  
  84.     global input_var1
  85.     global potential_moves
  86.  
  87.     score = 0
  88.     scores = []
  89.     winner = 3
  90.     probed_position_x = 0
  91.     probed_position_y = 0
  92.     player = 1
  93.     value = 0
  94.     position_index = 0
  95.     internal_board = board
  96.     next_player = not isMaximizing
  97.  
  98.     #draw_fake_gameboard(board)
  99.     #print(position)
  100.  
  101.     if isMaximizing == True:
  102.         player = 2
  103.         value = 1
  104.     else:
  105.         player = 1
  106.         value = -1 #we wil multiply check for winners by value to get us -1 in case of minimizing
  107.  
  108.     for x in range(len(input_var1)):
  109.         for y in range(len(input_var1[x])):
  110.             if input_var1[x][y] == position:
  111.                 board[x][y] = player_symbols[player]
  112.                 probed_position_x = x
  113.                 probed_position_y = y
  114.  
  115.     position_index = potential_moves.index(position)
  116.     potential_moves.remove(position)
  117.     winner =  check_for_winners(board, potential_moves)
  118.     if winner == 1 or winner == 0:
  119.         #there is a winner here
  120.         score = winner*value
  121.         board[probed_position_x][probed_position_y] = " "
  122.         potential_moves.insert(position_index, position)
  123.         return score
  124.     else:
  125.         for i in range(len(potential_moves)):
  126.             scores.append(mini_max(potential_moves[i], internal_board, next_player))
  127.             internal_board = board
  128.         if isMaximizing:
  129.             score = min(scores)
  130.         else:
  131.             score = max(scores)
  132.  
  133.         board[probed_position_x][probed_position_y] = " "
  134.         potential_moves.insert(position_index, position)
  135.  
  136.         return score
  137.        
  138.  
  139.  
  140.  
  141.  
  142.  
  143. def update_game(player_number):
  144.     global gameboard
  145.     global input_var1
  146.     global current_player
  147.  
  148.     if player_number == 1:
  149.         pos = get_player_input()
  150.     else:
  151.         pos = get_ai_input()
  152.  
  153.     if pos in available_moves:
  154.         available_moves.remove(pos)
  155.         potential_moves.remove(pos)
  156.  
  157.         for x in range(len(input_var1)):
  158.             for y in range(len(input_var1[x])):
  159.                 if input_var1[x][y] == pos:
  160.                     gameboard[x][y] = player_symbols[player_number]
  161.                     print(" ")
  162.                     print("Player", player_number, "move: ")
  163.     else:
  164.         print("this position is invalid or already taken")
  165.         update_game(player_number)
  166.  
  167.            
  168. def check_for_winners(gameboard_state, moves):
  169.     column_check = []
  170. #horizontal
  171.     for x in range(len(gameboard_state[0])):
  172.         if gameboard_state[x].count(gameboard_state[x][0]) == len(gameboard_state[x]) and gameboard_state[x][0] != " ":
  173.             #there is horizontal winner
  174.             return 1
  175. #vertical
  176.     for i in range(len(gameboard_state)):
  177.         for row in gameboard_state:
  178.             column_check.append(row[i])
  179.         if column_check.count(column_check[0]) == len(column_check) and column_check[0] != " ":
  180.             #there is vertical winner
  181.             return 1
  182.         else:
  183.             column_check.clear()
  184. #diagonal right
  185.     for counter, row in enumerate(gameboard_state):
  186.         column_check.append(row[counter])
  187.     if column_check.count(column_check[0]) == len(column_check) and column_check[0] != " ":
  188.             #there is diagonal winner
  189.             return 1
  190.     else:
  191.         column_check.clear()
  192.  
  193. #diagonal left
  194.     for counter, row in enumerate(gameboard_state):
  195.         column_check.append(row[-counter-1])
  196.     if column_check.count(column_check[0]) == len(column_check) and column_check[0] != " ":
  197.             #there is diagonal winner
  198.             return 1
  199.     else:
  200.         column_check.clear()
  201.  
  202. #check for TIE
  203.     if len(moves) == 0:
  204.         return 0
  205.  
  206.     return -3
  207.  
  208. def change_player():
  209.     global current_player
  210.  
  211.     if current_player == 1:
  212.         current_player = 2
  213.     else:
  214.         current_player = 1
  215.  
  216. def main_game_loop(game_ended):
  217.     global aigameboard
  218.     is_tie = False
  219.     while game_ended == False:
  220.        
  221.         draw_gameboard()
  222.         update_game(current_player)
  223.         if check_for_winners(gameboard, available_moves) != -3 and check_for_winners(gameboard, available_moves) == 0:
  224.             is_tie = True
  225.             game_ended = True
  226.         elif check_for_winners(gameboard, available_moves) != -3 and check_for_winners(gameboard, available_moves) != 0:
  227.             is_tie = False
  228.             game_ended = True
  229.         else:
  230.             aigameboard = gameboard
  231.             change_player()
  232.  
  233.     else:
  234.         end_game(is_tie)
  235.  
  236. main_game_loop(False)
Advertisement
Add Comment
Please, Sign In to add comment