Search code examples
pythonmatrixindexingtic-tac-toe

After changing a list value and running the function again, the value resets to what it originally was


I'm making a Tic Tac Toe game for a school project and made a function that takes in two parameters (row and col) and makes a Position object out of them. A Position object has an attribute index which makes a list out of its self.row and self.col values. The function is a method for a class Player.

Code snippet 1:

def getTurnCoordinates(self):
        row = int(input('In which row would you like to place the symbol ' + self.symbol + '?: '))
        col = int(input('In which column would you like to place the symbol ' + self.symbol + '?: '))
        pos = Position(row, col, self.symbol)
        if pos.isTaken():
            print("Position taken. Please choose another.")
            self.getTurnCoordinates()
        else:
            self.board.setPosition(pos.index, self.symbol)
            return self.board.getBoard()

This is the method for getting the parameters. Its purpose is to get int values which will later on be used to change the value of a specific index in a matrix Board.

Code snippet 2:

class Board(object):
    def __init__(self):
        ROWS = COLS = 3
        self.board = [[Position(i%ROWS, i%COLS, ' ').symbol for i in range(COLS)] for i in range(ROWS)]
        self.display = f"""
 Tic  Tac  Toe
{self.getBoard()}
  0    1    2"""

    def getBoard(self):
        return '\n'.join(map(str, self.board))
        
    def setPosition(self, position, sym):
        self.board[position[0]][position[1]] = sym

    def getPosition(self, position: list):
        return self.board[position[0]][position[1]]

This second code snippet is all of the Board class methods used in the former function. When I run my main.py file, I get this output.

main.py:

from classes.board import Board
from classes.player import Player

b = Board()

print(b.display)

p1 = Player('X')
p2 = Player('O')
players = Player.playerList

for ply in players:
    print(ply.getTurnCoordinates())

Output:

Tic  Tac  Toe
[' ', ' ', ' ']
[' ', ' ', ' ']
[' ', ' ', ' ']
  0    1    2
In which row would you like to place the symbol X?: 0
In which column would you like to place the symbol X?: 0
['X', ' ', ' ']
[' ', ' ', ' ']
[' ', ' ', ' ']
In which row would you like to place the symbol O?: 0
In which column would you like to place the symbol O?: 1
[' ', 'O', ' ']
[' ', ' ', ' ']
[' ', ' ', ' ']
>>>

Each time the function is run, the original Board object resets all of its indices to ' '. How can I prevent this from happening?


Solution

  • Each Player object has a separate self.board. The code is not replacing the board with its previous values; it is displaying a different board which is still in its initial configuration.

    You'll want to rethink your classes and their relationships. I would design this as one Board class with two Player instances, but you could certainly also make each Player independent and have its __init__ method receive a reference to a shared Board.