【问题标题】:AI tictactoe - future boards and computer movesAI tictactoe - 未来的棋盘和电脑动作
【发布时间】:2020-05-17 10:34:35
【问题描述】:

我被要求将我的玩家与玩家井字游戏改进为 AI 井字游戏,其中玩家与计算机对战: 为此,我需要编写两个函数: 一个获取当前玩家的棋盘和符号并返回所有可能的未来棋盘列表 - 每个未来棋盘都是一个包含两个元素的列表:一个是放置符号的位置,另一个是之后的棋盘放置符号 - 转一圈后的板(我正在使用嵌套列表板,如下面的代码所示(我收到了帮助,here

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

  1. 选择一个随机动作(仅作为开始,如果计算机先走)并播放它

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

  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】:

    有很多不同的方法可以使用它,一种相当简单的方法是使用Minimax Algorithm

    在一个简单的例子中,你的程序只向前看一圈,在游戏移动之后,你的 AI 会为它可能做出的每一个可能的移动生成一个棋盘,并从这些移动中生成玩家每一个可能的反击可以。

    现在您想为 AI 的每个可能动作分配一个分数,您希望如何定义评分算法取决于您,但它应该代表特定游戏状态对您的 AI 的好坏程度。

    每个 AI 潜在动作的得分应该等于玩家所有反击动作的最差得分,因为我们希望假设玩家会按照他们的最大利益行事。 p>

    因此,您将能够确定 AI 的哪些潜在动作使其最有可能从当前状态赢得比赛。我强烈建议阅读随附的文章以了解实现细节和更深入的了解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多