gpt4 book ai didi

python - 在Python中的零和十字游戏中将标记放置在棋盘上时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:50 24 4
gpt4 key购买 nike

我正在为 n x n 游戏创建一个零和十字游戏(用户将输入棋盘的大小)。

到目前为止,在用户输入他们想要的板尺寸后,我已经能够创建在列表的每个元素中都有下划线的板。

但是,我的问题是,当玩家通过输入行号和列号来放下标记时,它会输入整列的标记。

例如,板上放置标记的行是:board[int(row)-1][int(column)-1] = symbol。如果用户输入 1, 1,那么它应该在图板的最左上角、第 1 行第 1 列中放置一个标记。但它会输入整个列的标记。我很困惑它为什么这样做。感谢帮助!

(#Checking if someone won 下的行与此问题无关。要查看的主要行位于 #Putting the mark on 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

最佳答案

更改:

board.append(empty_list)

至:

board.append([])

通过一遍又一遍地使用相同的empty_list,您将创建对同一列表的许多引用。当您稍后更改列表时,可以通过所有引用看到它。

这里有更深入的解释:https://nedbatchelder.com/blog/201308/names_and_values_making_a_game_board.html

关于python - 在Python中的零和十字游戏中将标记放置在棋盘上时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52798745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com