四子棋,python代码

四子棋是一种经典的棋盘游戏,游戏规则是玩家轮流在棋盘的空格上放置棋子,如果有一个玩家在横、竖、斜线上连续放置了四颗棋子,则该玩家胜利。

下面是一份基于 Python 的四子棋代码的示例:

```

定义棋盘大小

BOARD_SIZE = 15

初始化棋盘

board = [['-' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

定义棋子颜色

BLACK = 'B' WHITE = 'W'

初始化当前玩家为黑棋

current_player = BLACK

定义获胜条件

def check_win(x, y):

检查水平方向是否有连续四个棋子

count = 1 for i in range(1, 4): if y + i < BOARD_SIZE and board[x][y + i] == current_player: count += 1 else: break for i in range(1, 4): if y - i >= 0 and board[x][y - i] == current_player: count += 1 else: break if count >= 4: return True

检查垂直方向是否有连续四个棋子

count = 1 for i in range(1, 4): if x + i < BOARD_SIZE and board[x + i][y] == current_player: count += 1 else: break for i in range(1, 4): if x - i >= 0 and board[x - i][y] == current_player: count += 1 else: break if count >= 4: return True

检查左上 - 右下方向是否有连续四个棋子

count = 1 for i in range(1, 4): if x + i < BOARD_SIZE and y + i < BOARD_SIZE and board[x + i][y + i] == current_player: count += 1 else: break for i in range(1, 4): if x - i >= 0 and y - i >= 0 and board[x - i

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值