When the game first starts the applet is blank as in

This preview shows page 15 - 18 out of 20 pages.

When the game first starts, the applet is blank as in the screen shot to the right. Whenever the mouse is pressed, the corresponding grid location is drawn in either red or blue, depending on whether that grid location corresponds to a ship or the sea. An example of the game in play is shown in the following series of screenshots, where the darker rectangles (red) represent a hit ship and the lighter rectangles (blue) are missed shots into the sea: You must complete the paint() method for this applet. You will need to cycle through all of the elements in the shots array. For each element that is true , which indicates a shot has been fired, you need to draw a rectangle in the corresponding grid location. The colour of the rectangle should be red if the corresponding element of the layout array represents a ship, or blue if the corresponding element of the layout array represents the sea. The GRID_SIZE constant determines how wide and high the grid rectangle should be drawn. Take care with X (for columns) and Y (for rows) positions in respect to the shots array. CONTINUED
Image of page 15

Subscribe to view the full document.

Question/Answer Sheet - Page 16 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... import java.awt.*; import java.applet.*; import java.awt.event.*; public class Battleship extends Applet implements MouseListener { static final int GRID_SIZE = 15; // Size that grids are drawn static final int X_DIMENSION = 8; // X dimension for the arrays static final int Y_DIMENSION = 8; // Y dimension for the arrays static final int SEA = 0; // Value for a sea position static final int SHIP = 1; // Value for a ship position // Array containing positions of ships and sea static final int[][] layout = { {SEA, SEA, SEA, SEA, SEA, SEA, SEA, SEA}, {SEA, SEA, SEA, SEA, SEA, SEA, SHIP, SHIP}, {SHIP, SEA, SEA, SEA, SEA, SEA, SEA, SEA}, {SHIP, SEA, SEA, SEA, SEA, SEA, SEA, SEA}, {SHIP, SEA, SEA, SEA, SEA, SHIP, SEA, SEA}, {SEA, SEA, SEA, SEA, SHIP, SEA, SEA, SEA}, {SEA, SEA, SEA, SHIP, SEA, SEA, SEA, SEA}, {SEA, SEA, SHIP, SEA, SEA, SEA, SEA, SEA} }; // Array to record if a shot has been made in a grid position private boolean[][] shots=new boolean[Y_DIMENSION][X_DIMENSION]; // Initialise the shots array to false (no shots) public void init() { for (int i = 0; i < shots.length; i++) for (int j = 0; j < shots[i].length; j++) shots[i][j] = false; addMouseListener(this); } /** Gets the mouse position If it is inside the playing area then record a shot by setting the corresponding element in the shots array to true, and repaint the screen @param e used to find location of mouse press */ public void mousePressed(MouseEvent e) { int mouseX = e.getX(); int mouseY = e.getY(); if ( (mouseX < X_DIMENSION * GRID_SIZE) && (mouseY < Y_DIMENSION * GRID_SIZE) ) shots[mouseY/GRID_SIZE][mouseX/GRID_SIZE] = true; repaint(); } CONTINUED
Image of page 16
Question/Answer Sheet - Page 17 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... /** Examine every element in the shots array: For each element that is true, examine the corresponding element in the layout array. Draw a rectangle in red if this represents a ship, and draw a rectangle in blue if this represents the sea.
Image of page 17

Subscribe to view the full document.

Image of page 18
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, ........., University Of Auckland

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern