map repr is working

This commit is contained in:
Christoph J. Scherr 2023-04-24 14:04:50 +02:00
parent e4e49db481
commit 14bf2d7236
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
12 changed files with 187 additions and 0 deletions

29
SchiffeVersenken/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View File

@ -0,0 +1,17 @@
public class BrokenSchiff extends Schiff {
// a broken ship that has sunken
@Override
public boolean insertToMap(int x, int y, Feld map, char orientation) {
return super.insertToMap(x, y, map, orientation);
}
public BrokenSchiff(int schiffLaenge) {
super();
}
public BrokenSchiff() {
super();
repr='X';
}
}

View File

@ -0,0 +1,67 @@
public class Feld {
public static final int sizeX = 16;
public static final int sizeY = 8;
public final NoSchiff none = new NoSchiff();
public Schiff[][] map = {
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
};
public Feld() {
}
public void printMap() {
StringBuilder output = new StringBuilder("\033[2J");
for (Schiff[] row : this.map) {
for (Schiff ship : row) {
// this seems to be the repr of the Schiff class, not the object actually?
output.append(ship.repr());
}
output.append('\n');
}
System.out.println(output);
}
public boolean insertShip(Schiff ship, int x, int y) {
System.out.println(String.format(
"Inserting ship into map: %s",
ship
));
if (x >= sizeX || y >= sizeY || y < 0 || x < 0) {
System.err.println(
String.format("Tried to insert a ship at invalid coordinates:\n"+
"x: %s\n"+
"y: %s\n", x, y)
);
return false;
}
this.map[x][y] = ship;
assert this.map[x][y] == ship;
return true;
}
public boolean hit(int x, int y) {
if (x >= sizeX || y >= sizeY || y < 0 || x < 0) {
System.err.println("Tried to hit a ship at invalid coordinates.");
return false;
}
if (map[x][y] instanceof RealSchiff) {
} else {
// no hit
return false;
}
// unreachable code
return false;
}
}

View File

@ -0,0 +1,12 @@
public class NoSchiff extends Schiff {
public final int laenge = 1;
@Override
public boolean insertToMap(int x, int y, Feld map, char orientation) {
return super.insertToMap(x, y, map, orientation);
}
public NoSchiff() {
repr = '~';
}
}

View File

@ -0,0 +1,11 @@
public class RealSchiff extends Schiff {
@Override
public boolean insertToMap(int x, int y, Feld map, char orientation) {
return super.insertToMap(x, y, map, orientation);
}
public RealSchiff(int schiffLaenge) {
super(schiffLaenge);
repr = 'S';
}
}

View File

@ -0,0 +1,51 @@
public abstract class Schiff {
public int laenge;
public char repr = '?';
public Schiff(int schiffLaenge) {
laenge = schiffLaenge;
}
public char repr() {
return this.repr;
}
public boolean insertToMap(int x, int y, Feld map, char orientation) {
if (orientation == '>') {
for (int i = 0; i < this.laenge; i++){
if (!map.insertShip(this, x+i, y)) {
return false;
}
}
return true;
}
else if (orientation == '<') {
for (int i = 0; i < this.laenge; i++){
if (!map.insertShip(this, x-i, y)) {
return false;
}
}
return true;
}
else if (orientation == 'v') {
for (int i = 0; i < this.laenge; i++){
if (!map.insertShip(this, x, y+i)) {
return false;
}
}
return true;
}
else if (orientation == '|') {
for (int i = 0; i < this.laenge; i++){
if (!map.insertShip(this, x, y-i)) {
return false;
}
}
return true;
}
return false;
}
public Schiff() {
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.