changed the file structure

This commit is contained in:
Sage The DM 2024-08-27 10:41:17 +02:00
parent cd81159162
commit b1c3582880
391 changed files with 675 additions and 0 deletions

Binary file not shown.

View file

@ -0,0 +1,349 @@
import java.util.Random;
import java.util.Scanner;
public class App {
// Board chars
public static final int BLANK = 0; // ' '
public static final int WATER = 1; // 'O'
public static final int SHIP = 2; // 'X'
public static final int SUNK = 3; // 'S'
public static final int HIDDEN_SHIP = 4; // ' '
public static final int REVEALED_SHIP = 5; // 'R'
// board[vertical desc --> y][horizontal left to right --> x]
public static int[][] board = new int[10][10];
// Array where the ships are stored
private static int[] shipX = new int[10];
private static int[] shipY = new int[10];
private static boolean[] shipDirection = new boolean[10];
private static int[] shipLength = new int[10];
// Number of shots fired
public static int firedShot = 0;
public static boolean game = true;
public static void main(String[] args) {
boolean areShipsRevealed = false;
// Explanation rules
initializeGame();
do {
printBoard();
System.out.println("Where do you want to shoot? (e.g., A5 or ? for a tip or -1 to end the game) ");
String input = getCoordinates();
switch (input) {
case "-1":
game = false;
break;
case "?":
revealHiddenShips();
areShipsRevealed = true;
break;
default:
// The Lines 47-49 should be deactivated while debugging to make it easier
if (areShipsRevealed) {
hideRevealedShips();
}
handleShot(input);
break;
}
} while (game);
}
// Handles the player's shot input, validates it, and processes the shot.
private static void handleShot(String input) {
try {
char xChar = input.toLowerCase().charAt(0);
int y = Integer.parseInt(input.substring(1)) - 1;
int x = xChar - 'a';
if (isInputValid(x, y)) {
String result = shot(x, y);
System.out.println(result);
System.out.println("Number of fired shots: " + firedShot);
if (allShipSunk()) {
printBoard();
System.out.println("All ships have been sunk" + "\nYou won the game\n");
printBoard();
game = false;
}
} else {
System.out.println("Invalid input. Please enter a valid coordinate (e.g., A5).");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input format. Please enter a valid coordinate (e.g., A5).");
}
}
// Reveals the positions of hidden ships on the board.
private static void revealHiddenShips() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (board[i][j] == HIDDEN_SHIP) {
board[i][j] = REVEALED_SHIP;
}
}
}
}
// Hides the previously revealed ships on the board.
private static void hideRevealedShips() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (board[i][j] == REVEALED_SHIP) {
board[i][j] = HIDDEN_SHIP;
}
}
}
}
// Initializes the game by setting up the board and randomly placing ships.
private static void initializeGame() {
// Fill board with blanks
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = BLANK;
}
}
// Place ships randomly
Random random = new Random();
int i = 0;
int[] ships = { 4, 3, 3, 2, 2, 2, 1, 1, 1, 1 };
for (int shipSize : ships) {
boolean placed = false;
while (!placed) {
int x = random.nextInt(10);
int y = random.nextInt(10);
boolean direction = random.nextBoolean();
if (isValidPlacement(x, y, shipSize, direction)) {
shipLength[i] = shipSize;
shipX[i] = x;
shipY[i] = y;
shipDirection[i] = direction;
i++;
placeShip(x, y, shipSize, direction);
placed = true;
}
}
}
}
// Validates the placement of a ship on the board.
private static boolean isValidPlacement(int x, int y, int shipSize, boolean direction) {
if (!isWithinBounds(x, y, shipSize, direction)) {
return false;
}
if (doesOverlap(x, y, shipSize, direction)) {
return false;
}
return hasSpaceOfWater(x, y, shipSize, direction);
}
// Checks if the ship placement is within the board boundaries.
private static boolean isWithinBounds(int x, int y, int shipSize, boolean direction) {
if (direction) {
return x >= 0 && x + shipSize <= 10 && y >= 0 && y < 10;
} else {
return y >= 0 && y + shipSize <= 10 && x >= 0 && x < 10;
}
}
// Checks if the ship placement overlaps with existing ships.
private static boolean doesOverlap(int x, int y, int shipSize, boolean direction) {
for (int i = 0; i < shipSize; i++) {
int row = direction ? y : y + i;
int col = direction ? x + i : x;
if (board[row][col] != BLANK) {
return true; // Overlaps with existing ship
}
}
return false;
}
// Ensures that the ship has a space of water around it.
private static boolean hasSpaceOfWater(int x, int y, int shipSize, boolean direction) {
for (int i = 0; i < shipSize; i++) {
int row = direction ? y : y + i;
int col = direction ? x + i : x;
for (int j = row - 1; j <= row + 1; j++) {
for (int k = col - 1; k <= col + 1; k++) {
if (j >= 0 && j < 10 && k >= 0 && k < 10 && board[j][k] != BLANK) {
return false; // Adjacent cell is not water
}
}
}
}
return true; // Has space of water
}
// Places a ship on the board at the specified coordinates.
private static void placeShip(int x, int y, int shipSize, boolean direction) {
if (direction) {
for (int i = 0; i < shipSize; i++) {
board[y][x + i] = HIDDEN_SHIP;
}
} else {
for (int i = 0; i < shipSize; i++) {
board[y + i][x] = HIDDEN_SHIP;
}
}
}
// Gets coordinates input from the user.
private static String getCoordinates() {
Scanner scan = new Scanner(System.in);
return scan.next();
}
// Validates the input coordinates.
private static boolean isInputValid(int x, int y) {
return x >= 0 && x < 10 && y >= 0 && y < 10;
}
// Processes a shot at the specified coordinates and returns the result.
private static String shot(int x, int y) {
switch (board[y][x]) {
case BLANK:
board[y][x] = WATER;
firedShot++;
return "Miss!";
case WATER:
return "Already shot this field!";
case HIDDEN_SHIP:
case REVEALED_SHIP:
board[y][x] = SHIP;
firedShot++;
if (isShipSunk(x, y)) {
sinkShip(x, y);
return "You sunk a ship!";
} else {
return "You hit a ship!";
}
default:
return "Invalid state!";
}
}
// Sinks a ship by marking all its parts as sunk.
private static void sinkShip(int x, int y) {
for (int i = 0; i < shipX.length; i++) {
int startX = shipX[i];
int startY = shipY[i];
int length = shipLength[i];
boolean direction = shipDirection[i]; // true for horizontal, false for vertical
// Check if the current shot is part of this ship
if (direction) {
// Horizontal ship
if (y == startY && x >= startX && x < startX + length) {
// Mark all parts of the ship as sunk
for (int j = 0; j < length; j++) {
board[startY][startX + j] = SUNK;
}
for (int j = 0; j < length; j++) {
placeWaterAroundShip(startX + j, startY);
}
return;
}
} else {
// Vertical ship
if (x == startX && y >= startY && y < startY + length) {
// Mark all parts of the ship as sunk
for (int j = 0; j < length; j++) {
board[startY + j][startX] = SUNK;
}
for (int j = 0; j < length; j++) {
placeWaterAroundShip(startX, startY + j);
}
return;
}
}
}
}
// Checks if a ship at the specified coordinates is completely sunk.
private static boolean isShipSunk(int x, int y) {
for (int i = 0; i < shipX.length; i++) {
int startX = shipX[i];
int startY = shipY[i];
int length = shipLength[i];
boolean direction = shipDirection[i]; // true for horizontal, false for vertical
// Check if the current shot is part of this ship
if (direction) {
// Horizontal ship
if (y == startY && x >= startX && x < startX + length) {
// Check all parts of the ship
for (int j = 0; j < length; j++) {
if (board[startY][startX + j] != SHIP && board[startY][startX + j] != SUNK) {
return false; // If any part is not hit, the ship is not sunk
}
}
return true; // All parts are hit, the ship is sunk
}
} else {
// Vertical ship
if (x == startX && y >= startY && y < startY + length) {
// Check all parts of the ship
for (int j = 0; j < length; j++) {
if (board[startY + j][startX] != SHIP && board[startY + j][startX] != SUNK) {
return false; // If any part is not hit, the ship is not sunk
}
}
return true; // All parts are hit, the ship is sunk
}
}
}
return false; // If the shot is not part of any ship (shouldn't happen in a valid game)
}
// Prints the current state of the board to the console.
private static void printBoard() {
System.out.println(" | A | B | C | D | E | F | G | H | I | J |");
System.out.println("--------------------------------------------");
char[] symbols = { ' ', 'O', 'X', 'S', ' ', 'R' };
for (int i = 0; i < board.length; i++) {
if (i < 9) {
System.out.print(" " + (i + 1) + " |");
} else {
System.out.print(" " + (i + 1) + "|");
}
for (int j = 0; j < board[i].length; j++) {
System.out.print(" " + symbols[board[i][j]] + " |");
}
System.out.println("\n--------------------------------------------");
}
}
// Places water around a sunk ship to mark its surrounding area.
private static void placeWaterAroundShip(int x, int y) {
for (int i = y - 1; i <= y + 1; i++) {
for (int j = x - 1; j <= x + 1; j++) {
if (i >= 0 && i < 10 && j >= 0 && j < 10 && board[i][j] == BLANK) {
board[i][j] = WATER;
}
}
}
}
// Checks if all ships on the board are sunk.
private static boolean allShipSunk() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == HIDDEN_SHIP || board[i][j] == REVEALED_SHIP) {
return false; // Found a ship that is not sunk
}
}
}
return true; // All ships are sunk
}
}

