如何使用numpy缩短井字游戏检查

slmsl1lt  于 2023-05-17  发布在  其他
关注(0)|答案(3)|浏览(90)

如何使用numpy来缩短我的代码,我检查你是否赢了
我需要找到一种方法来使用numpy来适应我的代码,因为它很长很难。我检查diag,horizontle和row,但我需要找到一种方法来缩短它与numpy

def checkHorizontle(board):
    global winner
    if board[0] == board[1] == board[2] and board[1] != "-":
        winner = board[0]
        return True
    elif board[3] == board[4] == board[5] and board[3] != "-":
        winner = board[3]
        return True
    elif board[6] == board[7] == board[8] and board[6] != "-":
        winner = board[6]
        return True

    
def checkRow(board):
    global winner
    if board[0] == board[3] == board[6] and board[0] != "-":
        winner = board[0]
        return True
    elif board[1] == board[4] == board[7] and board[1] != "-":
        winner = board[1]
        return True
    elif board[2] == board[5] == board[8] and board[2] != "-":
        winner = board[2]
        return True

def checkDiag(Board):
    global winner
    if board[0] == board[4] == board[8] and board[0] != "-":
        winner = board[0]
        return True
    elif board[2] == board[4] == board[6] and board[2] != "-":
        winner = board[2]
        return True

当我试图找到一个解决办法时,我的头脑就决定放弃。我知道你可以用幻方来找到它和numpy乘法函数,但我不知道如何

xn1cxnb4

xn1cxnb41#

不需要numpy,你可以使用一个简单的循环:

WINNING_TRIPLETS = [ (0, 1, 2), (3, 4, 5), ...] # add all of them

for square1, square2, square3 in WINNING_TRIPLETS:
    if ...: # same logic as in your code, but only once with square1, square2, square3
0md85ypi

0md85ypi2#

假设有一个这样的电路板,并想检查X是否获胜:

board = np.array([['X', 'O', np.nan],
                  ['O', 'X', np.nan],
                  ['X', 'X', 'X']])

您可以对行、列和对角线使用4个测试:

# check if any row is won
(board == 'X').all(axis=1).any()

# check if any column is won
(board == 'X').all(axis=0).any()

# check diagonals
diag1 = np.diagonal(board)
diag2 = np.diagonal(np.fliplr(board))
(diag1 == 'X').all() or (diag2 == 'X').all()

在测试中使用:

if ((board == 'X').all(axis=1).any() 
   or (board == 'X').all(axis=0).any()
   or (np.diagonal(board) == 'X').all()
   or (np.diagonal(np.fliplr(board)) == 'X').all()
   ):
    print('X won')

如果您想查看是否有玩家获胜:

# check if any row is won
(board[0] == board[1:]).all(0).any()

# check if any column is won
(board[:, [0]] == board[:, 1:]).all(1).any()

# check diagonals
diag1 = np.diagonal(board)
diag2 = np.diagonal(np.fliplr(board))
(diag1[0] == diag1[1:]).all() or (diag2[0] == diag2[1:]).all()
llew8vvj

llew8vvj3#

它很长很硬。。。。我得想办法用numpy缩短它
如果你的目标是让它变短,你不一定需要numpy。另一个想法是将棋盘表示为九个字符的字符串(每个字符是“X”,“O”或“.”),并使用正则表达式来测试是否获胜。
该正则表达式可以是:

(\w)(\1\1(...)*$|..\1..\1|...\1...\1|.\1.\1..$)

示例实施方式:

import re

class TicTacToe:
    def __init__(self):
        self.board = "........."

    def play(self, i):
        self.board = self.board[:i] + self.player() + self.board[i+1:]

    def player(self):
        return "OX"[self.board.count(".") % 2]

    def isdraw(self):
        return "." not in self.board
    
    def iswon(self):
        return bool(re.search(r"(\w)(\1\1(...)*$|..\1..\1|...\1...\1|.\1.\1..$)", self.board))
    
    def __repr__(self):
        return "\n".join(" ".join(self.board[i:i+3]) for i in range(0, 9, 3))

# demo
game = TicTacToe()
for move in 0, 4, 8, 2, 6, 3, 7:
    game.play(move)
print(game)
print("draw?", game.isdraw())
print("win?", game.iswon())

相关问题