SlideShare a Scribd company logo
1 of 20
Download to read offline
i have a runable code below that works with just guessing where the ships are. i need help editing
this code to be able to have a player play against computer and taking turns playing battleship.
BattleShipGame.java
package battleship;
import java.util.ArrayList;
import java.util.Scanner;
public class BattleshipGame {
private Ocean ocean;
private boolean[][] availableSpot;
private Scanner sc;
public BattleshipGame() {
// define a new ocean and a new 2D array to store available coordinates
ocean = new Ocean();
availableSpot = new boolean[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++){
availableSpot[i][j] = true;
}
}
}
/**
* prints the game menu and info
* //param select
*/
public void print(int select){
String info;
switch (select) {
case 1: info = "Welcome to the World of Battleship!";
break;
case 2: info = "Enter coordinates to fire: ";
break;
case 3: info = "Shots fired: "+ocean.getShotsFired()+", Ships sunk:
"+ocean.getShipsSunk();
break;
case 4: info = "Congratulations! You win!";
break;
case 99: info = "Invalid input. Please re-enter:";
break;
case 100: info = "--------------------------------------------";
break;
case 101: info = " ============================================";
break;
default: info = "Error selection";
break;
}
System.out.println(info);
}
/**
* check if the input is valid
* //param input
* //return boolean
*/
public boolean checkValidInput(String input){
ArrayList numList = new ArrayList();
for (int i=0;i<10;i++){
numList.add(""+i);
}
String[] coordinates = input.split(" ");
//returns false if there are not 2 strings
if (coordinates.length!=2){
return false;
}
//returns false if any of the strings is not a single digit number
for (String str: coordinates){
if (numList.contains(str)==false){
return false;
}
}
//returns false if the coordinates have already been shot at
int row = Integer.parseInt(coordinates[0]);
int column = Integer.parseInt(coordinates[1]);
if (this.availableSpot[row][column]==false){
return false;
}
return true;
}
/**
* get the coordinates to shoot at from the String input
* //param input
* //return int[] coordinates
*/
public int[] getCoordinates(String input){
int[] coordinates = new int[2];
String[] strList = input.split(" ");
int row = Integer.parseInt(strList[0]);
int column = Integer.parseInt(strList[1]);
coordinates[0] = row;
coordinates[1] = column;
return coordinates;
}
/**
* play the battleship game
*/
public void play(){
print(101);
print(1);
ocean.placeAllShipsRandomly();
boolean isGameOver = ocean.isGameOver();
sc = new Scanner(System.in);
//print the ocean and start the game
ocean.print();
print(3);
while (!isGameOver){
print(2);
String input = sc.nextLine();
//check if input is valid
while (!checkValidInput(input)){
print(99);
input = sc.nextLine();
}
//get coordinates and fire
int[] coordinates = getCoordinates(input);
int row = coordinates[0];
int column = coordinates[1];
ocean.shootAt(row, column);
availableSpot[row][column] = false;
isGameOver = ocean.isGameOver();
ocean.print();
print(3);
print(100);
}
//print info saying you win
print(4);
}
public static void main(String[] args) {
BattleshipGame battleshipGame = new BattleshipGame();
battleshipGame.play();
System.out.println("Continue? y/n");
Scanner sc = new Scanner(System.in);
String isPlay = sc.next();
while (isPlay.equals("y")){
battleshipGame = new BattleshipGame();
battleshipGame.play();
System.out.println("Continue? y/n");
isPlay = sc.next();
}
sc.close();
}
}
Ocean.java
package battleship;
import java.util.*;
public class Ocean {
private Ship[][] ships;
private int shotsFired;
private int hitCount;
private int shipsSunk;
Random random = new Random();
private boolean[][] shadow;
private Ship battleship;
private Ship cruiser1, cruiser2;
private Ship destroyer1, destroyer2, destroyer3;
private Ship submarine1, submarine2, submarine3, submarine4;
private ArrayList allShips;
//private boolean[][] shotLocations;
public Ocean() {
// TODO Auto-generated constructor stub
battleship = new Battleship();
cruiser1 = new Cruiser();
cruiser2 = new Cruiser();
destroyer1 = new Destroyer();
destroyer2 = new Destroyer();
destroyer3 = new Destroyer();
submarine1 = new Submarine();
submarine2 = new Submarine();
submarine3 = new Submarine();
submarine4 = new Submarine();
allShips = new ArrayList();
allShips.add(battleship);
allShips.add(cruiser1);
allShips.add(cruiser2);
allShips.add(destroyer1);
allShips.add(destroyer2);
allShips.add(destroyer3);
allShips.add(submarine1);
allShips.add(submarine2);
allShips.add(submarine3);
allShips.add(submarine4);
ships = new Ship[10][10];
shadow = new boolean[10][10];
//shotLocations = new boolean[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
this.ships[i][j] = new EmptySea();
this.ships[i][j].setBowRow(i);
this.ships[i][j].setBowColumn(j);
this.ships[i][j].setHorizontal(true);
this.shadow[i][j] = false;
//this.shotLocations[i][j] = false;
}
}
this.shotsFired = 0;
this.hitCount = 0;
this.shipsSunk = 0;
}
public void placeAllShipsRandomly() {
int row;
int column;
int trueOrFalse;
for (Ship ship: allShips){
row = (int) (Math.random() * 10);
column = (int) (Math.random() * 10);
trueOrFalse = (int) (Math.random() * 2);
boolean horizontal = false;
if (trueOrFalse == 1) {
horizontal = true;
}
else {
horizontal = false;
}
while (!ship.okToPlaceShipAt(row, column, horizontal, this)) {
row = (int) (Math.random() * 10);
column = (int) (Math.random() * 10);
trueOrFalse = (int) (Math.random() * 2);
if (trueOrFalse == 1) {
horizontal = true;
}
else {
horizontal = false;
}
}
ship.placeShipAt(row, column, horizontal, this);
}
}
public boolean isOccupied(int row, int column) {
if (this.ships [row][column].getShipType().equals("empty")) {
return false;
}
return true;
}
public boolean shootAt(int row, int column) {
int hit = 0;
int sunkNum = 0;
if (isOccupied(row, column) && !ships[row][column].isSunk()) {
this.hitCount += 1;
hit = 1;
}
this.shotsFired += 1;
//this.shotLocations[row][column] = true;
this.ships[row][column].shootAt(row, column);
for (Ship ship: this.allShips) {
if (ship.isSunk()){
sunkNum += 1;
}
}
this.shipsSunk = sunkNum;
if (hit == 1) {
return true;
}
return false;
}
public int getShotsFired() {
return this.shotsFired;
}
public int getHitCount() {
return this.hitCount;
}
public int getShipsSunk() {
return this.shipsSunk;
}
public boolean isGameOver() {
if (this.shipsSunk == 10) {
return true;
}
return false;
}
public Ship[][] getShipArray() {
return this.ships;
}
public void print() {
String s = " ";
int i;
int j;
for (i = -1; i < 10; i++) {
for (j = -1; j < 10; j++) {
if (i == -1){
if (j > -1){
s += " " + j;
}
}
else if (j == -1) {
s += i + " ";
}
else if (!this.isHit(i, j)) {
s += "." + " ";
}
else {
s += ships[i][j].toString() + " ";
}
}
s += " ";
}
System.out.println(s);
}
////////////////////////////////////////////////additional helper functions//////////////////////////
public boolean[][] getShadow() {
return this.shadow;
}
/**
* when put in one ship, shadow all its adjacent sea. Then the okToPrint function can make
judgment and forbid ships to place on the shadow.
*/
public void setShadow() {
for (int i = 0; i < 10 ; i++){
for (int j = 0; j < 10; j++) {
if (this.isOccupied(i,j)) {
for (int k = -1; k < 2; k++) {
for (int l = -1; l <2; l++ ) {
if ((i+k>=0) && (i+k<=9) && (j+l>=0) && (j+l <=9)) {
shadow[i+k][j+l] = true;
}
}
}
}
}
}
}
/**
* setter for ship class to place ship in the ocean
* //param row
* //param column
* //param ship
*/
public void placeShip(int row, int column, Ship ship) {
this.ships[row][column] = ship;
//update the shadow(places which don't allow ship to be placed)
this.setShadow();
}
/**
* all ships list getter for testing
* //return
*/
public ArrayList getAllShips() {
return this.allShips;
}
public void printTest() {
String s = " ";
int i;
int j;
for (i = -1; i < 10; i++) {
for (j = -1; j < 10; j++) {
if (i == -1){
if (j > -1){
s += " " + j;
}
}
else if (j == -1) {
s += i + " ";
}
else if (!isOccupied(i,j)) {
s += "." + " ";
}
else {
s += ships[i][j].toString() + " ";
}
}
s += " ";
}
System.out.println(s);
}
public boolean isHit(int row, int column) {
Ship ship = this.ships[row][column];
int bowRow = ship.getBowRow();
int bowColumn = ship.getBowColumn();
//System.out.println(row + " " + column + " " + ship + " " + bowRow + " " +
bowColumn + ship.isHorizontal());
if (ship.getShipType().equals("empty")) {
return (ship.getHitArray()[0]);
}
else if (ship.isHorizontal()) {
if (ship.getHitArray()[column - bowColumn]) {
return true;
}
return false;
}
else {
if (ship.getHitArray()[row - bowRow]) {
return true;
}
return false;
}
}
}
Ship.java
package battleship;
public abstract class Ship {
private int bowRow;
private int bowColumn;
protected int length;
private boolean horizontal;
protected boolean[] hit = new boolean[4];
public Ship() {
// TODO Auto-generated constructor stub
super();
}
/**
* returns bowRow
* //return bowRow
*/
public int getBowRow() {
return bowRow;
}
public void setBowRow(int bowRow) {
this.bowRow = bowRow;
}
/**
* returns bowColumn
* //return bowColumn
*/
public int getBowColumn() {
return bowColumn;
}
/**
* sets the value of bowColumn
* //param bowColumn
*/
public void setBowColumn(int bowColumn) {
this.bowColumn = bowColumn;
}
/**
* returns the length of this particular ship
* //return length of the ship
*/
public int getLength() {
return length;
}
/**
* returns horizontal as boolean
* //return isHorizontal
*/
public boolean isHorizontal() {
return horizontal;
}
/**
* sets the value of instance variable horizontal
* //param horizontal
*/
public void setHorizontal(boolean horizontal) {
this.horizontal = horizontal;
}
abstract String getShipType();
/**
* returns true if it is okay to put a ship of certain length with its bow in this location, with the
given orientation
* returns false otherwise
* //param row
* //param column
* //param horizontal
* //param ocean
* //return okToPlaceShipAt as boolean
*/
public boolean okToPlaceShipAt(int row, int column, boolean horizontal, Ocean ocean){
boolean okToPlace = true;
boolean[][] shadows = ocean.getShadow();
if (horizontal){
for (int i=0; i9){okToPlace = false;}
else if (shadows[row][column+i]){okToPlace = false;}
}
}
else{
for (int i=0; i9){okToPlace = false;}
else if (shadows[row+i][column]){okToPlace = false;}
}
}
return okToPlace;
}
/**
* puts the ship on a certain spot in the ocean
* //param row
* //param column
* //param horizontal
* //param ocean
*/
public void placeShipAt(int row, int column, boolean horizontal, Ocean ocean){
this.setHorizontal(horizontal);
this.setBowRow(row);
this.setBowColumn(column);
if (!this.isHorizontal()){
for (int i=0;i
Solution
Answer:
#include
#include
#include
#include
#include
#include
void displaygamerwindow();
void shiplocation();
void systemlocation();
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken);
void systemwindow();
void locationselect(int capacity, int left, int up, int word_input);
char shiplabel[10], systemlabel[10];
int x,locatevertical, locatehorizontal,locateupwards,locateleftwards,computevertical,
computehorizontal,computeupwards,computeleftwards;
char gamerboard [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char hiddensystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char displayedsystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
main()
{
HANDLE result;
result = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Loading Please Wait : "< 9 && computehorizontal < 9)
{
cout << "|"<< endl;
computehorizontal = computehorizontal + 1;
cout << (computehorizontal) << "|";
computevertical = 0;
}
if(computehorizontal < 10)
{
if(computevertical < 9)
{
cout << hiddensystem[computehorizontal] [computevertical] << " ";
}
if(computevertical > 8)
{
cout << hiddensystem[computehorizontal] [computevertical];
}
}
computevertical = computevertical + 1;
}
while(computehorizontal < 10);
computehorizontal = 0;
computevertical = 0;
cout << "|" << endl;
cout << "................................................................" << endl;
cout << "" << endl;
}
void systemlocation()
{
srand(time(NULL));
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(systemlabel,"Carrier");
break;
case 4:
strcpy(systemlabel,"Battleship");
break;
case 3:
strcpy(systemlabel,"Kruiser");
break;
case 2:
strcpy(systemlabel,"Destroyer");
break;
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=int(systemlabel[0]);
systemoption(x,computeleftwards,computeupwards, (int(systemlabel[0])));
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=00;
systemoption(3,computeleftwards,computeupwards, 00);
}
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken)
{
srand(time(NULL));
int select,circle;
select=1+rand()%(4);
switch (select)
{
case 1:
for(circle=1;circle= (0) && x==capacity1)
{
for(circle=1;circle(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards+(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle= (0) && x==capacity1)
{
for(circle=1;circle 9 && locatehorizontal < 9)
{
cout << "|"<< endl;
locatehorizontal = locatehorizontal + 1;
cout << (locatehorizontal) << "|";
locatevertical = 0;
}
if(locatehorizontal < 10)
{
if(locatevertical < 9)
{
cout << gamerboard[locatehorizontal] [locatevertical] << " ";
}
if(locatevertical > 8)
{
cout << gamerboard[locatehorizontal] [locatevertical]<>select;
switch (select)
{
case 1:
for(circle=1;circle= (0) && x==capacity)
{
for(circle=1;circle(9))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly."<(9))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly"<= (0) && x==capacity)
{
for(circle=1;circle=2;x--)
{
switch(x)
{
case 5:
strcpy(shiplabel,"Carrier");
break;
case 4:
strcpy(shiplabel,"Battleship");
break;
case 3:
strcpy(shiplabel,"Kruiser");
break;
case 2:
strcpy(shiplabel,"Destroyer");
break;
}
do
{
cout<<"Locating one end of the "<>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[loc
ateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=int(shiplabel[0]);
displaygamerwindow();
locationselect(x,locateleftwards,locateupwards, (int(shiplabel[0])));
}
do
{
cout<<"Locate one end of the Submarine."<<' ';
cout<<"Place left side coordinate followed by top "<< ' ';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[loc
ateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=00;
displaygamerwindow();
locationselect(3,locateleftwards,locateupwards, 00);
}

More Related Content

Similar to i have a runable code below that works with just guessing where the .pdf

send EDITED code pleaseBattleship.javapackage part3;public cla.pdf
send EDITED code pleaseBattleship.javapackage part3;public cla.pdfsend EDITED code pleaseBattleship.javapackage part3;public cla.pdf
send EDITED code pleaseBattleship.javapackage part3;public cla.pdfirshadoptical
 
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...British Council
 
c++ code#include iostream#include string#include stdlib.h.pdf
c++ code#include iostream#include string#include stdlib.h.pdfc++ code#include iostream#include string#include stdlib.h.pdf
c++ code#include iostream#include string#include stdlib.h.pdfapexjaipur
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdffazalenterprises
 
What's wrong with my code- I am coding a battleship game that uses a d (1).pdf
What's wrong with my code- I am coding a battleship game that uses a d (1).pdfWhat's wrong with my code- I am coding a battleship game that uses a d (1).pdf
What's wrong with my code- I am coding a battleship game that uses a d (1).pdfatexgarments
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdffazilfootsteps
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfNeed to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfflashfashioncasualwe
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfcontact32
 
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdfcreate a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdfduttakajal70
 
i need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdfi need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdfpetercoiffeur18
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfaravlitraders2012
 
Write a program in java in which you will build the“Sink theShipsGam.pdf
Write a program in java in which you will build the“Sink theShipsGam.pdfWrite a program in java in which you will build the“Sink theShipsGam.pdf
Write a program in java in which you will build the“Sink theShipsGam.pdfarchanenterprises
 
TilePUzzle class Anderson, Franceschi public class TilePu.docx
 TilePUzzle class Anderson, Franceschi public class TilePu.docx TilePUzzle class Anderson, Franceschi public class TilePu.docx
TilePUzzle class Anderson, Franceschi public class TilePu.docxKomlin1
 

Similar to i have a runable code below that works with just guessing where the .pdf (16)

Include
IncludeInclude
Include
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
send EDITED code pleaseBattleship.javapackage part3;public cla.pdf
send EDITED code pleaseBattleship.javapackage part3;public cla.pdfsend EDITED code pleaseBattleship.javapackage part3;public cla.pdf
send EDITED code pleaseBattleship.javapackage part3;public cla.pdf
 
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
 
c++ code#include iostream#include string#include stdlib.h.pdf
c++ code#include iostream#include string#include stdlib.h.pdfc++ code#include iostream#include string#include stdlib.h.pdf
c++ code#include iostream#include string#include stdlib.h.pdf
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
 
What's wrong with my code- I am coding a battleship game that uses a d (1).pdf
What's wrong with my code- I am coding a battleship game that uses a d (1).pdfWhat's wrong with my code- I am coding a battleship game that uses a d (1).pdf
What's wrong with my code- I am coding a battleship game that uses a d (1).pdf
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdf
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfNeed to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdfcreate a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
 
i need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdfi need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdf
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
 
Write a program in java in which you will build the“Sink theShipsGam.pdf
Write a program in java in which you will build the“Sink theShipsGam.pdfWrite a program in java in which you will build the“Sink theShipsGam.pdf
Write a program in java in which you will build the“Sink theShipsGam.pdf
 
TilePUzzle class Anderson, Franceschi public class TilePu.docx
 TilePUzzle class Anderson, Franceschi public class TilePu.docx TilePUzzle class Anderson, Franceschi public class TilePu.docx
TilePUzzle class Anderson, Franceschi public class TilePu.docx
 

More from armcomputers

Homework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdfHomework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdfarmcomputers
 
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different  Not D.pdfFind Tukey-Kramer resultsa. Group 1 to Group 2 Different  Not D.pdf
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdfarmcomputers
 
Female copperhead snakes have the ability to reproduce both sexually.pdf
Female copperhead snakes have the ability to reproduce both sexually.pdfFemale copperhead snakes have the ability to reproduce both sexually.pdf
Female copperhead snakes have the ability to reproduce both sexually.pdfarmcomputers
 
Explain some of the commonly held misconceptions about cloning. Why .pdf
Explain some of the commonly held misconceptions about cloning. Why .pdfExplain some of the commonly held misconceptions about cloning. Why .pdf
Explain some of the commonly held misconceptions about cloning. Why .pdfarmcomputers
 
Describe nN when the number of positive divisors of n is i) p2 for .pdf
Describe nN when the number of positive divisors of n is i) p2 for .pdfDescribe nN when the number of positive divisors of n is i) p2 for .pdf
Describe nN when the number of positive divisors of n is i) p2 for .pdfarmcomputers
 
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdfDescribe one derived trait in humans (Homo sapiens).SolutionThe.pdf
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdfarmcomputers
 
Consider the table below How many featuresattributes are in this s.pdf
Consider the table below  How many featuresattributes are in this s.pdfConsider the table below  How many featuresattributes are in this s.pdf
Consider the table below How many featuresattributes are in this s.pdfarmcomputers
 
Chickens vary in color. Black and white color variation can be traced.pdf
Chickens vary in color. Black and white color variation can be traced.pdfChickens vary in color. Black and white color variation can be traced.pdf
Chickens vary in color. Black and white color variation can be traced.pdfarmcomputers
 
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdfarmcomputers
 
write a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdfwrite a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdfarmcomputers
 
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
Why is the 16S rRNA gene a preferred marker for the identification, .pdfWhy is the 16S rRNA gene a preferred marker for the identification, .pdf
Why is the 16S rRNA gene a preferred marker for the identification, .pdfarmcomputers
 
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdfWhy do SSDs benefit from a scheduler like noopSolutionThe def.pdf
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdfarmcomputers
 
which of the following types of orgnisms possess the enzyme catalase.pdf
which of the following types of orgnisms possess the enzyme catalase.pdfwhich of the following types of orgnisms possess the enzyme catalase.pdf
which of the following types of orgnisms possess the enzyme catalase.pdfarmcomputers
 
Which of the five factors of production is most important for creati.pdf
Which of the five factors of production is most important for creati.pdfWhich of the five factors of production is most important for creati.pdf
Which of the five factors of production is most important for creati.pdfarmcomputers
 
What is the good producing business What is the good producing.pdf
What is the good producing business What is the good producing.pdfWhat is the good producing business What is the good producing.pdf
What is the good producing business What is the good producing.pdfarmcomputers
 
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
What is the future of PKI Acceptance of PKI solutions—and product s.pdfWhat is the future of PKI Acceptance of PKI solutions—and product s.pdf
What is the future of PKI Acceptance of PKI solutions—and product s.pdfarmcomputers
 
An easy temperament is best characterized asoptionshigh so.pdf
An easy temperament is best characterized asoptionshigh so.pdfAn easy temperament is best characterized asoptionshigh so.pdf
An easy temperament is best characterized asoptionshigh so.pdfarmcomputers
 
A protein that was isolated by a team of university scien4sts is sus.pdf
A protein that was isolated by a team of university scien4sts is sus.pdfA protein that was isolated by a team of university scien4sts is sus.pdf
A protein that was isolated by a team of university scien4sts is sus.pdfarmcomputers
 
A laser beam strikes one end of a slab of material of length L = 42..pdf
A laser beam strikes one end of a slab of material of length L = 42..pdfA laser beam strikes one end of a slab of material of length L = 42..pdf
A laser beam strikes one end of a slab of material of length L = 42..pdfarmcomputers
 
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdfarmcomputers
 

More from armcomputers (20)

Homework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdfHomework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdf
 
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different  Not D.pdfFind Tukey-Kramer resultsa. Group 1 to Group 2 Different  Not D.pdf
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
 
Female copperhead snakes have the ability to reproduce both sexually.pdf
Female copperhead snakes have the ability to reproduce both sexually.pdfFemale copperhead snakes have the ability to reproduce both sexually.pdf
Female copperhead snakes have the ability to reproduce both sexually.pdf
 
Explain some of the commonly held misconceptions about cloning. Why .pdf
Explain some of the commonly held misconceptions about cloning. Why .pdfExplain some of the commonly held misconceptions about cloning. Why .pdf
Explain some of the commonly held misconceptions about cloning. Why .pdf
 
Describe nN when the number of positive divisors of n is i) p2 for .pdf
Describe nN when the number of positive divisors of n is i) p2 for .pdfDescribe nN when the number of positive divisors of n is i) p2 for .pdf
Describe nN when the number of positive divisors of n is i) p2 for .pdf
 
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdfDescribe one derived trait in humans (Homo sapiens).SolutionThe.pdf
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
 
Consider the table below How many featuresattributes are in this s.pdf
Consider the table below  How many featuresattributes are in this s.pdfConsider the table below  How many featuresattributes are in this s.pdf
Consider the table below How many featuresattributes are in this s.pdf
 
Chickens vary in color. Black and white color variation can be traced.pdf
Chickens vary in color. Black and white color variation can be traced.pdfChickens vary in color. Black and white color variation can be traced.pdf
Chickens vary in color. Black and white color variation can be traced.pdf
 
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
 
write a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdfwrite a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdf
 
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
Why is the 16S rRNA gene a preferred marker for the identification, .pdfWhy is the 16S rRNA gene a preferred marker for the identification, .pdf
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
 
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdfWhy do SSDs benefit from a scheduler like noopSolutionThe def.pdf
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
 
which of the following types of orgnisms possess the enzyme catalase.pdf
which of the following types of orgnisms possess the enzyme catalase.pdfwhich of the following types of orgnisms possess the enzyme catalase.pdf
which of the following types of orgnisms possess the enzyme catalase.pdf
 
Which of the five factors of production is most important for creati.pdf
Which of the five factors of production is most important for creati.pdfWhich of the five factors of production is most important for creati.pdf
Which of the five factors of production is most important for creati.pdf
 
What is the good producing business What is the good producing.pdf
What is the good producing business What is the good producing.pdfWhat is the good producing business What is the good producing.pdf
What is the good producing business What is the good producing.pdf
 
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
What is the future of PKI Acceptance of PKI solutions—and product s.pdfWhat is the future of PKI Acceptance of PKI solutions—and product s.pdf
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
 
An easy temperament is best characterized asoptionshigh so.pdf
An easy temperament is best characterized asoptionshigh so.pdfAn easy temperament is best characterized asoptionshigh so.pdf
An easy temperament is best characterized asoptionshigh so.pdf
 
A protein that was isolated by a team of university scien4sts is sus.pdf
A protein that was isolated by a team of university scien4sts is sus.pdfA protein that was isolated by a team of university scien4sts is sus.pdf
A protein that was isolated by a team of university scien4sts is sus.pdf
 
A laser beam strikes one end of a slab of material of length L = 42..pdf
A laser beam strikes one end of a slab of material of length L = 42..pdfA laser beam strikes one end of a slab of material of length L = 42..pdf
A laser beam strikes one end of a slab of material of length L = 42..pdf
 
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
 

Recently uploaded

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Recently uploaded (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

i have a runable code below that works with just guessing where the .pdf

  • 1. i have a runable code below that works with just guessing where the ships are. i need help editing this code to be able to have a player play against computer and taking turns playing battleship. BattleShipGame.java package battleship; import java.util.ArrayList; import java.util.Scanner; public class BattleshipGame { private Ocean ocean; private boolean[][] availableSpot; private Scanner sc; public BattleshipGame() { // define a new ocean and a new 2D array to store available coordinates ocean = new Ocean(); availableSpot = new boolean[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++){ availableSpot[i][j] = true; } } } /** * prints the game menu and info * //param select */ public void print(int select){ String info; switch (select) { case 1: info = "Welcome to the World of Battleship!"; break; case 2: info = "Enter coordinates to fire: "; break; case 3: info = "Shots fired: "+ocean.getShotsFired()+", Ships sunk: "+ocean.getShipsSunk(); break; case 4: info = "Congratulations! You win!";
  • 2. break; case 99: info = "Invalid input. Please re-enter:"; break; case 100: info = "--------------------------------------------"; break; case 101: info = " ============================================"; break; default: info = "Error selection"; break; } System.out.println(info); } /** * check if the input is valid * //param input * //return boolean */ public boolean checkValidInput(String input){ ArrayList numList = new ArrayList(); for (int i=0;i<10;i++){ numList.add(""+i); } String[] coordinates = input.split(" "); //returns false if there are not 2 strings if (coordinates.length!=2){ return false; } //returns false if any of the strings is not a single digit number for (String str: coordinates){ if (numList.contains(str)==false){ return false; } } //returns false if the coordinates have already been shot at int row = Integer.parseInt(coordinates[0]); int column = Integer.parseInt(coordinates[1]);
  • 3. if (this.availableSpot[row][column]==false){ return false; } return true; } /** * get the coordinates to shoot at from the String input * //param input * //return int[] coordinates */ public int[] getCoordinates(String input){ int[] coordinates = new int[2]; String[] strList = input.split(" "); int row = Integer.parseInt(strList[0]); int column = Integer.parseInt(strList[1]); coordinates[0] = row; coordinates[1] = column; return coordinates; } /** * play the battleship game */ public void play(){ print(101); print(1); ocean.placeAllShipsRandomly(); boolean isGameOver = ocean.isGameOver(); sc = new Scanner(System.in); //print the ocean and start the game ocean.print(); print(3); while (!isGameOver){ print(2);
  • 4. String input = sc.nextLine(); //check if input is valid while (!checkValidInput(input)){ print(99); input = sc.nextLine(); } //get coordinates and fire int[] coordinates = getCoordinates(input); int row = coordinates[0]; int column = coordinates[1]; ocean.shootAt(row, column); availableSpot[row][column] = false; isGameOver = ocean.isGameOver(); ocean.print(); print(3); print(100); } //print info saying you win print(4); } public static void main(String[] args) { BattleshipGame battleshipGame = new BattleshipGame(); battleshipGame.play(); System.out.println("Continue? y/n"); Scanner sc = new Scanner(System.in); String isPlay = sc.next(); while (isPlay.equals("y")){ battleshipGame = new BattleshipGame(); battleshipGame.play(); System.out.println("Continue? y/n"); isPlay = sc.next(); } sc.close();
  • 5. } } Ocean.java package battleship; import java.util.*; public class Ocean { private Ship[][] ships; private int shotsFired; private int hitCount; private int shipsSunk; Random random = new Random(); private boolean[][] shadow; private Ship battleship; private Ship cruiser1, cruiser2; private Ship destroyer1, destroyer2, destroyer3; private Ship submarine1, submarine2, submarine3, submarine4; private ArrayList allShips; //private boolean[][] shotLocations; public Ocean() { // TODO Auto-generated constructor stub battleship = new Battleship(); cruiser1 = new Cruiser(); cruiser2 = new Cruiser(); destroyer1 = new Destroyer(); destroyer2 = new Destroyer(); destroyer3 = new Destroyer(); submarine1 = new Submarine(); submarine2 = new Submarine(); submarine3 = new Submarine(); submarine4 = new Submarine(); allShips = new ArrayList(); allShips.add(battleship); allShips.add(cruiser1); allShips.add(cruiser2); allShips.add(destroyer1);
  • 6. allShips.add(destroyer2); allShips.add(destroyer3); allShips.add(submarine1); allShips.add(submarine2); allShips.add(submarine3); allShips.add(submarine4); ships = new Ship[10][10]; shadow = new boolean[10][10]; //shotLocations = new boolean[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { this.ships[i][j] = new EmptySea(); this.ships[i][j].setBowRow(i); this.ships[i][j].setBowColumn(j); this.ships[i][j].setHorizontal(true); this.shadow[i][j] = false; //this.shotLocations[i][j] = false; } } this.shotsFired = 0; this.hitCount = 0; this.shipsSunk = 0; } public void placeAllShipsRandomly() { int row; int column; int trueOrFalse; for (Ship ship: allShips){ row = (int) (Math.random() * 10); column = (int) (Math.random() * 10); trueOrFalse = (int) (Math.random() * 2); boolean horizontal = false; if (trueOrFalse == 1) { horizontal = true; }
  • 7. else { horizontal = false; } while (!ship.okToPlaceShipAt(row, column, horizontal, this)) { row = (int) (Math.random() * 10); column = (int) (Math.random() * 10); trueOrFalse = (int) (Math.random() * 2); if (trueOrFalse == 1) { horizontal = true; } else { horizontal = false; } } ship.placeShipAt(row, column, horizontal, this); } } public boolean isOccupied(int row, int column) { if (this.ships [row][column].getShipType().equals("empty")) { return false; } return true; } public boolean shootAt(int row, int column) { int hit = 0; int sunkNum = 0; if (isOccupied(row, column) && !ships[row][column].isSunk()) { this.hitCount += 1; hit = 1; } this.shotsFired += 1; //this.shotLocations[row][column] = true; this.ships[row][column].shootAt(row, column); for (Ship ship: this.allShips) { if (ship.isSunk()){ sunkNum += 1;
  • 8. } } this.shipsSunk = sunkNum; if (hit == 1) { return true; } return false; } public int getShotsFired() { return this.shotsFired; } public int getHitCount() { return this.hitCount; } public int getShipsSunk() { return this.shipsSunk; } public boolean isGameOver() { if (this.shipsSunk == 10) { return true; } return false; } public Ship[][] getShipArray() { return this.ships; } public void print() { String s = " "; int i; int j; for (i = -1; i < 10; i++) { for (j = -1; j < 10; j++) { if (i == -1){ if (j > -1){ s += " " + j; }
  • 9. } else if (j == -1) { s += i + " "; } else if (!this.isHit(i, j)) { s += "." + " "; } else { s += ships[i][j].toString() + " "; } } s += " "; } System.out.println(s); } ////////////////////////////////////////////////additional helper functions////////////////////////// public boolean[][] getShadow() { return this.shadow; } /** * when put in one ship, shadow all its adjacent sea. Then the okToPrint function can make judgment and forbid ships to place on the shadow. */ public void setShadow() { for (int i = 0; i < 10 ; i++){ for (int j = 0; j < 10; j++) { if (this.isOccupied(i,j)) { for (int k = -1; k < 2; k++) { for (int l = -1; l <2; l++ ) { if ((i+k>=0) && (i+k<=9) && (j+l>=0) && (j+l <=9)) { shadow[i+k][j+l] = true; } } } }
  • 10. } } } /** * setter for ship class to place ship in the ocean * //param row * //param column * //param ship */ public void placeShip(int row, int column, Ship ship) { this.ships[row][column] = ship; //update the shadow(places which don't allow ship to be placed) this.setShadow(); } /** * all ships list getter for testing * //return */ public ArrayList getAllShips() { return this.allShips; } public void printTest() { String s = " "; int i; int j; for (i = -1; i < 10; i++) { for (j = -1; j < 10; j++) { if (i == -1){ if (j > -1){ s += " " + j; } } else if (j == -1) { s += i + " "; } else if (!isOccupied(i,j)) {
  • 11. s += "." + " "; } else { s += ships[i][j].toString() + " "; } } s += " "; } System.out.println(s); } public boolean isHit(int row, int column) { Ship ship = this.ships[row][column]; int bowRow = ship.getBowRow(); int bowColumn = ship.getBowColumn(); //System.out.println(row + " " + column + " " + ship + " " + bowRow + " " + bowColumn + ship.isHorizontal()); if (ship.getShipType().equals("empty")) { return (ship.getHitArray()[0]); } else if (ship.isHorizontal()) { if (ship.getHitArray()[column - bowColumn]) { return true; } return false; } else { if (ship.getHitArray()[row - bowRow]) { return true; } return false; } } } Ship.java
  • 12. package battleship; public abstract class Ship { private int bowRow; private int bowColumn; protected int length; private boolean horizontal; protected boolean[] hit = new boolean[4]; public Ship() { // TODO Auto-generated constructor stub super(); } /** * returns bowRow * //return bowRow */ public int getBowRow() { return bowRow; } public void setBowRow(int bowRow) { this.bowRow = bowRow; } /** * returns bowColumn * //return bowColumn */ public int getBowColumn() { return bowColumn; } /** * sets the value of bowColumn * //param bowColumn */ public void setBowColumn(int bowColumn) { this.bowColumn = bowColumn; }
  • 13. /** * returns the length of this particular ship * //return length of the ship */ public int getLength() { return length; } /** * returns horizontal as boolean * //return isHorizontal */ public boolean isHorizontal() { return horizontal; } /** * sets the value of instance variable horizontal * //param horizontal */ public void setHorizontal(boolean horizontal) { this.horizontal = horizontal; } abstract String getShipType(); /** * returns true if it is okay to put a ship of certain length with its bow in this location, with the given orientation * returns false otherwise * //param row * //param column * //param horizontal * //param ocean * //return okToPlaceShipAt as boolean */ public boolean okToPlaceShipAt(int row, int column, boolean horizontal, Ocean ocean){ boolean okToPlace = true; boolean[][] shadows = ocean.getShadow();
  • 14. if (horizontal){ for (int i=0; i9){okToPlace = false;} else if (shadows[row][column+i]){okToPlace = false;} } } else{ for (int i=0; i9){okToPlace = false;} else if (shadows[row+i][column]){okToPlace = false;} } } return okToPlace; } /** * puts the ship on a certain spot in the ocean * //param row * //param column * //param horizontal * //param ocean */ public void placeShipAt(int row, int column, boolean horizontal, Ocean ocean){ this.setHorizontal(horizontal); this.setBowRow(row); this.setBowColumn(column); if (!this.isHorizontal()){ for (int i=0;i Solution Answer: #include #include #include #include #include #include void displaygamerwindow(); void shiplocation();
  • 15. void systemlocation(); void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken); void systemwindow(); void locationselect(int capacity, int left, int up, int word_input); char shiplabel[10], systemlabel[10]; int x,locatevertical, locatehorizontal,locateupwards,locateleftwards,computevertical, computehorizontal,computeupwards,computeleftwards; char gamerboard [10][10]= { {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, }; char hiddensystem [10][10]= { {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, }; char displayedsystem [10][10]= { {00,00,00,00,00,00,00,00,00,00},
  • 16. {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, {00,00,00,00,00,00,00,00,00,00}, }; main() { HANDLE result; result = GetStdHandle(STD_OUTPUT_HANDLE); cout<<"Loading Please Wait : "< 9 && computehorizontal < 9) { cout << "|"<< endl; computehorizontal = computehorizontal + 1; cout << (computehorizontal) << "|"; computevertical = 0; } if(computehorizontal < 10) { if(computevertical < 9) { cout << hiddensystem[computehorizontal] [computevertical] << " "; } if(computevertical > 8) { cout << hiddensystem[computehorizontal] [computevertical]; } } computevertical = computevertical + 1; } while(computehorizontal < 10); computehorizontal = 0;
  • 17. computevertical = 0; cout << "|" << endl; cout << "................................................................" << endl; cout << "" << endl; } void systemlocation() { srand(time(NULL)); for (x=5;x>=2;x--) { switch(x) { case 5: strcpy(systemlabel,"Carrier"); break; case 4: strcpy(systemlabel,"Battleship"); break; case 3: strcpy(systemlabel,"Kruiser"); break; case 2: strcpy(systemlabel,"Destroyer"); break; } do { computeleftwards=rand()%(10); computeupwards=rand()%(10); } while((hiddensystem[computeleftwards][computeupwards]!=00)); hiddensystem[computeleftwards][computeupwards]=int(systemlabel[0]); systemoption(x,computeleftwards,computeupwards, (int(systemlabel[0]))); } do
  • 18. { computeleftwards=rand()%(10); computeupwards=rand()%(10); } while((hiddensystem[computeleftwards][computeupwards]!=00)); hiddensystem[computeleftwards][computeupwards]=00; systemoption(3,computeleftwards,computeupwards, 00); } void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken) { srand(time(NULL)); int select,circle; select=1+rand()%(4); switch (select) { case 1: for(circle=1;circle= (0) && x==capacity1) { for(circle=1;circle(9)) { hiddensystem[leftsidedirection][upwards]=00; x=x+1; break; } if(int(leftsidedirection-(capacity1-1)) <= (9) && x==capacity1) { for(circle=1;circle(9)) { hiddensystem[leftsidedirection][upwards]=00; x=x+1; break; } if(int(upwards+(capacity1-1)) <= (9) && x==capacity1) { for(circle=1;circle= (0) && x==capacity1)
  • 19. { for(circle=1;circle 9 && locatehorizontal < 9) { cout << "|"<< endl; locatehorizontal = locatehorizontal + 1; cout << (locatehorizontal) << "|"; locatevertical = 0; } if(locatehorizontal < 10) { if(locatevertical < 9) { cout << gamerboard[locatehorizontal] [locatevertical] << " "; } if(locatevertical > 8) { cout << gamerboard[locatehorizontal] [locatevertical]<>select; switch (select) { case 1: for(circle=1;circle= (0) && x==capacity) { for(circle=1;circle(9)) { gamerboard[left][up]=00; cout<<"Please check ships place correctly."<(9)) { gamerboard[left][up]=00; cout<<"Please check ships place correctly"<= (0) && x==capacity) { for(circle=1;circle=2;x--) { switch(x) { case 5: strcpy(shiplabel,"Carrier");
  • 20. break; case 4: strcpy(shiplabel,"Battleship"); break; case 3: strcpy(shiplabel,"Kruiser"); break; case 2: strcpy(shiplabel,"Destroyer"); break; } do { cout<<"Locating one end of the "<>locateleftwards; cin>>locateupwards; } while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[loc ateleftwards][locateupwards]!=00)); gamerboard[locateleftwards][locateupwards]=int(shiplabel[0]); displaygamerwindow(); locationselect(x,locateleftwards,locateupwards, (int(shiplabel[0]))); } do { cout<<"Locate one end of the Submarine."<<' '; cout<<"Place left side coordinate followed by top "<< ' '; cin>>locateleftwards; cin>>locateupwards; } while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[loc ateleftwards][locateupwards]!=00)); gamerboard[locateleftwards][locateupwards]=00; displaygamerwindow(); locationselect(3,locateleftwards,locateupwards, 00); }