JAVA课设-五子棋-团队博客

1. 团队名称、团队成员介绍

 刘海博 网络1814 201821123
 鲁俊文 网络1814 201821123
 岳小钢 网络1814 201821123109

2. 项目git地址


3 项目git提交记录截图


4. 项目功能架构图与主要功能流程图

架构图:

流程图:


5. 项目运行截图

6. 项目关键代码

1. 判断赢棋的代码。每下一个棋子都要判断是否有5个连续的棋子连成一条线,一旦5个棋子连成一条线就赢,棋局结束。
向下寻找
		for (int y = yIndex + 1; y <= ROWS; y++) {
			Color c = isBlack ? Color.black : Color.white;
			if (hasChess(xIndex, y, c) != null)
				continueCount++;
			else
				break;

		}
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;

		// 继续另一种情况的搜索:斜向
		// 右上寻找
		for (int x = xIndex + 1, y = yIndex - 1; y >= 0 && x <= COLS; x++, y--) {
			Color c = isBlack ? Color.black : Color.white;
			if (hasChess(x, y, c) != null) {
				continueCount++;
			} else
				break;
		}
		// 左下寻找
		for (int x = xIndex - 1, y = yIndex + 1; x >= 0 && y <= ROWS; x--, y++) {
			Color c = isBlack ? Color.black : Color.white;
			if (hasChess(x, y, c) != null) {
				continueCount++;
			} else
				break;
		}
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;

		// 继续另一种情况的搜索:斜向
		// 左上西北寻找
		for (int x = xIndex - 1, y = yIndex - 1; x >= 0 && y >= 0; x--, y--) {
			Color c = isBlack ? Color.black : Color.white;
			if (hasChess(x, y, c) != null)
				continueCount++;
			else
				break;
		}
		// 右下东南寻找
		for (int x = xIndex + 1, y = yIndex + 1; x <= COLS && y <= ROWS; x++, y++) {
			Color c = isBlack ? Color.black : Color.white;
			if (hasChess(x, y, c) != null)
				continueCount++;
			else
				break;
		}
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;

		return false;
	}

2. 判断当前位置有没有黑棋或者白棋
private Chess hasChess(int xIndex, int yIndex, Color color) {
		for (Chess p : chessList) {
			if (p != null && p.getX() == xIndex && p.getY() == yIndex && p.getColor() == color)
				return p;
		}
		return null;
	}
3
public void mousePressed(MouseEvent e) {// 鼠标在组件上按下时调用

		// 游戏结束时,不再能下
		if (gameOver)
		{
			return;
		}
		String colorName = isBlack ? "黑棋" : "白棋";
		// 将鼠标点击的坐标位置转换成网格索引
		xIndex = (e.getX() - MARGIN + SPAN / 2) / SPAN;
		yIndex = (e.getY() - MARGIN + SPAN / 2) / SPAN;
		// 落在棋盘外不能下
		if (xIndex < 0 || xIndex > ROWS || yIndex < 0 || yIndex > COLS)
			return;
		// 如果x,y位置已经有棋子存在,不能下
		if (findChess(xIndex, yIndex))
			return;
		// 可以进行时的处理
		try {
			music.putChess();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Chess ch = new Chess(xIndex, yIndex, isBlack ? Color.black : Color.white);
		chessList[chessCount++] = ch;
		repaint();// 通知系统重新绘制
		// 如果胜出则给出提示信息,不能继续下棋
		if (isWin()) {
			String msg = String.format("恭喜,%s赢了!", colorName);
			JOptionPane.showMessageDialog(this, msg);
			gameOver = true;
		}
		isBlack = !isBlack;
	}

7. 尚待改进或者新的想法

因为时间太仓促,所以界面不够完善。有很多功能没有实现,还有很多可以改进的地方,之后也想做得更精良。例如想尝试做人机版、联机版、等级功能、游戏计时等功能。

posted on 2020-01-10 04:42  风言风语r  阅读(218)  评论(0编辑  收藏  举报

导航