AI指令-将来的电路板和计算机移动

问题描述 投票:0回答:1

[我被要求将玩家对战tictactoe改进为AI tictactoe,其中玩家对战计算机:为此,我需要编写两个函数:一个用来获取当前玩家的棋盘和符号并返回所有可能的未来棋盘的列表-每个将来的棋盘都是一个包含两个元素的列表:一个是放置符号的位置,另一个是后面的棋盘放置符号-旋转一圈后的面板(如下面的代码所示,我正在使用嵌套的列表面板(在其中我获得了帮助,链接:tic tac toe does not change the player and does not win even if created condition

我需要的第二个功能是计算机执行的功能-它使用第一个功能并以下列方式之一选择最佳移动:

  1. 随机抽动(仅在开始时,如果计算机先行,然后播放)

OR

  1. 如果计算机可以在下一轮中获胜,他会选择并播放此选项

AND

  1. 如果玩家可以在下一回合中获胜,则计算机应“阻止”他。

我所拥有的是球员对球员的身分

代码:

def get_move(whoseturn, board):
  rowloc=int(input(f'{whoseturn},insert the deserved row to place your symbol: '))
  coloc=int(input(f'{whoseturn} insert the deserved column to place your symbol: '))
  while True:
    if not (0 <= rowloc < 3 and 0 <= coloc < 3):
      print('row and column must be 0, 1, or 2')
      rowloc = int(input(f'{whoseturn},insert the deserved row to place your symbol: '))
      coloc = int(input(f'{whoseturn} insert the deserved column to place your symbol: '))
    elif  board[rowloc][coloc] !='e':
      print("The deserved place is taken, choose again ")
      rowloc = int(input(f'{whoseturn},insert the deserved row to place your symbol: '))
      coloc = int(input(f'{whoseturn} insert the deserved column to place your symbol: '))
    else:
      board[rowloc][coloc] = whoseturn
      break

  return rowloc, coloc

def display_board(board):
  print('\n'.join([' '.join(board[i]) for i in range(3)]))

def win(board, whoseturn, x, y):
  if board[0][y] == board[1][y] == board [2][y] == whoseturn:
    return True
  if board[x][0] == board[x][1] == board [x][2] == whoseturn:
    return True
  if x == y and board[0][0] == board[1][1] == board [2][2] == whoseturn:
      return True
  if x + y == 2 and board[0][2] == board[1][1] == board [2][0] == whoseturn:
    return True

  return False

def isfull(board):
    for i in range(0,3):
        for j in range(0,3):
            if board[i][j]=='e':
                return False
    return True

def main():
    board = [['e','e','e']
            ,['e','e','e']
            ,['e','e','e']]
    print("Welcome to the great tic tac toe game!")

    player1=input("Player 1, select your symbol (X/O): ")
    if player1 =='O':
        print('X is player 2s symbol')
        player2 = 'X'
    else:
        print('O is player 2s symbol')
        player2 = 'O'
    print("Player 1 will start")


    whoseturn=player1
    while True:
      display_board(board)

      rowloc, coloc = get_move(whoseturn, board)
      if win(board,whoseturn, rowloc, coloc):
        print(f'{whoseturn} wins!')
        display_board(board)
        break

      if isfull(board):
        print('Tied')
        break
      if whoseturn=='O':
          whoseturn='X'
      else:
          whoseturn='O'


if __name__ == '__main__':
   main()

以及未来董事会职能的开始

代码:

def futuremove(board,whoseturn):
    newboard=copy.deepcopy(board)
    place = []
    copyboard = []
    arrangement=[]
    final=[]
    for i in range(3):
        for j in range(3):
            if newboard[i][j]=='e':
                newboard[i][j]=whoseturn
                if win(newboard,whoseturn,i,j)==True:
                    loctup=[i,j]
                    place.append(loctup)
                    copyboard.append(newboard)
                    arrangement.append(place)
                    arrangement.append(copyboard)
                    final.append(arrangement)
                    print(final)
                else:
                    break

请帮助我获得一个可以正常工作的人与计算机的拟人游戏!任何帮助都会非常有用!

python artificial-intelligence tic-tac-toe
1个回答
0
投票

您可以使用许多不同的方法,您可能会采用的一种相当简单的方法是利用Minimax Algorithm

在一个简单的例子中,您的程序仅向前看了一圈,在进行移动之后,您的AI会为它可能进行的每个可能动作生成一个棋盘,并从这些动作中,玩家可以进行的每个可能的反向动作制造。

现在,您要为AI的每个可能动作分配分数,如何定义评分算法取决于您自己,但是它应该表示特定游戏状态对您AI的优劣。

每个AI潜在动作的得分应等于所有玩家反击动作的最差得分,因为我们要假设玩家将按照他们的最佳利益行事。

这样,您将能够确定AI的哪些潜在举动使其有最大的机会从当前状态赢得比赛。我强烈建议阅读附件中的文章,以获取实现细节和更深入的了解。

© www.soinside.com 2019 - 2024. All rights reserved.