Skip to content

Commit

Permalink
feat(8): Finish Assignment8: Battleship
Browse files Browse the repository at this point in the history
  • Loading branch information
101zh committed Feb 2, 2024
1 parent 6d4767f commit d8abe42
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 0 deletions.
152 changes: 152 additions & 0 deletions src/main/java/Unit8/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package Unit8;

// Have to get rid of package statement

public class Board {

private String[][] squares;

public Board() {
squares = new String[10][10];
for (int r = 0; r < squares.length; r++) {
for (int c = 0; c < squares[r].length; c++) {
squares[r][c] = "-";
}
}
}

public String toString() {
String boardString = "";

for (int r = 0; r < squares.length; r++) {
for (int c = 0; c < squares[r].length; c++) {
boardString += squares[r][c] + " ";
}
boardString += "\n";
}

return boardString;
}

public boolean addShip(int row, int col, int len, boolean horizontal) {

if (!isCoordinatesValid(row, col, len, horizontal))
return false;

if (isShipInWay(row, col, len, horizontal))
return false;

placeShip(row, col, len, horizontal);

return true;
}

private boolean isCoordinatesValid(int row, int col, int len, boolean horizontal) {
boolean validCoordinates = (row >= 0 && row < squares.length) &&
(col >= 0 && col < squares[row].length);

if (horizontal) {
validCoordinates = validCoordinates && col + len <= squares[row].length;
} else {
validCoordinates = validCoordinates && row + len <= squares.length;
}

return validCoordinates;

}

private boolean isShipInWay(int row, int col, int len, boolean horizontal) {
if (horizontal) {
for (int c = col; c < col + len; c++) {
if (squares[row][c].equals("b")) {
return true;
}
}
} else {
for (int r = row; r < row + len; r++) {
if (squares[r][col].equals("b")) {
return true;
}
}
}

return false;
}

private void placeShip(int row, int col, int len, boolean horizontal) {
if (horizontal) {
for (int c = col; c < col + len; c++) {
squares[row][c] = "b";
}
} else {
for (int r = row; r < row + len; r++) {
squares[r][col] = "b";
}
}
}

public boolean foundShip(int len) {
// Searching row-by-row
for (int r = 0; r < squares.length; r++) {
int count = 0;
for (int c = 0; c < squares[r].length; c++) {
if (squares[r][c].equals("b")) {
count++;
} else {
if (count == len) {
return true;
}
count = 0;
}
}
if (count == len) {
return true;
}
}

// Searching column by column
for (int c = 0; c < squares[0].length; c++) {
int count = 0;
for (int r = 0; r < squares.length; r++) {
if (squares[r][c].equals("b")) {
count++;
} else {
if (count == len) {
return true;
}
count = 0;
}
}
if (count == len) {
return true;
}
}

return false;
}

public int shoot(int row, int col) {

if (!isCoordinatesValid(row, col, 0, false))
return -1;

if (squares[row][col].equals("-")) {
squares[row][col] = "m";
return 0;
} else if (squares[row][col].equals("b")) {
squares[row][col] = "x";
return 1;
}

return 2;
}

public boolean gameOver() {
if (foundShip(1)) {
return false;
}

return true;
}

}
80 changes: 80 additions & 0 deletions src/test/java/Unit8/Battleship.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package Unit8;

// Have to get rid of package statement

import java.util.Scanner;

public class Battleship {
public static void main(String[] args) {
// Set up board and print welcome
Board b = new Board();
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Battleship!\n");
boolean addNew = true;

while (addNew) {
System.out.println("Type \"a\" to add new ship, \"b\" to see the board, \"p\" to play or \"q\" to quit.");
String ans = scan.nextLine();
if (ans.toLowerCase().equals("q"))
return;
if (ans.toLowerCase().equals("a")) {
// Get parameters for new ship
System.out.println("Starting in which row?");
int r = scan.nextInt();
System.out.println("Starting in which column?");
int c = scan.nextInt();
System.out.println("How long?");
int l = scan.nextInt();
scan.nextLine();
System.out.println("Horizontal (h) or vertical (v)?");
String d = scan.nextLine();
boolean h = (d.toLowerCase().equals("h"));

// Call addShip method and return message based on true/false value
if (b.addShip(r, c, l, h)) {
System.out.println("\nNew ship added!\n");
} else
System.out.println("\nCan't put a ship there!\n");
} else if (ans.toLowerCase().equals("b"))
System.out.println("\n" + b + "\n");
else if (ans.toLowerCase().equals("p")) {
if (b.foundShip(3) && b.foundShip(4)) {
addNew = false;
System.out.println("\nOk, let's play!\n");
} else
System.out.println("\nYou need ships of length 3 and 4 to play!\n");
}
}

// As long as ships remain, play game
while (!b.gameOver()) {
System.out.println("Press \"s\" to shoot at a square, \"b\" to see the board, \"q\" to quit.");
String ans = scan.nextLine();
if (ans.toLowerCase().equals("q"))
return;
else if (ans.toLowerCase().equals("s")) {
// Get row and column to shoot
System.out.println("Input row.");
int r = scan.nextInt();
System.out.println("Input column.");
int c = scan.nextInt();

// Perform shot and store result
int result = b.shoot(r, c);

// Choose message based on result
if (result == 1)
System.out.println("\nHit!\n");
else if (result == 0)
System.out.println("\nMiss!\n");
else if (result == 2)
System.out.println("\nYou already tried that.\n");
else if (result == -1)
System.out.println("\nInvalid coordinates.\n");
scan.nextLine();
} else if (ans.toLowerCase().equals("b"))
System.out.println("\n" + b + "\n");
}
System.out.println("Game over!");
}
}

0 comments on commit d8abe42

Please sign in to comment.