email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

Battleship

Steps:

  1. Create the Location class (we called it Space). This is the class that will be sent back and forth. It will have:
    • IT NEEDS TO implement Serializable //and have import java.io.*;
    • r,c (for the row, col)
  2. Create the Ship class. This class will have: (I didnt include type and position-which might be useful if you draw the images of boats)
    • int size, boolean horizonta, int id
    • Constructor will set all of those (ie public Ship (int s,boolean h, int theID) )l
  3. Create the Board class
    • IT NEEDS TO implement Serializable //and have import java.io.*;
    • int grid[][] 10*10 where :
      • value item
        0 blank
        1,2,3... the ship id of alive parts of ship
        -1,-2,-3... the ship id of dead parts of ship
        -99 misses (just a random number away from id of ships
    • int width ->how large you want the boxes in your grid drawn maybe make it 50 originally?
    • int offsetX, offsetY -> this is how the corner of where the board will be drawn. Because there will be two boards drawn this is used so they are in unique places.
    • public Board(int offX,int offY) ->the constructor will set those offsets when the board is created.
    • Methods:
      • public void drawBoard(Graphics g, boolean showShips) //someone suggests this to draw the ships or not)
        • you will print out every square with width (you will also be using offsetX,offsetY) and the appropriate color [if showShips is true, you will be drawing the ship, otherwise it would be hit [if -1,-2,...] or blank (1,2,3]
      • public Location convertToGrid(int x,int y)
        • this will translate the x and y into a Location using offsetX, offsetY and width
        • it will return null if off the grid
      • public boolean validPlacement (Ship ship,Location p)
        • it will return true if the placement of the ship is valid (being valid means all spots are in the grid and open). To do this:
          • for every spot of the ship
            • if the ship is horizontal look at every spot horizontally making sure its in the grid and open (meaning the spot is blank)
            • if the ship is vertical look at every spot horizontally making sure its in the grid and open (meaning the spot is blank)
      • public boolean setShip(Ship ship, int x, int y) //just called during setup of game
        • this will convert x,y to a location (using above)
        • if that location is valid, check to see if the placement is valid (using above)
        • if that is the case, for every spot of the ship, set the grid to the id of the ship.
      • public String dropBomb (Location bomb) //we wont need gameStatus because the only time a game will be over is with this method which is called by each client every time.
        • this will determine if the bomb has hit one of the ships
        • it will return:
        • code  
          "invalid" this would happen if the location is null or grid has a bomb there already

          "miss"

           
          "hit"  
          "sunk" if the entire ship has been hit
          "over" if all ships have been hit
        • have if statements that check all of above.
          • you might want helper methods to check to see if the entire ship has been hit like:
          • public boolean anyOtherShip(int ship)
            • this will take in the id of the ship that was hit and check to see if it still exists in the grid
          • public boolean anyOtherShips()
            • this will search the entire grid looking for alive parts [note they are all positive]
  4. Create the Battleship class [ public class Battleship extends WatcherFrame implements MouseListener, KeyListener //you will need all the necessary imports and have the network folder from before.
    • int currentShip=0 which will keep track of which ship you are currently placing
    • Ship[] fleet=new Ship[5?];
    • Board userBoard =new Board (with whatever offset is appropriate)
    • Board oppBoard =new Board (with whatever offset is appropriate)
    • maybe a textfield to show information
    • boolean opponentSet=false; //this will keep track if you have received a board yet.
    • String gameStatus which is:
    • code  
      "setup" only until all boats have been placed
      "set" if all boats have been placed, but you are waiting for the opponent to setup
      "usersTurn"  
      "oppTurn"  
      "over"  
      • init
        • do usual stuff
        • initialize all your ships in fleet
      • paint //you can but dont have to use bufferGraphics
        • will paint buttons
        • have userBoard and oppBoard draw itself
      • keyPressed will rotate the ship. Maybe have it show up in the textfield of which way the boat is going?
      • mousePressed
        • if (gameStatus.equals("setup"))
          • try to place a boat -> remember setShip either returns true or false
          • if successful, increment which boat you are on (currentShip)
          • if you have placed all of your boats then:
            • send your board to the opponent ( connection.sendObject(userBoard);
            • if opponentSet is true (meaning you already have their board), well then game on,
              • tell user and gameStatus="oppTurn";
            • if opponentSet is false, well you need to wait,
              • tell user and gameStatus="set";
          • repaint();
        • if (gameStatus.equals("usersTurn"))
          • get the Location on the grid that matches the x,y
          • try to drop a bomb at that location -> remember what dropBomb returns
          • if that dropBomb is invalid
            • tell user
          • otherwise bomb is valid:
            • tell user what dropBomb told you
            • send the location to the oppoent ( connection.sendObject(bombLocation);
            • if game over
              • tell user
              • chage gameStatus="over"
            • repaint();
      • receiveUpdate (Object b) //this is the method that will get called if we receive something from the other client)
        • if (opponentSet==false) //well then we must be receiving the board from them
          • oppBoard.grid=((Board)(b)).grid; //this replaces the grid with what we received
          • opponentSet=true;
        • otherwise //then we are receiving locations of bombs
          • Location bomb=(Location)b; //convert it to a location
          • now call usersBoard droppping the bomb. remember dropBomb returns a message
          • process that message (tell user outcome)
          • if over ->tell user they lost
          • else gameStatus ="usersTurn" //it had to have been oppTurn to have received a message, true?

    •