Step 5


Overview

In this step you will modify the applet so that it alternates plays between the user and the computer. This requires adding a mechanism for making computer plays. This step will also add code to change the message to the user at the bottom of the applet. The message will tell the user what to do next.

The second and third sections of this step describe how to get the players to alternate. The second adds an otherPlayer() method to the TTTCell class. The third describes how to invoke otherPlayer() in the TicTacToe init() method.

By themselves, these changes do not make any sense because there is no mechanism for making plays by the computer. That issue is tackled in the fourth and fifth sections. The fourth adds a getBestPlay() method to the TicTacToe class. The fifth registers an action listener on the "Make Computer Play" button. This listener invokes getBestPlay() for the computer.

The sixth and seventh sections deal with the user message. The sixth involves writing a updateStatus() method that sets the message according to whose turn it is. The seventh describes where to invoke updateStatus().

The TTTCell Class otherPlayer() Method

This method should be declared static, indicating that it is handled by the class rather than an object. It returns a int and has a int parameter named plyr. You want the method to return TTTCell.X if plyr is TTTCell.O and vice versa. The easiest way to do this is to return the value 3 - plyr. To see why this works, look at the int values defined by TTTCell.X and TTTCell.O.

This is the only change to the TTTCell class for this step. The rest of the changes are all in the TicTacToe class. You should recompile the TTTCell class at this time to check for syntax errors.

Alternating Players in the TicTacToe Class

To make the players alternate, you need to change the value of the toPlay variable to the other player at the end of each play. This can be done by adding the following line at the end of the makePlay() method.
    toPlay = TTTCell.otherPlayer(toPlay);

Whenever a method is declared static, it is handled by the class rather than any of its objects. That is the reason that you send the otherPlayer() message to the TTTCell class. Generally, methods are declared static when an object other than the class object is not needed to determine the return value.

The TicTacToe Class getBestPlay() Method

This method returns a TTTCell. It has an int parameter that specifies a player. In a later step, this method will return the best cell to play for the player indicated by the parameter. For now, it will just return a cell that has not yet been marked.

You code for the method should begin with a TTTCell local variable named bestCell, which is initialized to null. The null value can be assigned to any object variable to indicate that there is no object.

The method should then loop through the cells checking each to see if its mark is TTTCell.NO_ONE. If it is then assign the current cell to bestCell. After the loop, return bestCell.

Making the "Make Computer Play" Button Work

To get any button to work, you need to register an action listener on it. You should register a listener on the makeComputerPlayButton variable, just after the button is disabled in the init() method. The listener's actionPerformed() method should have the following line of code.
    makePlay(myMark, getBestPlay(myMark));
This will make the computer make the best play for the computer when the button is clicked.

At this time, you have enough code that you can recompile the TicacToe class and run your applet. It should behave like the demonstration applet at the bottom of this web page except for the user messages.

Adding the TicTacToe Class updateStatus() Method

This method has void return type and no parameters. It is responsible for two things: setting the message for the user, and enabling and disabling buttons.

The method code will give you some practice with if statements. If toPlay equals myMark (the computer's mark) you should send a setText() message to the message variable, setting its text to "Push Make Computer Play Button". If toPlay equals userMark you should send a setText() message to the message variable, setting its text to "Your Play". Finally, you should enable makeComputerPlayButton if toPlay equals myMark and disable it they are not equal.

Invoking updateStatus()

There are two times when you want to change the message to the user and enable or disable buttons: when you start a new game and whenenever a play is made. Thus the updateStatus() method should be invoked in two places in the TicTacToe class: at the end of the startNewGame() method and at the end of the makePlay() method.

After you have added these two statements, recompile your TicTacToe class and run the applet with appletviewer. Your applet should look and behave like the following demonstration applet. It should not let either a user or computer play be made out of turn. The label at the bottom should indicate what the user should do next, except at the end of the game. That will be fixed in the next step.

Demonstration Applet