Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class SimpleDotCom{
- private int hitNum=0,shipSize=0;
- private int shipCell[]= new int[8];
- private int hitCell[]= new int[8];
- 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;
- }
- public void setHitCell(int num,int index){
- hitCell[index]=num;
- }
- // getters :
- public int getShipSize(){
- return shipSize;
- }
- public int getHitNum(){
- return hitNum;
- }
- //methods :
- public void putShipInBattlezone(){
- int randomNum=(int)(Math.random()*15); // the virtual array is of size 15
- for(int i=0;i<shipSize;i++){
- setLocation((randomNum+i),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);
- boolean checkHitFlag=false;
- for(int i=0;i<size;i++){
- if(shipCell[i]==guessedLocation){
- checkHitFlag = true;
- break;
- }
- }
- return checkHitFlag;
- }
- static int storeIndex=0;
- public void storeHit(int correctGuessedLocation){
- System.out.printf("storing : %d\n\n",correctGuessedLocation);
- hitCell[storeIndex]=correctGuessedLocation;
- storeIndex++;
- }
- public boolean alreadyHit(int guessedLocation){
- System.out.printf("in alreadyHit, location passed: %d \n\n",guessedLocation);
- boolean alreadyHitFlag=false;
- for(int element:hitCell){ // check if guess was already made
- if(element==guessedLocation){
- alreadyHitFlag=true;
- break;
- }
- }
- return alreadyHitFlag;
- }
- public boolean checkSunk(int num){
- System.out.printf("\nin checksunk,num passed: %d \n\n",num);
- boolean sunkFlag=false;
- if(shipSize==num){
- sunkFlag=true;
- }
- return sunkFlag;
- }
- public void shipHasSunk(){
- System.out.print("ouch! you have sunk the ship!");
- }
- }
- class MyBattleShipGame{
- 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
- if(game.alreadyHit(guess)){
- System.out.println("already made this hit, retry");
- continue;
- }
- else{
- game.storeHit(guess);
- }
- System.out.println("hit\n");
- game.setHitNum(1+game.getHitNum()); // increments hitNum, which is initially 0
- System.out.println("incrementing hitNum\n");
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement