67 lines
No EOL
2.1 KiB
Java
67 lines
No EOL
2.1 KiB
Java
import java.io.IOException;
|
|
import java.util.Scanner;
|
|
|
|
/** Aufgabe 2: Objekte zerstören
|
|
*
|
|
* Erstelle eine Klasse schiffeVersenken.
|
|
* Erstelle 3 Schiffe und weiße ihnen eine Position auf dem Feld zu.
|
|
*
|
|
* 1 Schiff mit der länge 1.
|
|
* 1 Schiff mit der länge 2.
|
|
* 1 Schiff mit der länge 3.
|
|
*
|
|
* Erstelle eine Benutzereingabe. Sobald ein Schiff versenkt ist soll
|
|
* das Objekt mit NULL gleich gesetzt werden um es für den Carbage collector frei zu geben.
|
|
*/
|
|
public class SchiffeVersenken {
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
Feld map = new Feld();
|
|
RealSchiff a = new RealSchiff(1);
|
|
a.insertToMap(1, 2, map, 'v');
|
|
RealSchiff b = new RealSchiff(2);
|
|
b.insertToMap(3, 2, map, '>');
|
|
RealSchiff c = new RealSchiff(3);
|
|
c.insertToMap(3, 14, map, '|');
|
|
RealSchiff d = new RealSchiff(16);
|
|
d.insertToMap(7, 0, map, '>');
|
|
a = null;
|
|
b = null;
|
|
c = null;
|
|
d = null;
|
|
boolean gameover = false;
|
|
Scanner scanner = new Scanner(System.in);
|
|
int x;
|
|
int y;
|
|
String userin;
|
|
String[] userItems;
|
|
boolean result;
|
|
while (!gameover) {
|
|
try {
|
|
System.out.println("Hit where? (x,y; 0-15, 0-7)");
|
|
userin = scanner.nextLine();
|
|
if (userin.equals("!map"))
|
|
map.printMap();
|
|
else if (userin.equals("!exit")) {
|
|
break;
|
|
}
|
|
userItems = userin.split(",");
|
|
if (userItems.length != 2) {
|
|
System.out.println("Bad input!");
|
|
continue;
|
|
}
|
|
x = Integer.parseInt(userItems[0]);
|
|
y = Integer.parseInt(userItems[1]);
|
|
result = map.hit(y, x);
|
|
System.out.println(result);
|
|
System.out.println(String.format("dbg: at x %x, y %x -> %s", x, y, map.map[y][x]));
|
|
}
|
|
catch(Exception e) {
|
|
System.err.println(e);
|
|
}
|
|
|
|
}
|
|
System.out.println("You win!");
|
|
map.printMap();
|
|
}
|
|
} |