d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Gui Battleship Game Project > Need Serious Assistance!
123Next
Add Reply New Topic New Poll
Member
Posts: 4,993
Joined: Aug 30 2009
Gold: 100.00
Apr 25 2010 11:08am
Hey everyone, it's approaching the end of the semester, and the last project I have to do is to create a Battleship game in a GUI.

I need to know how to start it. I have to create 3 programs, Ship.java, Square.java, and BattleBoard.java.

Ship.java:

Code
import java.util.*;

/**
* This class represents a single ship in a fleet of ships.
* It knows the length of the ship, the number of times it
* has been hit, it's orientation, the row with the upper
* left corner of the Ship, and the column containing the
* upper left corner of the Ship.
* @author Jackson Yuen
* @version 1.0
*/
public class Ship {

/**
 * This constructor must initialize the class's instance
 * variables using the parameter values.
 */
public Ship(int length, boolean isHorizontal, int startRow, int startCol) {
}

/**
 * This returns the length of the ship
 * @return length the length of the ship
 */
public int getLength() {
}

/**
 * This returns true if the ship has horizontal orientation,
 * false otherwise
 * @return true if ship has horizontal orientation
 * @return false if ship has vertical orientation
 */
public boolean isHorizontal {
}

/**
 * This returns the row of the upper left corner of
 * the ship.
 * @return int row of the upper left corner of the ship
 */
public int getStartRow() {
}

/**
 * This returns the column of the upper left corner of
 * the ship
 * @return int column of the upper left corner of the ship
 */
public int getStartCol() {
}

/**
 * This simulates hitting the ship (updates the appropriate
 * instance variable)
 */
public void hit() {
}

/**
 * This returns true if the ship has been sunk (hit as
 * many times as its length). It's false otherwise.
 * @return true if ship has been sunk
 * @return false if ship has not been sunk
 */
public boolean isSunk() {
}

/**
 * This returns a String representation of a Ship that lists
 * its length, location, orientation, number of times hit,
 * and if the ship is sunk. This will list all the info
 * needed about the ship.
 * @return
 */
public String toString() {
}
}



Square.java requirements:

The Square class represents a location in a BattleBoard's grid. A Square knows:

-if it has been hit by enemy fire
-the Ship located in its space (if there is a Ship)

This class will have private fields (instance variables) sufficient to keep track of the above information and it must have the following methods:

public Square(): Providing a constructor for this class is optional. Java will provide one if you choose not to, setting the instance variable keeping track of whether the square has been hit to false and setting the reference to a Ship to null.

public boolean hasBeenHit(): returns true if the Square has been hit by enemy fire.

public Ship getShip(): returns a Ship instance if there is a ship that includes this square or null if there is no such ship

public void fireAt(): the Square should update the fact that it has been hit and, if appropriate, let a ship occupying the square know that it has been hit.

public boolean hasShip(): returns true if the Square contains a Ship.

public void addShip(Ship ship): add the given Ship to the Square.

public String toString(): Returns a String representation of a Square as follows:

"-" (if the square does not contain a ship and has not been hit)
"W" (if the square has been hit and does not contain a ship)
"R" (if the square has been hit and contains a ship)
"1","2","3", or "4" (if the square has not been hit and contains a ship -- the number returned should correspond to the length of the ship)
These single-character representations allow a BattleBoard object to be printed in a compact form.


BattleBoard.java requirements:

The BattleBoard class is a model template for the human player's and computer's boards in BattleShip. A BattleBoard contains the following fields (instance variables):

-a 2 dimensional array of Squares
-an array of Ships
-the number of Ships on the board.

public BattleBoard(): This is the constructor of the BattleBoard class. The fields should be initialized. BattleGame provides three constants: BattleGame.ROWS, BattleGame.COLS, and BattleGame.NUMBER_OF_SHIPS. These constants should be used to initialize the BattleBoard's fields since the same constants are used to create the GUI.

public boolean addShip(int length, boolean isHorizontal, int startRow, int startCol): adds a Ship to the board. This means that all Squares the length of the ship starting at the startRow and startCol will be updated appropriately. The isHorizontal determines which direction to fill Squares. The method returns true if the Ship is successfully added to the BattleBoard and false if the Ship could not be added due to another Ship in the way, reaching a BattleBoard boundary, or if the array of Ships is full. If the Ship cannot be placed, then the squares should not change.

public int getNumberOfShips(): returns the number of Ships deployed on the BattleBoard.

public Ship[] getShips(): returns an array of the Ships deployed on the BattleBoard.

public boolean fireAtLocation(int row, int col): returns true if the enemy can successfully fire at the Square located at the specified row and column and actually fires at the location before returning. If the Square has already been fired at, the method should return false.

public boolean hasBeenHit(int row, int col): returns true if the enemy has already fired on the Square located at the specified row and column.

public boolean areAllShipsSunk(): returns true if all of the Ships on the BattleBoard have been sunk by enemy fire.

public int getNumberOfRows(): returns the number of rows in the BattleBoard.

public int getNumberOfColumns(): returns the number of columns in the BattleBoard.

public String toString(): returns a String representation of a BattleBoard. HINT: Using the toString() method of the Square class, you can easily create a String representation of the BattleBoard similar to the following, which will come in handy for testing and debugging:


4 4 R 4 - - - -
- - - - - - W -
- W - 2 2 - - -
- - - - - W - -
- W - - - - R -
- - - 1 - - 3 -
- - - - - - R -
- W - - - - - -

I tried starting this project, and I just don't know where to start. I know it's at Ship.java, but I'm not sure how to go about creating it.

Help is appreciated! Explanation is best because I want to understand it.

