A small Java console implementation of Peg Solitaire: jump pegs over neighbors into empty holes until you finish with one peg or run out of legal moves. You can play manually or ask the program to solve a starting board with a depth-first search (DFS). No GUI, no external libraries—just standard input/output.
- Pegs sit on a grid.
@is a peg,-is an empty hole, and#is unused space (neither peg nor hole). - A legal move jumps one peg orthogonally (up, down, left, or right—not diagonal) over an adjacent peg into an empty hole on the far side.
- The jumped-over peg is removed (your board updates to show empty holes where those pegs were).
- You repeat until you win, lose, or quit the program.
When you start the game, choose one of four starting layouts:
| Choice | Name | Description |
|---|---|---|
| 1 | Cross | Classic cross-shaped board |
| 2 | Circle | Circular pattern of pegs |
| 3 | Triangle | Triangular arrangement |
| 4 | Simple T | Compact “T” shape |
Layouts match the patterns documented in the source (createBoard).
From the Peg-Solitaire directory (adjust paths if your src layout differs):
javac -d out src/PegSolitaireGame.javaOr compile into the current folder:
javac src/PegSolitaireGame.javaIf you used -d out:
java -cp out PegSolitaireGameIf class files live next to src:
java -cp src PegSolitaireGameWhen the program starts:
- Play manually — Same interactive game as before: you enter column, row, and direction each turn.
- Solve automatically — Pick a board style; the program runs a DFS solver on that starting layout. It reports whether a solution exists, prints the ordered list of moves (column, row, direction), and can optionally print the board after each move in that solution.
The solver works on a copy of the board, tries every legal move recursively, and uses a serialized board string to avoid revisiting the same configuration. Larger boards (for example Cross) can take a long time or exhaust memory; Simple T and Triangle are easier to finish quickly.
- Run the program, choose 1 to play manually, then pick a board style (1–4).
- Each turn, the board is printed with row and column labels starting at 1.
- Enter:
- the column of the peg you want to move,
- the row of that peg,
- a direction: 1 UP, 2 DOWN, 3 LEFT, 4 RIGHT.
- If the move is illegal, you’ll see a message and can try again.
- Play continues until the game ends (see below).
Invalid numbers are rejected with a prompt to enter an integer in the allowed range.
- Win: Exactly one peg remains on the board (
@count is 1). - Lose: More than one peg remains but no legal move exists (you’re stuck).
When the game ends, the program prints a short closing message.