In my code, i'm trying to instantiate a simple battleship gameboard for two players. For each player, I am initializing two 5X5 boards and trying to add an "S" to one of the boards to represent a ship. I believe I am only adding the "S" to one of the boards but for some reason, both boards end up updating with the "S" - one should stay blank.
I don't know why this is happening - can anybody help explain?
class Gameboard:
def __init__(self, p1_boards = [[],[]], p2_boards = [[],[]], turn = 1):
self.p1_boards = p1_boards
self.p2_boards = p2_boards
self.turn = turn
i = 0
x = randint(0,4)
y = randint(0,4)
while i < 5:
rows = [" "," "," "," "," "]
self.p1_boards[0].append(rows)
self.p1_boards[1].append(rows)
self.p2_boards[0].append(rows)
self.p2_boards[1].append(rows)
i += 1
self.p1_boards[0][randint(0,4)][randint(0,4)] = SHIP
print(self.p1_boards[0])
print(self.p1_boards[1])
The two print statements at the end produce:
[[' ', ' ', ' ', 'S', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ']]
and
[[' ', ' ', ' ', 'S', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ']]