19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

688 Chapter 18 Applets and Multimedia<br />

The tic-tac-<strong>to</strong>e board consists of nine cells, created using new Cell[3][3]. To determine<br />

which player’s turn it is, you can introduce a variable named whoseTurn of the char type.<br />

whoseTurn is initially 'X', then changes <strong>to</strong> 'O', and subsequently changes between 'X' and<br />

'O' whenever a new cell is occupied. When the game is over, set whoseTurn <strong>to</strong> ' '.<br />

How do you know whether the game is over, whether there is a winner, and who the winner,<br />

if any, is? You can define a method named isWon(char <strong>to</strong>ken) <strong>to</strong> check whether a<br />

specified <strong>to</strong>ken has won and a method named isFull() <strong>to</strong> check whether all the cells are<br />

occupied.<br />

Clearly, two classes emerge from the foregoing analysis. One is the Cell class, which handles<br />

operations for a single cell; the other is the TicTacToe class, which plays the whole<br />

game and deals with all the cells. The relationship between these two classes is shown in<br />

Figure 18.12.<br />

Cell<br />

javax.swing.JApplet<br />

9<br />

1<br />

TicTacToe<br />

-whoseTurn: char<br />

-cell: Cell[][]<br />

-jlblStatus: JLabel<br />

+TicTacToe()<br />

+isFull(): boolean<br />

+isWon(<strong>to</strong>ken: char): boolean<br />

Indicates which player has the turn, initially X.<br />

A 3 3, two-dimensional array for cells.<br />

A label <strong>to</strong> display game status.<br />

Constructs the TicTacToe user interface.<br />

Returns true if all cells are filled.<br />

Returns true if a player with the specified <strong>to</strong>ken has won.<br />

FIGURE 18.12<br />

The TicTacToe class contains nine cells.<br />

Since the Cell class is only <strong>to</strong> support the TicTacToe class, it can be defined as an inner<br />

class in TicTacToe. The <strong>com</strong>plete program is given in Listing 18.10.<br />

main class TicTacToe<br />

LISTING 18.10<br />

TicTacToe.java<br />

1 import java.awt.*;<br />

2 import java.awt.event.*;<br />

3 import javax.swing.*;<br />

4 import javax.swing.border.LineBorder;<br />

5<br />

6 public class TicTacToe extends JApplet {<br />

7 // Indicate which player has a turn; initially it is the X player<br />

8 private char whoseTurn = 'X';<br />

9<br />

10 // Create and initialize cells<br />

11 private Cell[][] cells = new Cell[3][3];<br />

12<br />

13 // Create and initialize a status label<br />

14 private JLabel jlblStatus = new JLabel("X's turn <strong>to</strong> play");<br />

15<br />

16 /** Initialize UI */<br />

17 public TicTacToe() {<br />

18 // Panel p <strong>to</strong> hold cells

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!