当前位置: 首页 > news >正文

Java GUI实现五子棋游戏

五子棋是一种双人对弈的棋类游戏,通常在棋盘上进行。棋盘为 15×15 的方格,黑白双方各执棋子,轮流在棋盘的格点上落子,先在横、竖、斜线上形成五个相连的同色棋子者获胜。五子棋规则简单,易学难精,兼具攻防和谋略,是一种极具智慧和趣味性的游戏。

以下是使用Java编写的五子棋游戏的示例代码:

棋盘类:

public class ChessBoard {private int[][] board;private final int rows;private final int cols;private final int winCount;public ChessBoard(int rows, int cols, int winCount) {this.rows = rows;this.cols = cols;this.winCount = winCount;board = new int[rows][cols];}public int getRows() {return rows;}public int getCols() {return cols;}public int getWinCount() {return winCount;}public int getChessman(int row, int col) {return board[row][col];}public boolean canPutChessman(int row, int col) {return board[row][col] == 0;}public void putChessman(int row, int col, int player) {board[row][col] = player;}public boolean isFull() {for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {if (board[i][j] == 0) {return false;}}}return true;}public boolean hasWinner(int player) {for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {if (board[i][j] == player) {if (checkHorizontal(i, j, player)) {return true;}if (checkVertical(i, j, player)) {return true;}if (checkDiagonal1(i, j, player)) {return true;}if (checkDiagonal2(i, j, player)) {return true;}}}}return false;}private boolean checkHorizontal(int row, int col, int player) {int count = 1;for (int j = col + 1; j < cols && board[row][j] == player; j++) {count++;}for (int j = col - 1; j >= 0 && board[row][j] == player; j--) {count++;}return count >= winCount;}private boolean checkVertical(int row, int col, int player) {int count = 1;for (int i = row + 1; i < rows && board[i][col] == player; i++) {count++;}for (int i = row - 1; i >= 0 && board[i][col] == player; i--) {count++;}return count >= winCount;}private boolean checkDiagonal1(int row, int col, int player) {int count = 1;for (int i = row + 1, j = col + 1; i < rows && j < cols && board[i][j] == player; i++, j++) {count++;}for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) {count++;}return count >= winCount;}private boolean checkDiagonal2(int row, int col, int player) {int count = 1;for (int i = row + 1, j = col - 1; i < rows && j >= 0 && board[i][j] == player; i++, j--) {count++;}for (int i = row - 1, j = col + 1; i >= 0 && j < cols && board[i][j] == player; i--, j++) {count++;}return count >= winCount;}}

游戏界面类:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;public class GameUI extends JFrame {private final int rows;private final int cols;private final int winCount;private ChessBoard board;private int currentPlayer;private boolean gameOver;private final JPanel panel;public GameUI(int rows, int cols, int winCount) {this.rows = rows;this.cols = cols;this.winCount = winCount;setTitle("五子棋");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(cols * 40, rows * 40); // 每个棋子为正方形,大小为40setLocationRelativeTo(null);currentPlayer = 1;board = new ChessBoard(rows, cols, winCount);gameOver = false;panel = new JPanel() {@Overridepublic void paintComponent(Graphics g) {super.paintComponent(g);for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {int chessman = board.getChessman(i, j);if (chessman == 1) {g.setColor(Color.BLACK);g.fillOval(j * 40 + 5, i * 40 + 5, 30, 30);} else if (chessman == 2) {g.setColor(Color.WHITE);g.fillOval(j * 40 + 5, i * 40 + 5, 30, 30);g.setColor(Color.BLACK);g.drawOval(j * 40 + 5, i * 40 + 5, 30, 30);}}}}};panel.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {if (gameOver) {return;}int col = e.getX() / 40;int row = e.getY() / 40;if (row < rows && col < cols && board.canPutChessman(row, col)) {board.putChessman(row, col, currentPlayer);panel.repaint();if (board.hasWinner(currentPlayer)) {gameOver = true;System.out.println("Player " + currentPlayer + " wins.");} else if (board.isFull()) {gameOver = true;System.out.println("Tie game.");} else {currentPlayer = 3 - currentPlayer; // 切换玩家}}}});add(panel);setVisible(true);}}

主程序:

public class Main {public static void main(String[] args) {new GameUI(15, 15, 5);}}

在主程序中创建游戏界面对象,传入行数、列数和获胜所需连续棋子个数,即可开始游戏。

效果如下:

快去体验一下吧!

http://www.lryc.cn/news/234422.html

相关文章:

  • Python 集成 Nacos 配置中心
  • Debian 11 更新 Node.js 版本
  • python 对图像进行聚类分析
  • 程序员导航站
  • BIO、NIO、AIO三者的区别及其应用场景(结合生活例子,简单易懂)
  • 深度学习YOLO图像视频足球和人体检测 - python opencv 计算机竞赛
  • 系列七、JVM的内存结构【堆(Heap)】
  • 什么是Selenium?如何使用Selenium进行自动化测试?
  • 【蓝桥杯 第十五届模拟赛 Java B组】训练题(A - I)
  • 【数据结构】手撕双向链表
  • 性能测试 —— Jmeter接口处理不低于200次/秒-场景
  • Qt中使用QNetworkAccessManager类发送https请求时状态码返回0
  • Linux - 物理内存管理 - memmap
  • Python爬虫动态ip代理防止被封的方法
  • 01Urllib
  • python爬取酷我音乐 根据歌名进行爬取
  • 【深度学习】吴恩达课程笔记(五)——超参数调试、batch norm、Softmax 回归
  • 腾讯云轻量级服务器和云服务器什么区别?轻量服务器是干什么用的
  • 解决:虚拟机远程连接失败
  • SpringBoot项目集成发邮件功能
  • 【Spring篇】使用注解进行开发
  • Flink(六)【DataFrame 转换算子(下)】
  • 【2023春李宏毅机器学习】生成式学习的两种策略
  • Android13 adb 无法连接?
  • Ubuntu 20.04 调整交换分区大小
  • 将Agent技术的灵活性引入RPA,清华等发布自动化智能体ProAgent
  • 高济健康:数字化科技创新与新零售碰撞 助推医疗产业优化升级
  • SystemVerilog学习 (5)——接口
  • vue3插槽的使用
  • IPTABLES问题:DNAT下如何解决内网访问内部服务器问题