Assignment #2 SOS Game

Due Date: November 7

You may do this assignment in pairs.

Write a program to play a game of SOS. The program will allow the users to press a button to update the score. It will be played on a 10 x 10 grid. The user can click a square to place an S. If they click the same square again it will change to an 0. If they click again it will go back to blank. Cycle through those three choices as necessary. When an SOS is formed the user can draw a line through the letters by dragging the mouse. The program will automatically decide whether the user wants to place a letter or draw a line. Here is the initial screen:

Here is the screen after the first player has scored an SOS:

The user had to draw the line and they clicked the button to update the score. Players take turns placing letters unless they can form an SOS. If they form an SOS they get to go again.

You can do this program from scratch if you like. If you would like some help you can use the following template that has had parts of the working program removed.

public class Sos
{
   Point[] points = new Point[1000];
   int pointCount = 0;
   int [][] board = new int[10][10];
   int x = -1, y, x1, x2, y1, y2;
   JFrame frame = new JFrame("SOS Game");
   ImageIcon blankImage = new ImageIcon("blank.jpg");
   int score1 = 0, score2 = 0;
   
   public Sos()
   {
      frame.setSize(509, 570);
      score1Label.setFont(new Font("Serif", Font.BOLD, 26));
   }

   class Drawing extends JComponent
   {
      public void paint(Graphics g)
      {
         for (int i = 0; i <= 9; i++)
            for (int j = 0; j <= 9; j++)
            {
               int n = board[i][j] % 3;
               if (n == 0)
                  g.drawImage(blankImage.getImage(), i * 50 + 1, j * 50 + 1, 49, 49, this);
                  
      }

   }

   class MouseListen extends MouseAdapter
   {
      public void mouseReleased(MouseEvent e)
      {
         x2 = e.getX();
         y2 = e.getY();
         if (Math.abs(x2 - x1) < 3 && Math.abs(y2 - y1) < 3)
         
         else
         {
            points[pointCount++] = new Point(x1, y1);
            
         }
      }
      
   }
}  

You can also use the following image files if you like: the S image, the O image and the blank image.

The tricky thing about this program is that the repaint will clear all the drawings off the screen that we've previously placed there. We need some way to remember all the things that were on the screen so we can redraw them. To do this we can use a 2-D array to keep track of where all the S's and O's are. We use another array to store all the end points of the lines that the user has drawn. The paint method needs to go through these arrays to recreate the screen every time a new letter or line is added.

              Marking Scheme
____
 17

Proper Layout                (2) ______

Score buttons & labels       (3) ______

Places S & O properly        (3) ______

Draws lines properly         (3) ______

Remembers lines & letters    (3) ______

Process Work                 (3) ______