Advertisement
defineSN

MyBattleshipGame.java

Oct 31st, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 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.     private int hitCell[]= new int[8];
  7.     Scanner input = new Scanner(System.in);
  8.  
  9.     // setters :
  10.    
  11.     public void setShipSize(int size){
  12.         shipSize=size; 
  13.     }
  14.     public void setLocation(int cellLocation, int index){
  15.         shipCell[index]=cellLocation;
  16.     }
  17.     public void setHitNum(int num){
  18.         hitNum=num;
  19.     }
  20.     public void setHitCell(int num,int index){
  21.         hitCell[index]=num;
  22.     }  
  23.    
  24.     // getters :
  25.    
  26.     public int getShipSize(){
  27.         return shipSize;
  28.     }
  29.     public int getHitNum(){
  30.         return hitNum;
  31.     }
  32.  
  33.     //methods :
  34.    
  35.     public void putShipInBattlezone(){
  36.        
  37.         int randomNum=(int)(Math.random()*15); // the virtual array is of size 15
  38.        
  39.        
  40.        
  41.        
  42.         for(int i=0;i<shipSize;i++){
  43.            
  44.             setLocation((randomNum+i),i);
  45.            
  46.            
  47.         }
  48.         System.out.println("\n\nbattleship placed.. let the destruction begin!!\n\n......................................................");
  49.     }
  50.    
  51.    
  52.     public boolean checkHit(int guessedLocation,int size){
  53.        
  54.         System.out.printf("in checkHit, location passed: %d  size passed: %d \n",guessedLocation,size);
  55.         boolean checkHitFlag=false;
  56.         for(int i=0;i<size;i++){
  57.             if(shipCell[i]==guessedLocation){
  58.                 checkHitFlag = true;
  59.                 break;          
  60.             }          
  61.            
  62.         }
  63.         return checkHitFlag;
  64.     }
  65.    
  66.     static int storeIndex=0;
  67.     public void storeHit(int correctGuessedLocation){
  68.        
  69.         System.out.printf("storing : %d\n\n",correctGuessedLocation);
  70.         hitCell[storeIndex]=correctGuessedLocation;
  71.         storeIndex++;  
  72.     }
  73.    
  74.     public boolean alreadyHit(int guessedLocation){
  75.        
  76.         System.out.printf("in alreadyHit, location passed: %d \n\n",guessedLocation);
  77.        
  78.         boolean alreadyHitFlag=false;
  79.         for(int element:hitCell){ // check if guess was already made
  80.             if(element==guessedLocation){
  81.                 alreadyHitFlag=true;
  82.                 break;         
  83.             }
  84.         }
  85.         return alreadyHitFlag;
  86.     }
  87.    
  88.    
  89.     public boolean checkSunk(int num){
  90.        
  91.         System.out.printf("\nin checksunk,num passed: %d \n\n",num);
  92.        
  93.         boolean sunkFlag=false;
  94.        
  95.         if(shipSize==num){
  96.             sunkFlag=true;
  97.         }
  98.        
  99.         return sunkFlag;
  100.     }
  101.     public void shipHasSunk(){
  102.         System.out.print("ouch! you have sunk the ship!"); 
  103.    
  104.     }
  105.    
  106. }
  107.  
  108. class MyBattleShipGame{
  109.    
  110.     public static void main (String [] args) {
  111.  
  112.         Scanner input = new Scanner(System.in);
  113.         SimpleDotCom game = new SimpleDotCom();
  114.        
  115.         System.out.print("enter size of ship: "); // ship size entered
  116.         game.setShipSize(input.nextInt());
  117.         System.out.printf("entered ship size is: %d \n",game.getShipSize());
  118.        
  119.         game.putShipInBattlezone(); // ship is now put on battle feild, let the destruction begin
  120.        
  121.         int doFlag=0,hits=0;
  122.         do{
  123.             int guess=0; // make a guess
  124.             System.out.print("enter guess (0 to 10) : ");
  125.             guess=input.nextInt();
  126.            
  127.             if(game.checkHit(guess,game.getShipSize())){ // check if guess was correct
  128.                
  129.                 if(game.alreadyHit(guess)){
  130.                     System.out.println("already made this hit, retry");
  131.                     continue;              
  132.                 }
  133.                 else{
  134.                     game.storeHit(guess);
  135.                    
  136.                 }
  137.                 System.out.println("hit\n");
  138.                
  139.                 game.setHitNum(1+game.getHitNum()); // increments hitNum, which is initially 0
  140.                 System.out.println("incrementing hitNum\n");
  141.                 hits=game.getHitNum();
  142.                 System.out.printf("total hits scored: %d \n",hits);
  143.                
  144.                 if(game.checkSunk(hits)){ // passes incremented value of hitNum to sunk() to see if ship has sunk
  145.                     doFlag=1;
  146.                     game.shipHasSunk();
  147.                     break;
  148.                 }
  149.             }
  150.             else{
  151.                 System.out.println("miss");
  152.             }  
  153.         }while(doFlag!=1); // until ship has sunk
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement