Advertisement
defineSN

MyBattleShipGame2.java

Oct 31st, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class SimpleDotCom{
  4.     private int hitNum=0,shipSize=0;
  5.     private int shipCell[]= new int[8];
  6.     Scanner input = new Scanner(System.in);
  7.  
  8.     // setters :
  9.    
  10.     public void setShipSize(int size){
  11.         shipSize=size; 
  12.     }
  13.     public void setLocation(int cellLocation, int index){
  14.         shipCell[index]=cellLocation;
  15.     }
  16.     public void setHitNum(int num){
  17.         hitNum=num;
  18.     }
  19.        
  20.    
  21.     // getters :
  22.    
  23.     public int getShipSize(){
  24.         return shipSize;
  25.     }
  26.     public int getHitNum(){
  27.         return hitNum;
  28.     }
  29.  
  30.     //methods :
  31.    
  32.     public void putShipInBattlezone(){
  33.        
  34.         int randomNum=(int)(Math.random()*15); // the virtual array is of size 15
  35.        
  36.         for(int i=0;i<shipSize;i++){           
  37.             setLocation((randomNum+i),i);
  38.         }
  39.         System.out.println("\n\nbattleship placed.. let the destruction begin!!\n\n......................................................");
  40.     }
  41.    
  42.    
  43.     public boolean checkHit(int guessedLocation,int size){
  44.        
  45.         System.out.printf("in checkHit, location passed: %d  size passed: %d \n",guessedLocation,size);
  46.         boolean checkHitFlag=false;
  47.        
  48.         for(int i=0;i<size;i++){
  49.             if(shipCell[i]==guessedLocation){
  50.                 setLocation(-1,i); // setLocation takes a value and an index, here change the hit cell's value to -1, so we know its been hit
  51.                 checkHitFlag = true;
  52.                 break;          
  53.             }          
  54.            
  55.         }
  56.         return checkHitFlag;
  57.     }
  58.    
  59.        
  60.        
  61.    
  62.     public boolean checkSunk(int num){
  63.        
  64.         System.out.printf("\nin checksunk,num passed: %d \n\n",num);
  65.         boolean sunkFlag=false;
  66.        
  67.         if(shipSize==num){
  68.             sunkFlag=true;
  69.         }      
  70.         return sunkFlag;
  71.     }
  72.     public void shipHasSunk(){
  73.         System.out.print("ouch! you have sunk the ship!"); 
  74.     }  
  75. }
  76.  
  77. class MyBattleShipGame2{
  78.    
  79.     public static void main (String [] args) {
  80.  
  81.         Scanner input = new Scanner(System.in);
  82.         SimpleDotCom game = new SimpleDotCom();
  83.        
  84.         System.out.print("enter size of ship: "); // ship size entered
  85.         game.setShipSize(input.nextInt());
  86.         System.out.printf("entered ship size is: %d \n",game.getShipSize());
  87.        
  88.         game.putShipInBattlezone(); // ship is now put on battle feild, let the destruction begin
  89.        
  90.         int doFlag=0,hits=0;
  91.         do{
  92.             int guess=0; // make a guess
  93.             System.out.print("enter guess (+ve values only) : ");
  94.             guess=input.nextInt();
  95.            
  96.             if(game.checkHit(guess,game.getShipSize())){ // check if guess was correct
  97.                
  98.                 System.out.println("hit\n");
  99.                
  100.                 game.setHitNum(1+game.getHitNum()); // increments hitNum, which is initially 0
  101.                 System.out.println("incrementing hitNum\n");
  102.                 hits=game.getHitNum();
  103.                 System.out.printf("total hits scored: %d \n",hits);
  104.                
  105.                 if(game.checkSunk(hits)){ // passes incremented value of hitNum to sunk() to see if ship has sunk
  106.                     doFlag=1;
  107.                     game.shipHasSunk();
  108.                     break;
  109.                 }
  110.             }
  111.             else{
  112.                 System.out.println("miss");
  113.             }  
  114.         }while(doFlag!=1); // until ship has sunk
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement