在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → 五子棋敏捷AI(源码)

五子棋敏捷AI(源码)

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:0.02M
  • 下载次数:27
  • 浏览次数:167
  • 发布时间:2021-05-11
  • 实例类别:Python语言基础
  • 发 布 人:卷卷hyc
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 五子棋 AI

实例介绍

【实例简介】五子棋游戏,优化算法,

【实例截图】

从剪贴板

从剪贴板

【源码目录】

五子棋

├── GameMap.py
├── __pycache__
│   ├── ChessAI.cpython-38.pyc
│   ├── GameMap.cpython-38.pyc
│   └── computer.cpython-38.pyc
├── computer.py
└── main.py

1 directory, 6 files


【核心代码】

导入pygame

从pygame.locals导入*

从GameMap导入*

从计算机导入*

 

 

类Button():

    def __init __(自身,屏幕,文本,x,y,颜色,启用):

        self.screen =屏幕

        self.width = BUTTON_WIDTH

        self.height = BUTTON_HEIGHT

        self.button_color =颜色

        self.text_color =(255,255,255)

        self.enable =启用

        self.font = pygame.font.SysFont(None,BUTTON_HEIGHT * 2 // 3)

       

        self.rect = pygame.Rect(0,0,self.width,self.height)

        self.rect.topleft =(x,y)

        self.text =文字

        self.init_msg()

       

    def init_msg():

        如果自我启用:

            self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[0])

        else:

            self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[1])

        self.msg_image_rect = self.msg_image.get_rect()

        self.msg_image_rect.center = self.rect.center

       

    def draw(self):

        if self.enable:

            self.screen.fill(self.button_color[0], self.rect)

        else:

            self.screen.fill(self.button_color[1], self.rect)

        self.screen.blit(self.msg_image, self.msg_image_rect)

       

 

class StartButton(Button):

    def __init__(self, screen, text, x, y):

        super().__init__(screen, text, x, y, [(26, 173, 25),(158, 217, 157)], True)

   

    def click(self, game):

        if self.enable:

            game.start()

            game.winner = None

            self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[1])

            self.enable = False

            return True

        return False

   

    def unclick(self):

        if not self.enable:

            self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[0])

            self.enable = True

       

class GiveupButton(Button):

    def __init__(self, screen, text, x, y):

        super().__init__(screen, text, x, y, [(230, 67, 64),(236, 139, 137)], False)

       

    def click(self, game):

        if self.enable:

            game.is_play = False

            if game.winner is None:

                game.winner = game.map.reverseTurn(game.player)

            self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[1])

            self.enable = False

            return True

        return False

 

    def unclick(self):

        if not self.enable:

            self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[0])

            self.enable = True

 

class Game():

    def __init__(self, caption):

        pygame.init()

        self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

        pygame.display.set_caption(caption)

        self.clock = pygame.time.Clock()

        self.buttons = []

        self.buttons.append(StartButton(self.screen, 'Start', MAP_WIDTH 30, 15))

        self.buttons.append(GiveupButton(self.screen, 'Giveup', MAP_WIDTH 30, BUTTON_HEIGHT 45))

        self.is_play = False

 

        self.map = Map(CHESS_LEN, CHESS_LEN)

        self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE

        self.action = None

        self.AI = ChessAI(CHESS_LEN)

        self.useAI = False

        self.winner = None

   

    def start(self):

        self.is_play = True

        self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE

        self.map.reset()

 

    def play(self):

        self.clock.tick(60)

       

        light_yellow = (247, 238, 214)

        pygame.draw.rect(self.screen, light_yellow, pygame.Rect(0, 0, MAP_WIDTH, SCREEN_HEIGHT))

        pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(MAP_WIDTH, 0, INFO_WIDTH, SCREEN_HEIGHT))

       

        for button in self.buttons:

            button.draw()

       

        if self.is_play and not self.isOver():

            if self.useAI:

                x, y = self.AI.findBestChess(self.map.map, self.player)

                self.checkClick(x, y, True)

                self.useAI = False

 

            if self.action is not None:

                self.checkClick(self.action[0], self.action[1])

                self.action = None

           

            if not self.isOver():

                self.changeMouseShow()

           

        if self.isOver():

            self.showWinner()

 

        self.map.drawBackground(self.screen)

        self.map.drawChess(self.screen)

 

   

    def changeMouseShow(self):

        map_x, map_y = pygame.mouse.get_pos()

        x, y = self.map.MapPosToIndex(map_x, map_y)

        if self.map.isInMap(map_x, map_y) and self.map.isEmpty(x, y):

            pygame.mouse.set_visible(False)

            light_red = (213, 90, 107)

            pos, radius = (map_x, map_y), CHESS_RADIUS

            pygame.draw.circle(self.screen, light_red, pos, radius)

        else:

            pygame.mouse.set_visible(True)

   

    def checkClick(self,x, y, isAI=False):

        self.map.click(x, y, self.player)

        if self.AI.isWin(self.map.map, self.player):

            self.winner = self.player

            self.click_button(self.buttons[1])

        else:  

            self.player = self.map.reverseTurn(self.player)

            if not isAI:   

                self.useAI = True

   

    def mouseClick(self, map_x, map_y):

        if self.is_play and self.map.isInMap(map_x, map_y) and not self.isOver():

            x, y = self.map.MapPosToIndex(map_x, map_y)

            if self.map.isEmpty(x, y):

                self.action = (x, y)

   

    def isOver(self):

        return self.winner is not None

 

    def showWinner(self):

        def showFont(screen, text, location_x, locaiton_y, height):

            font = pygame.font.SysFont(None, height)

            font_image = font.render(text, True, (0, 0, 255), (255, 255, 255))

            font_image_rect = font_image.get_rect()

            font_image_rect.x = location_x

            font_image_rect.y = locaiton_y

            screen.blit(font_image, font_image_rect)

        if self.winner == MAP_ENTRY_TYPE.MAP_PLAYER_ONE:

            str = 'Winner is White'

        else:

            str = 'Winner is Black'

        showFont(self.screen, str, MAP_WIDTH 25, SCREEN_HEIGHT - 60, 30)

        pygame.mouse.set_visible(True)

   

    def click_button(self, button):

        如果button.click(self):

            对于self.buttons中的tmp:

                如果tmp!=按钮:

                    tmp.unclick()

                   

    def check_buttons(自己,mouse_x,mouse_y):

        对于self.buttons中的按钮:

            如果button.rect.collidepoint(mouse_x,mouse_y):

                self.click_button(按钮)

                休息

           

游戏=游戏(“β五子棋狗” GAME_VERSION)

而True:

    game.play()

    pygame.display.update()

   

    对于pygame.event.get()中的事件:

        如果event.type == pygame.QUIT:

            pygame.quit()

            出口()

        elif event.type == pygame.MOUSEBUTTONDOWN:

            mouse_x,mouse_y = pygame.mouse.get_pos()

            game.mouseClick(mouse_x,mouse_y)

            game.check_buttons(mouse_x,mouse_y)


标签: 五子棋 AI

实例下载地址

五子棋敏捷AI(源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警