简单的扫雷!!!
程序员文章站
2022-04-12 10:34:07
...
/*
简单的扫雷,用了数组。//可以把棋子设为一个类,添加棋子的属性。。。
*/
import java.util.Random;
public class Mymine {
private int[][] arr=new int[10][10]; //显示棋盘的数组
private int[][] arr2=new int[10][10]; //接收周围点击后的数值的变化,根据此变化判断输出的内容
private int[][] arr3=new int[10][10];
//private int[][] arr3=new int [100][2];
private int[][] arr1=new int[10][2]; //定义存放横纵坐标的数组
int k=0;
boolean flag=true;
//设置表示状态的量
//显示棋盘
public void show(){
System.out.print(" ");
for(int i=0;i<10;i++)
System.out.print(i+"-");
System.out.println();
for(int i=0;i<arr.length;i++){
System.out.print(i+" ");
//如果是雷的时候显示
for(int j=0;j<arr[i].length;j++){
if(flag==true){
if(arr[i][j]==-1){
System.out.print("E-");
}
else if(arr[i][j]>0){
System.out.print(arr[i][j]+"-");
}else{
System.out.print("+-");
}
}
if(flag==false){
if(arr2[i][j]==1){
System.out.print(arr[i][j]+"-");//-1表示是雷
}else{
System.out.print("+-");
}
} //显示其他不是雷的东西
}
System.out.println();
}
}
//显示的重载
public void show1(){
System.out.print(" ");
for(int i=0;i<10;i++)
System.out.print(i+"-");
System.out.println();
for(int i=0;i<arr.length;i++){
System.out.print(i+" ");
//如果是雷的时候显示
for(int j=0;j<arr[i].length;j++){
if(arr2[i][j]==-1){
System.out.print(" -");
}
else if(arr2[i][j]==1){
System.out.print(arr[i][j]+"-");
}else
System.out.print("+-");
}
System.out.println();
}
}
//当点击是空格的时候判断它周围的东西,有一点缺陷,在棋盘的外围一周不能判断
public void judge1(int x,int y){
if(x-1>=0&&x+1<=9&&y-1>=0&&y+1<=9){
if(arr3[x-1][y]==0 ){
if(arr[x-1][y]>0 ){
arr2[x-1][y]=1;
arr3[x-1][y]=1;
}
else if(arr[x-1][y]==0 ) {
arr2[x-1][y]=-1;
arr3[x-1][y]=1;
judge1(x-1,y);
}
else{
arr2[x-1][y]=0;
arr3[x-1][y]=1;
}
}
////////////////////////
if(arr3[x+1][y]==0 && x<9){
if(arr[x+1][y]>0 ){
arr2[x+1][y]=1;
arr3[x+1][y]=1;
}
else if(arr[x+1][y]==0 ) {
arr2[x+1][y]=-1;
arr3[x+1][y]=1;
judge1(x+1,y);
}
else{
arr2[x+1][y]=0;
arr3[x+1][y]=1;
}
}
////////////
if(arr3[x+1][y-1]==0){
if(arr[x+1][y-1]>0 ){
arr2[x+1][y-1]=1;
arr3[x+1][y-1]=1;
}
else if(arr[x+1][y-1]==0 ) {
arr2[x+1][y-1]=-1;
arr3[x+1][y-1]=1;
judge1(x+1,y-1);
}
else{
arr2[x+1][y-1]=0;
arr3[x+1][y-1]=1;
}
}
/////////////
if( arr3[x+1][y+1]==0){
if(arr[x+1][y+1]>0 ){
arr2[x+1][y+1]=1;
arr3[x+1][y+1]=1;
}
else if(arr[x+1][y+1]==0) {
arr2[x+1][y+1]=-1;
arr3[x+1][y+1]=1;
judge1(x+1,y+1);
}
else{
arr2[x+1][y+1]=0;
arr3[x+1][y+1]=1;
}
}
/////////////
if(arr3[x-1][y-1]==0){
if(arr[x-1][y-1]>0 ){
arr2[x-1][y-1]=1;
arr3[x-1][y-1]=1;
}
else if(arr[x-1][y-1]==0 ) {
arr2[x-1][y-1]=-1;
arr3[x-1][y-1]=1;
judge1(x-1,y-1);
}
else{
arr2[x-1][y-1]=0;
arr3[x-1][y-1]=1;
}
}
/////////////
if(arr3[x][y-1]==0){
if(arr[x][y-1]>0 ){
arr2[x][y-1]=1;
arr3[x][y-1]=1;
}
else if(arr[x][y-1]==0 ) {
arr2[x][y-1]=-1;
arr3[x][y-1]=1;
judge1(x,y-1);
}
else{
arr2[x][y-1]=0;
arr3[x][y-1]=1;
}
}
/////////////
if(arr3[x][y+1]==0 ){
if(arr[x][y+1]>0 ){
arr2[x][y+1]=1;
arr3[x][y+1]=1;
}
else if(arr[x][y+1]==0 ) {
arr2[x][y+1]=-1;
arr3[x][y+1]=1;
judge1(x,y+1);
}
else{
arr2[x][y+1]=0;
arr3[x][y+1]=1;
}
}
/////////////
if(arr3[x-1][y+1]==0 ){
if(arr[x-1][y+1]>0){
arr2[x-1][y+1]=1;
arr3[x-1][y+1]=1;
}
else if(arr[x-1][y+1]==0) {
arr2[x-1][y+1]=-1;
arr3[x-1][y+1]=1;
judge1(x-1,y+1);
}
else{
arr2[x-1][y+1]=0;
arr3[x-1][y+1]=1;
}
}
////////////
}
}
//生成10个随机的雷横纵坐标,返回在一个数组里面
public void num(){
Random r=new Random();
for(int i=0;i<arr1.length;i++){
arr1[i][0]=r.nextInt(10);
arr1[i][1]=r.nextInt(10);
for(int j=0;j<i;j++){
if(arr1[i][0]==arr1[j][0]&&arr1[i][1]==arr1[i][1]){
i--;
break;
}
}
}
}
//把每个雷周围的几个空格的数字改变
public void findMine(){
int x=0,y=0;
for(int i=0;i<10;i++){
x=arr1[i][0];
y=arr1[i][1];
if(x-1>=0&&y-1>=0)
arr[x-1][y-1]++;
if(y-1>=0)
arr[x][y-1]++;
if(x-1>=0)
arr[x-1][y]++;
if(x+1<=9&&y-1>=0)
arr[x+1][y-1]++;
if(x-1>=0&&y+1<=9)
arr[x-1][y+1]++;
if(y+1<=9)
arr[x][y+1]++;
if(x+1<=9&&y+1<=9)
arr[x+1][y+1]++;
if(x+1<=9)
arr[x+1][y]++;
}
for(int i=0;i<10;i++){
arr[arr1[i][0]][arr1[i][1]]=-1;
}
}
//判断输入的值以及要显示的状态
public int judge(int x,int y){
if(arr[x][y]==-1){
k=1;
show();
}
else if(arr[x][y]>0){
arr2[x][y]=1;
k=2;
flag=false;
show();
flag=true;
}else{
k=3;
judge1(x,y);
show1();
}
return k;
}
}
//测试类的实现
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Mymine mymine=new Mymine();
mymine.show();
mymine.num();
mymine.findMine();
mymine.show();
int x=0;
int y=0;
int k=-1;
do{
System.out.println("请输入横纵坐标:");
x=input.nextInt();
y=input.nextInt();
if(x>=0&&x<=9&&y>=0&&y<=9){
k=mymine.judge(x,y);
if(k==1){
System.out.println("你输了!!!");
break;
}
}
else
System.out.println("您输入的有误,请输入横纵坐标:");
}while(true);
}
}