Binary file not shown.

View file

@ -0,0 +1,357 @@
import java.util.Random;
import java.util.Scanner;
public class test {
// Spielfeld-Zeichen
public static final int LEER = 0; // ' '
public static final int WASSER = 1; // 'O'
public static final int SCHIFF = 2; // 'X'
public static final int VERSENKT = 3; // 'S'
public static final int VERSTECKT = 4; // ' '
public static final int ENTHÜLLT = 5; // 'R'
// spielfeld[vertikal --> y][horizontal --> x]
public static int[][] spielfeld = new int[10][10];
// Array, in dem die Schiffe gespeichert sind
private static int[] schiffX = new int[10];
private static int[] schiffY = new int[10];
private static boolean[] schiffRichtung = new boolean[10];
private static int[] schiffLänge = new int[10];
// Anzahl der abgefeuerten Schüsse
public static int abgefeuerteSchüsse = 0;
public static boolean spielLäuft = true;
public static void main(String[] args) {
boolean sindSchiffeEnthüllt = false;
// Regeln erklären
spielInitialisieren();
do {
spielfeldZeigen();
System.out.println(
"Wohin möchtest du schießen? (z.B., A5 oder ? für einen Tipp oder -1 zum Beenden des Spiels)");
String eingabe = koordinateEingeben();
switch (eingabe) {
case "-1":
spielLäuft = false;
break;
case "?":
versteckteSchiffeZeigen();
sindSchiffeEnthüllt = true;
break;
default:
// Die Zeilen 47-49 sollten beim Debuggen deaktiviert werden, um es einfacher zu
// machen
if (sindSchiffeEnthüllt) {
enthüllteSchiffeVerstecken();
}
schussVerarbeiten(eingabe);
break;
}
} while (spielLäuft);
}
// Verarbeitet die Schusseingabe des Spielers, validiert sie und führt den
// Schuss aus.
private static void schussVerarbeiten(String eingabe) {
try {
char xChar = eingabe.toLowerCase().charAt(0);
int y = Integer.parseInt(eingabe.substring(1)) - 1;
int x = xChar - 'a';
if (eingabeIstGültig(x, y)) {
String ergebnis = schießen(x, y);
System.out.println(ergebnis);
System.out.println("Anzahl der abgefeuerten Schüsse: " + abgefeuerteSchüsse);
if (alleSchiffeVersenkt()) {
spielfeldZeigen();
System.out.println("Alle Schiffe wurden versenkt\nDu hast das Spiel gewonnen\n");
spielfeldZeigen();
spielLäuft = false;
}
} else {
System.out.println("Ungültige Eingabe. Bitte gib eine gültige Koordinate ein (z.B., A5).");
}
} catch (NumberFormatException e) {
System.out.println("Ungültiges Eingabeformat. Bitte gib eine gültige Koordinate ein (z.B., A5).");
}
}
// Zeigt die Positionen der versteckten Schiffe auf dem Spielfeld an.
private static void versteckteSchiffeZeigen() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (spielfeld[i][j] == VERSTECKT) {
spielfeld[i][j] = ENTHÜLLT;
}
}
}
}
// Versteckt die zuvor enthüllten Schiffe auf dem Spielfeld.
private static void enthüllteSchiffeVerstecken() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (spielfeld[i][j] == ENTHÜLLT) {
spielfeld[i][j] = VERSTECKT;
}
}
}
}
// Initialisiert das Spiel, indem das Spielfeld eingerichtet und Schiffe
// zufällig platziert werden.
private static void spielInitialisieren() {
// Spielfeld mit leeren Feldern füllen
for (int i = 0; i < spielfeld.length; i++) {
for (int j = 0; j < spielfeld[i].length; j++) {
spielfeld[i][j] = LEER;
}
}
// Schiffe zufällig platzieren
Random zufall = new Random();
int i = 0;
int[] schiffe = { 4, 3, 3, 2, 2, 2, 1, 1, 1, 1 };
for (int schiffGröße : schiffe) {
boolean platziert = false;
while (!platziert) {
int x = zufall.nextInt(10);
int y = zufall.nextInt(10);
boolean richtung = zufall.nextBoolean();
if (platzierungIstGültig(x, y, schiffGröße, richtung)) {
schiffLänge[i] = schiffGröße;
schiffX[i] = x;
schiffY[i] = y;
schiffRichtung[i] = richtung;
i++;
schiffPlatzieren(x, y, schiffGröße, richtung);
platziert = true;
}
}
}
}
// Validiert die Platzierung eines Schiffs auf dem Spielfeld.
private static boolean platzierungIstGültig(int x, int y, int schiffGröße, boolean richtung) {
if (!innerhalbGrenzen(x, y, schiffGröße, richtung)) {
return false;
}
if (überschneidung(x, y, schiffGröße, richtung)) {
return false;
}
return hatWasserPlatz(x, y, schiffGröße, richtung);
}
// Überprüft, ob die Schiffplatzierung innerhalb der Spielfeldgrenzen liegt.
private static boolean innerhalbGrenzen(int x, int y, int schiffGröße, boolean richtung) {
if (richtung) {
return x >= 0 && x + schiffGröße <= 10 && y >= 0 && y < 10;
} else {
return y >= 0 && y + schiffGröße <= 10 && x >= 0 && x < 10;
}
}
// Überprüft, ob sich die Schiffplatzierung mit vorhandenen Schiffen
// überschneidet.
private static boolean überschneidung(int x, int y, int schiffGröße, boolean richtung) {
for (int i = 0; i < schiffGröße; i++) {
int reihe = richtung ? y : y + i;
int spalte = richtung ? x + i : x;
if (spielfeld[reihe][spalte] != LEER) {
return true; // Überschneidet sich mit einem vorhandenen Schiff
}
}
return false;
}
// Stellt sicher, dass das Schiff einen Platz mit Wasser um sich herum hat.
private static boolean hatWasserPlatz(int x, int y, int schiffGröße, boolean richtung) {
for (int i = 0; i < schiffGröße; i++) {
int reihe = richtung ? y : y + i;
int spalte = richtung ? x + i : x;
for (int j = reihe - 1; j <= reihe + 1; j++) {
for (int k = spalte - 1; k <= spalte + 1; k++) {
if (j >= 0 && j < 10 && k >= 0 && k < 10 && spielfeld[j][k] != LEER) {
return false; // Benachbartes Feld ist kein Wasser
}
}
}
}
return true; // Hat Platz mit Wasser
}
// Platziert ein Schiff auf dem Spielfeld an den angegebenen Koordinaten.
private static void schiffPlatzieren(int x, int y, int schiffGröße, boolean richtung) {
if (richtung) {
for (int i = 0; i < schiffGröße; i++) {
spielfeld[y][x + i] = VERSTECKT;
}
} else {
for (int i = 0; i < schiffGröße; i++) {
spielfeld[y + i][x] = VERSTECKT;
}
}
}
// Holt die Koordinateneingabe vom Benutzer.
private static String koordinateEingeben() {
Scanner scan = new Scanner(System.in);
return scan.next();
}
// Überprüft, ob die Eingabekoordinaten gültig sind.
private static boolean eingabeIstGültig(int x, int y) {
return x >= 0 && x < 10 && y >= 0 && y < 10;
}
// Verarbeitet einen Schuss auf die angegebenen Koordinaten und gibt das
// Ergebnis zurück.
private static String schießen(int x, int y) {
switch (spielfeld[y][x]) {
case LEER:
spielfeld[y][x] = WASSER;
abgefeuerteSchüsse++;
return "Daneben!";
case WASSER:
return "Dieses Feld wurde bereits beschossen!";
case VERSTECKT:
case ENTHÜLLT:
spielfeld[y][x] = SCHIFF;
abgefeuerteSchüsse++;
if (schiffVersenkt(x, y)) {
schiffVersenken(x, y);
return "Du hast ein Schiff versenkt!";
} else {
return "Du hast ein Schiff getroffen!";
}
default:
return "Ungültiger Zustand!";
}
}
// Versenkt ein Schiff, indem alle seine Teile als versenkt markiert werden.
private static void schiffVersenken(int x, int y) {
for (int i = 0; i < schiffX.length; i++) {
int startX = schiffX[i];
int startY = schiffY[i];
int länge = schiffLänge[i];
boolean richtung = schiffRichtung[i]; // true für horizontal, false für vertikal
// Überprüfen, ob der aktuelle Schuss Teil dieses Schiffes ist
if (richtung) {
// Horizontales Schiff
if (y == startY && x >= startX && x < startX + länge) {
// Markiere alle Teile des Schiffes als versenkt
for (int j = 0; j < länge; j++) {
spielfeld[startY][startX + j] = VERSENKT;
}
for (int j = 0; j < länge; j++) {
wasserUmSchiffPlatzieren(startX + j, startY);
}
return;
}
} else {
// Vertikales Schiff
if (x == startX && y >= startY && y < startY + länge) {
// Markiere alle Teile des Schiffes als versenkt
for (int j = 0; j < länge; j++) {
spielfeld[startY + j][startX] = VERSENKT;
}
for (int j = 0; j < länge; j++) {
wasserUmSchiffPlatzieren(startX, startY + j);
}
return;
}
}
}
}
// Überprüft, ob ein Schiff an den angegebenen Koordinaten vollständig versenkt
// ist.
private static boolean schiffVersenkt(int x, int y) {
for (int i = 0; i < schiffX.length; i++) {
int startX = schiffX[i];
int startY = schiffY[i];
int länge = schiffLänge[i];
boolean richtung = schiffRichtung[i]; // true für horizontal, false für vertikal
// Überprüfen, ob der aktuelle Schuss Teil dieses Schiffes ist
if (richtung) {
// Horizontales Schiff
if (y == startY && x >= startX && x < startX + länge) {
// Überprüfen alle Teile des Schiffes
for (int j = 0; j < länge; j++) {
if (spielfeld[startY][startX + j] != SCHIFF && spielfeld[startY][startX + j] != VERSENKT) {
return false; // Wenn ein Teil nicht getroffen ist, ist das Schiff nicht versenkt
}
}
return true; // Alle Teile sind getroffen, das Schiff ist versenkt
}
} else {
// Vertikales Schiff
if (x == startX && y >= startY && y < startY + länge) {
// Überprüfen alle Teile des Schiffes
for (int j = 0; j < länge; j++) {
if (spielfeld[startY + j][startX] != SCHIFF && spielfeld[startY + j][startX] != VERSENKT) {
return false; // Wenn ein Teil nicht getroffen ist, ist das Schiff nicht versenkt
}
}
return true; // Alle Teile sind getroffen, das Schiff ist versenkt
}
}
}
return false; // Wenn der Schuss kein Teil eines Schiffes ist (sollte in einem gültigen Spiel
// nicht passieren)
}
// Zeigt den aktuellen Zustand des Spielfelds auf der Konsole an.
private static void spielfeldZeigen() {
System.out.println(" | A | B | C | D | E | F | G | H | I | J |");
System.out.println("--------------------------------------------");
char[] symbole = { ' ', 'O', 'X', 'S', ' ', 'R' };
for (int i = 0; i < spielfeld.length; i++) {
if (i < 9) {
System.out.print(" " + (i + 1) + " |");
} else {
System.out.print(" " + (i + 1) + "|");
}
for (int j = 0; j < spielfeld[i].length; j++) {
System.out.print(" " + symbole[spielfeld[i][j]] + " |");
}
System.out.println("\n--------------------------------------------");
}
}
// Platziert Wasser um ein versenktes Schiff, um dessen Umgebung zu markieren.
private static void wasserUmSchiffPlatzieren(int x, int y) {
for (int i = y - 1; i <= y + 1; i++) {
for (int j = x - 1; j <= x + 1; j++) {
if (i >= 0 && i < 10 && j >= 0 && j < 10 && spielfeld[i][j] == LEER) {
spielfeld[i][j] = WASSER;
}
}
}
}
// Überprüft, ob alle Schiffe auf dem Spielfeld versenkt sind.
private static boolean alleSchiffeVersenkt() {
for (int i = 0; i < spielfeld.length; i++) {
for (int j = 0; j < spielfeld[i].length; j++) {
if (spielfeld[i][j] == VERSTECKT || spielfeld[i][j] == ENTHÜLLT) {
return false; // Ein Schiff wurde gefunden, das nicht versenkt ist
}
}
}
return true; // Alle Schiffe sind versenkt
}
}