Java – Topic 28

(MouseListener)

 

 

TASK

 

 

QUESTION

 

I will only need the final copy of this work.  However, I highly suggest that you save your work as you go.

 

PART 1

 

Recreate applet that you see here.  Note that the message that is suppose to appear in the status bar of your browser might not work when running over the web.  You should be able to see a fully functional applet by downloading the zip file with the applet in it (no source code).

 

Hint:

You can tell which square is clicked based on the coordinates of the mouse at click time.  (See Example 2 in the note if you need help with this.

 

PART 2

 

Make a copy of your code from part 1.  Alter it so that when you click on a square, either an X or an O appear in that square.  The X and the O should be alternated.

 

Note that all painting has to be done inside the paint() method.  All your listener methods do is set data fields of the class.  The paint() method then uses those data fields to decide what to place in the squares.

 

PART 3

 

Make a copy of your code from part 2.  Add the statements required so that you cannot overwrite the value that is already in a square.

 

This will likely involve having nine variables that keep track of whether the squares are available or not.

 

PART 4

 

You already have a fully functional Tic Tac Toe game.  If it is fully functional, you will get an 80 percent.  Add any one of the following features to get a 90+.

 

a)      A “X wins” or “O wins” message is displayed when somebody has won.

b)      A reset button (simply a square that is clicked) allows you to restart the game.

c)      An AI that allows the computer to play as one of the two letters.   The computer can simply randomly pick any available square.