首页 > 解决方案 > 将 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

标签: python

解决方案


推荐阅读