⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamepanel.txt

📁 本源码为五子棋源码 可以在java环境中运行
💻 TXT
字号:
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GamePanel extends JPanel implements MouseListener {

	private static final long serialVersionUID = 1L;
	private int x, y, row, column;
	// 代表棋盘的二维数组 0值为空 1为黑, 2为白
	// 客户端持黑子 - 先手, 服务端持白子 - 后手
	private int[][] allChess = new int[21][21];
	private int timeLimit = 30;
	private String inputAddress;
	private boolean  gameIsRunning, stopGame, linkToOther, isWaittingFormData = true;
	private Timer t;
	private Controller controller;

	GamePanel(Controller c) {
		this.setSize(540, 550);
		// addListener
		this.addMouseListener(this);
		t = new GamePanel.Timer();
		controller = c;
	}

	// 建立连接 - 开始游戏
	void isLink(String info) {

		gameIsRunning = true;
		try {
			t.start();
			if (linkToOther) {
				isWaittingFormData = false;
                // 提示用户落子

				
			} else {
				// 服务端 - 连接后等待数据
				isWaittingFormData = true;
				acceptChess(controller.acceptData());
				t.wait();
				
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	void acceptChess(int[] x) {
		// 接受来自controller的int[]
		allChess[x[0]] [x[1]] = x[2];
		isWaittingFormData = false;
		// 开始计时
		t.reset();
		t.notify();
		this.repaint();
		
	}
	void setClientModel() {
		
		linkToOther = true;
	
	}

	// 暂停
	void stop() {}

	// 继续
	void goOn() {}

	// 发送胜利消息
	void isWin() {}
	
	boolean gameIsRunning() {
		return gameIsRunning;
	}
	
	// 连接到.. 菜单
	private void showLinkDialog() {
		String userInput = null;

		userInput = JOptionPane.showInputDialog(this, "输入对方IP地址:", "连接到...",
				JOptionPane.QUESTION_MESSAGE);

		// 判断IP地址的合法性
		if (userInput != null) {
			if (checkIP(userInput)) {
				// 由Frame显示IP信息
				inputAddress = userInput;
				// 将 inputAddress 传给控制器
				controller.linkTo(inputAddress);
				
			} else {
				// 提示错误信息
				JOptionPane.showMessageDialog(this, "请输入正确格式的IP");
			}
		}

	}

	public void paint(Graphics g) {

		BufferedImage im = null;
		try {
			im = ImageIO.read(new File("Bgp.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		g.drawImage(im, 0, 20, this);
		g.drawLine(18, 92, 418, 92);
		g.drawLine(18, 492, 418, 492);
		g.drawLine(18, 92, 18, 492);
		g.drawLine(418, 92, 418, 492);
		g.fillOval(216, 290, 5, 5);
		g.fillOval(76, 150, 5, 5);
		g.fillOval(356, 150, 5, 5);
		g.fillOval(216, 150, 5, 5);
		g.fillOval(76, 290, 5, 5);
		g.fillOval(356, 290, 5, 5);
		g.fillOval(356, 430, 5, 5);
		g.fillOval(216, 430, 5, 5);
		g.fillOval(76, 430, 5, 5);
		for (int i = 0; i < 20; i++) {
			g.drawLine(18, 92 + 20 * i, 418, 92 + 20 * i);
		}
		for (int i = 0; i < 20; i++) {
			g.drawLine(18 + 20 * i, 92, 18 + 20 * i, 492);
		}
		g.setFont(new Font("宋体", Font.BOLD, 20));
		if(gameIsRunning){
		    g.drawString((isWaittingFormData == true ? "等待" : "")
				    + (linkToOther == true ? "黑方落子" : "白方落子")
				    + (isWaittingFormData == true ? "" : "  剩余时间为: "
						+ getTimeLimit()), 40, 520);
		}

		// 读取allChess中棋子所在交点的位置 并绘制
		for (row = 0; row < 21; row++) {

			for (column = 0; column < 21; column++) {

				if (allChess[row][column] == 1) {
					// 画黑子
					g.fillOval(row * 20 + 14, column * 20 + 88, 12, 12);
				}
				if (allChess[row][column] == 2) {
					// 画白子
					g.setColor(Color.WHITE);
					g.fillOval(row * 20 + 14, column * 20 + 88, 12, 12);
					g.setColor(Color.BLACK);
					g.drawOval(row * 20 + 13, column * 20 + 87, 12, 12);

				}
			}
			
		}
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mousePressed(MouseEvent me) {
		x = me.getX();
		y = me.getY();

		if (x > 437 && x < 529 && y > 92 && y < 125) {
			// 连接按钮, 打开连接菜单
			if(gameIsRunning) {
				JOptionPane.showMessageDialog(this, "游戏正在进行, 请先按[退出]按钮结束当前游戏");
			} else {
				showLinkDialog();
		    }
			   
		}

		if (x > 444 && x < 527 && y > 511 && y < 535) {
			// 帮助按钮
			//JOptionPane.showMessageDialog(this,"");
		}

		if (gameIsRunning && !isWaittingFormData) {
			
			// 暂停按钮
			if (x > 439 && x < 529 && y > 159 && y < 190) {}
			
			// 退出按钮 - 断开连接, 执行清空操作
			if (x > 440 && x < 530 && y > 453 && y < 483) {}

			// 监听棋盘内点击事件
			if (x > 16 && x < 424 && y > 90 && y < 494) {

				// 将坐标转化为棋盘上交点的位置
				row = Math.round((x - 12) / 20);
				column = Math.round((y - 88) / 20);

				if (allChess[row][column] == 0) {
					try {
						if (linkToOther) {
							// ****当前为客户端****
							allChess[row][column] = 1;
							// 发送 - 数据
							controller.chessIsDown(row, column, 1);
							
							isWaittingFormData = true;
							// 停止计时 - 等待
							t.wait();

							// 等待数据 - 接收数据
							acceptChess(controller.acceptData());

						} else {
							// ****当前为服务端****
							// **已经接收到服务端的数据**
							allChess[row][column] = 2;
							
							// 发送 - 数据
							controller.chessIsDown(row, column, 2);

							isWaittingFormData = true;
							// 停止计时 - 等待
							t.wait();
							
							// 等待 - 接收数据
							acceptChess(controller.acceptData());
							
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (checkWin()) {
						JOptionPane.showMessageDialog(this,
								(linkToOther == true ? "黑方" : "白方")
										+ "获胜");
						gameIsRunning = false;
					    isWaittingFormData =false;
						// 发送游戏结束的消息
					    isWin();
					}
					
				}
				// 画出点击位置的棋子
				this.repaint();
			}
		}
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	private boolean checkIP(String str) {
		Pattern patt = Pattern
				.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
		Matcher mat = patt.matcher(str);
		if(mat.matches()) {
			return true;
		}
		return false;
	} 
	
	// 胜利条件判断
	private boolean checkWin() {
		// 由当前棋子开始 找周围相同颜色的棋子的个数
		int countChess = 1, i = 1;
	    int color = allChess[row][column];

		// 左右
		while (true) {
			if (row == 20) {
				break;
			}

			if (color == allChess[row + i][column]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		i = 1;
		while (true) {
			if (row == 0) {
				break;
			}

			if (color == allChess[row - i][column]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		if (countChess == 5) {
			return true;
		}
		countChess = 1;

		// 上下
		while (true) {
			if (row == 20) {
				break;
			}

			if (color == allChess[row][column + i]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		i = 1;
		while (true) {
			if (row == 0) {
				break;
			}

			if (color == allChess[row][column - i]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		if (countChess == 5) {
			return true;
		}
		countChess = 1;

		// 左上 - 右下
		while (true) {
			if (row == 0 || column == 0) {
				break;
			}

			if (color == allChess[row - i][column - i]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		i = 1;
		while (true) {
			if (row == 20 || column == 20) {
				break;
			}

			if (color == allChess[row + i][column + i]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		if (countChess == 5) {
			return true;
		}
		countChess = 1;
		// 右上 - 左下
		while (true) {
			if (row == 20 || column == 0) {
				break;
			}

			if (color == allChess[row + i][column - i]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		i = 1;
		while (true) {
			if (row == 0 || column == 20) {
				break;
			}

			if (color == allChess[row - i][column + i]) {
				countChess++;
				i++;
			} else {
				break;
			}
		}
		if (countChess == 5) {
			return true;
		}

		return false;
	}

	String getTimeLimit() {

		return String.valueOf(timeLimit);
	}

	class Timer extends Thread {
		// 计时器
		void reset() {
			timeLimit = 30;
		}
		
		public void run() {
			
			while (timeLimit >= 0) {
				
				if (timeLimit == 0) {
					JOptionPane.showMessageDialog(GamePanel.this, "超时!\n"
							+ (linkToOther == false ? "黑方" : "白方") + "获胜");
					gameIsRunning = false;
					try {
						if (isAlive()) {
							wait();
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				timeLimit--;
				GamePanel.this.repaint();
			}
			
		}
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -