0
def boardprint():
    array_board = board
    print(
" _ _ _\n"+
"|"+(array_board[0][0])+"|"+(array_board[0][1])+"|"+(array_board[0][2])+"|\n"+
"|_|_|_|\n"+
"|"+(array_board[1][0])+"|"+(array_board[1][1])+"|"+(array_board[1][2])+"|\n"+
"|_|_|_|\n"+
"|"+(array_board[2][0])+"|"+(array_board[2][1])+"|"+(array_board[2][2])+"|\n"+
"|_|_|_|\n"
        )




board=[[" "]*3]*3
move=0


boardprint()
board_full=False
won=False
while (board_full!=True and won!=True):
    move+=1
    move_success=False
    while (move_success==False):
        row_position=int(input("In which row would you like to place a cross?"))
        col_position=int(input("In which column would you like to place a cross?"))
        if (board[col_position][row_position]==" "):
            (board[col_position][row_position])="x"
            move_success=True
        else:
            print("Try again, that position is full")
        boardprint()
    if (move>=9):
        board_full=True
    move+=1
    move_success=False
    while (move_success == False):
        row_position=int(input("In which row would you like to place a nought?"))
        col_position=int(input("In which column would you like to place a nought?"))
        if (board[col_position][row_position]==" "):
            board[col_position][row_position]="o"
            move_success=True
        else:
             print("Try again, that position is full")
        boardprint()

This is supposed to be a tic-tac-toe game. However, when the user enters the position they want in the board it fills the entire column with noughts/crosses, ignoring the rows.I know a 1D list would be easier but my Comp Sci teacher wants me to use a 2D list and he doesn't know why it does this.

10FG
  • 3
  • 2
  • 3
    Google 'deep copy and shallow copy python'. [This tutorial](http://www.python-course.eu/deep_copy.php) should be helpful. – Dan Feb 04 '16 at 15:51

1 Answers1

0

Replacing

board = [[" "]*3]*3

with

from copy import deepcopy
board = [deepcopy([' ' for x in range(3)]) for x in range(3)]

Should fix your issue, however your original issue stemmed from some deep copy vs shallow copy issues and you should really red Dan's link as it addresses your exact problem.

Although you may consider using the value the user inputs - 1 for the location the tic tac two mark that they intend to make. For instance if i enter 1, 1 i probably want to place my mark here:

 _ _ _
|X| | |
|_|_|_|
| | | |
|_|_|_|
| | | |
|_|_|_|

and not necessarily here:

 _ _ _
| | | |
|_|_|_|
| |x| |
|_|_|_|
| | | |
|_|_|_|

that's just me though, your way works just fine.

Mike Shlanta
  • 168
  • 1
  • 8