# pythonAI五子棋(二)

###这次主要做了
####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的写法，特别是大量运用拆包，使得代码可以很简洁，没有那么多看起来那么多的变量。还有将一些有重复性的功能模块化，减少了整体的代码量。

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]

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