Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.93 KB | None | 0 0
  1. #Extended Project
  2. #Tic Tac Toe Vs Comp.
  3. #Start - 7/2/2017 (Tuesday) - 4:54 PM
  4. #End -
  5.  
  6.  
  7. #Imports
  8. import time
  9. import random
  10.  
  11. ##### All Steps #####
  12.  
  13.  
  14. #STEP 1:
  15. #Create master loop
  16. #Create play board and header
  17. #Receive user input
  18. #Validate user input choice
  19. #Put user choice into board
  20.  
  21.  
  22. #STEP 2:
  23. #Consider all 8 ways of winning the game
  24. #Apply it for both players
  25.  
  26.  
  27. #STEP 3:
  28. #Create second player input
  29. #Apply function to check for win for this player
  30. #Check for a tie using function
  31.  
  32.  
  33. #STEP 4:
  34. #Create random move generator inside function
  35. #Smart AI
  36.  
  37. #STEP 5 (Smart AI):
  38. #1) Check to see if comp can win in 1 move, if can then take that move otherwise move to 2)
  39. #2) Check to see if player can win in 1 move, if can then block the player otherwise move to step 3)
  40. #3) Make variables for each round for player X's turn (Example: Round1 = 5 --> player X chose centre for round 1)
  41. #4) Follow mindmap flow chart to make the best move for each turn (Example: If Round1 == 5: *newline* return corner)
  42.  
  43.  
  44.  
  45. board = ["", " ", " ", " ", " ", " ", " ", " ", " ", " "]
  46. empty_spaces = []
  47. x_spaces = []
  48. o_spaces = []
  49.  
  50.  
  51. def print_header():
  52.     print("""--------------------------------------------------------------------------\n---> Welcome to Tic-Tac-Toe
  53. ---> To win you need to get 3 of your symbol in a row, in a column or diagonally\n
  54. __ __| _)          __ __|               __ __|            
  55.    |    |   __|       |   _` |   __|       |   _ \   _ \   1 | 2 | 3
  56.    |    |  (  _____|  |  (   |  (  _____|  |  (   |   __/    4 | 5 | 6
  57.   _|   _| \___|      _| \__,_| \___|      _| \___/  \___|    7 | 8 | 9\n\n""")
  58.  
  59.  
  60. def playing_board():
  61.     print("\nPlaying Board:")
  62.     print("   |   |   ")
  63.     print(" " + board[1] + " | " + board[2] + " | " + board[3] + " ")
  64.     print("---+---+---")
  65.     print(" " + board[4] + " | " + board[5] + " | " + board[6] + " ")
  66.     print("---+---+---")
  67.     print(" " + board[7] + " | " + board[8] + " | " + board[9] + " ")
  68.     print("   |   |   ")
  69.  
  70.  
  71. def is_player_win(board, player):
  72.     if board[1] == player and board[2] == player and board[3] == player or \
  73.        board[4] == player and board[5] == player and board[6] == player or \
  74.        board[7] == player and board[8] == player and board[9] == player or \
  75.        board[1] == player and board[4] == player and board[7] == player or \
  76.        board[2] == player and board[5] == player and board[8] == player or \
  77.        board[3] == player and board[6] == player and board[9] == player or \
  78.        board[1] == player and board[5] == player and board[9] == player or \
  79.        board[3] == player and board[5] == player and board[7] == player:
  80.         return True
  81.     else:
  82.         return False
  83.  
  84.  
  85. def copy_board(board):
  86.     copyBoard = []
  87.  
  88.     for loop in board:
  89.         copyBoard.append(loop)
  90.  
  91.     return copyBoard
  92.  
  93.  
  94. def is_empty_space(board, move):
  95.     if board[move] == ' ':
  96.         return True
  97.  
  98.  
  99. def draw_game(board):
  100.     if " " in board:
  101.         return False
  102.     else:
  103.         return True
  104.  
  105.  
  106. def clear_board(board):
  107.     for loop in range(1, 10):
  108.         board[loop] = " "
  109.  
  110.    
  111. def start_over(board):
  112.     while True:
  113.         choice = input("The game has ended. Do you wish to play another game? (Y/N) : ")
  114.         if choice == "Y":
  115.             clear_board(board)
  116.             return True
  117.         elif choice == "N":
  118.             print("\nThank you for playing")
  119.             return False
  120.         else:
  121.             print("That is not a valid input. Please try again.")
  122.  
  123.  
  124. def comp_move(board, x_choice, x_spaces, o_spaces):
  125.     #1) range of 1 to 10
  126.     #2) copy board
  127.     #3) check if space is free
  128.     #4) place a O in that spot
  129.     #5) see if comp win, if win, return that spot
  130.     #6) repeat for player, but block player if player can win
  131.  
  132.  
  133.     #If I (computer) can win, go ahead and win
  134.     for i in range(1, 10):
  135.         copy = copy_board(board)
  136.         if is_empty_space(copy, i):             #If the specific place in the board is empty (If the function returns True)
  137.             copy[i] = "o"                       #Place an "o" in that place
  138.             if is_player_win(copy, "o"):        #If the player "o" wins (If the function returns True), return that place value to win
  139.                 return i
  140.  
  141.     #If the opponent can win, block him
  142.     for j in range(1, 10):
  143.         copy = copy_board(board)
  144.         if is_empty_space(copy, j):             #If the specific place in the board is empty (If the function returns True)
  145.             copy[j] = "x"                       #Place an "x" in that place
  146.             if is_player_win(copy, "x"):        #If the player "x" wins (If the function returns True), return that place value to block
  147.                 return j
  148.  
  149.     corners = [1, 3, 7, 9]
  150.     oppCorners1 = [1, 9]
  151.     oppCorners2 = [3, 7]
  152.     edge2 = [1, 3]
  153.     edge4 = [1, 7]
  154.     edge6 = [3, 9]
  155.     edge8 = [7, 9]
  156.     corner1 = [2, 4]
  157.     corner3 = [2, 6]
  158.     corner7 = [4, 8]
  159.     corner9 = [6, 8]
  160.  
  161.     copy = copy_board(board)
  162.     x_spaces.append(x_choice)
  163.  
  164.  
  165.     #Round 1
  166.     if len(x_spaces) >= 1:
  167.        
  168.         #Branch 1.1
  169.         if x_spaces[0] in corners:
  170.  
  171.  
  172.             #Round 2
  173.             if len(x_spaces) >= 2:
  174.                 if x_spaces[0] in oppCorners1 and x_spaces[1] in oppCorners1 and x_spaces[0] != x_spaces[1]:
  175.                     if x_spaces[1] == 1:
  176.                         move = random.choice(corner1)
  177.                         return move, x_spaces, o_spaces
  178.                     elif x_spaces[1] == 3:
  179.                         move = random.choice(corner3)
  180.                         return move, x_spaces, o_spaces
  181.                     elif x_spaces[1] == 7:
  182.                         move = random.choice(corner7)
  183.                         return move, x_spaces, o_spaces
  184.                     elif x_spaces[1] == 9:
  185.                         move = random.choice(corner9)
  186.                         return move, x_spaces, o_spaces
  187.                 elif x_spaces[0] in oppCorners2 and x_spaces[1] in oppCorners2 and x_spaces[0] != x_spaces[1]:
  188.                     pass
  189.  
  190.  
  191.             #Branch 1.1 outcome
  192.             else:
  193.                 return 5, x_spaces, o_spaces
  194.  
  195.         #Branch 1.2
  196.         elif x_choice == 5:
  197.             move = random.choice(corners)
  198.             return move, x_spaces, o_spaces
  199.  
  200.         #Branch 1.3
  201.         elif x_choice == 2:
  202.             move = random.choice(edge2)
  203.             return move, x_spaces, o_spaces
  204.  
  205.         #Branch 1.4
  206.         elif x_choice == 4:
  207.             move = random.choice(edge4)
  208.             return move, x_spaces, o_spaces
  209.  
  210.         #Branch 1.5
  211.         elif x_choice == 6:
  212.             move = random.choice(edge6)
  213.             return move, x_spaces, o_spaces
  214.  
  215.         #Branch 1.6
  216.         elif x_choice == 8:
  217.             move = random.choice(edge8)
  218.             return move, x_spaces, o_spaces
  219.  
  220.     while True:
  221.         move = random.randint(1, 9)
  222.         if is_empty_space(board, place):
  223.             return move, x_spaces, o_spaces
  224.         else:
  225.             continue
  226.  
  227.  
  228.  
  229.    
  230. ##################################     MAIN PROGRAM     ##################################
  231.  
  232. while True:
  233.     print_header()
  234.     playing_board()
  235.  
  236.     while True:
  237.  
  238.         #Input from player "x"
  239.         try:
  240.             x_choice = int(input("It is x's turn (0 to restart): "))
  241.             if x_choice == 0:
  242.                 print("Restarting...\n")
  243.                 time.sleep(1)
  244.                 break
  245.         except ValueError:
  246.             continue
  247.  
  248.         if board[x_choice] == " ":
  249.             board[x_choice] = "x"
  250.             playing_board()
  251.             break
  252.         else:
  253.             print("This spot is taken. Please try again.")
  254.             time.sleep(1)
  255.  
  256.     #Check if player "x" wins
  257.     if is_player_win(board, "x"):
  258.         print_header()
  259.         playing_board()
  260.         print("Player 'x' wins!")
  261.         if start_over(board):
  262.             continue
  263.         else:
  264.             break
  265.  
  266.     #Check for tie
  267.     if draw_game(board):
  268.         playing_board()
  269.         print("It is a tie!")
  270.         if start_over(board):
  271.             continue
  272.         else:
  273.             break
  274.  
  275.     while True:
  276.  
  277.         if x_choice == 0:
  278.             break
  279.         #Input from player "o"
  280.         #o_choice = int(input("It is o's turn: "))
  281.         o_choice, x_spaces, o_spaces = comp_move(board, x_choice, x_spaces, o_spaces)
  282.  
  283.         if board[o_choice] == " ":
  284.             board[o_choice] = "o"
  285.             #playing_board()
  286.             break
  287.         else:
  288.             print("This spot is taken. Please try again.")
  289.             time.sleep(1)
  290.  
  291.     #Check if player "o" wins
  292.     if is_player_win(board, "o"):
  293.         print_header()
  294.         playing_board()
  295.         print("Player 'o' wins!")
  296.         if start_over(board):
  297.             continue
  298.         else:
  299.             break
  300.  
  301.     #Check for tie
  302.     if draw_game(board):
  303.         playing_board()
  304.         print("It is a tie!")
  305.         if start_over(board):
  306.             continue
  307.         else:
  308.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement