pythonAI五子棋(二)

以为第二篇很晚到来,主要是我的想法是等我把机器学习学个大概之后再回来优化。不过最近在深入的学习python,学到了一些pythonic的代码风格,所以决定回来重构一下我的五子棋代码
###这次主要做了
####1.优化了我的代码,使得代码更加简洁美观。可读性更高。
比如这段优化前的函数:

def robotChess(self):
        if self.player == 0:
            if len(self.bla_chessed) == 0 and len(self.whi_chessed) == 0:
                self.bla_chessed.append([25 + 30 * 7, 25 + 30 * 7, self.player % 2])
                self.can.create_oval(25 + 30 * 7 - 11, 25 + 30 * 7 - 11, 25 + 30 * 7 + 11, 25 + 30 * 7 + 11,
                                     fill="black")
                self.board[7][7] = 1
                return
            else:
                _x, _y, _ = self.robot.MaxValue_po(1, 0)
                #print([_x, _y], [x, y])
                newPoint = [_x * 30 + 25, _y * 30 + 25]
                self.can.create_oval(newPoint[0] - 11, newPoint[1] - 11, newPoint[0] + 11, newPoint[1] + 11,
                                     fill="black")
                self.bla_chessed.append([newPoint[0], newPoint[1], self.player % 2])
                self.board[_x][_y] = 0
        else:
            _x, _y, _ = self.robot.MaxValue_po(0, 1)
            newPoint = [_x * 30 + 25, _y * 30 + 25]
            self.can.create_oval(newPoint[0] - 11, newPoint[1] - 11, newPoint[0] + 11, newPoint[1] + 11,
                                 fill="white")
            self.whi_chessed.append([newPoint[0], newPoint[1], self.player % 2])
            self.board[_x][_y] = 1

优化后:

    def robotChess(self):
        """机器人下棋"""
        if self.player == 0:

            if len(self.bla_chessed) == 0 and len(self.whi_chessed) == 0:
                '''电脑执黑棋,开局优化'''
                self.draw_a_chess(*self.bla_start_pos, player=0)
                return

            else:
                _x, _y, _ = self.robot.MaxValue_po(0, 1)
                newPoint = pos_in_board(_x, _y)
                self.draw_a_chess(*newPoint, player=0)
        else:#白棋下
            _x, _y, _ = self.robot.MaxValue_po(1, 0)
            newPoint = pos_in_board(_x, _y)
            self.draw_a_chess(*newPoint, player=1)

很明显代码更加简洁,不会看起来很混乱


####2.添加了注释,可以帮助阅读


####3.运用了一些pythonic的写法,特别是大量运用拆包,使得代码可以很简洁,没有那么多看起来那么多的变量。还有将一些有重复性的功能模块化,减少了整体的代码量。
如果不知道拆包是什么,请看我的另一篇博文:Python进阶(一):python技巧

比如这一段代码就运用:

if len(self.whi_chessed) != 0:
    for tmp in self.whi_chessed:
    oval = pos_to_draw(*tmp[0:2])
    self.can.create_oval(oval, fill="white")

其中第三行:

oval = pos_to_draw(*tmp[0:2])

*temp[0:2]实际相当于tmp[0],tmp[1]

而pos_to_draw的原函数是:

def pos_to_draw(*args):
    """计算棋子在棋盘的顶,底,左,右的位置"""
    x, y = args
    return x - 11, y - 11, x + 11, y + 11

这里*arg运用到另一个知识点,请看: Python进阶(三):*args,**kwargs的使用


效果:
这里写图片描述


下载五子棋代码:下载链接
下载训练棋谱:下载链接

如果觉得我写的不错,扫描下面二维码添加我的公众号
这里写图片描述

  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值