Programming Tic-Tac-Toe in Java


<<Previous | ToC | Next >>
 

Scoring in GameEngine

This module assumes you have written and got running the TTT scoring program in Java, and that you have implemented at least one game in the GameEngine (Pong is a good place to start). Basically, everything you did in the ASCII-graphic scoring program you will keep except the board display. The main program will get replaced by event handlers, but they mostly do the same thing.

The first thing to do is go into the GameMaker and create a new game, with a new descriptive name. You only need 13 or 22 widgets, two long thin Rectangles each for vertical and horizontal dividing lines, and nine Text widgets to show the square numbers (if you so choose) and the Xs and Os as played -- I suggest using the "Sc[ore]" font -- plus transparent rectangles to take mouse clicks on a square to be played, if you choose to offer that as an input mode and if you prefer not to do the math to figure out which square was clicked (without separate clickable squares, you can make the whole game board clickable, then convert the click offset into a row+column by dividing the offsets by the square size). If you have separate clickable squares, you can eliminate the dividing line widgets by giving the background the color of the dividing lines, then making the squares one or two pixels smaller each way (the gaps will show the background through as dividing lines). To accept keystrokes as the human player's play, you need to make the game board accept keystrokes by default (as in the Pong game).

The loop body of the previous main program you preserve and copy to the KeyFld() method in the generated game. Or make a new subroutine (method) to start at that line and take the keyboard input (converted to a number) as a parameter, and call it from both your KeyFld() method and your ClickEvt() method (if you want clicks to select a square to play). The game board array and other persistent variables continue to be class variables.

In addition, you will need an array of references to the text widgets (the same as you did in Seaman).

If you are using separate widgets for click targets so the user can play their square by clicking, the easy way to convert those clicks into square numbers is explained in the corresponding note on the Calculator program with Java notes here.

The subroutine for displaying the game board is no longer useful, because the widgets automatically display whatever their current appearance is. You do need to update the text associated with a board square when the user plays that square. If you put references to the text widgets into an array, say

static GameWgt[] WgtBd = new GameWgt[10];
then it can be indexed by the same play number as your (existing) game board, and you can put either an "X" or "O" there. Don't forget to copy the widget references into the cells of the array, as you did in Seaman (except of course you don't want to hide them). Assuming the who variable is still type char, you will need to convert it to a String, which you can do in the same line as setting the text:
int tmp = WgtBd[play].PutTextLn(""+who);


I think you can do the rest on your own. If you get stuck, don't hesitate to ask. That's what we are here for.

After you have it working, you can stop here (perhaps browse some of the other chapters you nave not yet read), or else go on to program the computer to play "O" intelligently (turn the page).

<<Previous | ToC | Next >>

Revised: 2021 August 30