怎么用python实现五子棋 : 第四节,连五子赢棋算法

您所在的位置:网站首页 如何才能赢五子棋 怎么用python实现五子棋 : 第四节,连五子赢棋算法

怎么用python实现五子棋 : 第四节,连五子赢棋算法

#怎么用python实现五子棋 : 第四节,连五子赢棋算法| 来源: 网络整理| 查看: 265

这一节,主要讲连五子赢棋算法。

这从何说起呢?

先上图再说。

1),横向连五子。

2),纵向连五子。

3),从左上到右下连五子。

4),从右上到左下连五子。

增加一个函数实现上面连五子赢棋的算法。

def go_result(self):         """判断游戏的结局。0为游戏进行中,1为玩家获胜,2为电脑获胜,3为平局"""         # 1. 判断是否横向连续五子         for x in range(self.x - 4):             for y in range(self.y):                 if self.go_map[x][y] == 1 and self.go_map[x + 1][y] == 1 and self.go_map[x + 2][y] == 1 and self.go_map[x + 3][y] == 1 and self.go_map[x + 4][y] == 1:                     return 1                 if self.go_map[x][y] == 2 and self.go_map[x + 1][y] == 2 and self.go_map[x + 2][y] == 2 and self.go_map[x + 3][y] == 2 and self.go_map[x + 4][y] == 2:                     return 2         # 2. 判断是否纵向连续五子         for x in range(self.x):             for y in range(self.y - 4):                 if self.go_map[x][y] == 1 and self.go_map[x][y + 1] == 1 and self.go_map[x][y + 2] == 1 and self.go_map[x][y + 3] == 1 and self.go_map[x][y + 4] == 1:                     return 1                 if self.go_map[x][y] == 2 and self.go_map[x][y + 1] == 2 and self.go_map[x][y + 2] == 2 and self.go_map[x][y + 3] == 2 and self.go_map[x][y + 4] == 2:                     return 2         # 3. 判断是否有左上-右下的连续五子         for x in range(self.x - 4):             for y in range(self.y -4):                 if self.go_map[x][y] == 1 and self.go_map[x + 1][y + 1] == 1 and self.go_map[x + 2][y + 2] == 1 and self.go_map[x + 3][y + 3] == 1 and self.go_map[x + 4][y + 4] == 1:                     return 1                 if self.go_map[x][y] == 2 and self.go_map[x + 1][y + 1] == 2 and self.go_map[x + 2][y + 2] == 2 and self.go_map[x + 3][y + 3] == 2 and self.go_map[x + 4][y + 4] == 2:                     return 2         # 4. 判断是否有右上-左下的连续五子         for x in range(self.x - 4):             for y in range(self.y -4):                 if self.go_map[x + 4][y] == 1 and self.go_map[x + 3][y + 1] == 1 and self.go_map[x + 2][y + 2] == 1 and self.go_map[x + 1][y + 3] == 1 and self.go_map[x][y + 4] == 1:                     return 1                 if self.go_map[x + 4][y] == 2 and self.go_map[x + 3][y + 1] == 2 and self.go_map[x + 2][y + 2] == 2 and self.go_map[x + 1][y + 3] == 2 and self.go_map[x][y + 4] == 2:                     return 2         # 5. 判断是否为平局         for x in range(self.x):             for y in range(self.y):                 if self.go_map[x][y] == 0:  # 棋盘中还有剩余的格子,不能判断为平局                     return 0         return 3

更新一下gomoku_board,添加游戏的判断结果。

def gomoku_board(self, res): """画出棋盘""" self.str = '' for y in xrange(self.y): for x in xrange(self.x-1): # 该位置没有棋子 if self.go_map[x][y]==0: self.str += ' ' # 该位置已被我方占据 elif self.go_map[x][y]==1: self.str += 'O' # 该位置已被对方占据 elif self.go_map[x][y]==2: self.str += 'X' self.str += '-' self.str += '\n' if y != (self.y-1): for _ in xrange(self.x): self.str +=  '| ' self.str +=  '\n' print self.str if res == 0: print u'游戏正在进行中!' elif res == 1: print u'玩家获胜!' elif res == 2: print u'电脑获胜!' elif res == 3: print u'平局!'

主函数需要重新构造一下。

# 主函数      if __name__ == '__main__':     gomoku = Gomoku()     while True:         gomoku.player_down()         res=gomoku.go_result()         gomoku.gomoku_board(res)

来,最后让我们来直观目睹一下结果。



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3