Preface

The purposes of this book are: Each project is a common puzzle or game. They are approachable, yet reasonably ambitious. The requirements for each project are concrete - they are exactly the rules of each game. Once a project is complete, the user will have a complete solitaire implementation that can be enjoyed for years.

This book is not an introduction to programming or Java. It is intended to support a second semester course in either subject. It is assumed that the reader is comfortable with: variables and scope, flow of control constructs, arrays, functions, parameter passing mode(s), classes, objects, and the Java standard library. See appendix A for leveling topics. Recursion is used in two projects (a review is provided in appendix C).

Each project requires graphical user interface programming. This is only a means to an end. The focus is not on Javas AWT or Swing; the focus is exploring algorithm and data structure development. See Appendix B for sample GUI code to gauge the quality of Swing programming that will be used. If desired, the reader can copy this code to jump-start the GUI project.

Here are the highlights of each chapter.

Mastermind game (command line user interface)
  • algorithm to evaluate each guess
  • Mastermind abstraction in a class and driver code in main()
  • 3 iterations
    • Prompt for and receive input, supply a default answer
    • Create a MastermindModel component that computes the "black" response
    • Compute the "white" response in the MastermindModel
Mastermind game (GUI)
  • Model-View decoupling and reuse
  • resize the game as the window is resized
  • JFrame, Canvas, Graphics, MouseListener
  • 8 iterations
    • Draw the bare board
    • Draw the color palette at the bottom
    • Select a color and put it in the first row
    • Add the JButton at the bottom and put colors in each row
    • Add GUI exposure processing
    • Integrate MastermindModel and add black-white responses
    • Allow a new game to be started when the game is won
    • Allow the answer to be toggled above the board
 

Fifteen Square puzzle (command line user interface)
  • data structure to represent squares
  • recursion
  • FifteenSquare abstraction in a class and driver code in main()
  • 5 iterations
    • Display a 4x4 grid, receive input, update display
    • Randomize the squares
    • Implement single-square move
    • Implement multi-square move
    • Refactor to support user-defined width and height
Fifteen Square puzzle (GUI)
  • Model-View decoupling and reuse
  • JFrame, JButton, GridLayout, ActionListener
  • 2 iterations
    • Draw the board
    • Refactor into model, DOS view, and Swing view
 

Fourteen Peg puzzle (command line user interface)
  • data structure to represent pegs
  • data structure to represent jumps
  • 3 iterations
    • Draw the board
    • Input a position and remove or reinstate its peg
    • Input and implement move requests
Fourteen Peg puzzle (GUI)
  • Model-View decoupling and reuse
  • JFrame, Canvas, Graphics, MouseListener
  • 3 iterations
    • Draw the board
    • Select a position and remove or reinstate its peg
    • Refactor into model, DOS view, and Swing view
Fourteen Peg puzzle move advisor
  • recursion
  • algorithm to find all possible answers
  • depth-first search
  • 3 iterations
    • Write a program that uses random moves to find solutions
    • Write a program that uses depth first search to find every possible solution
    • Integrate the second program into the results from the last chapter
 

Set game (GUI)
  • multi-tiered algorithm to draw playing cards
  • algorithm to evaluate each guess
  • algorithm to find all possible answers
  • JFrame, Canvas, Graphics, MouseListener 9 iterations
    • Draw 12 blank cards
    • Draw 9 cards that implement number, shape
    • Draw 9 cards that implement number, shape, color, fill
    • Encode the 4 dimensions and represent 81 cards
    • Draw 81 cards
    • Select, hilite, and remove 3 cards at a time
    • Shuffle cards, build entire UI, count the number of sets found, refill holes
    • Design the model, validate each set selected
    • Compute all sets present, display the list of sets present

This book is modeled after the 1978 classic The Elements of Programming Style. That text took many programs culled from programming textbooks and reworked them while deriving rules like,

All the code was written in FORTRAN or PL/I; but almost all of its insight is as meaningful today, as it was then. The authors confessed in their preface, "We have no doubt that a few of our 'good' programs will provide 'bad' examples for some future writer." I believe the same can be said about this book.