JAVA 实现低配版扫雷

1.Demo1

package boom;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

class Map extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private int rows;
	private int cols;
	private int boom;
	private char[][] gamemap;
	
	JButton [][] buttons;
	JTextArea text;
	
	public Map(int rows,int cols,int boom) {
		this.rows = rows;
		this.cols = cols;
		this.boom = boom;
		
		init();
		//System.out.println(buttons.length);

	}
	
	private void init() {
		buttons = new JButton[rows][cols];
		gamemap = new char[rows][cols];
		text = new JTextArea();
		

		addButtons();
		addBoom();
	}
	public void addButtons(){
		this.setLayout(new GridLayout(rows,cols));

		for(int i=0;i<rows;i++){
			for(int j=0;j<cols;j++){

				JButton button = new JButton();
				button.setBackground(Color.gray);
				button.setOpaque(true);//设置button不透明
				button.addActionListener(this);
				buttons[i][j]=button;
				this.add(button);
			}
		}
	}
	public void addBoom() {
		Random rand=new Random();
		int randRow,randCol;
		for(int i=0;i<boom;i++){
			randRow=rand.nextInt(rows);
			randCol=rand.nextInt(cols);
			if(gamemap[randRow][randCol]== 'X'){//如果布雷的地方已经有雷了那么i--
				i--;
			}
			else{
				gamemap[randRow][randCol]='X';
				//buttons[randRow][randCol].setText("*");
			
			}
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		//buttons[randRow][randCol].setText("X");
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				if(e.getSource().equals(buttons[i][j])) {
					//System.out.println(i+"  "+j);
					if(gamemap[i][j]=='X') {
						buttons[i][j].setText("X");
						buttons[i][j].setBackground(Color.red);
						text.setText("You are Dead");
						lose();
					}
					else {
						buttons[i][j].setBackground(Color.green);
						gamemap[i][j]='1';
						calcMap(i,j);
						win();
					}
				}
			}
		}
	}
	
	public void lose() {
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				buttons[i][j].setEnabled(false);
			}
		}
	}
	
	public void win() {
		int count = 0;
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				if(gamemap[i][j]=='1') {
					count++;
				}
			}
		}
		if(count==rows*cols-boom)
		{
			System.out.println(count);
			text.setText("You are win");
		}
	}
	
	public void calcMap(int x,int y) {
		int count = 0;
		class Grid{
			int x;
			int y;
			
			public Grid(int x,int y) {
				this.x = x;
				this.y = y;
			}
		}
		
		Grid now = new Grid(x,y);
		Grid next;
		final int[][]dir = {{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
		
		for(int i=0;i<8;i++) {
			next = new Grid(now.x+dir[i][0],now.y+dir[i][1]);
			
			if(next.x>=0&&next.x<cols&&next.y>=0&&next.y<rows) {
				if(gamemap[next.x][next.y]=='X') {
					count++;
				}
			}
		}
		//System.out.println(count);
		buttons[x][y].setText(String.valueOf(count));
	}
}


class GameFrame extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	private int rows,cols,boom;
	Map m;
	JButton reset;
	JTextArea frametext;
	
	public GameFrame(int rows,int cols,int boom) {
		this.rows = rows;
		this.cols = cols;
		this.boom = boom;
		m = new Map(rows,cols,boom);
		frametext = m.text;
		init();
	}
	
	private void init() {
		add(m);
		addResetButton();
		add(frametext,"South");
		setBounds(200,200,800,800);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void addResetButton(){
		reset = new JButton("Reset");
		reset.setBackground(Color.yellow);
		reset.setOpaque(true);
		reset.addActionListener(this);
		this.add(reset,BorderLayout.NORTH);
	}
	
	public void actionPerformed(ActionEvent e) {
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				m.buttons[i][j].setEnabled(true);
				m.buttons[i][j].setBackground(Color.gray);
				m.buttons[i][j].setText(" ");
				frametext.setText("");
			}
		}
	}
}


public class BoomDemo{
	public static void main(String[] args) {
		GameFrame frame = new GameFrame(10,10,20);
		
	}
}


  1. Demo2(难度可选)

(代码中还存在一些问题,逻辑有些混乱,存在有些无用代码,后期有时间会改)

package boom;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

