I am trying to create a tic-tac-toe bot that uses the minimax algorithm to help me.
I would like help with the function that detects if a state is a terminal state.
For some reason it is always return False.
Note that the state is a list of strings, one string for every grid space.
1: your enemy’s mark(o/x)
0: your mark(x/o)
space: empty space
def terminal(state):
lines = [
" # # # ",
" ### ",
"### ",
" ###",
"# # # ",
" # # #",
"# # #",
" # # # ",
]
player = "0"
playerlines = [
list(str(line).replace("#", player)) for line in lines
] # lines but with hashtags replaced wth the player
evalstate = list(
str(state).replace(otherplayer(player), " ")
) # the gameboard with only the current player for detection
if evalstate in playerlines:
return 1
player = "1"
playerlines = [list(str(line).replace("#", player)) for line in lines]
evalstate = list(str(state).replace(otherplayer(player), " "))
if evalstate in playerlines:
return -1
if not " " in state:
return 0
return False