Moin,
ich habe mich gerade eben hier registriert, weil ich hoffe, kompetente Antworten zu erhalten.
Ich bin seit einem Monat Informatik-Student und habe noch sehr wenig Erfahrung mit Java. Bisher kam ich nur mit C++ in Kontakt, aber eigentlich auch nur die Basics.
Wie dem auch sei. Ich habe nun eine Hausaufgabe aufbekommen, ein Schiffe versenken für die Konsole zu programmieren. Aktuell bin ich soweit, dass die Schiffe platziert werden. Doch mir kommt der ganze Code viel zu lang vor, nur damit man Schiffe setzt.
Könnt ihr mir Tipps geben, wie ich vllt das eine oder andere optimieren könnte?
Hier der Code:
ich habe mich gerade eben hier registriert, weil ich hoffe, kompetente Antworten zu erhalten.
Ich bin seit einem Monat Informatik-Student und habe noch sehr wenig Erfahrung mit Java. Bisher kam ich nur mit C++ in Kontakt, aber eigentlich auch nur die Basics.
Wie dem auch sei. Ich habe nun eine Hausaufgabe aufbekommen, ein Schiffe versenken für die Konsole zu programmieren. Aktuell bin ich soweit, dass die Schiffe platziert werden. Doch mir kommt der ganze Code viel zu lang vor, nur damit man Schiffe setzt.
Könnt ihr mir Tipps geben, wie ich vllt das eine oder andere optimieren könnte?
Hier der Code:
Java:
/**
* Created by --- on 22.11.2015.
*/
import java.util.Scanner;
public class Ha {
public static void main(String[] args) {
int koordy1=0;
int koordx1=0;
int koordy2=0;
int koordx2=0;
int koordy3=0;
int koordx3=0;
int koordy4=0;
int koordx4=0;
int koordy5=0;
int koordx5=0;
String ausrichtung;
Scanner s = new Scanner(System.in);
System.out.println("Dies ist dein Spielfeld. Setze nun durch Angabe von Koordinaten deine Schiffe."); // Begrüßung
print(test); // Ausgabe Spielfeld
for(int uboote=4; uboote != 0; uboote--){ // Setzen von U-Booten
System.out.println("Du hast noch " + uboote + " Uboote. Setze das naechste ein!");
System.out.println("Koord Y: ");
koordy1=s.nextInt();
System.out.println("Koord X: ");
koordx1 = s.nextInt();
System.out.println("Ausrichtung (l)inks,(r)echts,(h)och, r(u)nter: "); //Nur eine Koordinate, danach Setzen durch Angabe der Ausrichtung
ausrichtung = s.next();
if(ausrichtung.equals("l")){
koordy2 = koordy1;
koordx2 = koordx1-1;
}
else if(ausrichtung.equals("r")){
koordy2 = koordy1;
koordx2 = koordx1+1;
}
else if(ausrichtung.equals("h")){
koordy2 = koordy1-1;
koordx2 = koordx1;
}
else if(ausrichtung.equals("u")){
koordy2 = koordy1+1;
koordx2 = koordx1;
}
test[koordy1][koordx1] = '#'; // Eintragen der Schiffe ins Spielfeld
test[koordy2][koordx2] = '#';
}
for(int zerstoerer=3; zerstoerer != 0; zerstoerer--) { // Setzen von Zerstörern
System.out.println("Du hast noch " + zerstoerer + " Zerstoerer. Setze den Naechsten ein!");
System.out.println("Koord Y: ");
koordy1 = s.nextInt();
System.out.println("Koord X: ");
koordx1 = s.nextInt();
System.out.println("Ausrichtung (l)inks,(r)echts,(h)och, r(u)nter: ");
ausrichtung = s.next();
if (ausrichtung.equals("l")) {
koordy2 = koordy1;
koordx2 = koordx1 - 1;
koordy3 = koordy2;
koordx3 = koordx2 - 1;
} else if (ausrichtung.equals("r")) {
koordy2 = koordy1;
koordx2 = koordx1 + 1;
koordy3 = koordy2;
koordx3 = koordx2 + 1;
} else if (ausrichtung.equals("h")) {
koordy2 = koordy1 - 1;
koordx2 = koordx1;
koordy3 = koordy2 - 1;
koordx3 = koordx2;
} else if (ausrichtung.equals("u")) {
koordy2 = koordy1 + 1;
koordx2 = koordx1;
koordy3 = koordy2 + 1;
koordx3 = koordx2;
}
test[koordy1][koordx1] = '#';
test[koordy2][koordx2] = '#';
test[koordy3][koordx3] = '#';
}
for(int kreuzer=2; kreuzer != 0; kreuzer--){ // Setzen von Kreuzern
System.out.println("Du hast noch " + kreuzer + " Kreuzer. Setze den Naechsten ein!");
System.out.println("Koord Y: ");
koordy1=s.nextInt();
System.out.println("Koord X: ");
koordx1 = s.nextInt();
System.out.println("Ausrichtung (l)inks,(r)echts,(h)och, r(u)nter: ");
ausrichtung = s.next();
if(ausrichtung.equals("l")){
koordy2 = koordy1;
koordx2 = koordx1-1;
koordy3 = koordy2;
koordx3 = koordx2-1;
koordy4 = koordy3;
koordx4 = koordx3-1;
}
else if(ausrichtung.equals("r")){
koordy2 = koordy1;
koordx2 = koordx1+1;
koordy3 = koordy2;
koordx3 = koordx2+1;
koordy4 = koordy3;
koordx4 = koordx3+1;
}
else if(ausrichtung.equals("h")){
koordy2 = koordy1-1;
koordx2 = koordx1;
koordy3 = koordy2-1;
koordx3 = koordx2;
koordy4 = koordy3-1;
koordx4 = koordx3;
}
else if(ausrichtung.equals("u")){
koordy2 = koordy1+1;
koordx2 = koordx1;
koordy3 = koordy2+1;
koordx3 = koordx2;
koordy4 = koordy3+1;
koordx4 = koordx3;
}
test[koordy1][koordx1] = '#';
test[koordy2][koordx2] = '#';
test[koordy3][koordx3] = '#';
test[koordy4][koordx4] = '#';
}
for(int schlacht=1; schlacht != 0; schlacht--){ // Setzen von Schlachtschiff
System.out.println("Du hast noch " + schlacht + " Schlachtschiff. Setze das Naechste ein!");
System.out.println("Koord Y: ");
koordy1=s.nextInt();
System.out.println("Koord X: ");
koordx1 = s.nextInt();
System.out.println("Ausrichtung (l)inks,(r)echts,(h)och, r(u)nter: ");
ausrichtung = s.next();
if(ausrichtung.equals("l")){
koordy2 = koordy1;
koordx2 = koordx1-1;
koordy3 = koordy2;
koordx3 = koordx2-1;
koordy4 = koordy3;
koordx4 = koordx3-1;
koordy5 = koordy4;
koordx5 = koordx4-1;
}
else if(ausrichtung.equals("r")){
koordy2 = koordy1;
koordx2 = koordx1+1;
koordy3 = koordy2;
koordx3 = koordx2+1;
koordy4 = koordy3;
koordx4 = koordx3+1;
koordy5 = koordy4;
koordx5 = koordx4+1;
}
else if(ausrichtung.equals("h")){
koordy2 = koordy1-1;
koordx2 = koordx1;
koordy3 = koordy2-1;
koordx3 = koordx2;
koordy4 = koordy3-1;
koordx4 = koordx3;
koordy5 = koordy4-1;
koordx5 = koordx4;
}
else if(ausrichtung.equals("u")){
koordy2 = koordy1+1;
koordx2 = koordx1;
koordy3 = koordy2+1;
koordx3 = koordx2;
koordy4 = koordy3+1;
koordx4 = koordx3;
koordy5 = koordy4+1;
koordx5 = koordx4;
}
test[koordy1][koordx1] = '#';
test[koordy2][koordx2] = '#';
test[koordy3][koordx3] = '#';
test[koordy4][koordx4] = '#';
test[koordy5][koordx5] = '#';
}
print(test); // Setzen von Schiffen abgeschlossen
}
static char[][] test = { // Spielfeld
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row0
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row1
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row2
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row3
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row4
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row5
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row6
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row7
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, // row8
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'} // row9
};
public static void print(char[][] grid){ // Methode zur Ausgabe des Spielfeldes
System.out.println("y|" + "0" + "1" + "2" + "3" + "4" + "5" + "6" + "7" + "8" + "9" + " x");
System.out.println("0 " + test[0][0] + test[0][1] + test[0][2] + test[0][3] + test[0][4] + test[0][5] + test[0][6] + test[0][7] + test[0][8] + test[0][9]);
System.out.println("1 " + test[1][0] + test[1][1] + test[1][2] + test[1][3] + test[1][4] + test[1][5] + test[1][6] + test[1][7] + test[1][8] + test[1][9]);
System.out.println("2 " + test[2][0] + test[2][1] + test[2][2] + test[2][3] + test[2][4] + test[2][5] + test[2][6] + test[2][7] + test[2][8] + test[2][9]);
System.out.println("3 " + test[3][0] + test[3][1] + test[3][2] + test[3][3] + test[3][4] + test[3][5] + test[3][6] + test[3][7] + test[3][8] + test[3][9]);
System.out.println("4 " + test[4][0] + test[4][1] + test[4][2] + test[4][3] + test[4][4] + test[4][5] + test[4][6] + test[4][7] + test[4][8] + test[4][9]);
System.out.println("5 " + test[5][0] + test[5][1] + test[5][2] + test[5][3] + test[5][4] + test[5][5] + test[5][6] + test[5][7] + test[5][8] + test[5][9]);
System.out.println("6 " + test[6][0] + test[6][1] + test[6][2] + test[6][3] + test[6][4] + test[6][5] + test[6][6] + test[6][7] + test[6][8] + test[6][9]);
System.out.println("7 " + test[7][0] + test[7][1] + test[7][2] + test[7][3] + test[7][4] + test[7][5] + test[7][6] + test[7][7] + test[7][8] + test[7][9]);
System.out.println("8 " + test[8][0] + test[8][1] + test[8][2] + test[8][3] + test[8][4] + test[8][5] + test[8][6] + test[8][7] + test[8][8] + test[8][9]);
System.out.println("9 " + test[9][0] + test[9][1] + test[9][2] + test[9][3] + test[9][4] + test[9][5] + test[9][6] + test[9][7] + test[9][8] + test[9][9]);
}