0

I am using a function to generate a tic-tac-toe board based on user input:

def write_board(size):
    h=[size*[0]]
    board=h*size
    return board

This function is utilized in another function to automatically generates moves, i.e it's saved to a variable which is called upon:

def game(scen,size):
    move=1
    board=write_board(size)
    win=False
    res=None
    while win==False:
        if move%2==1:
            coordinates=[(x, y) for x, l in enumerate(board) for y, i in enumerate(l) if i == 0]
            if coordinates==[]:
                print("Over")
                return 0
                break
            x,y=random.choice(coordinates)
            board[x][y]=1

However, multiple coordinates are ticked for one move. For instance, if the move is (1,4), then the result is:

[[0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]

Instead of just:

[[0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

However, if the board is hardcoded for the board variable:

    board=[[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0]]

Then result is correct, i.e for coordinate(1,4):

[0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Hope this explanation is clear enough. Thanks!

user2629628
  • 161
  • 1
  • 3
  • 11

0 Answers0