五子棋(一)

一、做一个界面

之前做界面用JFrame,为了把面板分块使用(将按钮组件和整个棋盘分开),现在用JPanle容器组件(JPanle将面板划分为五个区块)

需要用到的类:JFrame
BorderLayout 边框布局,把容器分为5部分,上北下南左西右东中间
JPanel 面板容器组件
FlowLayout
Dimension 封装组件宽度和高度的类
JButton
JLabel
JRadioButton 单选按钮
ButtonGroup 单选按钮分组类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的五子棋代码示例,使用 Python 语言实现: ```python # 定义棋盘大小 BOARD_SIZE = 15 # 定义棋盘 board = [[0] * BOARD_SIZE for i in range(BOARD_SIZE)] # 定义当前玩家(1 或 2) current_player = 1 # 判断是否胜利 def check_win(row, col, player): # 水平方向 count = 0 for i in range(col - 4, col + 5): if i >= 0 and i < BOARD_SIZE and board[row][i] == player: count += 1 if count == 5: return True else: count = 0 # 垂直方向 count = 0 for i in range(row - 4, row + 5): if i >= 0 and i < BOARD_SIZE and board[i][col] == player: count += 1 if count == 5: return True else: count = 0 # 左上到右下方向 count = 0 for i in range(-4, 5): r = row + i c = col + i if r >= 0 and r < BOARD_SIZE and c >= 0 and c < BOARD_SIZE and board[r][c] == player: count += 1 if count == 5: return True else: count = 0 # 右上到左下方向 count = 0 for i in range(-4, 5): r = row + i c = col - i if r >= 0 and r < BOARD_SIZE and c >= 0 and c < BOARD_SIZE and board[r][c] == player: count += 1 if count == 5: return True else: count = 0 return False # 主循环 while True: # 打印当前棋盘 for row in range(BOARD_SIZE): print(' '.join(str(board[row][col]) for col in range(BOARD_SIZE))) print() # 获取玩家落子位置 while True: try: row = int(input('请输入行号:')) col = int(input('请输入列号:')) if row < 0 or row >= BOARD_SIZE or col < 0 or col >= BOARD_SIZE or board[row][col] != 0: raise ValueError break except ValueError: print('无效的输入,请重新输入!') # 落子 board[row][col] = current_player # 判断胜利 if check_win(row, col, current_player): print('玩家 %d 胜利!' % current_player) break # 切换玩家 current_player = 3 - current_player ``` 该代码实现了一个简单的五子棋游戏,玩家可以输入行号和列号来落子,程序会判断是否胜利,并在胜利时输出胜利信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值