控制台版扫雷游戏
1、二维数组实现扫雷界面
2、格子类实现数据操作
package game.bean;
/**
* 格子实体类
* @author ThinkPad
*/
public class Grid {
//属性
private char content;
private boolean type;
//封装
public char getContent() {
return content;
}
public void setContent(char content) {
this.content = content;
}
public boolean isType() {
return type;
}
public void setType(boolean type) {
this.type = type;
}
}
package game.core;
import java.util.Random;
import game.bean.Grid;
/**
* 与游戏核心类
* @author ThinkPad
*包含游戏核心功能:雷区制定,布雷实现,踩雷事件,显示雷区
*/
public class Core {
//定义一个存放格子对象的二维数组
Grid[][] grid=new Grid[9][9];
//创建雷区方法
public void creat() {
for(int i=0;i<grid.length;i++) {
for(int j=0;j<grid[i].length;j++) {
grid[i][j]=new Grid();
grid[i][j].setContent(' ');
grid[i][j].setType(false);
}
}
}
//显示界面方法
public void show() {
System.out.println("xy 0 1 2 3 4 5 6 7 8 ");
for(int i=0;i<grid.length;i++) {
System.out.print(i+" ");
for(int j=0;j<grid[i].length;j++) {
if(grid[i][j].isType()) {
System.out.print(grid[i][j].getContent()+" ");
}else {
System.out.print("[]");
}
}
System.out.println();
}
}
//随机布雷方法
int cent=10;
public void setMine() {
do{
Random ran=new Random();
int x=ran.nextInt(9);
int y=ran.nextInt(9);
if(grid[x][y].getContent()!='*') {
grid[x][y].setContent('*');
cent--;
}
}while(cent>0);
}
//产生数字
public void produceDigital() {
for(int i=0;i<grid.length;i++) {
for(int j=0;j<grid[i].length;j++) {
int z=48;
if(grid[i][j].getContent()!='*') {
//int x=i-1;
//int y=j-1;
for(int x=i-1;x<i+2;x++) {
for(int y=j-1;y<j+2;y++) {
if(x>=0&&x<9&&y>=0&&y<9&&grid[x][y].getContent()=='*') {
z++;
}
}
}
if(z==48) {
grid[i][j].setContent(' ');
}else {
grid[i][j].setContent((char)z);
}
}
}
}
}
//打开盒子
public boolean click(int x,int y) {
if(grid[x][y].isType()) {
//盒子类已经打开
return true;
}else {
if(grid[x][y].getContent()=='*') {
//是雷直接显示
show();
return false;
}else {
if(grid[x][y].getContent()==' ') {
//是空格
grid[x][y].setType(true);
clickBlank(x,y);
return true;
}else {
//是数字
grid[x][y].setType(true);
return true;
}
}
}
}
//判断空白的格子
public void clickBlank(int x,int y) {
if(x-1>-1&&x-1<9&&grid[x-1][y].getContent()!='*') {
click(x-1,y);
}
if(x+11>-1&&x+1<9&&grid[x+1][y].getContent()!='*') {
click(x+1,y);
}
if(y-1>-1&&y-1<9&&grid[x][y-1].getContent()!='*') {
click(x,y-1);
}
if(y+1>-1&&y+1<9&&grid[x][y+1].getContent()!='*') {
click(x,y+1);
}
}
//计算没开的盒子
public int detection() {
int a=0;
for(int i=0;i<grid.length;i++) {
for(int j=0;j<grid[i].length;j++) {
if(grid[i][j].isType()==false) {
a++;
}
}
}
return a;
}
//点到雷后,显示所有的格子
public void showAll() {
for(int i=0;i<grid.length;i++) {
for(int j=0;j<grid[i].length;j++) {
grid[i][j].setType(true);
}
}
}
}
package game.test;
import java.util.Scanner;
import game.core.Core;
/**
* 测试类
* @author ThinkPad
*
*/
public class test {
public static void main(String[] args) {
Core core=new Core();
core.creat();
core.setMine();
core.produceDigital();
core.show();
Scanner scan=new Scanner(System.in);
int x;
int y;
while(true) {
System.out.println("请输入X的值:");
x=scan.nextInt();
System.out.println("请输入Y的值:");
y=scan.nextInt();
boolean boo=core.click(x, y);
if(!boo) {
core.showAll();
core.show();
System.out.println("游戏失败!!!");
break;
}
if(core.detection()==10) {
core.showAll();
core.show();
System.out.println("游戏成功");
break;
}
core.show();
}
}
}