# python-ai / src / tic-tac-toe / game_reference.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92``` ```import struct, string class TicTacToeBoard: def __init__(self): self.board = (['N']*3,['N']*3,['N']*3) def PrintBoard(self): print(self.board[0][0] + "|" + self.board[1][0] + "|" + self.board[2][0]) print(self.board[0][1] + "|" + self.board[1][1] + "|" + self.board[2][1]) print(self.board[0][2] + "|" + self.board[1][2] + "|" + self.board[2][2]) def play_square(self, col, row, val): self.board[col][row] = val def get_square(self, col, row): return self.board[col][row] def full_board(self): for i in range(3): for j in range(3): if(self.board[i][j]=='N'): return False return True #if there is a winner this will return their symbol (either 'X' or 'O'), #otherwise it will return 'N' def winner(self): #check the cols for col in range(3): if(self.board[col][0]!=0 and self.board[col][0] == self.board[col][1] and self.board[col][0]==self.board[col][2] ): return self.board[col][0] #check the rows for row in range(3): if(self.board[0][row]!=0 and self.board[0][row] == self.board[1][row] and self.board[0][row]==self.board[2][row] ): return self.board[0][row] #check diagonals if(self.board[0][0]!=0 and self.board[0][0] == self.board[1][1] and self.board[0][0]==self.board[2][2] ): return self.board[0][0] if(self.board[2][0]!=0 and self.board[2][0] == self.board[1][1] and self.board[2][0]==self.board[0][2]): return self.board[2][0] return 'N' def make_simple_cpu_move(board, cpuval): for i in range(3): for j in range(3): if(board.get_square(i,j)=='N'): board.play_square(i,j,cpuval) return True return False def play(): Board = TicTacToeBoard() humanval = 'X' cpuval = 'O' Board.PrintBoard() while( Board.full_board()==False and Board.winner() == 'N'): print("your move, pick a row (0-2)") row = int(input()) print("your move, pick a col (0-2)") col = int(input()) if(Board.get_square(col,row)!='N'): print("square already taken!") continue else: Board.play_square(col,row,humanval) if(Board.full_board() or Board.winner()!='N'): break else: Board.PrintBoard() print("CPU Move") make_simple_cpu_move(Board,cpuval) Board.PrintBoard() Board.PrintBoard() if(Board.winner()=='N'): print("Cat game") elif(Board.winner()==humanval): print("You Win!") elif(Board.winner()==cpuval): print("CPU Wins!") def main(): play() main() ```