Python Tic-tac-toe博弈逻辑问题
[已编辑] 为了简单起见,我将在这里包括整个文件:Python Tic-tac-toe博弈逻辑问题,python,python-3.x,Python,Python 3.x,[已编辑] 为了简单起见,我将在这里包括整个文件: choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] Is_Current_One = True won = False wrongEntryPlayer1 = False wrongEntryPlayer2 = False while not won: if not wrongEntryPlayer1 and not wrongEntryPlayer2: pri
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
while not won:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print('\n')
print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print('----------')
print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print('----------')
print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
if Is_Current_One:
if choices[choice - 1] == int:
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
else:
if choices[choice - 1] == int:
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
# code to toggle between True and False
Is_Current_One = not Is_Current_One
for pos_x in range(0, 3):
pos_y = pos_x * 3
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 3)]) and (
choices[pos_x] == choices[(pos_x + 6)]):
won = True
if ((choices[0] == choices[4] and choices[0] == choices[8])
or (choices[2] == choices[4] and choices[4] == choices[6])):
won = True
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
如果你真的读了代码,你会发现我正在做一个井字游戏。
我声明了两个布尔变量:ErrorEntryPlayer1
和ErrorEntryPlayer2
,这两个变量最初默认设置为False
,因为游戏刚刚开始。
在玩这个游戏时,玩家需要输入一个整数,这个整数在游戏板上显示,在那里放置一个X
或O
。
如果玩家没有输入整数,程序就会崩溃,我不希望这样。相反,我使用了try
语句,在他们没有输入整数时打印一条用户友好的消息。
发生这种情况时,我不希望Python再次打印游戏板,因此当玩家输入错误的值时,我将相应的ErrorEntryPlayer变量设置为True。这样,如果任何一个玩家输入了错误的值,我将完全跳过打印游戏板
然而,当我试着运行它时,在第一个回合输入了错误的值,然后输入了正确的值,游戏板仍然不会出现。即使轮到玩家2,游戏板仍然没有打印
当玩家输入正确的值(整数)时,游戏板仍然不会显示
此外,当玩家输入已被另一玩家占用的值时,程序应提示玩家再次输入。相反,它只是跳过了玩家的回合
我是python初学者,所以有些代码可能看起来很傻。有人能帮我解释一下如何解决这个问题吗?谢谢
多亏了Sanetro,我原来的问题得到了解决。现在又发生了另一个错误。详细问题发布在Sanetro回答的评论部分。我在这里包括我的详细编辑代码
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
while not won:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print('\n')
print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print('----------')
print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print('----------')
print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
if Is_Current_One:
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
else:
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
# code to toggle between True and False
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
for pos_x in range(0, 3):
pos_y = pos_x * 3
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 3)]) and (
choices[pos_x] == choices[(pos_x + 6)]):
won = True
if ((choices[0] == choices[4] and choices[0] == choices[8])
or (choices[2] == choices[4] and choices[4] == choices[6])):
won = True
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
[编辑2]
好的,Sanetro,这实际上是最后一次编辑。
我正在做一个退出命令。当播放机键入>退出时,程序退出。我本来以为这会很简单。你做了一个while循环,当玩家退出时打破循环,就这样!嗯,这并不简单(至少对我来说)。由于choice=input(…)在循环中,因此我不能将其置于循环之外(或在声明choice变量之前),这是不好的。我在顶部创建了一个新变量:exitTrigger,并在while循环中添加了新行。现在它不起作用了。当播放器类型退出时,它只是说请只输入有效字段。这可能是因为except语句仍然有效,我想添加一个条件来触发except语句,并且当exitTrigger为True时,我只想使用except语句。我可以这样做吗?谢谢
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
exitTrigger = False
while not won and not exitTrigger:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print("Type exit to quit game")
print('\n')
print('|' + choices[0] + ' |' + choices[1] + ' |' + choices[2] + ' |' + choices[3] + ' |')
print('-------------')
print('|' + choices[4] + ' |' + choices[5] + ' |' + choices[6] + ' |' + choices[7] + ' |')
print('-------------')
print('|' + choices[8] + ' |' + choices[9] + '|' + choices[10] + '|' + choices[11] + '|')
print('-------------')
print('|' + choices[12] + '|' + choices[13] + '|' + choices[14] + '|' + choices[15] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
if choice == "exit":
exitTrigger = True
else:
wrongEntryPlayer1 = False
except ValueError:
if not exitTrigger:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
except IndexError:
if not exitTrigger:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
if choice == "exit":
exitTrigger = True
else:
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
except IndexError:
if not exitTrigger:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
if not exitTrigger:
if Is_Current_One:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
except: # <============ HERE
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
else:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
except: # <============ HERE
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
# code to toggle between True and False
if not exitTrigger:
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
for pos_x in range(0, 4):
pos_y = pos_x * 4
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]) and (
choices[pos_y] == choices[(pos_y + 3)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 4)]) and (
choices[pos_x] == choices[(pos_x + 8)]) and (
choices[pos_x] == choices[(pos_x + 12)]):
won = True
if ((choices[0] == choices[5] and choices[0] == choices[10] and choices[0] == choices[15])
or (choices[3] == choices[6] and choices[3] == choices[9] and choices[3] == choices[12])):
won = True
if won:
print('\n')
print('|' + choices[0] + ' |' + choices[1] + ' |' + choices[2] + ' |' + choices[3] + ' |')
print('-------------')
print('|' + choices[4] + ' |' + choices[5] + ' |' + choices[6] + ' |' + choices[7] + ' |')
print('-------------')
print('|' + choices[8] + ' |' + choices[9] + '|' + choices[10] + '|' + choices[11] + '|')
print('-------------')
print('|' + choices[12] + '|' + choices[13] + '|' + choices[14] + '|' + choices[15] + '|')
# above code is to print board layouts
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
if exitTrigger:
print("Game quited")
choices=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']
_Current_One=True
韩元=假
ErrorEntryPlayer1=错误
ErrorEntryPlayer2=错误
exitTrigger=False
当未赢得且未退出时,装配工:
如果不是错误的玩家1和错误的玩家2:
打印(“键入退出退出游戏”)
打印(“\n”)
打印(“|”+选项[0]+“|”+选项[1]+“|”+选项[2]+“|”+选项[3]+“|”)
打印('--------------')
打印(“|”+选项[4]+“|”+选项[5]+“|”+选项[6]+“|”+选项[7]+“|”)
打印('--------------')
打印(“|”+选项[8]+“|”+选项[9]+“|”+选项[10]+“|”+选项[11]+“|”)
打印('--------------')
打印(“|”+选项[12]+“|”+选项[13]+“|”+选项[14]+“|”+选项[15]+”)
#以上代码用于打印电路板布局
如果是当前的,则:
打印(“轮到玩家1。\n选择一个数字,在那里放一个X。”)
尝试:
choice=int(输入(“>”).strip()
如果选项==“退出”:
exitTrigger=True
其他:
ErrorEntryPlayer1=错误
除值错误外:
如果未退出装配工:
打印(“请仅输入电路板(1-16)中的有效字段”)
ErrorEntryPlayer1=True
持续
除索引器外:
如果未退出装配工:
打印(“请仅输入电路板(1-16)中的有效字段”)
ErrorEntryPlayer1=True
持续
其他:
打印(“轮到玩家2了。\n选择一个数字,在那里放一个O。”)
尝试:
choice=int(输入(“>”).strip()
如果选项==“退出”:
exitTrigger=True
其他:
ErrorEntryPlayer2=错误
除值错误外:
打印(“请仅输入电路板(1-16)中的有效字段”)
ErrorEntryPlayer2=正确
持续
除索引器外:
如果未退出装配工:
打印(“请仅输入电路板(1-16)中的有效字段”)
ErrorEntryPlayer2=正确
持续
如果未退出装配工:
如果是当前的,则:
试试看:#我想不用
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
你应使用:
if wrongEntryPlayer1 or wrongEntryPlayer2:
这将解决印制电路板的部分问题。我想不用
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
你应使用:
if wrongEntryPlayer1 or wrongEntryPlayer2:
这将修复印刷电路板的部分。您的代码实际上有两个问题-首先,您请求帮助的问题是由于切换标志是当前的\u One
而引起的,即使在播放器输入“错误”输入时也是如此。换句话说,当玩家一输入“asdf”时,我们希望他们有机会再次输入“良好”输入,而不是立即切换到玩家二。将if
块放在该行上,该行显示if not ErrorEntryPlayer1和not ErrorEntryPlayer2:
第二个更大的问题是你的类型检查玩家的输入是否为int
无效。首先,choices[choice-1]==int
将数组中的值与类型进行比较,
# code to toggle between True and False
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
if Is_Current_One:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
except: # <============ HERE
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
except: # <============ HERE
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue