java help plz
Page 1 of 1
jenkins2k5




Posts: 5

PostPosted: Wed, 19th Apr 2006 18:30    Post subject: java help plz
i've been writing a battleship program, but i've hit a brick wall and my head is fried!!!
the problem is that when i set the enemy ships with the character representation of '*' in the baddiesShot method(); it puts the characters on it's own ships and on the blank spaces??? does anyone know how to prevent this i think it has something to do with this peice of code: grid[col-1][row-1]='*';

thanks for any help

here's my code:

Code:
import java.lang.Math;

public class Battleship {

    static final int MAXGRID = 6;
    static final int MAXSHIPS = 6;
    int playerScore=0;
    int computerScore=0;
    boolean play;
    char emptyGrid = '.';
    char [][] grid = new char[MAXGRID][MAXGRID];
    char [][] enemy = new char[MAXGRID][MAXGRID];
   
    //--------starts or stops the game --------------------
    private void setPlay(boolean p)
    {
        play = p;
    }
    //--------Returns the state of the play boolean --------
    private boolean getPlay()
    {
        return play;
    }
   
     private void goodieWin(){
     
     System.out.println("*********************YOU WIN!!!********************");
     
    } 
    private void baddieWin(){
       
     System.out.println("*********************YOU LOSE!!!*******************");
    } 
    //--------Fills the empty grid with dots --------------------------
    private void initialiseGrid()
    {
        for (int i = 0; i<MAXGRID; i++)
        {
            for (int j = 0; j<MAXGRID; j++)
            {
                grid[i][j] = emptyGrid;
            }//end for
        }//enf for   
    }
    //--------Draws the grid -----------------------------------------
    private void drawGrid(){
       
        System.out.println("Ships sank "+computerScore+"  Enemy sank "+playerScore);
        System.out.println("------------------------------");
        System.out.print(" ");
        for (int col = 1; col <=MAXGRID; col++)
            System.out.print("  "+col);
           
        System.out.println();
        for (int i = 0; i<MAXGRID; i++)
        {
            System.out.print(i+1);
            for (int j = 0; j<MAXGRID; j++)
            {
                System.out.print("  "+grid[j][i]);
            }//end for
            System.out.println(); 
        }//end for   
        System.out.println();
    }
 
   
    //------------------ user chooses position of goodies ships----------
    public void setGoodiesShips()
    {

        int col=0,row=0;
        boolean fine = false;
        System.out.println("You have 6 ships");
        for (int count= 0; count <MAXGRID; count++){
             do
             {
                 System.out.print("Enter a column for ship "+(count+1)+": ");
                 col=UserInput.readInt();
              if(col < 1|| col > 6)
              {
                  System.out.println("Pick again, choose between 1 and 6");
              }
              else
              {
                fine= true;
              }
             
              } while((col<=0) ||(col>6));
              do
              {
                System.out.print("Enter a row for ship "+(count+1)+": ");
                row=UserInput.readInt();
                System.out.println();
                if (row <1 || row>6)
                {
                  System.out.println("Pick again, choose between 1 and 6");
                }
                else
                {
                    fine= true;
                }
               
              } while((row<=0)||(row>6));
             grid[col-1][row-1]='0';
             drawGrid();
        }

        System.out.println("Your Ships Are Set");
        System.out.println();
    } 
   
