#tictactoe.py import random def mark(num): '''return 'O' ....and 1 to 'X', -3 to space''' if num == -3: return ' ' elif num == 0: return 'O' else: return 'X' def display(board): print("―――――――") for i in range(3): print(f"│{mark(board[i][0])}│{mark(board[i][1])}│{mark(board[i][2])}│\n―――――――") #print(f"│{board[i][0]}│{board[i][1]}│{board[i][2]}│\n―――――――") def is_winner(B,player): #player is 1('X') or 0('O') for i in range(3): if sum(B[i]) == player*3: #row return True if B[0][i]+B[1][i]+B[2][i] == player*3: #column return True #diagonals: if B[0][0]+B[1][1]+B[2][2] == player*3 or \ B[0][2]+B[1][1]+B[2][0] == player*3: return True return False def make_good_move(board): # 'X' generates a move. '''If a win, take it. Elif block other's win, play that. elif center open, take it else random''' #9 possible positions...Play the win if board[0][0]==-3: if board[0][1]==1 and board[0][2]==1 or\ board[1][0]==1 and board[2][0]==1 or\ board[1][1]==1 and board[2][2]==1: return 0,0 if board[0][1]==-3: if board[0][0]==1 and board[0][2]==1 or\ board[1][1]==1 and board[2][1]==1: return 0,1 if board[0][2]==-3: if board[0][0]==1 and board[0][1]==1 or\ board[1][2]==1 and board[2][2]==1 or\ board[1][1]==1 and board[2][0]==1: return 0,2 if board[1][0]==-3: if board[0][0]==1 and board[2][0]==1 or\ board[1][1]==1 and board[1][2]==1: return 1,0 if board[1][1]==-3: if board[0][0]==1 and board[2][2]==1 or\ board[2][0]==1 and board[0][2]==1 or\ board[1][0]==1 and board[1][2]==1 or\ board[0][1]==1 and board[2][1]==1: return 1,1 if board[1][2]==-3: if board[0][2]==1 and board[2][2]==1 or\ board[1][0]==1 and board[1][1]==1: return 1,2 if board[2][0]==-3: if board[0][0]==1 and board[1][0]==1 or\ board[1][1]==1 and board[0][2]==1 or\ board[2][1]==1 and board[2][2]==1: return 2,0 elif board[2][1]==-3: if board[0][1]==1 and board[1][1]==1 or\ board[2][0]==1 and board[2][2]==1: return 2,1 if board[2][2]==-3: if board[0][0]==1 and board[1][1]==1 or\ board[1][2]==1 and board[0][2]==1 or\ board[2][0]==1 and board[2][1]==1: return 2,2 #9 possible positions...Play the block a win by other print("at Block") if board[0][0]==-3: if board[0][1]==0 and board[0][2]==0 or\ board[1][0]==0 and board[2][0]==0 or\ board[1][1]==0 and board[2][2]==0: return 0,0 if board[0][1]==-3: if board[0][0]==0 and board[0][2]==0 or\ board[1][1]==0 and board[2][1]==0: return 0,1 if board[0][2]==-3: if board[0][0]==0 and board[0][1]==0 or\ board[1][2]==0 and board[2][2]==0 or\ board[1][1]==0 and board[2][0]==0: return 0,2 if board[1][0]==-3: if board[0][0]==0 and board[2][0]==0 or\ board[1][1]==0 and board[1][2]==0: return 1,0 if board[1][1]==-3: if board[0][0]==0 and board[2][2]==0 or\ board[2][0]==0 and board[0][2]==0 or\ board[1][0]==0 and board[1][2]==0 or\ board[0][1]==0 and board[2][1]==0: return 1,1 if board[1][2]==-3: if board[0][2]==0 and board[2][2]==0 or\ board[1][0]==0 and board[1][1]==0: return 1,2 if board[2][0]==-3: if board[0][0]==0 and board[1][0]==0 or\ board[1][1]==0 and board[0][2]==0 or\ board[2][1]==0 and board[2][2]==0: return 2,0 if board[2][1]==-3: if board[0][1]==0 and board[1][1]==0 or\ board[2][0]==0 and board[2][2]==0: return 2,1 if board[2][2]==-3: if board[0][0]==0 and board[1][1]==0 or\ board[1][2]==0 and board[0][2]==0 or\ board[2][0]==0 and board[2][1]==0: return 2,2 if board[1][1] == -3: #take center if available return 1,1 #...take one of the corners print("at random") while True: #randomly find a free row = random.randint(0,2) col = random.randint(0,2) if board[row][col] == -3: return row,col def get_valid_move(board): # 'O' user chooses free position '''let user enter 1,2,3 but return 0,1,2''' is_valid_move = False while not is_valid_move: inpL = input("Enter row (1,2,3) and col (1,2,3) separated by space: ").\ strip().split() row = int(inpL[0]) col = int(inpL[1]) if row<1 or row>3 or col<1 or col>3: print("Invalid row and/or column number(s)! Try again") else: if board[row-1][col-1] != -3: print("That position is already played! Try again") else: return row-1,col-1 B = [[-3,-3,-3], [-3,-3,-3], [-3,-3,-3]] display(B) if random.choice(['you','me']) == 'me': print("I play first. I am X") turn = 1 # 'X' else: print("You play first. You are O") turn = 0 # 'O' plays = 1 while plays<10: if turn == 1: # 'X' play_coords = make_good_move(B) print(play_coords[0],play_coords[1]) B[play_coords[0]][play_coords[1]] = 1 display(B) if is_winner(B,1): break turn = 0 else: play_coords = get_valid_move(B) # 'O' B[play_coords[0]][play_coords[1]] = 0 display(B) if is_winner(B,0): break turn = 1 plays += 1 if plays == 10: print("Draw") elif turn == 1: print("I win") else: print("You win")