nested-loops - 如何使用嵌套列表在井字游戏中实现 Tie 功能
问题描述
在我的游戏中,我试图通过在我的获胜确定器下使用 else 添加一个平局功能,但我遇到了麻烦。有人可以帮我在 win 确定器下的代码中添加一个 tie 函数吗?我试图在获胜判定器下添加其他内容,但平局最终会打印出游戏随机以平局结束的信息。一些帮助将不胜感激,谢谢!
player1 = str(input("What is player X's name?"))
print()
player2 = str(input("What is player O's name?"))
turn = 1
location = [
[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' '],
]
while turn <= 9:
if turn % 2 == 1:
print()
print("It is currently " +player1+ "'s turn")
row = int(input("What row would you like to mark?"))-1
print()
col = int(input(" What column would you like to mark?"))-1
print()
while "O" in location[row][col]:
print(player1+" that spot has already been taken")
print("Please pick again")
row = int(input("What row would you like to mark?"))-1
print()
col = int(input(" What column would you like to mark?"))-1
print()
location[row][col] = "X"
print(location[0][0] + '|' + location[0][1] + '|' + location[0][2])
print("-----")
print(location[1][0] + '|' + location[1][1] + '|' + location[1][2])
print("-----")
print(location[2][0] + '|' + location[2][1] + '|' + location[2][2])
turn = turn + 1
if ((location[0][0] == 'X' and location[0][1] == 'X' and location[0][2]
== 'X') or
(location[1][0] == 'X' and location[1][1] == 'X' and location[1][2] ==
'X') or
(location[2][0] == 'X' and location[2][1] == 'X' and location[2][2] ==
'X') or
(location[0][0] == 'X' and location[1][0] == 'X' and location[2][0] ==
'X') or
(location[0][1] == 'X' and location[1][1] == 'X' and location[2][1] ==
'X') or
(location[0][2] == 'X' and location[1][2] == 'X' and location[2][2] ==
'X') or
(location[0][0] == 'X' and location[1][1] == 'X' and location[2][2] ==
'X') or
(location[0][2] == 'X' and location[1][1] == 'X' and location[2][0] ==
'X')):
print (player1+" won the game!")
turn = 10
else:
print("The game ended in a tie!")
else:
print("It is currently " +player2+ "'s turn")
row = int(input("What row would you like to mark?"))-1
print()
col = int(input(" What column would you like to mark?"))-1
print()
while "X" in location[row][col]:
print(player2+" that spot has already been taken")
print("Please pick again")
row = int(input("What row would you like to mark?"))-1
print()
col = int(input(" What column would you like to mark?"))-1
print()
location[row][col] = "O"
print(location[0][0] + '|' + location[0][1] + '|' + location[0][2])
print("-----")
print(location[1][0] + '|' + location[1][1] + '|' + location[1][2])
print("-----")
print(location[2][0] + '|' + location[2][1] + '|' + location[2][2])
turn = turn + 1
if ((location[0][0] == 'O' and location[0][1] == 'O' and location[0][2]
== 'O') or
(location[1][0] == 'O' and location[1][1] == 'O' and location[1][2] ==
'O') or
(location[2][0] == 'O' and location[2][1] == 'O' and location[2][2] ==
'O') or
(location[0][0] == 'O' and location[1][0] == 'O' and location[2][0] ==
'O') or
(location[0][1] == 'O' and location[1][1] == 'O' and location[2][1] ==
'O') or
(location[0][2] == 'O' and location[1][2] == 'O' and location[2][2] ==
'O') or
(location[0][0] == 'O' and location[1][1] == 'O' and location[2][2] ==
'O') or
(location[0][2] == 'O' and location[1][1] == 'O' and location[2][0] ==
'O')):
print (player2+" won the game!")
turn = 10
else:
print("The game ended in a tie!")
解决方案
你的问题是,如果轮到它的玩家赢得了游戏,代码会在每一回合后检查,如果他没有赢(还)它会打印你的“else”,意思是The game ended in a tie!
您的解决方案是向您添加另一个if:
else-es 来检查是否所有 9 个字段都已填写,X
或者O
如果是这种情况,您必须打印您的The game ended in a tie!
推荐阅读
- javascript - 当我向另一个选项卡发送打印请求时 Chrome 崩溃
- rest - 通过 Pentaho 数据集成连接到 QuickBase REST API
- javascript - 以编程方式打开对话框以共享到 Twitter
- c - 使用 CubeMx 通过 USART 传输多个字符串
- java - JSP 页面如何处理 Spring 控制器中未定义的变量?
- c# - C# 如何调用基类 == 运算符,而不显式类型转换?
- python - 如何让pyplot将多行放在一个图中
- html - `:hover::before` 的目的是什么?
- javascript - jQuery Ajax 不会 POST 到 php
- function - ncelab 中的错误:F*MISLUN:缺少顶层模块、设计单元名称