#๐ tic tac toe computer
35 messages ยท Page 1 of 1 (latest)
@pallid birch
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
am i stupid or is that integer division right there returning a float?
:warning: Your 3.13 eval job has completed with return code 0.
[No output]
!e print(3.0//3)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
1.0
huhhh
!e print(3//3)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
1
wow
apparently float//int := float, but im like 90% certain it didnt get a float there
:white_check_mark: Your 3.13 eval job has completed with return code 0.
1.0
:white_check_mark: Your 3.13 eval job has completed with return code 0.
1.0
๐
ooh nice
from copy import deepcopy as dc
def best_move(board,player):
winner = is_won(board)
if winner == 1:
return 1
elif winner == 0:
return 0
elif isfull(board):
return 0.5
tries = []
for k,i in enumerate(board):
for k_,i_ in enumerate(i):
if i_ == None:
tries.append(dc(board))
tries[-1][k][k_] = player
if player == 1:
return max(best_move(i,0) for i in tries)
else:
return min(best_move(i,0) for i in tries)
def isfull(board):
if not any(any(i == None for i in j) for j in board):
return True
return False
def is_won(board):
if any(all(j == 1 for j in i) for i in board):
return 1
elif any(all(j == 0 for j in i) for i in board):
return 0
elif any(all(board[i][j] == 1 for i in range(3)) for j in range(3)):
return 1
elif any(all(board[i][j] == 0 for i in range(3)) for j in range(3)):
return 0
elif all(board[i][i] == 1 for i in range(3)):
return 1
elif all(board[i][i] == 0 for i in range(3)):
return 0
elif all(board[i][2-i] == 1 for i in range(3)):
return 1
elif all(board[i][2-1] == 0 for i in range(3)):
return 0
else: return None
def render(board):
print(("\n"+"-"*11+"\n").join("|".join([[" X "," O "][j] if not j is None else " " for j in i]) for i in board))
def game(board,player):
if isfull(board):
return -1
if player == 0:
move = int(input("Player 1 enter your move: "))
board[int(move//3)][int(move%3)] = 1
render(board)
elif player == 1:
print("computing best move")
move = best_move(board,1)
board[int(move//3)][int(move%3)] = 0
render(board)
winner = is_won(board)
if winner:
return winner
return game(board, not player)
board = [[None]*3 for _ in range(3)]
game(board,0)
for some reason the computer can just override fields
just ignore everything but best_move(), thats where the mistake is
tic tac toe computer
oh yeah by the way, the tree for tic tac toe is small enough that you can precompute it in memory
i know, i have them all memorized anyways
but its about the concept
i could throw that at connect 4 or chess in the future
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.