Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.66 KB | None | 0 0
  1. #-----Global Variables -------#
  2.  
  3. #Game Board
  4.  
  5. #if game is still going
  6.  
  7. #who won? or tie?
  8.  
  9. #who's turn is it
  10.  
  11. import copy
  12. import random
  13.  
  14. # Shows the board
  15. def display_board(cur_board):
  16.     print(cur_board[0] + " | " + cur_board[1] + "  |  " + cur_board[2] + "  |  ")
  17.     print(cur_board[3] + " | " + cur_board[4] + "  |  " + cur_board[5] + "  |  ")
  18.     print(cur_board[6] + " | " + cur_board[7] + "  |  " + cur_board[8] + "  |  ")
  19. #Play a game of tic tac toe
  20. def play_game(cur_board, cur_play, play1, play2):
  21.     #driving function
  22.     #display initial board state
  23.     display_board(cur_board)
  24.  
  25.     #while the game is still going
  26.     while True:
  27.  
  28.         #handle a single turn of an arbitrary player
  29.         play1_copy = copy.deepcopy(play1)
  30.         play2_copy = copy.deepcopy(play2)
  31.         cur_board = handle_turn(cur_play, cur_board, play1_copy, play2_copy)
  32.  
  33.         display_board(cur_board)
  34.         print("\n===============================================\n")
  35.         #check if the game has ended
  36.         gameover = check_if_game_over(cur_board)
  37.         if gameover:
  38.             if gameover != "Tie":
  39.                 print("The winner is: " + gameover)
  40.             else:
  41.                 print("The game was a tie")
  42.             break
  43.  
  44.        
  45.         #Flip to other player
  46.         cur_play = flip_player(cur_play, play1, play2)
  47.  
  48.  
  49. #Handle a single turn of an arbitrary player
  50. def handle_turn(current_player, cur_board, player1, player2):
  51.     new_board = copy.deepcopy(cur_board)
  52.     # Warns the player if it enters number not between 1-9
  53.     if current_player[1] == "human":
  54.         position = input("Choose a position from 1 to 9: ")
  55.         while (position not in ['1' , '2' , '3', '4', '5', '6', '7', '8', '9']) or new_board[int(position) - 1] != "-":
  56.                 print("Enter a Valid Number between 1 to 9 that doesn't already have a move made. Try again")
  57.                 position = input("Choose a position from 1 to 9: ")
  58.         position = int(position) - 1
  59.     else:
  60.         position = ai_turn(cur_board, current_player, player1, player2)
  61.  
  62.     new_board[position] = current_player[0]
  63.     return new_board
  64.  
  65.  
  66. #see if hte game is over
  67. def check_if_game_over(cur_board):
  68.     winner = check_for_winner(cur_board)
  69.     tie = check_if_tie(cur_board)
  70.     if winner:
  71.         return winner
  72.     elif tie:
  73.         return tie
  74.     else:
  75.         return False
  76.  
  77.  
  78. def check_for_winner(cur_board):
  79.     #set up global variable
  80.  
  81.     #check rows
  82.     row_winner = check_rows(cur_board)
  83.     #check columns
  84.     column_winner = check_columns(cur_board)
  85.     #check diagonals
  86.     diagonal_winner = check_diagonals(cur_board)
  87.  
  88.     if row_winner:
  89.         winner = row_winner
  90.     elif column_winner:
  91.         winner = column_winner
  92.     elif diagonal_winner:
  93.         winner = diagonal_winner
  94.     else: #there was no win
  95.         winner = False
  96.     return winner
  97.  
  98.  
  99.  
  100.  
  101. def check_rows(cur_board):
  102.     #set global
  103.     #check if any of the rows have the same value and is not empty
  104.     row_1 = cur_board[0] == cur_board[1] == cur_board[2] !="-"
  105.     row_2 = cur_board[3] == cur_board[4] == cur_board[5] !="-"
  106.     row_3 = cur_board[6] == cur_board[7] == cur_board[8] !="-"
  107.     # if any row has a match flag that there is a win
  108.     if row_1 or row_2 or row_3:
  109.         game_still_going = False
  110.     if row_1:
  111.         return cur_board[0]
  112.     elif row_2:
  113.         return cur_board[3]
  114.     elif row_3:
  115.         return cur_board[6]
  116.     return
  117.  
  118. def check_columns(cur_board):
  119.  #set global
  120.     #check if any of the rows have the same value and is not empty
  121.     column_1 = cur_board[0] == cur_board[3] == cur_board[6] !="-"
  122.     column_2 = cur_board[1] == cur_board[4] == cur_board[7] !="-"
  123.     column_3 = cur_board[2] == cur_board[5] == cur_board[8] !="-"
  124.     # if any row has a match flag that there is a win
  125.     if column_1 or column_2 or column_3:
  126.         game_still_going = False
  127.     #return winner (X or O)
  128.     if column_1:
  129.         return cur_board[0]
  130.     elif column_2:
  131.         return cur_board[1]
  132.     elif column_3:
  133.         return cur_board[2]
  134.     return
  135.  
  136. def check_diagonals(cur_board):
  137.      #set global
  138.     #check if any of the diagonals_have the same value and is not empty
  139.     diagonals_1 = cur_board[0] == cur_board[4] == cur_board[8] !="-"
  140.     diagonals_2 = cur_board[6] == cur_board[4] == cur_board[2] !="-"
  141.     # if any diagonals_ has a match flag that there is a win
  142.     if diagonals_1 or diagonals_2:
  143.         game_still_going = False
  144.     #return winner (X or O)
  145.     if diagonals_1:
  146.         return cur_board[0]
  147.     elif diagonals_2:
  148.         return cur_board[6]
  149.     return
  150.  
  151.  
  152. def check_if_tie(cur_board):
  153.     if (not check_for_winner(cur_board)) and set(cur_board) == set({"X", "O"}):
  154.         return "Tie"
  155.     else:
  156.         return False
  157. def flip_player(current_player, player1, player2):
  158.     if current_player == player1:
  159.         return player2
  160.     else:
  161.         return player1
  162.     return player2
  163.  
  164.  
  165. def empty_cells(cur_board):
  166.     empty_cells = []
  167.     for i in range(len(cur_board)):
  168.         if cur_board[i] == "-":
  169.             empty_cells.append(i)
  170.     return empty_cells
  171.  
  172. def ai_turn(cur_board, cur_play, play1, play2):
  173.     print("AI TURN")
  174.     play1_copy = copy.deepcopy(play1)
  175.     play2_copy = copy.deepcopy(play2)
  176.     return minimax(cur_board, cur_play, play1_copy, play2_copy, {})[1]
  177.    
  178. def minimax(cur_board, cur_play, player1, player2, memo):
  179.     potential_win = check_for_winner(cur_board)
  180.     if potential_win:
  181.         if potential_win == cur_play[0]:
  182.             return (1, None)
  183.         else:
  184.             return (-1, None)
  185.  
  186.     move = -1
  187.     score = -2
  188.     available_moves = empty_cells(cur_board)
  189.     for i in range(len(available_moves)):
  190.         board_with_new_move = copy.deepcopy(cur_board)
  191.         board_with_new_move[available_moves[i]] = cur_play[0]
  192.         if str(board_with_new_move) in memo.keys():
  193.             score_for_the_move = memo[str(board_with_new_move)]
  194.         else:
  195.             score_for_the_move = -minimax(board_with_new_move, flip_player(cur_play, player1, player2), player1, player2, memo)[0]
  196.             memo[str(board_with_new_move)] = score
  197.         if score_for_the_move > score:
  198.             score = score_for_the_move
  199.             move = available_moves[i]
  200.     if move == -1:
  201.         return (0,-2)
  202.     return (score, move)
  203.  
  204.    
  205.  
  206.  
  207.  
  208.  
  209.  
  210. player1 = ("X", "cpu")
  211. player2 = ("O", "human")
  212. first_player = player1
  213.  
  214.  
  215.  
  216.  
  217. board = ["-","-","-",
  218.          "-","-","-",
  219.          "-","-","-"]
  220.  
  221. play_game(board, first_player, player1, player2)
  222.  
  223.  
  224.  
  225.  
  226. # board
  227. #display board
  228. # play game
  229. # create ai
  230. #handle turn
  231.     #check rows
  232.     #check colmn
  233.     #check diagonals
  234. #check tie
  235. #flip player
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement