I want to make a tic - tac - toe game, where a bot plays against a human player.
I coded the game, but for unknown reason the program gives me an error:
line 15, in __char__won
if board[1] == board[2] and board[1] == board[3] and board[1] == mark:
RecursionError: maximum recursion depth exceeded in comparison**
The code:
board = ['', ' #1 ', ' #2 ', ' #3 ',
' #4 ', ' #5 ', ' #6 ',
' #7 ', ' #8 ', ' #9 ']
def Printboard():
print()
print(' | ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' | ' )
print(' ---------------------- ')
print(' | ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' | ')
print(' ---------------------- ')
print(' | ' + board[7] + ' | ' + board[8] + ' | ' + board[9] + ' | ')
print()
def __char__won(mark):
if board[1] == board[2] and board[1] == board[3] and board[1] == mark:
return True
elif (board[4] == board[5] and board[4] == board[6] and board[4] == mark):
return True
elif (board[7] == board[8] and board[7] == board[9] and board[7] == mark):
return True
elif (board[1] == board[4] and board[1] == board[7] and board[1] == mark):
return True
elif (board[2] == board[5] and board[2] == board[8] and board[2] == mark):
return True
elif (board[3] == board[6] and board[3] == board[9] and board[3] == mark):
return True
elif (board[1] == board[5] and board[1] == board[9] and board[1] == mark):
return True
elif (board[7] == board[5] and board[7] == board[3] and board[7] == mark):
return True
else:
return False
def __draw():
count = 0
for i in range(9):
if board[i] != ' o ' or ' x ':
count += 1
if count > 0:
return False
else:
return True
def insert_Letter(player, position):
if board[position] == ' x ' or board[position] == ' o ':
print('Can´t play that move!')
print('Try another move!')
else:
board[position] = player
Printboard()
take_input()
def x_turn():
x = 0
o = 0
for i in range(9):
if board[i] == ' x ':
x += 1
for i in range(9):
if board[i] == ' o ':
o += 1
if o > x:
return True
else:
return False
def take_input():
if __char__won(' x '):
print('X won!')
elif __char__won(' o '):
print('O, won!')
elif __draw():
print('Draw!')
elif x_turn():
compMove()
else:
position = int(input('Type in your move for " o " '))
insert_Letter(' o ', position)
def compMove():
bestscore = -1000
bestmove = 0
for i in range(9):
if board[i] != ' x ' or board[i] != ' o ':
c = board[i]
board[i] = ' x '
score = minimax(board, False)
board[i] = c
if score > bestscore:
bestscore = score
bestmove = i
insert_Letter(' x ', bestmove)
return
def minimax(newBoard, Maximizing):
if __char__won(' x '):
return 10
elif __char__won(' o '):
return -10
elif __draw():
return 0
if Maximizing:
bestscore = -1000
for i in range(9):
if newBoard[i] != ' x ' or newBoard[i] != ' o ':
c = board[i]
newBoard[i] = ' x '
score = minimax(board, False)
newBoard[i] = c
if score > bestscore:
bestscore = score
return bestscore
else:
bestscore = 1000
for i in range(9):
if newBoard[i] != ' x ' or newBoard[i] != ' o ':
c = newBoard[i]
board[i] = ' o '
score = minimax(board, True)
newBoard[i] = c
if score < bestscore:
bestscore = score
return bestscore
Printboard()
take_input()