TriSolve Source Code

Contents

Introduction

TriSolve is an application I wrote as an exercise while I learned Windows programming. TriSolve is my first Windows application. This web page introduces the source code. It does not provide user documentation. (User documentation is in help file, accessible from the Help menu in TriSolve.) Here is a brief introduction to TriSolve.

TriSolve solves polyiamond puzzles. A polyiamond is a puzzle piece made of equilateral triangles. Here are some polyiamonds:

[Sample polyiamond pieces]
Sets of polyiamond pieces can be put together to form a multitude of shapes. Here is a shape like the letter T that has been formed with polyiamonds:
TriSolve takes a set of pieces and a desired goal shape and finds ways to make the goal with the pieces. Polyiamond puzzle sets are sold by Kadon Enterprises. There is more information about polyiamonds here.

About the Code

TriSolve took about five weeks to develop. It was written almost entirely from scratch. The Solver engine existed previously with some simple character-cell I/O and was largely rewritten for TriSolve. The source files total about 6,300 lines. The code demonstrates use of many Windows features, including dialog boxes and controls, scroll bars, threads, timers, the registry, graphics, coordinate transformations, printing, and tracking the mouse. The source is in C in spite of the CPP file extensions.

Microsoft's Windows documentation and code samples and textbooks for Windows are fraught with global variables and local static variables. Such objects prevent the use of multiple threads or even multiple windows in a class that uses static variables to record context. TriSolve contains no variables that prevent multithreading or multiple windows. TriSolve contains no global variables, and it contains no static variables except read-only constants and a few process-wide constants that are initialized once. One such constant is used to access thread-local storage and is necessitated by Windows' failure to pass any context to a print abort procedure. As a result, each Window and each operation in TriSolve supports multiple instances. For example, several print operations can be in progress at once, if the user can send print commands quickly enough. Also, printing is done in a background thread, leaving the main application free for user interaction during printing. The Solver engine also supports multiple threads, although the main window only executes one at a time.

The solution-finding algorithm is extremely primitive and was not redesigned for TriSolve since the purpose of the exercise was to learn Windows programming.

The Source Modules

Here is a guide to the source code modules. The files are in this directory.

TriSolve.cpp contains WinMain and the code for the main window, of the TriSolve class. This window manages two small windows displaying the pieces and the goal; one large exhibit window showing the pieces, the goal, or the solutions, as selected; and one status/dialog window. The TriSolve window also manages much of the user interface, including menu commands and some navigation keys; manages the open pieces and goal; and starts the Solver engine.

TriDisplay.cpp contains the code for the TriDisplay window class and support routines. The TriDisplay windows paint polyiamond shapes, manage scrolling and scaling, and provide rudimentary editing capabilities.

Status.cpp contains the code for the status/dialog box. This dialog displays the Solver status, provides controls for the user to select views of the Solver progress, and provides controls to navigate through the found solutions. It also contains a graphic image of an on/off switch to start and stop the Solver.

Dialogs.cpp contains the code for small dialogs such as selecting application options, setting a view zoom percentage, and so on.

Shapes.cpp is a library of routines for operating on polyiamond shapes. This includes routines to read shapes from and write shapes to files, to find geometric transforms of shapes, to release the memory of shape structures, and so on. Not all of the information about shape structures is contained in this module, as TriDisplay.cpp also uses knowledge about the shapes to paint them in the display.

Solver.cpp contains the actual problem solving code. As noted above, the Solver engine is very primitive. It is run in a separate thread with a lower priority, so the solution search runs in the background and other work can continue in the application and in other processes in the system.

Print.cpp contains all the code for printing shapes in the background except the actual drawing routine, which is in TriDisplay.cpp. This includes prompting the user for print parameters, creating a thread to print in the background, the print abort procedure and abort dialog, and the print job code.

Utilities.cpp contains general subroutines, currently just routines to display error messages.

Sobol.cpp contains a routine from Numerical Recipes that prepares a list of color values intended to maximally separate the colors in the color space.

Potential Features

This is a list of features that could be added in future versions of TriSolve. Since I do not plan to produce additional versions, this is more an acknowledgement of shortcomings than a plan to develop the program.

Copyright and License

TriSolve is copyright 2002 by Eric Postpischil.

The source code is published on the web for educational and demonstration purposes.

If you would like to make other use of the source code or have any questions, please contact me at TriSolve@edp.org or by other means as shown in my calling card.

The TriSolve Source Files

The source files are in this directory.

© Copyright 2002 by Eric Postpischil.