×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Yani Fernandez
Added: Jul 12, 2017 12:57 PM
Views: 2450
Tags: no tags
  1. package relocation;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.*;
  6. class ExceptionParameter extends Exception{
  7.     public ExceptionParameter(String s){
  8.         super(s);
  9.     }
  10.     public ExceptionParameter(String s,location pms){
  11.         super(s);    
  12.     }
  13. }
  14. public class trial {
  15.      public static void main(String[] args)throws ExceptionParameter{
  16.         location dis = null;
  17.         try{
  18.             dis = new location();
  19.             dis.gameStart();
  20.         }catch(ExceptionParameter abc){
  21.             new ExceptionParameter("Wrong input by User:"+abc);
  22.         }
  23.      }
  24. }
  25. class location{
  26.     boolean[][]openFlag;
  27.     boolean[][]mineFlag;
  28.     int[][]numberOfmine;
  29.     int counter;
  30.     int fieldX;
  31.     int lengthend;
  32.     String fieldY;
  33.     String inputStdIn[];
  34.    @SuppressWarnings("OverridableMethodCallInConstructor")
  35.     public location()throws ExceptionParameter{
  36.          int x,y,inputMine;
  37.          Scanner scan = new Scanner(System.in);
  38.         System.out.println("Enter length for x axis: (integer value)");
  39.         x = scan.nextInt();
  40.         System.out.println("Enter length for y axis: (integer value) ");
  41.         y = scan.nextInt();
  42.          System.out.println("Enter number of mind: (integer value) ");
  43.         inputMine = scan.nextInt();
  44.         standardInputLength();
  45.        
  46.         while(lengthend != 0){      
  47.                 if(lengthend == 1){
  48.                         showSelection(x,y,inputMine + 2);
  49.                         initializedField(x,y,inputMine + 2);
  50.                     }else if(lengthend == 2){
  51.                         showSelection(x,y,inputMine + 3);
  52.                         initializedField(x,y,inputMine + 3);
  53.                     }else if(lengthend == 3){
  54.                         showSelection(x,y,inputMine + 5);
  55.                         initializedField(x,y,inputMine + 5);
  56.                     }else if( lengthend >= 3){
  57.                         standardInputLength();
  58.                     }else{      
  59.                       throw new ExceptionParameter("Invalid input!");
  60.                  }
  61.            break;    
  62.           }          
  63.       }          
  64.     void standardInputLength()throws ExceptionParameter{//use for optional input
  65.         Scanner scan = new Scanner(System.in);
  66.         System.out.println("Enter Difficulty: ");
  67.         System.out.println("1: NORMAL 2: HARD 3: DIFFICULT");
  68.         lengthend = scan.nextInt();
  69.         if(!(1 <= lengthend && lengthend <= 3)){
  70.             throw new ExceptionParameter("Invalid input!");
  71.         }
  72.     }
  73.     void showSelection(int x,int y,int inputMine)throws ExceptionParameter{
  74.        
  75.         System.out.printf("Field size %d x %d\n",x,y,inputMine);
  76.         System.out.println("Number of mine: "+inputMine);
  77.         System.out.println("X represents a mine \n\n if you select a grid that contains mine,\n you lose the game.");
  78.         System.out.println("--------------------");
  79.     }
  80.     void printField(){
  81.         System.out.printf("%s ","  ");
  82.         char c = 'a';
  83.             for(int i = 0; i < fieldX;i++){
  84.                 System.out.printf("%c ",c++);
  85.             }
  86.         System.out.println();
  87.                 for(int i = 0; i <fieldX;i++){
  88.                     System.out.printf("%02d ", i + 1);
  89.                         for(int j = 0; j < Integer.parseInt(""+fieldY);j++){
  90.                             if(openFlag[i][j] == false){
  91.                                 System.out.printf("N ");
  92.                               }else if(mineFlag[i][j] == true){
  93.                                   System.out.printf("X ");
  94.                              }else{
  95.                                   System.out.printf("%d ",numberOfmine[i][j]);
  96.                             }
  97.                          }
  98.                         System.out.println();
  99.                  }
  100.     }
  101.     void printFieldStatus(){
  102.         System.out.println();
  103.         for(int i = 0; i <fieldX; i++){
  104.             for(int j = 0; j < Integer.parseInt(""+fieldY);j++){
  105.                 if(mineFlag[i][j] == true){
  106.                     System.out.print(1+" ");
  107.                 }else{
  108.                     System.out.print(0+" ");
  109.               }
  110.             }
  111.             System.out.println();
  112.         }
  113.     }
  114.     void initializedField(int x, int y, int inMine){
  115.         openFlag = new boolean[x][y];
  116.         mineFlag = new boolean[x][y];
  117.         numberOfmine= new int[x][y];
  118.         this.fieldX = x;
  119.         this.fieldY = String.valueOf(y);
  120.            for(int i = 0; i < inMine;i++){
  121.                Random rand = new Random();
  122.                int randx = 0;
  123.                int randy = 0;
  124.                do{
  125.                    randx = rand.nextInt(x);
  126.                    randy = rand.nextInt(y);
  127.                }while(mineFlag[randx][randy]);
  128.                     mineFlag[randx][randy] = true;
  129.                     for(int j = -1; j < x;j++){
  130.                         for(int k = -1; k < y;k++){
  131.                             if(randx + j >=0 && randx + j < x && randy + k >=0 && randy + k < y){
  132.                                 numberOfmine[randx +j][randy +k]++;
  133.                             }
  134.                         }
  135.                     }
  136.            }
  137.     }
  138.     void handlingInput(){
  139.         try{
  140.             while(true){
  141.  
  142.                 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  143.                 String temp = reader.readLine();
  144.                 if(!temp.equals(null) && !temp.equals("")){
  145.                     inputStdIn = temp.split("[\\s]+");
  146.                 }else{
  147.                     continue;
  148.                 }
  149.                   if(inputStdIn[0].charAt(0) == 'q'){
  150.                       System.exit(0);;
  151.                   }
  152.                   break;
  153.              }
  154.         }catch(IOException abc){
  155.                 System.out.println(abc.getMessage());
  156.                 System.out.println("Force termination");
  157.        }
  158.     }
  159.     boolean checkField(){
  160.         for(int i = 0; i < fieldX;i++){
  161.             for(int j = 0; j < Integer.parseInt(""+fieldY);j++){
  162.                 if(openFlag[i][j] == false && mineFlag[i][j] == false){
  163.                     return false;
  164.                 }
  165.             }
  166.         }
  167.         return true;
  168.     }
  169.     void openField(int px, int py){
  170.         if(px < 0 || px >= fieldX || py <0 || py >= Integer.parseInt(""+fieldY)){
  171.               return;
  172.         }
  173.            if(openFlag[px][py] == true){
  174.                return;
  175.            }
  176.            openFlag[px][py] = true;
  177.            if(mineFlag[px][py] == true){
  178.                return;
  179.            }
  180.               if(mineFlag[px][py] == false){
  181.                   counter++;
  182.                   System.out.println("Your score is: "+counter);
  183.               }
  184.               else{
  185.                   System.out.println("You got no score");
  186.               }
  187.        //    counter++;
  188.       //       System.out.println("your score: "+counter);
  189.            if(mineFlag[px][py] == false && numberOfmine[px][py] == 0 ){
  190.                for(int i = -1; i < 2;i++){
  191.                    for(int j = -1; j < 2; j++){
  192.                        openField(px +1,py +1);
  193.                    }
  194.                }
  195.              counter++;
  196.              System.out.println("your score: "+counter);
  197.            }
  198.            
  199.     }
  200.     void gameStart()throws ExceptionParameter{
  201.         boolean gameover = false;
  202.        
  203.         while(!gameover){
  204.             printField();  
  205.            
  206.             char inputX;
  207.             String inputY;
  208.             int sx = 0;
  209.             int sy = 0;
  210.            
  211.             while(true){
  212.                
  213.                 System.out.println();
  214.                 System.out.print("To Open the mind!\n please input letter (space) number.");
  215.                 System.out.println(" (((q -> force quit)))");
  216.                
  217.                 handlingInput();
  218.                
  219.                 if(inputStdIn.length > 2){
  220.                     continue;
  221.                 }
  222.                
  223.                 inputX = inputStdIn[0].charAt(0);
  224.                 inputY = inputStdIn[1];
  225.                
  226.                 if('a' <= inputX && inputX <= 'z'){
  227.                     sx = (int)(inputX - 'a');
  228.                 }
  229.                 else if('A' <= inputX && inputX <= 'Z'){
  230.                     sx = (int)(inputX - 'A');
  231.                 }
  232.                 else{
  233.                     System.out.println("Please input again.");
  234.                     continue;
  235.                 }
  236.                
  237.                 if(1 <= Integer.parseInt("" + inputY) && Integer.parseInt("" + inputY) <= Integer.parseInt(fieldY)){
  238.                     sy = Integer.parseInt("" + inputY) - 1;
  239.                 }
  240.                 else{
  241.                     System.out.println("Please input again.");
  242.                     continue;
  243.                 }
  244.                
  245.                 break;
  246.             }
  247.            
  248.             openField(sy, sx);
  249.            
  250.             if(mineFlag[sy][sx]){
  251.                 break;
  252.             }
  253.            
  254.             gameover = checkField();
  255.         }
  256.        
  257.         gameFinish(gameover);
  258.      
  259.     }
  260.     void gameFinish(boolean flag){
  261.         printField();
  262.        
  263.         /* output */
  264.         if(flag){
  265.             System.out.println();
  266.             System.out.println("You've finished searching whole field avoiding mine!");
  267.             System.out.println("Game clear!");
  268.         }
  269.         else{
  270.             System.out.println();
  271.             System.out.println("You were blown  by mine.");
  272.             System.out.println("Game over.");
  273.             System.out.println("You only got:"+counter+" score.");
  274.         }
  275.     }
  276. }
  277.  
  278.  
  279.  

1 comment

Yani Fernandez 5 years ago
If anyone concerns about it. email me@ brian25198928@gmail.com
I'm a newbie programmer

Write a comment