2 Replies - 239 Views - Last Post: 13 April 2019 - 06:41 PM Rate Topic: -----

#1 dhivyava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-April 19

battleship in java

Posted 12 April 2019 - 04:20 PM

Hello Everyone I have an battleship program that I would like to improve in performance. I know that using certain algorithms it can be improve however I am beginner in Java. I would like any recommendations and or hint in how to improve this game. The are 5 ships per game and the computers plays 10000. I just want to sink all ships in as many shots as possible. Down my code thanks for any help.
package solution;

import battleship.BattleShip;

/**
 * Starting code for Comp10152 - Lab#6 
 * @author mark.yendt
 */
public class COMP10152_Lab6
{
   static final int NUMBEROFGAMES = 1000;
   public static void startingSolution()
  {
    int totalShots = 0;
    System.out.println(BattleShip.version());

        BattleShip battleShip = new BattleShip();

        SampleBot sampleBot = new SampleBot(battleShip);

        int stats[][] = new int[10][10];

 

 
            sampleBot = new SampleBot(battleShip);

            

    
    for (int game = 0; game < NUMBEROFGAMES; game++) {

      battleShip = new BattleShip();
      sampleBot = new SampleBot(battleShip);
     
       sampleBot.stats = stats;

      // Call SampleBot Fire randomly - You need to make this better!
      while (!battleShip.allSunk()) {
        sampleBot.fireRandomShot();
        sampleBot.printStats(stats);
      try
      {sampleBot.wait(1);
      }catch(Exception ex)
      {
          
      }}
      int gameShots = battleShip.totalShotsTaken();
      totalShots += gameShots;
    }
    System.out.printf("SampleBot - The Average # of Shots required in %d games to sink all Ships = %.2f\n", NUMBEROFGAMES, (double)totalShots / NUMBEROFGAMES);
    
  }
  public static void main(String[] args)
  {
    startingSolution();
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: battleship in java

#2 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11497
  • View blog
  • Posts: 19,569
  • Joined: 19-March 11

Re: battleship in java

Posted 12 April 2019 - 09:28 PM

While we're happy to help with your homework, I think it'll make more sense if you go first. What have you tried, or thought of, to improve the part just after the comment saying // Call SampleBot Fire randomly - You need to make this better?

Spell out what you've tried or thought of, and ask questions, and I'm sure you'll get good answers to your questions.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12585
  • View blog
  • Posts: 45,711
  • Joined: 27-December 08

Re: battleship in java

Posted 13 April 2019 - 06:41 PM

I would also encourage you to properly indent your code. Indentations make it easier to recognize code blocks. If a section of code belongs to an if statement, it should be properly indented further inwards to denote this.

As it stands, your code is very hard to read; and therefore, hard to debug.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1