草拟井字游戏

时间:2020-06-01 13:59:55

标签: python python-3.x numpy

import numpy as np
player1 = True


def create_board():
    board = np.zeros((3,3), dtype=int)
    return board


def place(board , player1 , position ):
    if player1 == True:
        player1 = False
        if board[position] == 0:
            board = position
        elif board[position] != 0:
            print("False , place taken 1")
    else:
        if player1 == False:
            if board[position] == 0:
                board = position
            elif board[position] != [0, 0]:
                print("False , place taken 2 ")
    return board

函数调用:

place(board=create_board() , player1=1 , position=(0,0))

这是我的代码,这段代码中发生了很多事情,但是有一部分我无法正确理解。在粗体部分中,我正在尝试替换

([0,0,0],
 [0,0,0], 
 [0,0,0])

([1,0,0],
 [0,0,0],
 [0,0,0])

我可以知道,我该怎么做...谢谢!

3 个答案:

答案 0 :(得分:1)

您在if语句中错误地将output = q(input_t_t) output = torch.min(output[:,:nq], output[:,nq:]).clamp(min=0) nbs = output.sum(dim=-1) 变量更改为board

position

您应该在索引if board[position] == 0: board = position 的{​​{1}}内更改值

board

我也清除了一些代码

position

答案 1 :(得分:1)

您正在使用board = position将董事会分配给您的职位信息,我认为您的意思是board[position] == 1

def place(board , player, position ):
    if board[position] == 0: #if the position is unclaimed
        board[position] = player #claim it for the player
    else:
        print(f"place was already taken by player {board[position]}")
    return board

#call using the player number
place(board=create_board() , player=1 , position=(0,0))

此功能将为您提供所需的内容。

答案 2 :(得分:0)

当您尝试在board = position中更新板子时,实际上是在使用一对坐标来更新整个板子。

您真正想要的是查明必须更新以存储占用单元格的二维数组中的确切位置。您可以归档

来更改该行
board[position] = 1 # occupied cell

但是,正如哈马德在另一个答案中所说,更改您的

0 for unoccupied
1 for occupied

并且必须依靠知道当前正在转弯的人来

0 for unoccupied
1 for occupied by player one
2 for occupied by player two

所以您总是知道谁占据了那个牢房。 希望这会有所帮助!