void main() { //déclaration de variables int n; int compteur; int x; int y; int A; int t1; int t2; int t3; int t4; int x1; int y1; int x2; int y2; int x3; int y3; int x4; int y4; int s1; int s2; int s3; int s4; int reponse; int compteurcroiseur; int compteursousmarin; int compteurtorpilleur; int compteurtorpilleur1; //Affectation de variables n = 0; compteur = 0; x = 0; y = 0; x1=0; y1=0; compteurcroiseur = 0; compteursousmarin = 0; compteurtorpilleur = 0; compteurtorpilleur1 = 0; //début de jeu println("Voulez-vous jouer au jeu de la bataille navale ? (oui=1 , non=2 )"); reponse = readInteger(); while (reponse ==1){ //Règle du jeu println("Le jeu de la bataille navale"); println("Les règles du jeu sont simples : "); println("Vous devez couler les quatre bateaux de l'ennemi"); println("Sachant que la flotte ennemie est composée de: "); println("un croiseur qui occupe quatre cases"); println("un sous-marin qui occupe trois cases"); println("deux torpilleurs qui occupent deux cases"); println("Les bateaux peuvent être placer en position horizontale et verticale"); // Déclaration et affectation du tableaux int tab[][] = new int[9][9]; for (int i= 1; i<=8; i=i+1) { for (int j=1; j<= 8; j=j+1) { tab[i][j] = 0; } } //Affectation aléatoire de l'emplacement des bateaux t1 = 4; s1 = random(1, 2); t2 = 3; s2 = random(1, 2); t3 = 2; s3 = random(1, 2); t4 = 2; s4 = random(1, 2); //Croisseur if (s1 == 1) { x1 = random(1,(8 - t1)); y1 = random(1, 8); for (A = 0; A<=(t1 - 1); A=A+1) { tab [x1+A][y1] = 5; } } else { x1 = random(1, 8); y1 = random(1,(8 - t1)); for (A = 0; A<=(t1 - 1); A=A+1) { tab [x1][y1+A] = 5; } } y2=y1; x2=x1; //Sous-marin if (s2 == 1) { x2 = random(1, (8 - t2)); y2 = random(1, 8); for (A = 0; A<=(t2 - 1); A=A+1){ if(tab [x1+A][y1] == tab [x2+A][y2]){ x2 = random(1, (8 - t2)); y2 = random(1, 8); }else{tab [x2+A][y2] = 6; } } }else { x2 = random(1, 8); y2 = random(1, (8 - t2)); for (A = 0; A<=(t2 - 1); A=A+1) { if(tab [x1][y1+A] == tab [x2][y2+A]){ x2 = random(1, 8); y2 = random(1, (8 - t2)); }else{ tab [x2][y2+A]= 6; } } } x3=x1; y3=y1; //Torpilleur if (s3 == 1) { x3 = random(1, (8 - t3)); y3 = random(1, 8); for (A = 0; A<=(t3 - 1); A=A+1) { if((x1+A)==(x3+A) && y1==y3 || (x2+A)==(x3+A) && y2==y3){ x3 = random(1, (8 - t3)); y3 = random(1, 8); }else{tab [x3+A][y3] = 7; } } } else { x3 = random(1, 8); y3 = random(1, (10 - t3)); for (A = 0; A<=(t3 - 1); A=A+1) { if(x1==x3 && (y1+A)==(y3+A) || x2==x3 && (y2+A)==(y3+A) ){ }else{tab [x3][y3+A] = 7; } } } x4=x1; y4=y1; if (s4 == 1) { y4 = random(1, 8); x4 = random(1, (10 - t4)); for (A = 0; A<=(t4 - 1); A=A+1) { if((x1+A)==(x4+A) && y1==y4 ||(x2+A)==(x4+A) && y2==y4 || (x3+A)==(x4+A) && y3==y4){ }else{tab [x4+A][y4] = 8; } } } else { x4 = random(1, 8); y4 = random(1, (10 - t4)); for (A = 0; A<=(t4 - 1); A=A+1) { if(x1==x4 && (y1+A)==(y4+A) || x2==x4 && (y2+A)==(y4+A) || x3==x4 && (y3+A)==(y4+A)){ }else{tab [x4][y4+A] = 8; } } } println("Les bateaux ennemis sont placés, à vous de jouer"); //Coordonnée du tir while (compteur < (t1 + t2 + t3 + t4)) { while (x < 1 || x > 8) { println("Donnez un nombre entier entre 1 et 8 pour l'abscisse d'une case"); x = readInteger("Donnez un nombre entier entre 1 et 8 pour l'abscisse d'une case"); println(x); } while (y < 1 || y > 8) { println("Donnez un nombre entier entre 1 et 8 pour l'ordonnée d'une case"); y = readInteger("Donnez un nombre entier entre 1 et 8 pour l'ordonnée d'une case"); println(y); } //Ou est tombé le tir clear(); n = n + 1; if (tab[x][y] == 5 || tab[x][y] == 6 || tab[x][y] == 7 || tab[x][y] == 8) { println("Vous avez déjà tiré à cet endroit !"); } if (tab[x][y] == 1) { println("Bien tiré, vous avez touché un croiseur !"); compteur = compteur + 1; tab[x][y] = 5; compteurcroiseur = compteurcroiseur +1; if (compteurcroiseur == t1){ println("vous avez coulé le croiseur"); } }if (tab[x][y] == 2) { println("Bien tiré, vous avez touché un sous-marin !"); compteur = compteur + 1; tab[x][y] = 6; compteursousmarin = compteursousmarin + 1; if (compteursousmarin == t2){ println("vous avez coulé le sous-marin"); } }if (tab[x][y] == 3) { println("Bien tiré, vous avez touché un torpilleur!"); compteur = compteur + 1; tab[x][y] = 7; compteurtorpilleur = compteurtorpilleur + 1; if (compteurtorpilleur == t3){ println("vous avez coulé l'un des torpilleurs"); } }if (tab[x][y] == 4) { println("Bien tiré, vous avez touché un torpilleur!"); compteur = compteur + 1; tab[x][y] = 8; compteurtorpilleur1 = compteurtorpilleur1 + 1; if (compteurtorpilleur1 == t4){ println("vous avez coulé l'un des torpilleurs"); } }if (tab[x][y] == 0 || tab[x][y] == 9) { println("Dommage vous avez tiré dans l'eau !"); tab[x][y] = 9; } // Afficher grille avec les coups tirés for (int j = 1; j <= 8; j = j + 1) { for (int i = 1; i <= 8; i = i + 1) { if (tab[i][j] == 5) { print("C"); }if (tab[i][j] == 1 || tab[i][j] == 2 || tab[i][j] == 3 || tab[i][j] == 4) { print("o"); }if (tab[i][j] == 9) { print("~"); }if (tab[i][j] == 0) { print("o"); }if (tab[i][j] == 6){ print("S"); }if(tab[i][j] == 7 || tab[i][j] == 8){ print("T"); } } println(" "); } x = 0; y = 0; } clear(); // Grille finale for (int i = 1; i <= 8; i = i + 1) { for (int j = 1; j <= 8; j = j + 1) { if (tab[i][j] == 5) { print("C"); }if (tab[i][j] == 1 || tab[i][j] == 2 || tab[i][j] == 3 || tab[i][j] == 4) { print("o"); }if (tab[i][j] == 9) { print("~"); }if (tab[i][j] == 0) { print("o"); }if (tab[i][j] == 6){ print("S"); }if(tab[i][j] == 7 || tab[i][j] == 8){ print("T"); } } println(" "); } //Fin du jeu println("Gagné ! Vous avez coulé toute la flotte adverse"); println("Vous avez effectué " + n + " coups pour cela"); println (" Voulez vous jouer ? 1 = oui , 2 = non "); reponse = readInteger(); } }