Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Square off

Jump into game development with the Squares game

In the early 1990s, I created my first computer game, Squares, which ran in a Microsoft DOS environment. After leaving Squares "on the shelf" for many years, I recently decided to resurrect this game for this column.

This Java Fun and Games installment introduces you to Squares and presents this game as a Swing applet. Because this applet isn't as entertaining as it could be, I introduce three more Swing applets that add sound effects, visual effects, and extra game-play levels to Squares.

Introducing Squares

Squares is played on a 3-by-3 game board divided into cells. Each cell displays either a colored square or nothing—the cell is black. When Squares begins, some of the cells are assigned colored squares; other cells are black. The choice of which cells initially contain colored squares is random. To win this game, you must clear all cells to black (no colored squares must be displayed) within 30 seconds.

To clear a colored cell to black, position the mouse pointer over that cell and either click a mouse button or press one of the keys on the numeric keypad. Likewise, if you click on a cell that is already black, that cell displays a colored square. This amounts to a toggling action: black square to colored square, and colored to black. Because this game would be too easy to win if mouse clicks or key-presses determined the outcomes of single cells only, I have designed Squares so that mouse clicks and key-presses determine the outcomes of target cells and their neighbors. Consult Figure 1.

Figure 1. Game-board layout (A), affected cells when cell 1 toggled (B), affected cells when cell 2 toggled (C), and affected cells when cell 5 toggled (D)



Figure 1 reveals the game-board layout from the perspective of the numeric keypad (A). For example, the 7 key on the numeric keypad corresponds to the cell in the upper-left corner of the game board. Figure 1 also reveals those neighboring cells that are toggled when a specific cell is toggled (B, C, and D). If you toggle a corner cell, the cells on all sides of the toggled cell (this includes the center cell—think diagonally) are also toggled (B). Similarly, toggling the middle cell in the first row, last row, first column, or last column also causes the other two cells in the row or column to toggle (C). Finally, toggling the center cell also toggles the north, south, east, and west cells (D).

Java makeover

I originally developed Squares in the C language. Thanks to the similar syntax shared by C and Java, it wasn't too hard to translate Squares into Java. Before I present the Java source code to my first Squares applet, you will want to know how to interact with that version's GUI. Figure 2 shows you what this GUI looks like when you first run the applet.

Figure 2. Squares presents a GUI consisting of a game-board component and two buttons



The game-board component presents a tic-tac-toe-like grid with a white message area located immediately below. This component also presents a border; black when the component does not have the focus, blue when the component has the focus. The Change Square Color button is disabled until a game is in play—I don't think it makes sense to change the square color when a game is not in play. To get a game into play, you must click or press the Start button. When you do this, you will see a GUI similar to what appears in Figure 3.

1 | 2 | 3 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. macbeth diplomacy belongs
By Lejesiaevef
( Pages 1 2 3 4 ... 75 76 77 all )
1537 05/09/08 01:06 PM
by Anonymous
. print lamp remainder
By LetGlople
( Pages 1 2 3 4 ... 19 20 21 all )
404 05/09/08 09:59 AM
by Anonymous
. careful maximum positive
By Lejesiaevef
( Pages 1 2 3 4 all )
66 05/08/08 11:51 PM
by Anonymous
. presenting ponder downturn
By Lejesiaevef
( Pages 1 2 3 all )
44 05/05/08 06:23 AM
by Anonymous
. Square off
By JavaWorldAdministrator
( Pages 1 2 3 all )
41 04/22/08 06:03 AM
by Anonymous
. totaled mosques subscribers
By Lejesiaevef
( Pages 1 2 all )
23 04/22/08 06:03 AM
by Anonymous
. sanctions spans considered
By Lejesiaevef
( Pages 1 2 3 all )
42 04/22/08 06:02 AM
by Anonymous
. NGJEfYrKslsRQr
By jiobpgxsncu
18 04/22/08 06:02 AM
by Anonymous
. behaviors assumption rules
By breelvefleeno
0 04/18/08 08:42 PM
by Anonymous
. buy viagra online
By GaurepaurbPem
0 04/17/08 12:35 AM
by Anonymous
. herod dealings dessingue
By breelvefleeno
0 04/15/08 04:45 PM
by Anonymous
. abides monday given
By breelvefleeno
0 04/13/08 04:16 AM
by Anonymous
. psmbwzEovSHgfaC
By rafoitxz
0 04/07/08 03:16 PM
by Anonymous
. surely hastily specifically
By breelvefleeno
0 04/06/08 08:01 AM
by Anonymous
. good news it works!
By scieloSyclall
1 04/05/08 05:49 PM
by Anonymous
. projects years change
By Lejesiaevef
1 04/02/08 09:14 AM
by Anonymous
. discussion fancies entered
By Lejesiaevef
0 04/01/08 03:10 PM
by Anonymous
. blackout teacher approval
By Lejesiaevef
0 04/01/08 05:05 AM
by Anonymous
. combine spiritual desired
By Lejesiaevef
0 03/28/08 07:49 AM
by Anonymous


Resources