Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Extended Project
- #Tic Tac Toe Vs Comp.
- #Start - 7/2/2017 (Tuesday) - 4:54 PM
- #End -
- #Imports
- import time
- import random
- ##### All Steps #####
- #STEP 1:
- #Create master loop
- #Create play board and header
- #Receive user input
- #Validate user input choice
- #Put user choice into board
- #STEP 2:
- #Consider all 8 ways of winning the game
- #Apply it for both players
- #STEP 3:
- #Create second player input
- #Apply function to check for win for this player
- #Check for a tie using function
- #STEP 4:
- #Create random move generator inside function
- #Smart AI
- #STEP 5 (Smart AI):
- #1) Check to see if comp can win in 1 move, if can then take that move otherwise move to 2)
- #2) Check to see if player can win in 1 move, if can then block the player otherwise move to step 3)
- #3) Make variables for each round for player X's turn (Example: Round1 = 5 --> player X chose centre for round 1)
- #4) Follow mindmap flow chart to make the best move for each turn (Example: If Round1 == 5: *newline* return corner)
- board = ["", " ", " ", " ", " ", " ", " ", " ", " ", " "]
- empty_spaces = []
- x_spaces = []
- o_spaces = []
- def print_header():
- print("""--------------------------------------------------------------------------\n---> Welcome to Tic-Tac-Toe
- ---> To win you need to get 3 of your symbol in a row, in a column or diagonally\n
- __ __| _) __ __| __ __|
- | | __| | _` | __| | _ \ _ \ 1 | 2 | 3
- | | ( _____| | ( | ( _____| | ( | __/ 4 | 5 | 6
- _| _| \___| _| \__,_| \___| _| \___/ \___| 7 | 8 | 9\n\n""")
- def playing_board():
- print("\nPlaying Board:")
- print(" | | ")
- print(" " + board[1] + " | " + board[2] + " | " + board[3] + " ")
- print("---+---+---")
- print(" " + board[4] + " | " + board[5] + " | " + board[6] + " ")
- print("---+---+---")
- print(" " + board[7] + " | " + board[8] + " | " + board[9] + " ")
- print(" | | ")
- def is_player_win(board, player):
- if board[1] == player and board[2] == player and board[3] == player or \
- board[4] == player and board[5] == player and board[6] == player or \
- board[7] == player and board[8] == player and board[9] == player or \
- board[1] == player and board[4] == player and board[7] == player or \
- board[2] == player and board[5] == player and board[8] == player or \
- board[3] == player and board[6] == player and board[9] == player or \
- board[1] == player and board[5] == player and board[9] == player or \
- board[3] == player and board[5] == player and board[7] == player:
- return True
- else:
- return False
- def copy_board(board):
- copyBoard = []
- for loop in board:
- copyBoard.append(loop)
- return copyBoard
- def is_empty_space(board, move):
- if board[move] == ' ':
- return True
- def draw_game(board):
- if " " in board:
- return False
- else:
- return True
- def clear_board(board):
- for loop in range(1, 10):
- board[loop] = " "
- def start_over(board):
- while True:
- choice = input("The game has ended. Do you wish to play another game? (Y/N) : ")
- if choice == "Y":
- clear_board(board)
- return True
- elif choice == "N":
- print("\nThank you for playing")
- return False
- else:
- print("That is not a valid input. Please try again.")
- def comp_move(board, x_choice, x_spaces, o_spaces):
- #1) range of 1 to 10
- #2) copy board
- #3) check if space is free
- #4) place a O in that spot
- #5) see if comp win, if win, return that spot
- #6) repeat for player, but block player if player can win
- #If I (computer) can win, go ahead and win
- for i in range(1, 10):
- copy = copy_board(board)
- if is_empty_space(copy, i): #If the specific place in the board is empty (If the function returns True)
- copy[i] = "o" #Place an "o" in that place
- if is_player_win(copy, "o"): #If the player "o" wins (If the function returns True), return that place value to win
- return i
- #If the opponent can win, block him
- for j in range(1, 10):
- copy = copy_board(board)
- if is_empty_space(copy, j): #If the specific place in the board is empty (If the function returns True)
- copy[j] = "x" #Place an "x" in that place
- if is_player_win(copy, "x"): #If the player "x" wins (If the function returns True), return that place value to block
- return j
- corners = [1, 3, 7, 9]
- oppCorners1 = [1, 9]
- oppCorners2 = [3, 7]
- edge2 = [1, 3]
- edge4 = [1, 7]
- edge6 = [3, 9]
- edge8 = [7, 9]
- corner1 = [2, 4]
- corner3 = [2, 6]
- corner7 = [4, 8]
- corner9 = [6, 8]
- copy = copy_board(board)
- x_spaces.append(x_choice)
- #Round 1
- if len(x_spaces) >= 1:
- #Branch 1.1
- if x_spaces[0] in corners:
- #Round 2
- if len(x_spaces) >= 2:
- if x_spaces[0] in oppCorners1 and x_spaces[1] in oppCorners1 and x_spaces[0] != x_spaces[1]:
- if x_spaces[1] == 1:
- move = random.choice(corner1)
- return move, x_spaces, o_spaces
- elif x_spaces[1] == 3:
- move = random.choice(corner3)
- return move, x_spaces, o_spaces
- elif x_spaces[1] == 7:
- move = random.choice(corner7)
- return move, x_spaces, o_spaces
- elif x_spaces[1] == 9:
- move = random.choice(corner9)
- return move, x_spaces, o_spaces
- elif x_spaces[0] in oppCorners2 and x_spaces[1] in oppCorners2 and x_spaces[0] != x_spaces[1]:
- pass
- #Branch 1.1 outcome
- else:
- return 5, x_spaces, o_spaces
- #Branch 1.2
- elif x_choice == 5:
- move = random.choice(corners)
- return move, x_spaces, o_spaces
- #Branch 1.3
- elif x_choice == 2:
- move = random.choice(edge2)
- return move, x_spaces, o_spaces
- #Branch 1.4
- elif x_choice == 4:
- move = random.choice(edge4)
- return move, x_spaces, o_spaces
- #Branch 1.5
- elif x_choice == 6:
- move = random.choice(edge6)
- return move, x_spaces, o_spaces
- #Branch 1.6
- elif x_choice == 8:
- move = random.choice(edge8)
- return move, x_spaces, o_spaces
- while True:
- move = random.randint(1, 9)
- if is_empty_space(board, place):
- return move, x_spaces, o_spaces
- else:
- continue
- ################################## MAIN PROGRAM ##################################
- while True:
- print_header()
- playing_board()
- while True:
- #Input from player "x"
- try:
- x_choice = int(input("It is x's turn (0 to restart): "))
- if x_choice == 0:
- print("Restarting...\n")
- time.sleep(1)
- break
- except ValueError:
- continue
- if board[x_choice] == " ":
- board[x_choice] = "x"
- playing_board()
- break
- else:
- print("This spot is taken. Please try again.")
- time.sleep(1)
- #Check if player "x" wins
- if is_player_win(board, "x"):
- print_header()
- playing_board()
- print("Player 'x' wins!")
- if start_over(board):
- continue
- else:
- break
- #Check for tie
- if draw_game(board):
- playing_board()
- print("It is a tie!")
- if start_over(board):
- continue
- else:
- break
- while True:
- if x_choice == 0:
- break
- #Input from player "o"
- #o_choice = int(input("It is o's turn: "))
- o_choice, x_spaces, o_spaces = comp_move(board, x_choice, x_spaces, o_spaces)
- if board[o_choice] == " ":
- board[o_choice] = "o"
- #playing_board()
- break
- else:
- print("This spot is taken. Please try again.")
- time.sleep(1)
- #Check if player "o" wins
- if is_player_win(board, "o"):
- print_header()
- playing_board()
- print("Player 'o' wins!")
- if start_over(board):
- continue
- else:
- break
- #Check for tie
- if draw_game(board):
- playing_board()
- print("It is a tie!")
- if start_over(board):
- continue
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement