簡單的掃雷!!!
阿新 • • 發佈:2018-11-03
/*
簡單的掃雷,用了陣列。//可以把棋子設為一個類,新增棋子的屬性。。。
*/
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); }
}