YyGgi 2023-03-18 16:17 采纳率: 37.5%
浏览 24
已结题

关于#python#的问题:简单AI代码解读

这是一个简单的AI,哪位big old可以给我解读一下用到的技术和方法吗? 有没有评估函数和搜索树的涉及

from game_def import *

class SimpleAI(object):

def __init__(self, alpha=2.5, beta=0.75):
    self.player = None
    self.goal = None
    self.board = None
    self.alpha = alpha
    self.beta = beta

def set_player_ind(self, p, state):
    self.player = p
    self.state = state
    self.pieces = bk_init_player_pieces(self.state)

def __str__(self):
    return "SimpleAI {}".format(self.player)
    
def is_human(self):
    return False
    
def is_winner(self):
    return bk_is_winner(self.state, self.pieces)
    
def update_pieces(self, move_from, move_to):
    for p in range(10):
        if self.pieces[p] == move_from:
            self.pieces[p] = move_to
            break
            
def set_goal(self):
    if self.state == 1:
        for pos in [284, 267, 266, 250, 249, 248, 233, 232, 231, 230]:
            if self.board.states[pos] != self.state:
                self.goal = pos
                return
        return
    elif self.state == 4:
        for pos in [4, 21, 22, 38, 39, 40, 55, 56, 57, 58]:
            if self.board.states[pos] != self.state:
                self.goal = pos
                return
        return
        
def is_arrived(self, pos):
    h_goal = self.goal // board_width
    h_pos = pos // board_width
    if self.state == 1: return h_pos > h_goal or pos >= self.goal
    else: return h_pos < h_goal or pos <= self.goal
    
def get_physic_pos(self, pos):
    h = pos // board_width
    w = pos % board_width
    w = w * 2 - h
    h = h * 1.732
    return [w, h]

def get_dist(self, pos):
    if self.is_arrived(pos): return 0
    [a, b] = self.get_physic_pos(pos)
    [c, d] = self.get_physic_pos(self.goal)
    return (( a - c ) ** 2 * self.alpha + ( b - d ) ** 2) ** self.beta
    
def get_action(self, board):
    self.board = board
    self.set_goal()
    max_score = -10000
    max_move = None
    for pos in self.pieces:
        move_set = bk_search_movable_set(self.board.table, self.board.states, pos)
        for aim in move_set:
            score = self.get_dist(pos) - self.get_dist(aim)
            if score > max_score:
                max_score = score
                max_move = (pos, aim)
    return max_move
代码如上,small white表示看不懂
  • 写回答

4条回答 默认 最新

  • lshen01 2023-03-18 16:19
    关注

    参考GPT和自己的思路:

    这是一个简单的AI代码用于一个棋类游戏。它使用了一些基本的方法和函数,包括计算物理位置、计算距离、搜索可移动选项、评估移动得分等等。它不涉及评估函数和搜索树。下面是对其中的一些方法的简要解释:

    • bk_init_player_pieces:初始化玩家的棋子位置
    • bk_is_winner:检查当前状态是否为获胜状态
    • update_pieces:更新棋子的位置
    • set_goal:设置目标位置
    • is_arrived:检查是否到达目标位置
    • get_physic_pos:计算物理位置
    • get_dist:计算两个位置之间的距离(用于评估移动得分)
    • get_action:寻找最佳的移动选项并返回该移动

    需要注意的是,上述方法需要使用一些其他的库和函数来完成其功能,例如“game_def”模块中的一些函数。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 已采纳回答 3月20日
  • 创建了问题 3月18日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装