计算特定的井字游戏

时间:2019-11-02 21:00:47

标签: python counting tic-tac-toe

在井字游戏中有9!= 362880个可能的棋盘,而忽略了一名早期获胜的玩家。因此,这些362880板中应该有8 * 3!* 6 !! =第5步移动时,有起始玩家的34560个游戏在一行中有3个石头。但是,不幸的是,下面的代码仅计数21600个板。

有人看到错误了吗?

all_boards = list()
winning_boards = [[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9],
                  [1, 4, 7],
                  [2, 5, 8],
                  [3, 6, 9],
                  [1, 5, 9],
                  [3, 5, 7]]


def build_all_boards(cur, moves_left):
    if moves_left == 0:
        all_boards.append(cur)
        return
    for i in range(1, 10):
        if i in cur:
            continue
        else:
            temp = cur.copy()
            temp.append(i)
            build_all_boards(temp, moves_left - 1)


def check_win(moves):
    for winning_board in winning_boards:
        s1 = set(moves)
        s2 = set(winning_board)
        if s1 == s2:
            return True
    return False


def count(c):
    build_all_boards([], c)
    won_boards = list()
    for board in all_boards:
        temp = board[::2][:3]
        if check_win(temp):
            won_boards.append(temp)
    print(len(won_boards))


count(9)

编辑:固定代码。

0 个答案:

没有答案