Advertisement
zsoltizbekk

minesweeper_shell

May 25th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.82 KB | None | 0 0
  1. package shell;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import shell.Shell;
  6. import shell.Command;
  7.  
  8.  
  9. public class myShell extends Shell{
  10.     char[][] matrix = new char[10][10];
  11.    
  12.     int aknak_szama;
  13.     int zaszlok_szama;
  14.     int vege = 0; //0-nem ert veget; 1-gyozelem; 2-vereseg
  15.     Boolean ini = false;
  16.     List<Result> mezo = new ArrayList<>();
  17.     public void matrix_mezok(){
  18.         for (int i = 0; i < 10; i++){
  19.             for (int j = 0; j < 10; j++){
  20.                 matrix[i][j] = 'H'; //felderitetlen mezo
  21.             }
  22.         }
  23.     }
  24.    
  25.     public void kiir(){
  26.         format("Aktualis allapot:\n");
  27.                
  28.                 format("   A B C D E F G H I J\n");
  29.                 for (int i = 0; i < 10; i++){
  30.                     if (i != 9)
  31.                     format(" %d", i+1);
  32.                     else
  33.                     format("%d", i+1);
  34.                     for (int j = 0; j < 10; j++){
  35.                         format(" %c", matrix[i][j]);
  36.                     }
  37.                    
  38.                     format("\n");
  39.                 }
  40.                
  41.                 format("\nAknak szama: %d\n", aknak_szama);
  42.                 format("Zaszlok szama: %d\n", zaszlok_szama);
  43.                 format("Jatek statusza: ");
  44.                 if (vege == 0)
  45.                     format("Nem ert meg veget\n");
  46.                 if (vege == 1)
  47.                     format("Gyozelem\n");
  48.                 if (vege == 2)
  49.                     format("Vereseg\n");
  50.        
  51.     }
  52.    
  53.     @Override
  54.     protected void init(){
  55.         vege = 0;
  56.         ini = true;
  57.         aknak_szama = 10;
  58.         zaszlok_szama = 10;
  59.         matrix_mezok();
  60.     }
  61.    
  62.     public myShell(){
  63.        
  64.         addCommand(new Command("new") {
  65.             @Override
  66.             public boolean execute(String... args) {
  67.                 if (args.length > 1)
  68.                     return false;
  69.                 if (args.length == 0){
  70.                     init();
  71.                 } else {
  72.                     char[] betu = args[0].toCharArray();
  73.                     for (int i = 0; i < args[0].length(); i++)
  74.                     if (!Character.isDigit(betu[i]))
  75.                         return false;
  76.                     if (Integer.parseInt(args[0]) < 1 ||
  77.                             Integer.parseInt(args[0]) > 99)
  78.                         return false;
  79.                     init();
  80.                     aknak_szama = Integer.parseInt(args[0]);
  81.                 }
  82.                
  83.                 kiir();
  84.                     return true;
  85.             }
  86.            
  87.         });
  88.        
  89.         addCommand(new Command("print") {
  90.             @Override
  91.             public boolean execute(String... args) {
  92.                 if (args.length > 0)
  93.                     return false;
  94.                 if (ini == false)
  95.                     return false;
  96.                 kiir();
  97.                 return true;
  98.                            
  99.             }
  100.         });
  101.        
  102.         addCommand(new Command("flag") {
  103.             @Override
  104.             public boolean execute(String... args) {
  105.                 if (args.length != 2)
  106.                     return false;
  107.                 if (ini == false)
  108.                     return false;
  109.                 if (zaszlok_szama == 0)
  110.                     return false;
  111.                 if (vege != 0)
  112.                     return false;
  113.                 if (args[0].length()!=1)
  114.                     return false;
  115.                 int oszlopindex;
  116.                 if (args[0].equals("A"))
  117.                     oszlopindex = 0;
  118.                 else if (args[0].equals("B"))
  119.                     oszlopindex = 1;
  120.                 else if (args[0].equals("C"))
  121.                     oszlopindex = 2;
  122.                 else if (args[0].equals("D"))
  123.                     oszlopindex = 3;
  124.                 else if (args[0].equals("E"))
  125.                     oszlopindex = 4;
  126.                 else if (args[0].equals("F"))
  127.                     oszlopindex = 5;
  128.                 else if (args[0].equals("G"))
  129.                     oszlopindex = 6;
  130.                 else if (args[0].equals("H"))
  131.                     oszlopindex = 7;
  132.                 else if (args[0].equals("I"))
  133.                     oszlopindex = 8;
  134.                 else if (args[0].equals("J"))
  135.                     oszlopindex = 9;
  136.                 else
  137.                     return false;
  138.                 char[] sor = args[1].toCharArray();
  139.                 for (int i = 0; i < args[1].length(); i++){
  140.                     if (!Character.isDigit(sor[i]))
  141.                         return false;
  142.                 }
  143.                 int sorindex = Integer.parseInt(args[1]);
  144.                 if (sorindex < 1 || sorindex > 10)
  145.                     return false;
  146.                 if (matrix[sorindex-1][oszlopindex] == 'F')
  147.                     return false;
  148.                 matrix[sorindex-1][oszlopindex] = 'F'; //zaszloval megjelolt mezo
  149.                 zaszlok_szama--;
  150.                
  151.                 return true;
  152.             }
  153.         });
  154.        
  155.         addCommand(new Command("unflag") {
  156.             @Override
  157.             public boolean execute(String... args) {
  158.                 if (args.length != 2)
  159.                     return false;
  160.                 if (ini == false)
  161.                     return false;
  162.                 if (zaszlok_szama == aknak_szama)
  163.                     return false;
  164.                 int oszlopindex;
  165.                 if (args[0].equals("A"))
  166.                     oszlopindex = 0;
  167.                 else if (args[0].equals("B"))
  168.                     oszlopindex = 1;
  169.                 else if (args[0].equals("C"))
  170.                     oszlopindex = 2;
  171.                 else if (args[0].equals("D"))
  172.                     oszlopindex = 3;
  173.                 else if (args[0].equals("E"))
  174.                     oszlopindex = 4;
  175.                 else if (args[0].equals("F"))
  176.                     oszlopindex = 5;
  177.                 else if (args[0].equals("G"))
  178.                     oszlopindex = 6;
  179.                 else if (args[0].equals("H"))
  180.                     oszlopindex = 7;
  181.                 else if (args[0].equals("I"))
  182.                     oszlopindex = 8;
  183.                 else if (args[0].equals("J"))
  184.                     oszlopindex = 9;
  185.                 else
  186.                     return false;
  187.                 char[] sor = args[1].toCharArray();
  188.                 for (int i = 0; i < args[1].length(); i++){
  189.                     if (!Character.isDigit(sor[i]))
  190.                         return false;
  191.                 }
  192.                 int sorindex = Integer.parseInt(args[1]);
  193.                 if (sorindex < 1 || sorindex > 10)
  194.                     return false;
  195.                 if (matrix[sorindex-1][oszlopindex] != 'F')
  196.                     return false;
  197.                 matrix[sorindex-1][oszlopindex] = 'H';
  198.                 zaszlok_szama++;
  199.                
  200.                 return true;
  201.                
  202.             }
  203.         });
  204.        
  205.         addCommand(new Command("fire") {
  206.             @Override
  207.             public boolean execute(String... args) {
  208.                 if (args.length != 2)
  209.                     return false;
  210.                 if (ini == false)
  211.                     return false;
  212.                 if (vege != 0)
  213.                     return false;
  214.                 int oszlopindex;
  215.                 if (args[0].equals("A"))
  216.                     oszlopindex = 0;
  217.                 else if (args[0].equals("B"))
  218.                     oszlopindex = 1;
  219.                 else if (args[0].equals("C"))
  220.                     oszlopindex = 2;
  221.                 else if (args[0].equals("D"))
  222.                     oszlopindex = 3;
  223.                 else if (args[0].equals("E"))
  224.                     oszlopindex = 4;
  225.                 else if (args[0].equals("F"))
  226.                     oszlopindex = 5;
  227.                 else if (args[0].equals("G"))
  228.                     oszlopindex = 6;
  229.                 else if (args[0].equals("H"))
  230.                     oszlopindex = 7;
  231.                 else if (args[0].equals("I"))
  232.                     oszlopindex = 8;
  233.                 else if (args[0].equals("J"))
  234.                     oszlopindex = 9;
  235.                 else
  236.                     return false;
  237.                 char[] sor = args[1].toCharArray();
  238.                 for (int i = 0; i < args[1].length(); i++){
  239.                     if (!Character.isDigit(sor[i]))
  240.                         return false;
  241.                 }
  242.                 int sorindex = Integer.parseInt(args[1]);
  243.                 if (sorindex < 1 || sorindex > 10)
  244.                     return false;
  245.                
  246.                 if (matrix[sorindex-1][oszlopindex] == 'F' || matrix[sorindex-1][oszlopindex] != 'H')
  247.                     return false;
  248.                
  249.                 mezo = resultOfShot(sorindex-1, oszlopindex);
  250.                 if (mezo.size() == 1){
  251.                     matrix[sorindex-1][oszlopindex] = 'X';
  252.                     vege = 2;
  253.                     return true;
  254.                 }
  255.                 for (int i = 0; i < mezo.size(); i++){
  256.                     matrix[mezo.get(i).getRow()][mezo.get(i).getColumn()] = Integer.toString(mezo.get(i).getValue()).charAt(0);
  257.                 }
  258.                 kiir();
  259.                 return true;
  260.             }
  261.         });
  262.        
  263.         addCommand(new Command("solution") {
  264.             @Override
  265.             public boolean execute(String... args) {
  266.                 if (args.length > 0)
  267.                     return false;
  268.                 if (ini == false)
  269.                     return false;
  270.                 if (vege == 0)
  271.                     return false;
  272.                 if (vege == 1 || vege == 2){
  273.                 String game_board = solution();
  274.                 System.out.println(game_board);
  275.                 return true;
  276.                 }
  277.                 else return true;
  278.             }
  279.         });
  280.        
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement