Hallo, ich soll das Spiel Schiffe versenken programmieren, komme aber nicht weiter. Ich muss einstellen, dass ich nur eine beliebige Anzahl an Schiffen positionieren kann, dass die Schiffe nicht übers Spielfeldrand kommen und dass Schiffe nicht aufeinander platzierbar sind. Das bekomme ich irgendwie nicht hin.
Einige Ansätze habe ich schon (als Kommentar), diese haben aber nicht funktioniert.
Einige Ansätze habe ich schon (als Kommentar), diese haben aber nicht funktioniert.
Java:
import java.util.Scanner;
public class Schiff11 {
private static Scanner s = new Scanner(System.in);
public static final int SCHLACHT = 5;
public static final int KREUZER = 4;
public static final int ZERSTOERER = 3;
public static final int UBOOT = 2;
public static void main (String[] args) {
while(true) {
print(player);
spielfeld();
print(gegner);
}
}
static char[][] player = {
{'.','.','.','.','.','.','.','.','.','.'}, //0. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //1. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //2. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //3. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //4. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //5. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //6. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //7. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //8. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //9. Reihe
};
static char [][] gegner = {
{'.','.','.','.','.','.','.','.','.','.'}, //0. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //1. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //2. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //3. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //4. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //5. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //6. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //7. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //8. Reihe
{'.','.','.','.','.','.','.','.','.','.'}, //9. Reihe
};
public static void print (char[][] grid) {
System.out.println("\n 0 1 2 3 4 5 6 7 8 9");
char [] buchstaben = new char[10];
char a = 65;
for (int i=0; i < buchstaben.length; i++, a++) {
buchstaben[i] = a;
System.out.print(buchstaben[i] + " ");
for (int j=0; j < 10; j++) {
System.out.print(grid [i][j]+" ");
}
System.out.println();
}
}
public static int [] ShipLength = {2,2,2,2,3,3,3,4,4,5};
public static void spielfeld() {
System.out.println("\n2 3 4 oder 5");
int schiff = s.nextInt();
int schiff1;
schiff1 = 0;
int counter5, counter4, counter3, counter2;
counter5=1;
if (schiff == 5)
schiff1 = SCHLACHT;
if (schiff == 4)
schiff1 = KREUZER;
if (schiff == 3)
schiff1 = ZERSTOERER;
if (schiff == 2)
schiff1 = UBOOT;
if (schiff > 5 || schiff < 2){
System.out.println("Bitte nur 5, 4, 3, oder 2 eingeben");
spielfeld();
} else { System.out.println("Zeile eingeben");
String zeile = s.next();
int x = zeile.charAt(0);
if (x < 65 || x > 75) {
System.out.println("Nur Buchstaben von A bis J eingeben");
spielfeld();
}
System.out.println("Spalte eingeben");
int y = s.nextInt();
if (y < 0 || y > 9) {
System.out.println("Nur Zahlen von 0 - 9");
spielfeld();
}
System.out.println("1:links, 2:rechts, 3:unten, 4:oben");
int richtung = s.nextInt();
if (richtung < 1 || richtung > 4) {
System.out.println("Falsche Eingabe. Bitte erneut versuchen");
spielfeld();
}
if (richtung == 1) {
for (int l=y - schiff1 + 1; l < y + 1; l++) {
// if (y - schiff1 + 1 < 0 || y + 1 > 9) {
// System.out.println("Schiff geht ueber Spielfeldrand.");
// spielfeld();
// }
player[x-65][l] = '#';
// if (player[x-65][l] != '.') {
// System.out.println("\n Schiff ist hier bereits vorhanden. Bitte wo anders positionieren");
// spielfeld();
// }
}
}
if (richtung == 2) {
for (int r=y ; r < y + schiff1; r++) {
if ( y < 0 || y + schiff1 > 9) {
System.out.println("Schiff geht ueber Spielfeldrand.");
spielfeld();
}
player[x-65][r] = '#';
// if (player[x-65][r] == '#') {
// System.out.println("\n Schiff ist hier bereits vorhanden. Bitte wo anders positionieren");
// }
}
}
if (richtung == 3) {
for (int u=x-65; u < x-65 + schiff1; u++) {
if ( x-65 < 0 || x-65 + schiff1 > 9) {
System.out.println("Schiff geht ueber Spielfeldrand.");
spielfeld();
}
player[u][y] = '#';
// if (player[u][y] == '#') {
// System.out.println("\n Schiff ist hier bereits vorhanden. Bitte wo anders positionieren");
// }
}
}
if (richtung == 4) {
for (int o=x-65 - schiff1 + 1 ; o < x-65 + 1 ; o++) {
if ( x-65 - schiff1 +1 < 0 || x-65 + 1 > 9) {
System.out.println("Schiff geht ueber Spielfeldrand.");
spielfeld();
}
player[o][y] = '#';
// if (player[o][y] == '#') {
// System.out.println("\n Schiff ist hier bereits vorhanden. Bitte wo anders positionieren");
// }
}
}
}
}
}