import java.util.Scanner;
public class SchiffeVersenken {
private static char[][] firstplayer = {
{'#', '#', '#', '#', '.', '.', '.', '.', '.', '#', },
{'.', '.', '.', '.', '.', '#', '#', '.', '.', '#', },
{'.', '.', '#', '.', '#', '#', '#', '.', '.', '.', },
{'.', '.', '#', '.', '.', '.', '.', '#', '#', '#', },
{'.', '.', '#', '.', '.', '#', '.', '.', '.', '.', },
{'.', '.', '#', '.', '.', '#', '.', '.', '.', '.', },
{'.', '.', '#', '.', '.', '#', '.', '.', '#', '.', },
{'#', '.', '.', '.', '.', '.', '.', '.', '#', '.', },
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '#', '#', '#', '#', '.', '.', } };
private static char[][] secondplayer = {
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', },
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', } };
private static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("\n \n ********************************");
System.out.println("\n We welcome you to Battleship");
System.out.println("\n ******************************\n");
String modus;
System.out.println("Please type in your name:");
String player1 = s.nextLine();
System.out.println("Please type in your enemies name:");
String player2 = s.nextLine();
System.out.println("If you are ready choose heads or tails and press 'enter' to continue.");
modus = s.nextLine();
if ("".equals(modus)) {
SchiffeVersenken.cointoss();
} else {
return;
}
modus = s.nextLine();
while (true) {
System.out.println("\n Field of " + player1 + "\n");
SchiffeVersenken.print(SchiffeVersenken.firstplayer);
System.out.println("\n ************");
System.out.println("\n Field of " + player2 + "\n");
SchiffeVersenken.print(SchiffeVersenken.secondplayer);
System.out.println("\n Please type in (attack,defend,exit) : ");
modus = s.nextLine();
if ("attack".equals(modus)) {
SchiffeVersenken.attack();
}
if ("defend".equals(modus)) {
SchiffeVersenken.defend();
}
if ("exit".equals(modus)) {
break;
}
}
System.out.println("\n Game finished");
}
public static void print(char[][] gitter) {
System.out.println(" 0123456789");
for (int r = 0; r < gitter.length; r++) {
System.out.print("ABCDEFGHIJ".charAt(r));
for (int c = 0; c < gitter.length; c++) {
System.out.print(gitter[r][c]);
}
System.out.println("");
}
}
public static void attack() {
Scanner s = new Scanner(System.in);
String input;
char zeile;
System.out.println("Please type in row: ");
input = s.next();
zeile = input.charAt(0);
String input1;
char spalte;
System.out.println("Please type in column: ");
input1 = s.next();
spalte = input1.charAt(0);
int row;
row = (int)zeile;
int column;
column = (int)spalte;
boolean hit;
System.out.println("Did the attack hit? (type in true or false):");
hit = s.nextBoolean();
if (hit) {
secondplayer[row][column] = 'X';
} else {
secondplayer[row][column] = 'o';
}
}
public static void defend() {
Scanner s = new Scanner(System.in);
String input2;
char row;
System.out.println("Please type in row: ");
input2 = s.next();
row = input2.charAt(0);
String input3;
char column;
System.out.println("Please type in column: ");
input3 = s.next();
column = input3.charAt(0);
if (firstplayer[row][column] == '#') {
firstplayer[row][column] = 'X';
} else {
firstplayer[row][column] = 'o';
}
}
public static void cointoss() {
Scanner m = new Scanner(System.in);
double zufallszahl = Math.random();
if (zufallszahl <= 0.49) {
System.out.println("\n Heads \n");
} else {
System.out.println("\n Tails \n");
}
}
}