0% found this document useful (0 votes)
96 views6 pages

Battleship

This class represents a game board for a battleship game. It initializes a 2D array of strings to represent empty squares on the board. Methods are included to add ships to the board if they are valid placements, check if ships have been sunk after a shot, take shots on the board, and check if all ships have been sunk to end the game.

Uploaded by

api-547879336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views6 pages

Battleship

This class represents a game board for a battleship game. It initializes a 2D array of strings to represent empty squares on the board. Methods are included to add ships to the board if they are valid placements, check if ships have been sunk after a shot, take shots on the board, and check if all ships have been sunk to end the game.

Uploaded by

api-547879336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

public class Board{

private String[][] squares;

public Board(){
squares = new String[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
squares[i][j] = "-";
}
}
}

public boolean onBoard(int row, int col) {


if (row < squares.length && col < squares[0].length && row >= 0 && col >= 0) {
return true;
}
return false;
}

public String toString(){


String blank = "";
for (int i = 0; i < squares.length; i++) {
for (int j = 0; j < squares[i].length; j++) {
blank += squares[i][j] + " ";
}
if (i != 9) {
blank += "\n";
}
}
return blank;
}

public boolean addShip(int row, int col, int len, boolean horizontal){
if (onBoard(row, col) == false) {
return false;
}
if (horizontal == true) {
if ((squares[0].length - col) < len) {
return false;
}
for (int i = col; i < col + len; i++) {
if (row == 0) {
if (i != 0) {
if (squares[row][i - 1].equals("b") || squares[row + 1][i].equals("b")) {
return false;
}
} else {
if (squares[row + 1][i].equals("b")) {
return false;
}
}
if (squares[row][i].equals("b")) {
return false;
}
}
else if (row == 9) {
if (i != 0) {
if (squares[row][i - 1].equals("b") || squares[row - 1][i].equals("b")) {
return false;
}
}
else {
if (squares[row - 1][i].equals("b")) {
return false;
}
}
if (squares[row][i].equals("b")) {
return false;
}
}
else {
if (i != 0) {
if (squares[row][i - 1].equals("b") || squares[row - 1][i].equals("b") || squares[row +
1][i].equals("b")) {
return false;
}
} else {
if (squares[row][i - 1].equals("b") || squares[row - 1][i].equals("b")) {
return false;
}
}
if (squares[row][i].equals("b")) {
return false;
}
}
}
for (int i = col; i < col + len; i++) {
squares[row][i] = "b";
}
}
if (horizontal == false) {
if ((squares.length - row) < len) {
return false;
}
for (int i = row; i < row + len; i++) {
if (col == 0) {
if (i != 0) {
if (squares[i - 1][col].equals("b") || squares[i][col + 1].equals("b")) {
return false;
}
} else {
if (squares[i][col + 1].equals("b")) {
return false;
}
}
} else if (col == 9) {
if (i != 0) {
if (squares[i - 1][col].equals("b") || squares[i][col - 1].equals("b")) {
return false;
}
} else {
if (squares[i][col - 1].equals("b")) {
return false;
}
}
} else {
if (i != 0) {
if (squares[i - 1][col].equals("b") || squares[i][col + 1].equals("b") || squares[i][col -
1].equals("b")) {
return false;
}
}
else {
if (squares[i][col - 1].equals("b") || squares[i][col + 1].equals("b")) {
return false;
}
}
}
if (squares[i][col].equals("b")) {
return false;
}
}
for (int i = row; i < row + len; i++) {
squares[i][col] = "b";
}
}
return true;
}

//Check vertical and horizontal lengths for each cell of matrix


public boolean foundShip(int len){
for (int i = 0; i < squares.length; i++) {
for (int j = 0; j < squares[0].length; j++) {
int countV = 0;
int countH = 0;
if (i != 0 && !(squares[i - 1][j].equals("b"))) {
for (int k = i; k < squares.length; k++) {
if (squares[k][j].equals("b")) {
countV++;
System.out.print(countV);
} else {
break;
}
}
} else if (i == 0) {
for (int k = i; k < squares.length; k++) {
if (squares[k][j].equals("b")) {
countV++;
} else {
break;
}
}
}
if (countV == len) {
return true;
}
if (j != 0 && !(squares[i][j - 1].equals("b"))) {
for (int g = j; g < squares[0].length; g++) {
if (squares[i][g].equals("b")) {
countH++;
System.out.print(countH);
} else {
break;
}
}
} else if (i == 0) {
for (int g = j; g < squares[0].length; g++) {
if (squares[i][g].equals("b")) {
countH++;
} else {
break;
}
}
}
if (countH == len) {
return true;
}
}
}
return false;
}

public int shoot(int row, int col) {


if (row >= squares.length || row < 0 || col >= squares[0].length || col < 0) {
return -1;
}
switch (squares[row][col]) {
case "-":
squares[row][col] = "m";
return 0;
case "b":
squares[row][col] = "x";
return 1;
case "x":
return 2;
case "m":
return 2;
}
return -1;
}

public boolean gameOver(){


for (int i = 0; i < squares.length; i++) {
for (int j = 0; j < squares[0].length; j++) {
if (squares[i][j].equals("b")) {
return false;
}
}
}
return true;
}

You might also like