The Mathematica Journal
Volume 10, Issue 2


In This Issue
In and Out
Trott's Corner
Beyond Sudoku
New Products
New Publications
News Bulletins
New Resources

Download This Issue 

About the Journal
Editorial Policy
Staff and Contributors
Back Issues
Contact Information

Exploring Board Game Strategies
Yves Papegay

Interface with GUIKit

"GUIKit simplifies the construction and layout of common user interface programming and eliminates the need to write code using the underlying Java programming language." GUIKit online documentation (

Although we have succeeded in easily building a graphical interface for playing using the notebooks and buttons in them, the result is very poor in terms of graphics and functionalities.

Until a new version of graphics management exists in Mathematica, there is no way, within notebooks, to make a part of a graphic object active. But GUIKit makes the power of J/Link and the richness of AWT and Swing Java libraries accessible.

In this section, we go back to the HMaki example and show how to build a nice and efficient interface with this new package.

Visualizing the Board

GUIKit provides Widget["IndexedImagePanel"], which allows us to display an array or rectangles with different colors. Very few settings are necessary to represent the HMaki board with this widget.

To transform this widget into an active one, we have to bind the event of clicking with the mouse to the action of playing and refreshing the board.

A JPlayGame function is then defined, initializing the game, loading, and running the interface definition.

Here comes the playable Java version of HMaki!

Adding Interactivity

We now would like to be able to start a new game or replay the current game directly from the interface.

A good way to implement this interactivity is to define Widget["Action"] for each action and attach the corresponding Script to it. The script can refer to any Mathematica function, provided its context is within the scope of the script. Here, we use the function NewGame in our scripts.

It is then easy to define a menu, associating an action to each of the menu items

or associating actions with buttons.

Adding Scores

When you play HMaki, you can target various goals: suppressing as many tiles as possible, having as many tiles as possible once all the groups have been suppressed, suppressing as many biggest groups as possible, and so on. From the board, it is possible to compute a score, which measures how close to your goal you are.


The simplest score function computes the number of tiles on the board.


To get the score on the interface, we should first set a label widget to show it as text.

We should also modify what happens when the mouse is clicked inside the board (i.e., change the definition of MouseClickBinding).

The previous command produces the Java interface (Figure 9).

Figure 9. GUIKit-generated Java version of HMaki.

About Mathematica | Download Mathematica Player 
© Wolfram Media, Inc. All rights reserved.