# tic tac toe import random def drawBoard(board): # prints out the tic tac toe board from the list taken in # board is a list of 9 strings representing the board ''' 0 | 1 | 2 ---+---+--- 3 | 4 | 5 ---+---+--- 6 | 7 | 8 ''' print(f' {board[0]} | {board[1]} | {board[2]}') print('---+---+---') print(f' {board[3]} | {board[4]} | {board[5]}') print('---+---+---') print(f' {board[6]} | {board[7]} | {board[8]}') def inputPlayerLetter(): # Lets the player choose X's or O's # Returns a list with the player's letter first and # computer letter second: [X, O] or [O, X] letter = '' while not (letter == 'X' or letter == 'O'): print("Do you want to be X's or O's?") letter = input().upper() if letter == 'X': return ['X', 'O'] # player is X's, computer is O's else: return ['O', 'X'] # player is O's, computer is X's def whoGoesFirst(): # randomly choose which player goes first if random.randint(0,1) == 0: return 'computer' else: return 'player' def makeMove(board, letter, move): board[move] = letter def isWinner(board, letter): # Given the board and the letter to check, see if that # player has won return (board[0] == letter and board[1] == letter and board[2] == letter) or (board[3] == letter and board[4] == letter and board[5] == letter) or (board[6] == letter and board[7] == letter and board[8] == letter) or (board[0] == letter and board[3] == letter and board[6] == letter) or (board[1] == letter and board[4] == letter and board[7] == letter) or (board[2] == letter and board[5] == letter and board[8] == letter) or (board[0] == letter and board[4] == letter and board[8] == letter) or (board[2] == letter and board[4] == letter and board[6] == letter) # diagonal check def getBoardCopy(board): # make a copy of the board list and return it boardCopy = [] for i in board: boardCopy.append(i) return boardCopy def isSpaceFree(board, move): # returns True if the move parameter space on the board is empty return board[move] == ' ' def getPlayerMove(board): # let the player enter their move move = ' ' # player has to choose 0 - 9 and it has to be a free space while True: print('What is your next move? (0-8)') move = input() if move.isdigit() and int(move) in range(9): move = int(move) if isSpaceFree(board, move): return move else: print('Spot already taken!') else: print('Invalid input! Enter a number 0-8') # while move not in '0 1 2 3 4 5 6 7 8'.split() or not isSpaceFree(board, int(move)): # if move.isdigit() and int(move) in list(range(9)): # if not isSpaceFree(board, int(move)): # print('Spot already taken!') # print('What is your next move? (0-8)') # move = input() # return int(move) def chooseRandomMoveFromList(board, movesList): # returns a random, valid move from the list passed in # returns None if there are no valid moves possibleMoves = [] for move in movesList: if isSpaceFree(board, move): possibleMoves.append(move) if len(possibleMoves) != 0: return random.choice(possibleMoves) else: return None def getComputerMove(board, computerLetter): # given a board and the computer's letter, determine # the best move for the computer to make, and make it if computerLetter == 'X': playerLetter = 'O' else: playerLetter = 'X' # Computer check for winning move first for spot in range(9): boardCopy = getBoardCopy(board) if isSpaceFree(boardCopy, spot): makeMove(boardCopy, computerLetter, spot) if isWinner(boardCopy, computerLetter): return spot # if no winning moves, check if we need to block for spot in range(9): boardCopy = getBoardCopy(board) if isSpaceFree(boardCopy, spot): makeMove(boardCopy, playerLetter, spot) if isWinner(boardCopy, playerLetter): return spot # if no need to block player, choose a corner (0, 2, 6, or 8) move = chooseRandomMoveFromList(board, [0, 2, 6, 8]) if move != None: return move # if no corners, take the center (4) if isSpaceFree(4): return 4 # if center is not free, choose a side spot (1, 5, 7, or 9) return chooseRandomMoveFromList(board, [1, 5, 7, 3]) def isBoardFull(board): # if every space on the board is taken, True. Otherwise false for spot in board: if spot == ' ': return False # no blank spots, board is full return True print('Welcome to Tic-Tac-Toe!') print('The board has spots 0 through 8. When you pick ' \ 'your spot, use the key to make sure you know where ' \ 'you are going.') print('''KEY: 0 | 1 | 2 ---+---+--- 3 | 4 | 5 ---+---+--- 6 | 7 | 8 ''') while True: # Reset the board board = [' '] * 9 playerLetter, computerLetter = inputPlayerLetter() turn = whoGoesFirst() print('The ' + turn + ' will go first.') gameIsPlaying = True while gameIsPlaying: if turn == 'player': # player turn logic drawBoard(board) move = getPlayerMove(board) makeMove(board, playerLetter, move) if isWinner(board, playerLetter): drawBoard(board) print('You won!') gameIsPlaying = False elif isBoardFull(board): drawBoard(board) print('The game is a tie!') break else: turn = 'computer' else: # computer turn logic move = getComputerMove(board, computerLetter) makeMove(board, computerLetter, move) if isWinner(board, computerLetter): drawBoard(board) print('You lost!') gameIsPlaying = False elif isBoardFull(board): drawBoard(board) print('The game is a tie!') break else: turn = 'player' print('Do you want to play again? (yes or no)') if not input().lower().startswith('y'): break