class Board():
"""Board class for Tic Tac Toe game. Board can be 3x3, 4x4, or 5x5."""
def __init__(self, size):
# offset by 1 to make indexing easier
self.board = [None]
self.board_size = size
def create_board(self):
"""Create an empty board with dimensions of board_size argument."""
row = [None] + ([' '] * self.board_size)
for i in range(self.board_size):
self.board.append(row)
def draw_mark(self, choice, player_mark):
"""Draw an x or o on the space chosen by the player if the space is
unoccupied.
Choice is a list containing the coordinates of the space to place a
mark [row, column].
Return True if move was valid, return False if not.
"""
row = choice[0]
column = choice[1]
if self.board[row][column] == ' ':
self.board[row][column] = player_mark
return True
else:
return False
oBoard = Board(3)
oBoard.create_board()
oBoard.draw_mark([1, 1], 'x')
print(oBoard.board)
When I instantiate an board object and call the draw_mark
method on that object I expect this as the output:
[None, [None, 'x', ' ', ' '], [None, ' ', ' ', ' '], [None, ' ', ' ', ' ']]
However the code produces this output:
[None, ['x', ' ', ' ', ' '], ['x', ' ', ' ', ' '], ['x', ' ', ' ', ' ']]
I tried creating the grid as a global variable in the python shell and attempting the assignment directly and that produced the desired result.
My question is what is it about my implementation of classes that is causing these extra assignments to happen?