class Map2 extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private int rows;
	private int cols;
	private int boom;
	private char[][] gameMap2;
	
	JButton [][] buttons;
	JTextArea text;
	
	public Map2(int rows,int cols,int boom) {
		this.rows = rows;
		this.cols = cols;
		this.boom = boom;
		
		init();
		//System.out.println(buttons.length);

	}
	
	private void init() {
		buttons = new JButton[rows][cols];
		gameMap2 = new char[rows][cols];
		text = new JTextArea();
		

		addButtons();
		addBoom();
	}
	public void addButtons(){
		this.setLayout(new GridLayout(rows,cols));

		for(int i=0;i<rows;i++){
			for(int j=0;j<cols;j++){

				JButton button = new JButton();
				button.setBackground(Color.gray);
				button.setOpaque(true);//设置button不透明
				button.addActionListener(this);
				buttons[i][j]=button;
				this.add(button);
			}
		}
	}
	public void addBoom() {
		Random rand=new Random();
		int randRow,randCol;
		for(int i=0;i<boom;i++){
			randRow=rand.nextInt(rows);
			randCol=rand.nextInt(cols);
			if(gameMap2[randRow][randCol]== 'X'){//如果布雷的地方已经有雷了那么i--
				i--;
			}
			else{
				gameMap2[randRow][randCol]='X';
				//buttons[randRow][randCol].setText("*");
			
			}
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		//buttons[randRow][randCol].setText("X");
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				if(e.getSource().equals(buttons[i][j])) {//重要!!!判断是哪个按钮点击
					//System.out.println(i+"  "+j);
					if(gameMap2[i][j]=='X') {
						buttons[i][j].setText("X");
						buttons[i][j].setBackground(Color.red);
						text.setText("You are Dead");
						lose();
					}
					else {
						buttons[i][j].setBackground(Color.green);
						gameMap2[i][j]='1';
						calcMap2(i,j);
						win();
					}
				}
			}
		}
	}
	
	public void lose() {
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				buttons[i][j].setEnabled(false);
			}
		}
	}
	
	public void win() {
		int count = 0;
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				if(gameMap2[i][j]=='1') {
					count++;
				}
			}
		}
		if(count==rows*cols-boom)
		{
			System.out.println(count);
			text.setText("You are win");
		}
	}
	
	public void calcMap2(int x,int y) {
		int count = 0;
		class Grid{
			int x;
			int y;
			
			public Grid(int x,int y) {
				this.x = x;
				this.y = y;
			}
		}
		
		Grid now = new Grid(x,y);
		Grid next;
		final int[][]dir = {{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
		
		for(int i=0;i<8;i++) {
			next = new Grid(now.x+dir[i][0],now.y+dir[i][1]);
			
			if(next.x>=0&&next.x<cols&&next.y>=0&&next.y<rows) {
				if(gameMap2[next.x][next.y]=='X') {
					count++;
				}
			}
		}
		//System.out.println(count);
		buttons[x][y].setText(String.valueOf(count));
	}
}

class Diff extends JComboBox implements ItemListener{
	String[] level = {"Easy","Normal","Difficult","Customize"};
	private int rows;
	private int cols;
	private int boom;
	
	public Diff(){
		
		addItem("Choose the level");
		for(String i:level) {
			addItem(i);
		}
		
		this.addItemListener(this);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		String selectedLevel = this.getSelectedItem().toString();
		//selectedLevel = selectedLevel.substring(0, selectedLevel.indexOf("\n"));//不知道为啥会出现两个level所以在这里去掉一个
		setLevel(selectedLevel);
		System.out.println(rows+"  "+cols);
		//System.out.println(selectedLevel.substring(0,5));
		
	}
	
	
	public void setLevel(String level) {
		//System.out.println(level);
		if(level=="Easy") {
			this.rows = 10;
			this.cols = 10;
			this.boom = 10;
		}
		else if(level=="Normal") {
			this.rows = 15;
			this.cols = 15;
			this.boom = 15;			
		}
		else if(level=="Difficult") {
			this.rows = 20;
			this.cols = 20;
			this.boom = 50;			
		}
		GameFrame2 frame = new GameFrame2(cols,rows,boom);
	}
}


class GameFrame2 extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	private int rows,cols,boom;
	Map2 m;
	JButton reset;
	JTextArea frametext;
	JPanel pane;
	Diff level;
	
	public GameFrame2(int rows,int cols,int boom) {
		this.rows = rows;
		this.cols = cols;
		this.boom = boom;

		pane = new JPanel();
		level = new Diff();
		
		System.out.println(rows+"  "+cols+"  "+boom);
		m = new Map2(rows,cols,boom);
		frametext = m.text;
		init();
	}
	
	private void init() {
		pane.setLayout(new FlowLayout());
		pane.add(level);
		add(m);
		addResetButton();
		add(frametext,"South");
		add(pane,"North");
		setBounds(100,50,1000,1000);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void addResetButton(){
		reset = new JButton("Reset");
		reset.setBackground(Color.yellow);
		reset.setOpaque(true);
		reset.addActionListener(this);
		pane.add(reset);
	}
	
	public void actionPerformed(ActionEvent e) {
		for(int i=0;i<rows;i++) {
			for(int j=0;j<cols;j++) {
				m.buttons[i][j].setEnabled(true);
				m.buttons[i][j].setBackground(Color.gray);
				m.buttons[i][j].setText(" ");
				frametext.setText("");
			}
		}
	}
}


public class BoomDemo2{
	public static void main(String[] args) {
		
		GameFrame2 frame = new GameFrame2(10,10,10);
		
	}
}


在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值