Java课程设计:基于Swing的五子棋小游戏(内附源码)

源码分享在文末!


一、项目介绍

五子棋是一款古老而受欢迎的棋类游戏,它的规则简单但策略性很高。通过使用Java编程语言和Swing图形库,我们可以轻松地实现一个简单而有趣的五子棋小游戏。在本篇博客中,我们将探索如何使用Java和Swing创建五子棋游戏,并分享一些编程和设计技巧,帮助你提高你的游戏开发水平。

二、游戏实现

要实现五子棋小游戏,我们需要以下几个关键组件:

  • 游戏窗口:使用Swing库创建一个窗口,作为游戏的主界面。可以设置窗口的大小、标题和关闭行为。

  • 游戏面板:在游戏窗口中创建一个面板,用于显示游戏的画面。游戏面板将承载游戏中的棋盘和棋子。

  • 棋盘:在游戏面板中创建一个棋盘,用于进行游戏的操作。棋盘由多个格子组成,玩家需要在格子上放置棋子。

  • 棋子:在游戏面板中创建一组棋子,用于进行游戏的操作。棋子可以根据玩家的选择放置在棋盘上的不同位置。

  • 游戏逻辑:编写游戏逻辑,包括棋子的放置、胜负判断和游戏结束条件的判断。可以使用适当的算法和数据结构来实现游戏逻辑。

  • 游戏控制:处理用户输入,如鼠标点击事件,来控制棋子的放置和游戏的开始、暂停和重新开始。

三、核心代码

项目中的变量

	final int temp = 600 / 18;
	//图形边界尺寸等属性
	int width, height, boardWidth, boardHeight, xbase, ybase;
	// 模拟棋盘,这里进行一些棋子的判断
	int allChess[][] = new int[19][19];
	boolean isBlack = true;
	// 这里进行画图时候圆的位置(1,2)在第一行第二列
	int x, y;	
	// 为了实现认输的位置
	int countx = 0, county = 0; 
	//存储棋盘位置,方便回溯
	int chessX[] = new int[255];	
	int chessY[] = new int[255];
	// 能不能继续玩
	boolean canPlay = true;

	// 三个面板分别放消息提示框,主面板,一些按钮
	public JPanel jpMessage = new JPanel();
	public JPanel jpMain;
	public JPanel jpButton = new JPanel();
	public JLabel jbMessage;
	JButton restart;
	JButton exit;
	JButton fail;
	JButton regret;

构造方法,棋盘结构初始化定义

	public wuziqi() {
		jpMain = new PaintBoard();
		this.add(jpMain, BorderLayout.CENTER);

		jbMessage = new JLabel("黑方下子");
		jpMessage.add(jbMessage);
		this.add(jpMessage, BorderLayout.NORTH);
		// 按钮区的绘制
		restart = new JButton("重新开始");
		restart.addActionListener(this);
		exit = new JButton("退出游戏");
		exit.addActionListener(this);
		fail = new JButton("认输");
		fail.addActionListener(this);
		regret = new JButton("悔棋");
		regret.addActionListener(this);
		jpButton.add(restart);
		jpButton.add(exit);
		jpButton.add(fail);
		jpButton.add(regret);
		this.add(jpButton, BorderLayout.SOUTH);
		this.setBounds(0, 0, 800, 800);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

paintComponent()方法:绘画棋盘

		protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				width = this.getSize().width; //面板大小
				height = this.getSize().height;
				//  棋盘大小
				boardWidth = 600;
				boardHeight = 600;
				// 左上角第一个点,为了让面板居中
				xbase = (width - boardWidth) / 2;
				ybase = (height - boardHeight) / 2;
				Graphics2D g2d = (Graphics2D) g;
				int x0, y0, x1, y1;//画线的时候用到的四个点
				this.setBackground(new Color(246, 186, 114));
//	            this.addMouseListener(this);
				for (int i = 0; i <= 18; i++) {
					if (i == 0 || i == 18) {
						//调整画线的大小
						g2d.setStroke(new BasicStroke(3.0f));    //画棋盘边界时,线条加粗。setStroke()方法设置线条粗细,

					} else {
						g2d.setStroke(new BasicStroke(1.0f));
					}
					//开始确定每一个点的坐标,然后画行
					x0 = xbase;
					y0 = ybase + i * temp;
					x1 = xbase + 18 * temp;
					y1 = ybase + i * temp;
					g2d.drawLine(x0, y0, x1, y1);
					// 开始画列
					x0 = xbase + temp * i;
					y0 = ybase;
					x1 = xbase + temp * i;
					y1 = ybase + 18 * temp;
					g2d.drawLine(x0, y0, x1, y1);
				}
				int radius = 16;
				// 开始画黑心点圆和白心圆
				for (int i = 0; i < 19; i++) {
					for (int j = 0; j < 19; j++) {
						if (allChess[i][j] == 1) {
							g2d.setColor(Color.black);
							g2d.fillOval(i * temp + xbase - radius / 2, j * temp + ybase - 7, radius, radius);
						} else if (allChess[i][j] == 2) {
							g2d.setColor(Color.white);
							g2d.fillOval(i * temp + xbase - radius / 2, j * temp + ybase - 7, radius, radius);
							g2d.drawOval(i * temp + xbase - radius / 2, j * temp + ybase - 7, radius, radius);
						}

					}
				}
			}

isWin()方法:监测是不是有一方赢了游戏

		private boolean isWin() {
				int color = allChess[x][y];
				int count;
				count = this.Count(1, 0, color);
				if (count >= 5) {
					return true;
				} else {
					count = this.Count(0, 1, color);
					if (count >= 5) {
						return true;
					} else {
						count = this.Count(1, 1, color);
						if (count >= 5)
							return true;
						else {
							count = this.Count(1, -1, color);
							if (count >= 5)
								return true;
						}
					}
				}
				return false;
			}

四、项目展示

项目启动
在这里插入图片描述
开始游戏
在这里插入图片描述
悔棋
在这里插入图片描述
赢游戏,游戏默认结束
在这里插入图片描述

五、源码获取

因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!

点我获取源码

  • 19
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值