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

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

这从何说起呢?

先上图再说。

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)

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值