G
Gelöschtes Mitglied 53953
Gast
Hallo,
ich möchte, dass sich das Programm nach jeder Eingabe merkt, welche Felder bereits eingetippt wurden. Beispiel: User tippt auf das Feld 2 und es ist ein Treffer - dann würde das folgendermaßen aussehen:
[ ] [ x ][ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Nun will ich, dass wenn der Nutzer "3" eintippt, das Programm weiß, was an zweiter Stelle steht,
also: [ ] [ x ][ x ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
und nicht [ ] [ ][ x ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Ich hoffe ihr versteht was ich meine ...
Hier ist der Code (es funktioniert alles wie es soll, bis auf das oben genannte. Ach und es kann sein, dass ich es umständlich geschrieben habe, bin ja noch ein Anfänger
):
ich möchte, dass sich das Programm nach jeder Eingabe merkt, welche Felder bereits eingetippt wurden. Beispiel: User tippt auf das Feld 2 und es ist ein Treffer - dann würde das folgendermaßen aussehen:
[ ] [ x ][ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Nun will ich, dass wenn der Nutzer "3" eintippt, das Programm weiß, was an zweiter Stelle steht,
also: [ ] [ x ][ x ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
und nicht [ ] [ ][ x ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Ich hoffe ihr versteht was ich meine ...
Hier ist der Code (es funktioniert alles wie es soll, bis auf das oben genannte. Ach und es kann sein, dass ich es umständlich geschrieben habe, bin ja noch ein Anfänger
Code:
import java.util.*;
public class Schiffeversenken {
public static void main(String[] args) {
System.out.println("Schiffeversenken - Mini");
Scanner scr = new Scanner(System.in);
int pos1=0;
int pos2=0;
int pos3=0;
int win;
//Position des Schiffes festlegen:
int p1 = (int) (Math.random()*8+1);
int p2 = p1+1;
int p3 = p1+2;
int test;
//Eingabe + Anzeige:
String[] felder = new String[11];
do {
System.out.println();
System.out.print("Position zwischen 1/10: ");
int pUser = scr.nextInt();
if (pUser == p1) {
System.out.println();
System.out.println("Treffer auf Feld "+pUser+"!");
System.out.println();
pos1=1;
for (int x=1;x<felder.length ;x++ ) {
if (pUser == x && pos1==1) {
System.out.print("[ x ] ");
}
else {
System.out.print("[ ] ");
}
}
}
else if (pUser == p2) {
System.out.println();
System.out.println("Treffer auf Feld "+pUser+"!");
System.out.println();
pos2=1;
for (int x=1;x<felder.length ;x++ ) {
if (pUser == x) {
System.out.print("[ x ] ");
}
else {
System.out.print("[ ] ");
}
}
}
else if (pUser == p3) {
System.out.println();
System.out.println("Treffer auf Feld "+pUser+"!");
System.out.println();
pos3=1;
for (int x=1;x<felder.length ;x++ ) {
if (pUser == x) {
System.out.print("[ x ] ");
}
else {
System.out.print("[ ] ");
}
}
}
else {
System.out.println();
System.out.println("Leider kein Treffer auf Feld "+pUser+"!");
System.out.println();
for (int x=1;x<felder.length ;x++ ) {
if (pUser == x) {
System.out.print("[ - ] ");
}
else {
System.out.print("[ ] ");
}
}
}
win = pos1+pos2+pos3;
System.out.println();
System.out.println();
if (win==3) {
System.out.println("Gewonnen!");
System.out.println();
}
} while (win==0 || win==1 || win==2);
}
}