python - 将 noughts and crosss 游戏转换为 4 x 4 网格
问题描述
我一直在我的子例程中遇到列表索引错误,该错误决定何时有赢家,我不知道该怎么做,但我并不热衷于找到对角线获胜选项,如果需要更多信息,请询问。这是我在 3 x 3 网格上的原始代码:
Board = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
def CheckXOr0HasWon(Board):
XOrOHasWon = False
for Column in range(1,4):
if (Board[Column][1] == Board[Column][2]) and (Board[Column][2] == Board[Column][3]) and
(Board[Column][2] != ' '):
XOrOHasWon = True
for Row in range(1,4):
if (Board[1][Row] == Board[2][Row]) and (Board[2][Row] == Board[3][Row]) and (Board[2][Row] != ' '):
XOrOHasWon = True
if (Board[1][1] == Board[2][2]) and (Board[2][2] == Board[3][3]) and (Board[2][2] != ' '):
XOrOHasWon = True
if (Board[3][1] == Board[2][2]) and (Board[2][2] == Board[1][3]) and (Board[2][2] != ' '):
XOrOHasWon = True
return XOrOHasWon
这是我为 4 x 4 网格所做的:
Board = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
def CheckXOr0HasWon(Board):
XOrOHasWon = False
for Column in range(1,5):
if (Board[Column][1] == Board[Column][2]) and (Board[Column][2] == Board[Column][3]) and
(Board[Column][3] == Board[Column][4]) and (Board[Column][2][3] != ' '):
XOrOHasWon = True
for Row in range(1,5):
if (Board[1][Row] == Board[2][Row]) and (Board[2][Row] == Board[3][Row]) and (Board[3][Row] == Board[4][Row]) and (Board[2][3][Row] != ' '):
XOrOHasWon = True
if (Board[1][1] == Board[2][2]) and (Board[2][2] == Board[3][3]) and (Board[2][2] != ' '):
XOrOHasWon = True
if (Board[3][1] == Board[2][2]) and (Board[2][2] == Board[1][3]) and (Board[2][2] != ' '):
XOrOHasWon = True
return XOrOHasWon
解决方案
推荐阅读
- java - 澄清我如何通过 String args 传递参数
- angular - 如何格式化选定的值和补丁?
- javascript - 使用 if else 条件制作 YouTube 网页并获取用户反馈
- python-3.x - 字典列表的理解
- stack - 运行时检查失败 #2 - 变量“maxlong”周围的堆栈已损坏
- c# - C# Blazor 这有效,但我收到错误
- c# - 在 ASP.NET Core MVC 中运行时动态绑定模型
- python - ValueError- tf.keras 中多个输出的从头训练
- c# - XmlDocument.Save() 与 XDocument.Save()
- transformer - XLM-RoBERTa 令牌 - id 关系