If you invoke "my_board.set_o(1,2)" after printing the board, the error in pyhton is
Traceback (most recent call last):
File "t.py", line 35, in <module>
my_board.set_o(2,4)
File "t.py", line 11, in set_o
o.position = (r,c)
NameError: global name 'o' is not defined
I think in set_o only the position on board has to be marked as 'O' & can be easily done by :
self.board[r][c]='O'
Below I have tried to correct hte prog:
class TikTacToeBoard:
def __init__(self):
self.board = [['-'] * 3, ['-'] * 3, ['-'] * 3]
def validate(self,r,c):
if r<3 and c < 3 and self.board[r][c] == '-':
return True
else:
return False
def set_x(self,r,c):
'''x.position[0] = [r]
x.position[1] = [c]'''
if self.validate(r,c) :
self.board[r][c]='X'
return True
return False
def set_o(self,r,c):
'''
o.position = (r,c)
[r,c] = 2,4
o.position[0] = [r]
o.position[1] = [c]
print(o.position)
'''
if self.validate(r,c) :
self.board[r][c]='O'
return True
return False
def clear(self):
self.board = [['-'] * 3, ['-'] * 3, ['-'] * 3]
pass
def __str__(self):
#return "Brd Seldf\n"
return '\n'.join([' '.join(row) for row in self.board])
#['_ * _', '_ * _', '_ * _']
# Representation
my_board = TikTacToeBoard()
print(my_board)
my_board.set_o(1,2)
print ("After 0 set at 1,2 \n")
print(my_board)
#my_board.clear()
print ("Try to set X on 1,2 & print \n")
my_board.set_x(1,2)
print(my_board)