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

📄 drawpanel.java

📁 这个是用JAVA开发的五子棋程序
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawPanel extends JPanel implements MouseListener {
	boolean isBlack = true;
	boolean isGameOver = false;
	final int rows = 15;//棋盘行数
	final int cols = 15; //棋盘列数
	final int cellwidth = 30; //单元格宽/高
	final int margin = 30; //边缘距离
	Chess[] chessList = new Chess[rows * cols]; //定义一个棋子数组
	int chessCount = 0;
	public DrawPanel() {
		setBackground(Color.pink);
		addMouseListener(this);//为面版增加监听器
	}
	
	public void restartGame() {
		for(int i=0; i<chessCount; i++)
			chessList[i] = null;
		chessCount = 0;
		isBlack = true;
		isGameOver = false;
		repaint();
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		//绘制棋盘
		for(int i=0; i<=rows; i++) {//画直线
			g.drawLine(margin + i*cellwidth, margin, margin + i*cellwidth, margin+rows * cellwidth);
		}
		for(int i=0; i<=cols; i++) {//画横线
			g.drawLine(margin, margin + i*cellwidth, margin+cols * cellwidth, margin + i*cellwidth);
		}
				
		for(int i=0; i<chessCount; i++){
				g.setColor(chessList[i].getColor());
				g.fillOval(chessList[i].getX() * cellwidth + margin - 15, chessList[i].getY() * cellwidth + margin - 15, 30, 30);
		}
	}
	public void mousePressed(MouseEvent e) {
		int xIdx = (e.getX() - margin +15 )/cellwidth; //索引
		int yIdx = (e.getY() - margin +15 )/cellwidth; //网格索引
		
		//判断是否下在棋盘外
		if(xIdx < 0 || xIdx > rows || yIdx < 0 || yIdx > cols)
			return;
		//判断是否在下过的地方下棋
		for(int i=0; i<chessCount; i++) {
			if(chessList[i].getX() == xIdx && chessList[i].getY() == yIdx) //xIdx, yIdx该位置已经下过棋,不能下
				return;
		}
		if(isGameOver)
			return;
		
		Color c = isBlack ? Color.black : Color.white;
		Chess ch = new Chess(xIdx, yIdx, c); //创建一个黑色的棋子, 位置在点击的地方
		chessList[chessCount] = ch;
		chessCount++;
		isBlack = !isBlack;
		repaint();//通知系统需要重新绘制。
		//判断胜负
		if(isWin()) {
			isGameOver = true;
			JOptionPane.showMessageDialog(this, "恭喜, 您赢了!");
		}
	}	
	
	public boolean isWin() {
		Chess ch = chessList[chessCount-1]; //获得刚刚下过的棋
		int counter = 1;
		int xIdx = ch.getX() - 1; //x方向向左
		while(xIdx >= 0) {
			if(findChess(xIdx, ch.getY(), ch.getColor()))
				counter++;
			else
				break; 
			xIdx--;
		}
		
		xIdx = ch.getX() + 1; //x方向向右
		while(xIdx <= rows) {
			if(findChess(xIdx, ch.getY(), ch.getColor()))
				counter++;
			else
				break; 
			xIdx++;
		}
		
		if(counter >= 5)
			return true;		
		
		//y方向寻找
		counter = 1;
		int yIdx = ch.getY() - 1; //y方向向上
		while(yIdx >= 0) {
			if(findChess(ch.getX(), yIdx, ch.getColor()))
				counter++;
			else
				break; 
			yIdx--;
		}				
		
		yIdx = ch.getY() + 1; //y方向向下
		while(yIdx <= cols) {
			if(findChess(ch.getX(), yIdx, ch.getColor()))
				counter++;
			else
				break; 
			yIdx++;
		}

		if(counter >= 5)
			return true;		
			
		
		//斜西北,东南
		xIdx = ch.getX() -1;
		yIdx = ch.getY() -1;
		counter = 1;
		while(xIdx >=0 && yIdx >= 0) {
			if(findChess(xIdx, yIdx, ch.getColor()))
				counter++;
			else
				break; 
			xIdx--; yIdx--;
		}
			
		xIdx = ch.getX() +1;
		yIdx = ch.getY() +1;
		while(xIdx <=rows && yIdx <= cols) {
			if(findChess(xIdx, yIdx, ch.getColor()))
				counter++;
			else
				break; 
			xIdx++; yIdx++;
		}
		
		if(counter >= 5) 
			return true;
			
		//斜东北,西南
		xIdx = ch.getX() -1;
		yIdx = ch.getY() +1;
		counter = 1;
		while(xIdx >=0 && yIdx <= cols) {
			if(findChess(xIdx, yIdx, ch.getColor()))
				counter++;
			else
				break; 
			xIdx--; yIdx++;
		}
			
		xIdx = ch.getX() +1;
		yIdx = ch.getY() -1;
		while(xIdx <=rows && yIdx >= 0) {
			if(findChess(xIdx, yIdx, ch.getColor()))
				counter++;
			else
				break; 
			xIdx++; yIdx--;
		}
		
		if(counter >= 5) 
			return true;

		return false;
	}
	
	//寻找棋子
	public boolean findChess(int x, int y, Color color) {
		for(int i=0; i<chessCount; i++) {
			if(chessList[i].getX() == x && chessList[i].getY() == y && chessList[i].getColor() == color)
				return true;
		}
		return false;
	}
	
	
	public void mouseReleased(MouseEvent e) {}	
	public void mouseClicked(MouseEvent e) {}	
	public void mouseExited(MouseEvent e) {}	
	public void mouseEntered(MouseEvent e) {}	
	
	
}

⌨️ 快捷键说明

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