Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- class Main {
- static boolean[][] arr;
- static char[][] table;
- static int x = 0;
- static int y = 0;
- static int counter;
- public static void main(String[] args) {
- int ships = 0;
- int tabSize = 0;
- do {
- try {
- ships = Integer.parseInt(JOptionPane.showInputDialog("Quante navi vuoi posizionare?"));
- } catch (NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "Comando errato, inserire solo numeri interi");
- }
- if (ships<1) {
- JOptionPane.showMessageDialog(null, "Dev'esserci almeno una nave");
- }
- } while (ships<1);
- do {
- try {
- tabSize = Integer.parseInt(JOptionPane.showInputDialog("Quanto dev'essere larga/alta la tabella?"));
- } catch (NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "Comando errato, inserire solo numeri interi");
- }
- if (tabSize*tabSize<=ships+1) {
- JOptionPane.showMessageDialog(null, "La tabella deve avere più spazi di quante navi ci siano e lasciare "
- + "almeno uno spazio vuoto.\nHai inserito " +
- tabSize + ", la tabella avrà " + tabSize + "x" + tabSize + " = " + tabSize*tabSize + " spazi, "
- + "non sono abbastanza o non hai lasciato uno spazio vuoto.");
- } else if (tabSize<3) {
- JOptionPane.showMessageDialog(null, "La tabella dev'essere almeno 3x3");
- }
- } while (tabSize*tabSize<=ships+1 || tabSize<3);
- arr = new boolean[tabSize][tabSize];
- table = new char[tabSize][tabSize];
- for (int i=0; i<table.length; i++) {
- for (int j=0; j<table[i].length; j++) {
- table[i][j] = '~';
- }
- }
- for (counter=0; counter<ships; counter++) {
- do {
- x = (int)(Math.random()*tabSize);
- y = (int)(Math.random()*tabSize);
- } while (arr[x][y]);
- arr[x][y] = true;
- }
- do {
- try {
- game();
- } catch (NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "Comando errato, inserire solo numeri da 0 a 4");
- }
- } while (counter>0);
- }
- static void game() {
- do {
- do {
- for (int k=0; k<table.length; k++) {
- System.out.printf("%1s", " " + k);
- }
- System.out.println("");
- for (int i=0; i<table.length; i++) {
- System.out.print(i);
- for (int j=0; j<table[i].length; j++) {
- System.out.printf("%2s", table[i][j]);
- }
- System.out.println("");
- }
- System.out.println("");
- x = Integer.parseInt(JOptionPane.showInputDialog("Inserisci coordinata X da 0 a 4"));
- y = Integer.parseInt(JOptionPane.showInputDialog("Inserisci coordinata Y da 0 a 4"));
- if (x>arr.length-1 || x<0 || y>arr.length-1 || y<0) {
- JOptionPane.showMessageDialog(null, "Coordinate errate.");
- }
- } while (x>arr.length-1 || x<0 || y>arr.length-1 || y<0);
- if (arr[x][y]) {
- arr[x][y] = false;
- table[x][y] = 'x';
- JOptionPane.showMessageDialog(null, "Colpita!");
- counter--;
- } else {
- JOptionPane.showMessageDialog(null, "Acqua!");
- if (table[x][y] != 'x') {
- table[x][y] = 'o';
- }
- }
- } while (counter>0);
- for (int k=0; k<table.length; k++) {
- System.out.printf("%1s", " " + k);
- }
- System.out.println("");
- for (int i=0; i<table.length; i++) {
- System.out.print(i);
- for (int j=0; j<table[i].length; j++) {
- System.out.printf("%2s", table[i][j]);
- }
- System.out.println("");
- }
- System.out.println("");
- JOptionPane.showMessageDialog(null, "Le hai affondate tutte!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement