i dont understand the reason why im geting the errors i am with this code... errors with arraylists, also a few other errors like symbol not found etc s.. i corrected the ones i could, but the ones (5 of them in total )im still geting, i dont understand them...
the code is my version of a battleship game found in head first java, 6th chapter..
it takes user input on number of ships, and allocates some blocks to them, when all blocks are hit, the ship sinks, and when all ships are sunk... you win the game...
this is the code:
import java.util.Scanner;
import java.util.ArrayList;
class ship{
private int shipSize=0;
private String shipName;
ArrayList<int> shipCell = new ArrayList<int>();
// setters :
public void setShipName(String name){
shipName=name;
}
public void setShipSize(int size){
shipSize=size;
}
public void setLocation(int cellLocation){
shipCell.add(cellLocation);
}
// getters :
public int getShipSize(){
return shipSize;
}
//methods :
public void putShipInBattlezone(int start){
// int seed = (int)((Math.random()*15)+5); // for later use, once the simpler version runs errorfree
// int randomNum=(int)(Math.random()*10); // the virtual array is of size 15
for(int i=0;i<shipSize;i++){
setLocation(start+i);
}
System.out.println("\n\nbattleship placed.. let the destruction begin!!\n\n......................................................");
}
public boolean checkHit(int guessedLocation/*,int size*/){
//System.out.printf("in checkHit, location passed: %d size passed: %d \n",guessedLocation,size);
System.out.printf("in checkHit, location passed: %d \n",guessedLocation);
boolean checkHitFlag=shipCell.contains(guessedLocation);
if(checkHitFlag){
shipCell.remove(guessedLocation); // arrayList shrunked
}
return checkHitFlag;
}
public boolean checkSunk(){
System.out.printf("\nin checksunk,\n");
boolean sunkFlag=shipCell.isEmpty();
return sunkFlag;
}
public void shipHasSunk(){
System.out.print("Bravo!! you have sunk the %s!",shipName);
}
}
class BattleShipEnhanced{
Scanner input = new Scanner(System.in);
ArrayList<ship> shipList = new ArrayList<ship>();
private void setUpGame(){
System.out.print("enter number of ships: ");
int numOfShips=input.nextInt();
ship[] game = new ship[numOfShips]; // game>> refernce to array object of type SimpleDotCom (our ship class)
for(int i=0;i<numOfShips;i++){
//get a name for a instance to create
System.out.print("enter name of ship: ");
// create an instance of this nae of ship class
game[i]= new ship();
game[i].setShipName(input.nextLine());
//set size
System.out.print("enter size of ship: "); // ship size entered
game[i].setShipSize(input.nextInt());
//place it on battlezone
System.out.print("enter starting cell of ship: ");
game[i].putShipInBattlezone(input.nextInt());
//put the newly created instance inside array list
shipList.add(game[i]);
}
}
private void startPlaying(){
while(!shipList.isEmpty()){
int guess=0; // make a guess
System.out.print("enter guess (+ve values only) : ");
guess=input.nextInt();
checkGuess(guess);
}
finishGame();// finishes game when arrayList is empty
}
private void checkGuess(int guess){
for(ship currentShip : shipList){
if(currentShip.checkHit(guess)){ // check if guess was correct
System.out.println("hit\n");
if(currentShip.checkSunk()){
CurrentShip.shipHasSunk();
shipList.remove(CurrentShip);// shipList shrunk
}
}
else{
System.out.println("miss");
}
}
}
private void finishGame(){
System.out.println("you have sunk all the ships!! go get soe drinks!");
}
public static void main (String [] args) {
BattleShipEnhanced game = new BattleShipEnhanced();
game.setUpGame();
game.startPlaying();
}
}
any help would be really great... iv worked hard on this, n a bit disheartened with the errors that still remain.. really hoping to get this to compile and run error free :)
thanks
somjit :)