If there is a price, let me know as well.
Member
Posts: 3,689
Joined: Jun 23 2008
Gold: 19,297.00
Apr 25 2010 02:26pm
arent you hungarian?
Member
Posts: 4,993
Joined: Aug 30 2009
Gold: 100.00
Apr 25 2010 03:42pm
Quote (Rune_Warhammer @ Apr 25 2010 04:26pm)
arent you hungarian?


How does this relate to my question?

And no I'm Chinese. I got most of Ship done now!
Member
Posts: 3,689
Joined: Jun 23 2008
Gold: 19,297.00
Apr 25 2010 05:00pm
becouse my class just got the same problem to solve :D
if u want i can copy some of my code
Member
Posts: 9,475
Joined: Mar 14 2005
Gold: 110.00
Apr 25 2010 07:57pm
All of the instructions are spelled out for you. Without saying why you are stuck, we can only assume you are a moron. If we assume you are a moron, then someone would have to type up half a text book and no one feels like doing that.

Seriously, this assignment is like paint by numbers...
Banned
Posts: 3,326
Joined: Apr 25 2010
Gold: Locked
Trader: Scammer
Warn: 50%
Apr 25 2010 10:17pm
Without all the details im simply making assumptions so you will need to modify this to suit your needs. Just for shits and giggles i think i will work on the rest as well.

Ship:

Code
package battlefield;

public class Ship {

   private int length = 0;
   private int startRow = 0;
   private int startCol = 0;
   private int numHit = 0;
   private boolean isHorizontal = false;
   private boolean isHit = false;
   private boolean isSunk = false;

   public Ship(int length, boolean isHorizontal, int startRow, int startCol) {
       this.length = length;
       this.isHorizontal = isHorizontal;
       this.startRow = startRow;
       this.startCol = startCol;
   }

   public int getLength() {
       return length;
   }

   public boolean isHorizontal() {
       return isHorizontal;
   }

   public int getStartRow() {
       return startRow;
   }

   public int getStartCol() {
       return startCol;
   }

   public void setHit(boolean value) {
       isHit = value;
       numHit++;
   }

   public boolean getHit() {
       return isHit;
   }

   public void setSunk(boolean value) {
       isSunk = value;
   }

   public boolean getSunk() {
       return isSunk;
   }

   @Override
   public String toString() {
       String temp = "Ship Length: " + length;

       temp += "Number Of Times Hit: " + numHit + "\nCurrent Direction: ";

       if(this.isHorizontal()) {
           temp += "Horizontal";
       }
       else {
           temp += "Not Horizontal";
       }

       temp += "\nAre We Sunk?: " + this.getSunk();
       
       return temp;

   }
}


Squares:

Code

package battlefield;

public class Square{

   private int xPos = 0;
   private int yPos = 0;

   private boolean beenHit = false;

   private Ship myShip;

   public Square(int xPos, int yPos) {
       this.xPos = xPos;
       this.yPos = yPos;
   }

   public int getXPos() {
       return xPos;
   }

   public int getYPos() {
       return yPos;
   }

   public void firedAt() {
       beenHit = true;

       if(this.hasShip()) {
           myShip.setHit(true);
       }
   }

   public boolean hasBeenHit() {
       return beenHit;
   }

   public boolean hasShip() {
       if(myShip != null)
           return true;
       else
           return false;
   }

   public void addShip(Ship ship) {
       if(myShip != null) {
           myShip = ship;
       }
       else
           System.out.println("There is a ship here already. Error??");
   }

   public Ship getShip() {
       return myShip;
   }

   @Override
   public String toString() {

       if(!this.hasShip() && !this.hasBeenHit())
           return "-";
       else if(this.hasBeenHit() && this.hasShip())
           return "W";
       else if(this.hasBeenHit() && this.hasShip())
           return "R";
       else if(this.hasShip()) {
           switch(myShip.getLength()) {
               case 1:
                   return "1";
               case 2:
                   return "2";
               case 3:
                   return "3";
               case 4:
                   return "4";
           }
       }

       return "Error";
   }
}


Will update more as i do it.

This post was edited by Telecom on Apr 25 2010 10:43pm
Member
Posts: 4,993
Joined: Aug 30 2009
Gold: 100.00
Apr 25 2010 10:52pm
Quote (llamaoo7 @ Apr 25 2010 09:57pm)
All of the instructions are spelled out for you.  Without saying why you are stuck, we can only assume you are a moron.  If we assume you are a moron, then someone would have to type up half a text book and no one feels like doing that.

Seriously, this assignment is like paint by numbers...


Yes they are spelled out, but I wasn't sure on how to start it.

I already did most of the ship portion. I'm getting steadily along.

I know the basics on how to do it. but now I know how to do some parts of it.

And Telecom, I'll look at your code and comprehend it, but chances are I'm not going to copy it completely. Thank you very much for the detailed feedback.

And Warhammer I think it's a common java project for beginners. ;)

This post was edited by Skai20 on Apr 25 2010 10:52pm
Member
Posts: 98
Joined: Apr 14 2010
Gold: 0.00
Apr 26 2010 08:19am
Where is the GUI part?
Member
Posts: 4,993
Joined: Aug 30 2009
Gold: 100.00
Apr 26 2010 10:22am
Quote (BookbinderKoch @ Apr 26 2010 10:19am)
Where is the GUI part?


i didn't list it, but it's one of the programs that's given to us

there are 8 in total. 5 are given, and we have to create 3 of them ourselves, which i listed above. i'm working on them right now
Banned
Posts: 3,326
Joined: Apr 25 2010
Gold: Locked
Trader: Scammer
Warn: 50%
Apr 26 2010 09:26pm
Hows your implementation going? Need any other help?
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll