当前位置: 首页 > 知识库问答 >
问题:

AI tictactoe - 未来的电路板和计算机移动

微生昌胤
2023-03-14

我被要求将我的玩家与玩家的打球游戏改进为玩家与电脑对抗的AI打球游戏:为此,我需要编写两个函数:一个获取棋盘和当前玩家的符号,并返回所有可能的未来棋盘列表-每个未来棋盘都是一个包含两个元素的列表:一个是放置符号的位置另一个是放置符号后的板-一圈后的板(我使用的是嵌套的列表板,如下面的代码所示(我在这里收到了帮助)

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

    < li >随机选择一步棋(如果计算机先走,则只是开始)并玩它

运筹学

我拥有的是一个球员对球员的战术

代码:

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

请帮我弄个工作玩家vs电脑井字游戏!非常感谢任何帮助!

共有1个答案

凌声
2023-03-14

有很多不同的方法可以处理它,您可以采取的一种相当简单的方法是利用最小最大值算法

在一个简单的例子中,当你的程序只向前看一个回合时,在游戏进行移动后,你的AI会为它可能做出的每一个移动生成一个棋盘,并根据这些移动生成玩家可能做出的每个反移动。

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

AI的每个潜在动作的分数应该等于玩家所有反击动作的最差分数,因为我们想假设玩家会以他们的最佳利益行事。

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

 类似资料:
  • 基本问题 1. 封装成帧 2. 透明传输 3. 差错检测 信道分类 1. 广播信道 2. 点对点信道 信道复用技术 1. 频分复用 2. 时分复用 3. 统计时分复用 4. 波分复用 5. 码分复用 CSMA/CD 协议 PPP 协议 MAC 地址 局域网 以太网 交换机 虚拟局域网 基本问题 1. 封装成帧 将网络层传下来的分组添加首部和尾部,用于标记帧的开始和结束。 2. 透明传输 透明表示一

  • 一面: 自我介绍 项目介绍 八股文: 1. GBDT 2. xgboost 3.逻辑回归,svm,决策树的优缺点,适用场景 4.决策树和随机森林的区别 5.是否了解attention,transform的kqv 6.用过的loss函数,是否了解triplet loss之类的,好几个没听过的loss,没记住 7.batchnorm的参数是否可训练,b*c*w*h有多少个参数 8.如何进行上采样,上采

  • 我一直在计算机上从事laravel5项目,但现在我想继续另一个项目,但不知道如何:( 我使用的是wampserver,项目位于“www”文件夹中,这是我在尝试打开项目时遇到的错误:“内部服务器错误” 服务器遇到内部错误或配置错误,无法完成您的请求“

  • 当调用find any pictures时,它似乎抛出了一个运行时异常,我不知道为什么它会告诉我我没有适当的权限。我已经在android manifest中包含了它提到的权限,但我不知道grantUriPermission()是什么?下面是堆栈跟踪  

  • 1.1.1 计算机与计算 计算机是当代最伟大的发明之一。自从人类制造出第一台电子数字计算机,迄今已近 70 年。经过这么多年的发展,现在计算机已经应用到社会、生活的几乎每一个方面。人们用计 算机上网冲浪、写文章、打游戏或听歌看电影,机构用计算机管理企业、设计制造产品或从 事电子商务,大量机器被计算机控制,手机与电脑之间的差别越来越分不清,……总之计算 机似乎无处不在、无所不能。那么,计算机究竟是如

  • 本文向大家介绍计算机体系结构和计算机组织之间的差异。,包括了计算机体系结构和计算机组织之间的差异。的使用技巧和注意事项,需要的朋友参考一下 计算机架构 计算机体系结构是用于计算机系统设计和实现的蓝图。它提供了计算机系统的功能详细信息和行为,并且在计算机组织之前就已经存在。计算机体系结构处理“该怎么办?” 电脑组织 计算机组织是计算机系统的操作部分如何链接在一起的方式。它实现了所提供的计算机体系结构