This is my first post! I am reasonably new to programming and I am trying to write a text based connect4 game.
I wanted to rewrite my function for generating the playing board (before it was written manually into the code). This is so that the size of the board can be changed from the standard 7 x 6, if a user wanted.
I had a go myself and although I was able to generate a board, which looks right when printed:
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
When I try and reassign one of the elements the whole column changes.
Here is my code:
HEIGHT_OF_GAME_BOARD = 6
WIDTH_OF_GAME_BOARD = 7
def generate_game_board():
spaces_in_a_row = []
board = []
for i in range(WIDTH_OF_GAME_BOARD):
spaces_in_a_row.append("O")
for j in range(HEIGHT_OF_GAME_BOARD):
board.append(elements_in_row)
return board
board = generate_game_board()
board[0][1] = "R"
for x in board:
print (x)
Outputs:
['O', 'R', 'O', 'O', 'O', 'O', 'O']
['O', 'R', 'O', 'O', 'O', 'O', 'O']
['O', 'R', 'O', 'O', 'O', 'O', 'O']
['O', 'R', 'O', 'O', 'O', 'O', 'O']
['O', 'R', 'O', 'O', 'O', 'O', 'O']
['O', 'R', 'O', 'O', 'O', 'O', 'O']
Is anyone able to tell me what is going wrong here?
I found another more efficient way to generate the board here on stack overflow (don't understand it fully yet, but it works!), which I am going to use. But it would still be interesting to know exactly what is going on with my original code?
Different way of generating board which works when reassigning an element:
HEIGHT_OF_GAME_BOARD = 6
WIDTH_OF_GAME_BOARD = 7
def generate_game_board():
board = [["O" for _ in range(WIDTH_OF_GAME_BOARD)]
for _ in range(HEIGHT_OF_GAME_BOARD)]
return board
board = generate_game_board()
board[0][1] = "R"
for x in board:
print (x)
This outputs:
['O', 'R', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O', 'O', 'O']
Thank you in advance!