我正在为n x n游戏创建一个零交叉游戏(用户将输入棋盘的大小)。
到目前为止,我已经能够创建一个在用户输入他们想要的板子尺寸后在列表的每个元素中都带有下划线的板子。
但是,我的问题是,当玩家放下一个标记时,他们要通过输入行号和列号来实现,它输入整列的标记。 >
例如,标记在板上放置的行是以下行:board[int(row)-1][int(column)-1] = symbol
。如果用户输入1、1,则应在板的左上角第1行和第1列中放置一个标记。但是,它将在整列中输入标记。我很困惑为什么要这样做。帮助表示赞赏!
(#Checking if someone won
下的行与该问题无关。要查看的主行在#Putting the marker on the board
和#Creating the board
下。)
#todo:
#check if marker already is there
board = []
empty_list = []
board_length = input("How many rows would you like for the board? ")
while board_length.isdigit() == False:
board_length = input("That is not a number. How many rows would you like for the board? ")
#Creating the Board
for x in range(int(board_length)):
board.append(empty_list)
board[x].append('_')
#Printing the board
for i in range(len(board)):
print(board[i])
print(board)
print ("Player 1 is X (Crosses), Player 2 is O (Naughts). Let's begin!")
player_turn = True
symbol = "X"
symbols = ["X","O"]
player_name = "1"
#Setting correct answers for coordinates
correct_answers = []
for i in range(len(board)):
correct_answers.append(str(i+1))
#Game
for i in range(len(board)**2):
global player_turn
if player_turn == True:
symbol = "X"
player_name = "1"
else:
symbol = "O"
player_name = "2"
#Making sure the coordinates entered are correct
row = input("Player " + player_name + ": Please enter the row for your marker: ")
while row not in correct_answers:
row = input("Invalid answer. Please enter the row for your marker: ")
column = input("Player " + player_name + ": Please enter the column for your marker: ")
while column not in correct_answers:
column = input("Invalid answer. Please enter the column for your marker: ")
#Putting the marker on the board
board[int(row)-1][int(column)-1] = symbol
#find and remove symbol from where it isn't supposed to be?
for n in range(len(board)):
print(n)
print(board[n])
player_turn = not player_turn
#Checking if someone won
game_over = False
for i in range(len(board)):
the_set = set(board[i])
if len(the_set) == 1 and '_' not in the_set:
print(str(symbols.index(board[i][0])) + " wins by row " + str(i+1) + "!")
break
board_list = []
for x in range(len(board)):
board_list.append(board[x][x])
if x == len(board) - 1 and len(set(board_list)) == 1 and '_' not in board_list:
print(str(symbols.index(board_list[0])) + " wins by diagonal (top left to right)!")
game_over = True
break
if game_over == True:
break
board_list = []
for x in range(len(board)):
board_list.append(board[x][len(board)-1-x])
if x == len(board) - 1 and len(set(board_list)) == 1 and '_' not in board_list:
print(str(symbols.index(board_list[0])) + " wins by diagonal (top right to left)!")
game_over = True
break
if game_over == True:
break
board_list = []
for x in range(len(board)):
board_list.append(board[x][i])
if x == len(board) - 1 and len(set(board_list)) == 1 and '_' not in board_list:
print(str(symbols[symbols.index(board_list[0])]) + " wins by column " + str(i+1) + "!")
game_over = True
break
if game_over == True:
break
if game_over == True:
break
if game_over == True:
break
答案 0 :(得分:0)
更改:
board.append(empty_list)
收件人:
board.append([])
通过一遍又一遍地使用相同的empty_list,您将创建许多对同一列表的引用。以后更改列表时,通过所有引用都可以看到它。
这在这里有更深入的解释:https://nedbatchelder.com/blog/201308/names_and_values_making_a_game_board.html