this code is my attempt at modifying a simple game i found in the book "head first java".. (pg-98 if anyone's interested)
the game is about sinking battleships! ...
actually... its just about declaring some blocks of a virtual array to comprise a battleship, and the user makes guesses about which blocks contain the battleship.
when all the correct blocks are guessed, the game ends, whith a display that the battleship has sunk.
my problem is that no matter how much i change the values and conditions, the for loops that control the number of times the user is asked to make a guess, always run a fixed number of time. which is equal to whatever size i declare the shipCell array in the line private int shipCell[]= new int[8];
of the 1st class... which,in this case, is 8.
this is my code for it(the nomenclature of the two classes follows from that in the book):
import java.util.Scanner;
class SimpleDotCom{
private int hitNum=0,shipSize=0;
private int shipCell[]= new int[8]; // this stores the blocks that comprise our battleship..
Scanner input = new Scanner(System.in);
// setters :
public void setShipSize(int size){
shipSize=size;
}
public void setLocation(int cellLocation, int index){
shipCell[index]=cellLocation;
}
public void setHitNum(int num){
hitNum=num;
}
// getters :
public int getShipSize(){
return shipSize;
}
public int getHitNum(){
return hitNum;
}
//methods :
public void putShipInBattlezone(){
for(int i=0;i<shipSize;i++){
System.out.print("enter ship location (0 to 10) : ");
setLocation(input.nextInt(),i);
}
}
public boolean checkHit(int guessedLocation,int size){
boolean flag=false;
for(int i=0;i<size;i++){
if(shipCell[i]==guessedLocation){
flag = true;
break;
}
}
return flag;
}
public boolean checkSunk(int num){
boolean sunkFlag=false;
if(shipCell.length==num){
sunkFlag=true;
}
return sunkFlag;
}
public void shipHasSunk(){
System.out.print("ouch! you have sunk the ship!");
}
}
class SimpleDotComGame{
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
SimpleDotCom game = new SimpleDotCom();
System.out.print("enter size of ship: "); // ship size entered
game.setShipSize(input.nextInt());
System.out.printf("entered ship size is: %d \n",game.getShipSize());
game.putShipInBattlezone(); // ship is now put on battle feild, let the destruction begin
int doFlag=0,hits=0;
do{
int guess=0; // make a guess
System.out.print("enter guess (0 to 10) : ");
guess=input.nextInt();
if(game.checkHit(guess,game.getShipSize())){ // check if guess was correct
System.out.println("hit");
game.setHitNum(1+game.getHitNum()); // increments hitNum, which is initially 0
hits=game.getHitNum();
System.out.printf("total hits scored: %d \n",hits);
if(game.checkSunk(hits)){ // passes incremented value of hitNum to sunk() to see if ship has sunk
doFlag=1;
game.shipHasSunk();
break;
}
}
else{
System.out.println("miss");
}
}while(doFlag!=1); // until ship has sunk
}
}
i'v just started learning java about a week or so, n im weak on concepts ... tried changing evrything i thought could be the problem, but it still remains.
would highly appreciate if someone could help me out in this code a bit :)
thanks
somjit :)