1

我试图找出一个 for 循环和 if 语句,它将检查 tic tac toe 游戏是否在 python 中获胜。我需要这个,因为我有一个 connect 4 游戏并且我打算使用相同的伪代码。

这是我到目前为止所拥有的:

col_size = 3
        row_size = 3
        ttt = list()
        for n in range(col_size):
            rows = input(msg[n]+ ":")
            ttt.append(rows)
        print(ttt)
        row_ttt= list()
        for i in range(row_size):
            one_row = list()
            for j in range(col_size):
                one_row+= ttt[i][j]
            row_ttt.append(one_row)
        diagonal_ttt= list()
        for j in range(col_size):
            one_diagonal= ""
            for i in range(row_size):
                one_diagonal+= ttt[i][j+1:2]
            diagonal_ttt.append(one_diagonal)
        print(row_ttt)
        valid_symbols = ['x','X','o','O','.']

            for j in range(col_size):
                for i in range(row_size):
                    if row_ttt[i][j]== row_ttt[i:i+1][j] == row_ttt[i:i+2][j]:
                        #print(row_ttt[i][j]) to check the element
                        #print(row_ttt[i:i+1][j]) to check element
                        #print(row_ttt[i:i+2][j]) to check element
                        valid = True
                print("valid board - " + ttt[i][j] + " is the winner")
                break

我在这里手动写了出来,但我想要一个更短的代码:

            if ttt[0][0] == ttt[1][1] == ttt[2][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][2] == ttt[1][1] == ttt[2][0]:
                print("valid board " + ttt[2][0] + " is the winner")
                break
            if ttt[0][0] == ttt[0][1] == ttt[0][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[1][0] == ttt[1][1] == ttt[1][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[2][0] == ttt[2][1] == ttt[2][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][0] == ttt[1][1] == ttt[2][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][0] == ttt[1][0] == ttt[2][0]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][1] == ttt[1][1] == ttt[2][1]:
                print("valid board " + ttt[0][1] + " is the winner")
                break
            if ttt[0][2] == ttt[1][2] == ttt[2][2]:
                print("valid board " + ttt[0][2] + " is the winner")
                break
4

2 回答 2

1
>>> import numpy
>>> game_board = [[0,0,0],[0,1,2],[1,2,1]]
>>> #let 1 be X's and 2 be O's .... 0 is empty
>>> def check_win(board,player):
...     b= numpy.array(board)
...     x= b == player
...     return numpy.hstack([x.all(0),x.all(1),numpy.diag(x).all(),numpy.diag(x[:,::-1]).all()]).any()
...
>>> print check_win(game_board,1) #check for X win
>>> print check_win(game_board,2) # check for O win
于 2013-10-15T20:18:47.477 回答
0

这是我为最近制作的井字游戏提出的解决方案。为您改编:

win = False
rowsBoard = ttt

colsBoard = [[board[i][j] for i in range(3)] for j in range(3)]
diagsBoard = [[board[i][i] for i in range(3)],[board[i][2-i] for i in range(3)]]
boards = [rowsBoard, colsBoard, diagsBoard]

for board in boards:
    for row in b:
        if row[0] == row[1] == row[2] != "":
            print("valid board "+ row[0] + " is the winner")
            win = True

if "" not in rowsBoard[0] and "" not in rowsBoard[1] and "" not in rowsBoard[2] and not win:
    print("game is a draw!")
于 2013-10-15T20:07:38.153 回答