├── tictactoePart1Finished.py ├── tictactoe ├── tictactoe.py └── TicTacToeMinimax.py /tictactoePart1Finished.py: -------------------------------------------------------------------------------- 1 | board = {1: ' ', 2: ' ', 3: ' ', 2 | 4: ' ', 5: ' ', 6: ' ', 3 | 7: ' ', 8: ' ', 9: ' '} 4 | player = 'O' 5 | bot = 'X' 6 | 7 | def printBoard(board): 8 | print(board[1] + '|' + board[2] + '|' + board[3]) 9 | print('-+-+-') 10 | print(board[4] + '|' + board[5] + '|' + board[6]) 11 | print('-+-+-') 12 | print(board[7] + '|' + board[8] + '|' + board[9]) 13 | print("\n") 14 | 15 | 16 | def spaceIsFree(position): 17 | if board[position] == ' ': 18 | return True 19 | else: 20 | return False 21 | 22 | 23 | def insertLetter(letter, position): 24 | if spaceIsFree(position): 25 | board[position] = letter 26 | printBoard(board) 27 | if (checkDraw()): 28 | print("Draw!") 29 | exit() 30 | if checkForWin(): 31 | if letter == 'X': 32 | print("Bot wins!") 33 | exit() 34 | else: 35 | print("Player wins!") 36 | exit() 37 | 38 | return 39 | 40 | 41 | else: 42 | print("Can't insert there!") 43 | position = int(input("Please enter new position: ")) 44 | insertLetter(letter, position) 45 | return 46 | 47 | 48 | def checkForWin(): 49 | if (board[1] == board[2] and board[1] == board[3] and board[1] != ' '): 50 | return True 51 | elif (board[4] == board[5] and board[4] == board[6] and board[4] != ' '): 52 | return True 53 | elif (board[7] == board[8] and board[7] == board[9] and board[7] != ' '): 54 | return True 55 | elif (board[1] == board[4] and board[1] == board[7] and board[1] != ' '): 56 | return True 57 | elif (board[2] == board[5] and board[2] == board[8] and board[2] != ' '): 58 | return True 59 | elif (board[3] == board[6] and board[3] == board[9] and board[3] != ' '): 60 | return True 61 | elif (board[1] == board[5] and board[1] == board[9] and board[1] != ' '): 62 | return True 63 | elif (board[7] == board[5] and board[7] == board[3] and board[7] != ' '): 64 | return True 65 | else: 66 | return False 67 | 68 | 69 | def checkDraw(): 70 | for key in board.keys(): 71 | if (board[key] == ' '): 72 | return False 73 | return True 74 | 75 | 76 | def playerMove(): 77 | position = int(input("Enter the position for 'O': ")) 78 | insertLetter(player, position) 79 | return 80 | 81 | 82 | def compMove(): 83 | position = int(input("Enter the position for 'X': ")) 84 | insertLetter(bot, position) 85 | return 86 | 87 | printBoard(board) 88 | 89 | 90 | 91 | 92 | while not checkForWin(): 93 | compMove() 94 | playerMove() -------------------------------------------------------------------------------- /tictactoe: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | board = ["-", "-", "-", 5 | "-", "-", "-", 6 | "-", "-", "-"] 7 | currentPlayer = "X" 8 | winner = None 9 | gameRunning = True 10 | 11 | # game board 12 | def printBoard(board): 13 | print(board[0] + " | " + board[1] + " | " + board[2]) 14 | print("---------") 15 | print(board[3] + " | " + board[4] + " | " + board[5]) 16 | print("---------") 17 | print(board[6] + " | " + board[7] + " | " + board[8]) 18 | 19 | 20 | # take player input 21 | def playerInput(board): 22 | inp = int(input("Select a spot 1-9: ")) 23 | if board[inp-1] == "-": 24 | board[inp-1] = currentPlayer 25 | else: 26 | print("Oops player is already at that spot.") 27 | 28 | 29 | # check for win or tie 30 | def checkHorizontle(board): 31 | global winner 32 | if board[0] == board[1] == board[2] and board[0] != "-": 33 | winner = board[0] 34 | return True 35 | elif board[3] == board[4] == board[5] and board[3] != "-": 36 | winner = board[3] 37 | return True 38 | elif board[6] == board[7] == board[8] and board[6] != "-": 39 | winner = board[6] 40 | return True 41 | 42 | def checkRow(board): 43 | global winner 44 | if board[0] == board[3] == board[6] and board[0] != "-": 45 | winner = board[0] 46 | return True 47 | elif board[1] == board[4] == board[7] and board[1] != "-": 48 | winner = board[1] 49 | return True 50 | elif board[2] == board[5] == board[8] and board[2] != "-": 51 | winner = board[3] 52 | return True 53 | 54 | 55 | def checkDiag(board): 56 | global winner 57 | if board[0] == board[4] == board[8] and board[0] != "-": 58 | winner = board[0] 59 | return True 60 | elif board[2] == board[4] == board[6] and board[4] != "-": 61 | winner = board[2] 62 | return True 63 | 64 | 65 | def checkIfWin(board): 66 | global gameRunning 67 | if checkHorizontle(board): 68 | printBoard(board) 69 | print(f"The winner is {winner}!") 70 | gameRunning = False 71 | 72 | elif checkRow(board): 73 | printBoard(board) 74 | print(f"The winner is {winner}!") 75 | gameRunning = False 76 | 77 | elif checkDiag(board): 78 | printBoard(board) 79 | print(f"The winner is {winner}!") 80 | gameRunning = False 81 | 82 | 83 | def checkIfTie(board): 84 | global gameRunning 85 | if "-" not in board: 86 | printBoard(board) 87 | print("It is a tie!") 88 | gameRunning = False 89 | 90 | 91 | # switch player 92 | def switchPlayer(): 93 | global currentPlayer 94 | if currentPlayer == "X": 95 | currentPlayer = "O" 96 | else: 97 | currentPlayer = "X" 98 | 99 | 100 | def computer(board): 101 | while currentPlayer == "O": 102 | position = random.randint(0, 8) 103 | if board[position] == "-": 104 | board[position] = "O" 105 | switchPlayer() 106 | 107 | 108 | while gameRunning: 109 | printBoard(board) 110 | playerInput(board) 111 | checkIfWin(board) 112 | checkIfTie(board) 113 | switchPlayer() 114 | computer(board) 115 | checkIfWin(board) 116 | checkIfTie(board) 117 | 118 | 119 | -------------------------------------------------------------------------------- /tictactoe.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | board = ["-", "-", "-", 5 | "-", "-", "-", 6 | "-", "-", "-"] 7 | currentPlayer = "X" 8 | winner = None 9 | gameRunning = True 10 | 11 | # game board 12 | def printBoard(board): 13 | print(board[0] + " | " + board[1] + " | " + board[2]) 14 | print("---------") 15 | print(board[3] + " | " + board[4] + " | " + board[5]) 16 | print("---------") 17 | print(board[6] + " | " + board[7] + " | " + board[8]) 18 | 19 | 20 | # take player input 21 | def playerInput(board): 22 | inp = int(input("Select a spot 1-9: ")) 23 | if board[inp-1] == "-": 24 | board[inp-1] = currentPlayer 25 | else: 26 | print("Oops player is already at that spot.") 27 | 28 | 29 | # check for win or tie 30 | def checkHorizontle(board): 31 | global winner 32 | if board[0] == board[1] == board[2] and board[0] != "-": 33 | winner = board[0] 34 | return True 35 | elif board[3] == board[4] == board[5] and board[3] != "-": 36 | winner = board[3] 37 | return True 38 | elif board[6] == board[7] == board[8] and board[6] != "-": 39 | winner = board[6] 40 | return True 41 | 42 | def checkRow(board): 43 | global winner 44 | if board[0] == board[3] == board[6] and board[0] != "-": 45 | winner = board[0] 46 | return True 47 | elif board[1] == board[4] == board[7] and board[1] != "-": 48 | winner = board[1] 49 | return True 50 | elif board[2] == board[5] == board[8] and board[2] != "-": 51 | winner = board[3] 52 | return True 53 | 54 | 55 | def checkDiag(board): 56 | global winner 57 | if board[0] == board[4] == board[8] and board[0] != "-": 58 | winner = board[0] 59 | return True 60 | elif board[2] == board[4] == board[6] and board[4] != "-": 61 | winner = board[2] 62 | return True 63 | 64 | 65 | def checkIfWin(board): 66 | global gameRunning 67 | if checkHorizontle(board): 68 | printBoard(board) 69 | print(f"The winner is {winner}!") 70 | gameRunning = False 71 | 72 | elif checkRow(board): 73 | printBoard(board) 74 | print(f"The winner is {winner}!") 75 | gameRunning = False 76 | 77 | elif checkDiag(board): 78 | printBoard(board) 79 | print(f"The winner is {winner}!") 80 | gameRunning = False 81 | 82 | 83 | def checkIfTie(board): 84 | global gameRunning 85 | if "-" not in board: 86 | printBoard(board) 87 | print("It is a tie!") 88 | gameRunning = False 89 | 90 | 91 | # switch player 92 | def switchPlayer(): 93 | global currentPlayer 94 | if currentPlayer == "X": 95 | currentPlayer = "O" 96 | else: 97 | currentPlayer = "X" 98 | 99 | 100 | def computer(board): 101 | while currentPlayer == "O": 102 | position = random.randint(0, 8) 103 | if board[position] == "-": 104 | board[position] = "O" 105 | switchPlayer() 106 | 107 | 108 | while gameRunning: 109 | printBoard(board) 110 | playerInput(board) 111 | checkIfWin(board) 112 | checkIfTie(board) 113 | switchPlayer() 114 | computer(board) 115 | checkIfWin(board) 116 | checkIfTie(board) 117 | 118 | 119 | -------------------------------------------------------------------------------- /TicTacToeMinimax.py: -------------------------------------------------------------------------------- 1 | board = {1: ' ', 2: ' ', 3: ' ', 2 | 4: ' ', 5: ' ', 6: ' ', 3 | 7: ' ', 8: ' ', 9: ' '} 4 | player = 'O' 5 | computer = 'X' 6 | 7 | def printBoard(board): 8 | print(board[1] + "|" + board[2] + "|" + board[3]) 9 | print("-+-+-") 10 | print(board[4] + "|" + board[5] + "|" + board[6]) 11 | print("-+-+-") 12 | print(board[7] + "|" + board[8] + "|" + board[9]) 13 | print("\n") 14 | 15 | def spaceIsFree(position): 16 | if board[position] == ' ': 17 | return True 18 | return False 19 | 20 | def insertLetter(letter, position): 21 | if spaceIsFree(position): 22 | board[position] = letter 23 | printBoard(board) 24 | if checkDraw(): 25 | print("Draw!") 26 | exit() 27 | if checkWin(): 28 | if letter == 'X': 29 | print("Bot wins!") 30 | exit() 31 | else: 32 | print("Player wins!") 33 | exit() 34 | return 35 | else: 36 | print("Invalid position") 37 | position = int(input("Please enter a new position: ")) 38 | insertLetter(letter, position) 39 | return 40 | 41 | def checkWin(): 42 | if (board[1] == board[2] and board[1] == board[3] and board[1] != ' '): 43 | return True 44 | elif (board[4] == board[5] and board[4] == board[6] and board[4] != ' '): 45 | return True 46 | elif (board[7] == board[8] and board[7] == board[9] and board[7] != ' '): 47 | return True 48 | elif (board[1] == board[4] and board[1] == board[7] and board[1] != ' '): 49 | return True 50 | elif (board[2] == board[5] and board[2] == board[8] and board[2] != ' '): 51 | return True 52 | elif (board[3] == board[6] and board[3] == board[9] and board[3] != ' '): 53 | return True 54 | elif (board[1] == board[5] and board[1] == board[9] and board[1] != ' '): 55 | return True 56 | elif (board[7] == board[5] and board[7] == board[3] and board[7] != ' '): 57 | return True 58 | else: 59 | return False 60 | 61 | def checkWhichMarkWon(mark): 62 | if (board[1] == board[2] and board[1] == board[3] and board[1] == mark): 63 | return True 64 | elif (board[4] == board[5] and board[4] == board[6] and board[4] == mark): 65 | return True 66 | elif (board[7] == board[8] and board[7] == board[9] and board[7] == mark): 67 | return True 68 | elif (board[1] == board[4] and board[1] == board[7] and board[1] == mark): 69 | return True 70 | elif (board[2] == board[5] and board[2] == board[8] and board[2] == mark): 71 | return True 72 | elif (board[3] == board[6] and board[3] == board[9] and board[3] == mark): 73 | return True 74 | elif (board[1] == board[5] and board[1] == board[9] and board[1] == mark): 75 | return True 76 | elif (board[7] == board[5] and board[7] == board[3] and board[7] == mark): 77 | return True 78 | else: 79 | return False 80 | 81 | def checkDraw(): 82 | for key in board.keys(): 83 | if board[key] == ' ': 84 | return False 85 | return True 86 | 87 | def playerMove(): 88 | position = int(input("Enter a position for 'O': ")) 89 | insertLetter(player, position) 90 | return 91 | 92 | def compMove(): 93 | bestScore = -800 94 | bestMove = 0 95 | for key in board.keys(): 96 | if board[key] == ' ': 97 | board[key] = computer 98 | score = minimax(board, False) 99 | board[key] = ' ' 100 | if score > bestScore: 101 | bestScore = score 102 | bestMove = key 103 | insertLetter(computer, bestMove) 104 | return 105 | def minimax(board, isMaximizing): 106 | if checkWhichMarkWon(computer): 107 | return 1 108 | elif checkWhichMarkWon(player): 109 | return -1 110 | elif checkDraw(): 111 | return 0 112 | 113 | if isMaximizing: 114 | bestScore = -800 115 | for key in board.keys(): 116 | if board[key] == ' ': 117 | board[key] = computer 118 | score = minimax(board, False) 119 | board[key] = ' ' 120 | if score > bestScore: 121 | bestScore = score 122 | return bestScore 123 | else: 124 | bestScore = 800 125 | for key in board.keys(): 126 | if board[key] == ' ': 127 | board[key] = player 128 | score = minimax(board, True) 129 | board[key] = ' ' 130 | if score < bestScore: 131 | bestScore = score 132 | return bestScore 133 | 134 | 135 | while not checkWin(): 136 | compMove() 137 | playerMove() --------------------------------------------------------------------------------