    //------------------ set position of baddies ships----------
    public void setBaddiesShips()
        {
         int col=0,row=0;
               
             for (int i= 0; i <MAXGRID; i++){
               do{
                 
                   col=(int) (Math.random()*10);
                 } while((col<=1) ||(col>6));
                  do{
                     row=(int) (Math.random()*10);
                    } while((row<=1) ||(row>6));
                 enemy[col-1][row-1]='E';

               }
             System.out.println("6 enemy ships set");
         System.out.println();
}
    //------------------Goodies shoot ---------------------------
    public void goodiesShoot()
    {
     
      int col=0;
      int row=0;

            System.out.println("Your go: Enter -1 at any time during the game to quit ");
            System.out.println();
      do
      {   
            System.out.print("Enter a column number: ");
            col = UserInput.readInt();
            if(col == -1)
            {
            setPlay(false);
            return;
            }
            if ((col<1) || (col>6))
            {
                System.out.println("Wrong choice, enter a number between 1 - 6 only");
            }
        }while (col<1 || col>6);
     do
     {
         System.out.print("Enter a row number: ");
         row = UserInput.readInt();
         if ((row<1) || (row>6))
         {
             System.out.println("Wrong choice, enter a number between 1 - 6 only");
         }
     }while (row<1 || row>6);
         System.out.println();
         System.out.println("FIRING ....");
       
         if(enemy[row - 1][col - 1] == 'E')
         {
          grid[col - 1][row - 1] = 'E';
         enemy[col - 1][row - 1] = 'E';
          System.out.println("**Good shot**  ");
          {
              drawGrid();
              return;
          }
 
      }else if (grid[col-1][row-1] == '0')
           {
               System.out.println("**You Hit Your ship** ");
              grid[col-1 ][row-1] = '*';
           
              drawGrid();
              return;
            } else
            if (grid[col - 1][row - 1] != 'E');
            {
                grid[col - 1][row - 1] = ' ';
            }
            System.out.println("**Unlucky You Missed**");
            drawGrid();
               
 
}
    //------------------Baddies shoot ---------------------------
    public void baddiesShoot(){
        System.out.println("Computers turn");
        System.out.println("FIRING ....");
        int col=0;
        int row=0;
            do
              {     

               row=(int) (Math.random()*10);
               }while((row<=1) ||(row>6));
                do
                  { 
                  col=(int) (Math.random()*10);
                  }while((col<=1) ||(col>6));
                   
            grid[col-1][row-1]='*';
           
           
      if (grid[col-1][row-1] == '0')
        {
           grid[col-1][row-1] = '*';
           System.out.println("***Computer Hit Your Ship***");
        }else 
         {
           System.out.println("***Computer Misses Your Ship***");   
        }
       
       
        System.out.println();
         drawGrid();
       System.out.println();

    }
   
    //------------------------------------------------------------
 
    public static void main(String [] args)
    {
        Battleship bship = new Battleship();
        bship.setPlay(true);
        bship.initialiseGrid();
        bship.drawGrid();
        bship.setGoodiesShips();
        bship.setBaddiesShips();
       
        while(bship.getPlay())
        {
            bship.goodiesShoot();
            bship.baddiesShoot();
        }//end while
       
        System.out.println("Bye - thanks for playing.");
        System.exit(0);
    }//end main
}
Back to top
sabalasa




Posts: 369
Location: EST
PostPosted: Wed, 19th Apr 2006 20:57    Post subject:
Well. First review shows a bug (see below) where you replace a char in the array with '*' and then check if it's '0'. That condition will never be true, meaning the comp never hits.

Code:

//------------------Baddies shoot ---------------------------
public void baddiesShoot(){
        System.out.println("Computers turn");
        System.out.println("FIRING ....");
        int col=0;
        int row=0;
        do
        {     
               row=(int) (Math.random()*10);
        }while((row<=1) ||(row>6));
        do
        {
               col=(int) (Math.random()*10);
         }while((col<=1) ||(col>6));
                   
        grid[col-1][row-1]='*'; <----- here
           
        if (grid[col-1][row-1] == '0') <----- and here
        {
            grid[col-1][row-1] = '*';
            System.out.println("***Computer Hit Your Ship***");
        }else
       {
            System.out.println("***Computer Misses Your Ship***");   
        }
       
        System.out.println();
        drawGrid();
        System.out.println();
}


But actually I didn't quite understand what your exact problem was but if this was your problem then I'm glad I could help Wink


rgds
Sabalasa
Back to top
jenkins2k5




Posts: 5

PostPosted: Wed, 19th Apr 2006 21:12    Post subject:
i noticed those bugs too, they are fixed now, i just need the computer to stop covering it's ship with the charater representation of '*', here's the baddiesShoot method again:
Code:
]
     public void baddiesShoot(){
        System.out.println("Computers turn");
        System.out.println("FIRING ....");
        int col=0;
        int row=0;
            do
              {     

               row=(int) (Math.random()*10);
               }while((row<1) ||(row>6));
                do
                  { 
                  col=(int) (Math.random()*10);
                  }while((col<1) ||(col>6));
   
      if (grid[col-1][row-1] == 'E')
        {
           grid[col-1][row-1] = 'E';
           System.out.println("***Computer Misses Your Ship***");

        }
        else if (grid[col-1][row-1] == '0')
        {
          grid[col-1][row-1] = '*';
          System.out.println("***Computer Hits Your Ship***");   

        }
        else if (grid[col-1][row-1] == '.')
        {
          grid[col-1][row-1] = '*';
          System.out.println("***Computer Misses Your Ship***");   

        }
                 System.out.println();
                 drawGrid();
                System.out.println();
    }
   

any1 have any suggestions?
Back to top
LarzNFO




Posts: 147

PostPosted: Sat, 29th Apr 2006 18:33    Post subject:
A good program starts with a good design. Make a design (OO, since Java is OO) first, then build it. Procedural programming in an OO language is not a good idea (although you could probably get it to work).
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group