• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:PyGame每日一练五子棋小游戏

python 搞代码 4年前 (2022-02-20) 11次浏览 已收录 0个评论

class AI:

def __init__(self, line_points, chessman):
    self._line_points = line_points
    self._my = chessman
    self._opponent = BLACK_CHESSMAN if chessman == WHITE_CHESSMAN else WHITE_CHESSMAN
    self._checkerboard = [[0] * line_points for _ in range(line_points)]
def get_opponent_drop(self, point):
    self._checkerboard[point.Y][point.X] = self._opponent.Value
def AI_drop(self):
    point = None
    score = 0
    for i in range(self._line_points):
        for j in range(self._line_points):
            if self._checkerboard[j][i] == 0:
                _score = self._get_point_score(Point(i, j))
                if _score > score:
                    score = _score
                    point = Point(i, j)
                elif _score == score and _score > 0:
                    r = random.randint(0, 100)
                    if r % 2 == 0:
                        point = Point(i, j)
    self._checkerboard[point.Y][point.X] = self._my.Value
    return point
def _get_point_score(self, point):
    score = 0
    for os in offset:
        score += self._get_direction_score(point, os[0], os[1])
    return score
def _get_direction_score(self, point, x_offset, y_offset):
    count = 0   # 落子处我方间断子数
    _count = 0  # 落子处对方间断子数
    space = None   # 我方间断子中有无空格
    _space = None  # 对方间断子中有无空格
    both = 0    # 我方间断子两端有无阻挡
    _both = 0   # 对方间断子两端有无阻挡
    # 如果是 1 示意是边上是我方子,2 示意敌方子
    flag =[商品期货](https://www.gendan5.com/futures/cf.html) self._get_stone_color(point, x_offset, y_offset, True)
    if flag != 0:
        for step in range(1, 6):
            x = point.X + step * x_offset
            y = point.Y + step * y_offset
            if 0 <= x < self._line_points and 0 <= y < self._line_points:
                if flag == 1:
                    if self._checkerboard[y][x] == self._my.Value:
                        count += 1
                        if space is False:
                            space = True
                    elif self._checkerboard[y][x] == self._opponent.Value:
                        _both += 1
                        break
                    else:
                        if space is None:
                            space = False
                        else:
                            break   # 遇到第二个空格退出
                elif flag == 2:
                    if self._checkerboard[y][x] == self._my.Value:
                        _both += 1
                        break
                    elif self._checkerboard[y][x] == self._opponent.Value:
                        _count += 1
                        if _space is False:
                            _space = True
                    else:
                        if _space is None:
                            _space = False
                        else:
                            break
            else:
                # 遇到边也就是阻挡
                if flag == 1:
                    both += 1
                elif flag == 2:
                    _both += 1
    if space is False:
        space = None
    if _space is False:
        _space = None
    _flag = self._get_stone_color(point, -x_offset, -y_offset, True)
    if _flag != 0:
        for step in range(1, 6):
            x = point.X - step * x_offset
            y = point.Y - step * y_offset
            if 0 <= x < self._line_points and 0 <= y < self._line_points:
                if _flag == 1:
                    if self._checkerboard[y][x] == self._my.Value:
                        count += 1
                        if space is False:
                            space = True
                    elif self._checkerboard[y][x] == self._opponent.Value:
                        _both += 1
                        break
                    else:
                        if space is None:
                            space = False
                        else:
                            break   # 遇到第二个空格退出
                elif _flag == 2:
                    if self._checkerboard[y][x] == self._my.Value:
                        _both += 1
                        break
                    elif self._checkerboard[y][x] == self._opponent.Value:
                        _count += 1
                        if _space is False:
                            _space = True
                    else:
                        if _space is None:
                            _space = False
                        else:
                            break
            else:
                # 遇到边也就是阻挡
                if _flag == 1:
                    both += 1
                elif _flag == 2:
                    _both += 1
    score = 0
    if count == 4:
        score = 10000
    elif _count == 4:
        score = 9000
    elif count == 3:
        if both == 0:
            score = 1000
        elif both == 1:
            score = 100
        else:
            score = 0
    elif _count == 3:
        if _both == 0:
            score = 900
        elif _both == 1:
            score = 90
        else:
            score = 0
    elif count == 2:
        if both == 0:
            score = 100
        elif both == 1:
            score = 10
        else:
            score = 0
    elif _count == 2:
        if _both == 0:
            score = 90
        elif _both == 1:
            score = 9
        else:
            score = 0
    elif count == 1:
        score = 10
    elif _count == 1:
        score = 9
    else:
        score = 0
    if space or _space:
        score /= 2
    return score
# 判断指定地位处在指定方向上是我方子、对方子、空
def _get_stone_color(self, point, x_offset, y_offset, next):
    x = point.X + x_offset
    y = point.Y + y_offset
    if 0 <= x < self._line_points and 0 <= y < self._line_points:
        if self._checkerboard[y][x] == self._my.Value:
            return 1
        elif self._checkerboard[y][x] == self._opponent.Value:
            return 2
        else:
            if next:
                return self._get_stone_color(Point(x, y), x_offset, y_offset, False)
            else:
                return 0
    else:
        return 0

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:PyGame每日一练五子棋小游戏
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址