Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. class Main {
  4.       static boolean[][] arr;
  5.       static char[][] table;
  6.       static int x = 0;
  7.       static int y = 0;
  8.       static int counter;
  9.      
  10.   public static void main(String[] args) {
  11.      
  12.       int ships = 0;
  13.       int tabSize = 0;
  14.      
  15.       do {
  16.           try {
  17.           ships = Integer.parseInt(JOptionPane.showInputDialog("Quante navi vuoi posizionare?"));
  18.       } catch (NumberFormatException e) {
  19.           JOptionPane.showMessageDialog(null, "Comando errato, inserire solo numeri interi");
  20.       }
  21.           if (ships<1) {
  22.               JOptionPane.showMessageDialog(null, "Dev'esserci almeno una nave");
  23.           }
  24.       } while (ships<1);
  25.      
  26.       do {
  27.           try {
  28.           tabSize = Integer.parseInt(JOptionPane.showInputDialog("Quanto dev'essere larga/alta la tabella?"));
  29.       } catch (NumberFormatException e) {
  30.           JOptionPane.showMessageDialog(null, "Comando errato, inserire solo numeri interi");
  31.       }
  32.           if (tabSize*tabSize<=ships+1) {
  33.               JOptionPane.showMessageDialog(null, "La tabella deve avere più spazi di quante navi ci siano e lasciare "
  34.                       + "almeno uno spazio vuoto.\nHai inserito " +
  35.                       tabSize + ", la tabella avrà " + tabSize + "x" + tabSize + " = " + tabSize*tabSize + " spazi, "
  36.                               + "non sono abbastanza o non hai lasciato uno spazio vuoto.");
  37.           } else if (tabSize<3) {
  38.               JOptionPane.showMessageDialog(null, "La tabella dev'essere almeno 3x3");
  39.           }
  40.       } while (tabSize*tabSize<=ships+1 || tabSize<3);
  41.      
  42.      
  43.       arr = new boolean[tabSize][tabSize];
  44.       table = new char[tabSize][tabSize];
  45.      
  46.       for (int i=0; i<table.length; i++) {
  47.           for (int j=0; j<table[i].length; j++) {
  48.               table[i][j] = '~';
  49.           }
  50.       }
  51.      
  52.       for (counter=0; counter<ships; counter++) {
  53.           do {
  54.               x = (int)(Math.random()*tabSize);
  55.               y = (int)(Math.random()*tabSize);
  56.           } while (arr[x][y]);
  57.           arr[x][y] = true;
  58.       }
  59.      
  60.       do {
  61.           try {
  62.           game();
  63.       } catch (NumberFormatException e) {
  64.           JOptionPane.showMessageDialog(null, "Comando errato, inserire solo numeri da 0 a 4");
  65.       }
  66.       } while (counter>0);
  67.      
  68.   }
  69.  
  70.   static void game() {
  71.       do {
  72.           do {
  73.               for (int k=0; k<table.length; k++) {
  74.                   System.out.printf("%1s", " " + k);
  75.               }
  76.                   System.out.println("");
  77.               for (int i=0; i<table.length; i++) {
  78.                   System.out.print(i);
  79.                  
  80.                  for (int j=0; j<table[i].length; j++) {
  81.                       System.out.printf("%2s", table[i][j]);
  82.                   }
  83.                   System.out.println("");
  84.               }
  85.               System.out.println("");
  86.              
  87.               x = Integer.parseInt(JOptionPane.showInputDialog("Inserisci coordinata X da 0 a 4"));
  88.               y = Integer.parseInt(JOptionPane.showInputDialog("Inserisci coordinata Y da 0 a 4"));
  89.               if (x>arr.length-1 || x<0 || y>arr.length-1 || y<0) {
  90.                   JOptionPane.showMessageDialog(null, "Coordinate errate.");
  91.               }
  92.           } while (x>arr.length-1 || x<0 || y>arr.length-1 || y<0);
  93.          
  94.          
  95.           if (arr[x][y]) {
  96.               arr[x][y] = false;
  97.               table[x][y] = 'x';
  98.               JOptionPane.showMessageDialog(null, "Colpita!");
  99.               counter--;
  100.           } else {
  101.               JOptionPane.showMessageDialog(null, "Acqua!");
  102.               if (table[x][y] != 'x') {
  103.                   table[x][y] = 'o';
  104.               }
  105.           }
  106.       } while (counter>0);
  107.      
  108.       for (int k=0; k<table.length; k++) {
  109.                   System.out.printf("%1s", " " + k);
  110.               }
  111.               System.out.println("");
  112.               for (int i=0; i<table.length; i++) {
  113.                   System.out.print(i);
  114.                  
  115.                  for (int j=0; j<table[i].length; j++) {
  116.                       System.out.printf("%2s", table[i][j]);
  117.                   }
  118.                   System.out.println("");
  119.               }
  120.               System.out.println("");
  121.      
  122.       JOptionPane.showMessageDialog(null, "Le hai affondate tutte!");
  123.   }
  124.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement