Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Schiffeversenken {
- static int[][] wasser = new int[20][20];
- public static void main(String[] args) {
- wasser = setshiponwater(2, 4, wasser, 10);
- wasser = setshiponwater(3, 3, wasser, 11);
- wasser = setshiponwater(4, 2, wasser, 12);
- wasser = setshiponwater(5, 1, wasser, 13);
- for (int i = 0; i < wasser.length; i++) {
- IO.writeLn();
- for (int j = 0; j < wasser[0].length; j++) {
- IO.write(wasser[i][j], 3);
- }
- }
- IO.writeLn();
- IO.writeLn();
- IO.writeLn("Wieviel Munition fuehren sie mit sich?");
- int treffer = 30;
- int maxschuesse = IO.readCorrectInt();
- int schuesse = 0;
- int a1 = 0;
- int a2 = 0;
- int a3;
- int c = 1;
- int c1 = 1;
- int c2 = 1;
- int c3 = 1;
- int statm = 0;
- int statm1 = 0;
- int statm2 = 0;
- int statm3 = 0;
- while (schuesse <= maxschuesse) {
- IO.writeLn();
- a1 = IO.readCorrectInt() - 1;
- if (a1 > 19 && a1 >= 0) {
- IO
- .writeLn("Sie haben verlohren wegen eines Verstosses gegen Java");
- break;
- }
- IO.flush();
- a2 = IO.readCorrectInt() - 1;
- if (a1 > 19 && a1 >= 0) {
- IO
- .writeLn("Sie haben verlohren wegen eines Verstosses gegen Java");
- break;
- }
- IO.flush();
- a3 = checkiftreffer(a1, a2);
- switch (a3) {
- case 0:
- IO.writeLn();
- schuesse++;
- IO.writeLn("HIT WATER...");
- wasser[a1][a2] = 3;
- break;
- case 1:
- IO.writeLn();
- IO.writeLn("HIT SHIP...");
- schuesse++;
- treffer--;
- wasser[a1][a2] = 2;
- break;
- case 2:
- IO.writeLn();
- schuesse++;
- IO.writeLn("Already shot on the ship at this field");
- break;
- case 3:
- IO.writeLn();
- schuesse++;
- IO.writeLn("Already shot into the water at this place");
- break;
- case 10:
- IO.writeLn();
- schuesse++;
- treffer--;
- wasser[a1][a2] = 2;
- if (c < 2) {
- c++;
- IO.writeLn("HIT SHIP...");
- } else {
- c = 0;
- IO.writeLn("Ship with length 2 sunk.");
- statm++;
- }
- break;
- case 11:
- IO.writeLn();
- schuesse++;
- treffer--;
- wasser[a1][a2] = 2;
- if (c1 < 3) {
- c1++;
- IO.writeLn();
- IO.writeLn("HIT SHIP...");
- } else {
- c1 = 0;
- IO.writeLn("Ship with length 3 sunk.");
- statm1++;
- }
- break;
- case 12:
- IO.writeLn();
- schuesse++;
- treffer--;
- wasser[a1][a2] = 2;
- if (c2 < 4) {
- c2++;
- IO.writeLn();
- IO.writeLn("HIT SHIP...");
- } else {
- c2 = 0;
- IO.writeLn("Ship with length 4 sunk.");
- statm2++;
- }
- break;
- case 13:
- IO.writeLn();
- schuesse++;
- treffer--;
- wasser[a1][a2] = 2;
- if (c3 < 5) {
- c3++;
- IO.writeLn();
- IO.writeLn("HIT SHIP...");
- } else {
- c3 = 0;
- IO.writeLn("Ship with length 5 sunk.");
- statm3++;
- }
- break;
- }
- if (treffer == 0) {
- IO.writeLn("Fleet has sunk. The player needed " + schuesse
- + " shots.");
- break;
- }
- }
- IO.write(" ", 3);
- for (char b = 65; b < 85; b++) {
- IO.write(" " + b, 3);
- }
- for (int i = 1; i < wasser.length + 1; i++) {
- IO.writeLn();
- IO.write(i, 3);
- for (int j = 0; j < wasser[0].length; j++) {
- int abc = wasser[i - 1][j];
- if (abc == 0) {
- IO.write(" -", 3);
- } else {
- if (abc == 3) {
- IO.write(" W", 3);
- } else {
- if (abc == 2) {
- IO.write(" S", 3);
- } else {
- IO.write(" -", 3);
- }
- }
- }
- }
- }
- IO.writeLn();
- IO.writeLn("Game Statistic:");
- int n = 4 - statm;
- int n1 = 3 - statm1;
- int n2 = 2 - statm2;
- int n3 = 1 - statm3;
- IO.writeLn("Ships with len 2 (4): " + statm + " sunk, " + n
- + " remaining");
- IO.writeLn("Ships with len 3 (3): " + statm1 + " sunk, " + n1
- + " remaining");
- IO.writeLn("Ships with len 4 (2): " + statm2 + " sunk, " + n2
- + " remaining");
- IO.writeLn("Ships with len 5 (1): " + statm3 + " sunk, " + n3
- + " remaining");
- IO.writeLn(" Shots: " + schuesse);
- }
- static int checkiftreffer(int a1, int a2) {
- int a3 = wasser[a1][a2];
- return a3;
- }
- static int[][] setshiponwater(int a1, int a2, int[][] wasser, int q) {
- int i = 0;
- while (i < a2) {
- int dirrandi = (int) (Math.random() * 4);
- int xrandi = (int) (Math.random() * 19);
- int yrandi = (int) (Math.random() * 19);
- i++;
- for (int j = 0; j < a1; j++) {
- switch (dirrandi) {
- case 1:
- if ((yrandi + a1) > 19
- && checkship(wasser, yrandi, xrandi, a1, 1, 0) == 0) {
- yrandi = (int) (Math.random() * 19);
- j = 0;
- }
- wasser[xrandi][yrandi] = q;
- yrandi++;
- break;
- case 2:
- if ((xrandi + a1) > 19
- && checkship(wasser, yrandi, xrandi, a1, 2, 0) == 0) {
- xrandi = (int) (Math.random() * 19);
- j = 0;
- }
- wasser[xrandi][yrandi] = q;
- xrandi++;
- break;
- case 3:
- if ((yrandi - a1 < 0)
- && checkship(wasser, yrandi, xrandi, a1, 3, 0) == 0) {
- yrandi = (int) (Math.random() * 19);
- j = 0;
- }
- wasser[xrandi][yrandi] = q;
- yrandi--;
- break;
- case 4:
- if ((xrandi - a1 < 0)
- && checkship(wasser, yrandi, xrandi, a1, 4, 0) == 0) {
- xrandi = (int) (Math.random() * 20);
- j = 0;
- }
- wasser[xrandi][yrandi] = q;
- xrandi--;
- }
- }
- }
- return wasser;
- }
- static int checkship(int[][] wasser, int yrandi, int xrandi, int a1, int b,
- int k) {
- int yrandi1 = yrandi;
- int xrandi1 = xrandi;
- switch (b) {
- case 1:
- if (yrandi1 + a1 < 20) {
- for (int j = 0; j < a1; j++) {
- if (wasser[xrandi1][yrandi1] == 0) {
- yrandi1++;
- k = 0;
- } else {
- k = 1;
- break;
- }
- }
- } else {
- k = 1;
- }
- break;
- case 2:
- if (xrandi1 + a1 < 20) {
- for (int j = 0; j < a1; j++) {
- if (wasser[xrandi1][yrandi1] == 0) {
- xrandi1++;
- k = 0;
- } else {
- k = 1;
- break;
- }
- }
- } else {
- k = 1;
- }
- break;
- case 3:
- if (yrandi1 - a1 < 0) {
- for (int j = 0; j < a1; j++) {
- if (wasser[xrandi1][yrandi1] == 0) {
- xrandi1--;
- k = 0;
- } else {
- k = 1;
- break;
- }
- }
- } else {
- k = 1;
- }
- break;
- case 4:
- if (xrandi1 - a1 < 0) {
- for (int j = 0; j < a1; j++) {
- if (wasser[xrandi1][yrandi1] == 0) {
- xrandi1--;
- k = 0;
- } else {
- k = 1;
- break;
- }
- }
- } else {
- k = 1;
- }
- }
- if (k == 0) {
- return 0;
- } else {
- return 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment