Skip to content

rjedwardsss/Peg-Solitaire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Peg Solitaire

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.

How Peg Solitaire works

  • 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.

Board styles

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).

Compile

From the Peg-Solitaire directory (adjust paths if your src layout differs):

javac -d out src/PegSolitaireGame.java

Or compile into the current folder:

javac src/PegSolitaireGame.java

Run

If you used -d out:

java -cp out PegSolitaireGame

If class files live next to src:

java -cp src PegSolitaireGame

Modes

When the program starts:

  1. Play manually — Same interactive game as before: you enter column, row, and direction each turn.
  2. 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.

Gameplay instructions (manual mode)

  1. Run the program, choose 1 to play manually, then pick a board style (1–4).
  2. Each turn, the board is printed with row and column labels starting at 1.
  3. 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.
  4. If the move is illegal, you’ll see a message and can try again.
  5. Play continues until the game ends (see below).

Invalid numbers are rejected with a prompt to enter an integer in the allowed range.

Win / lose conditions

  • 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.

About

This assignment was originally designed as a self-assessment for students considering enrollment in CS300 at the University of Wisconsin-Madison.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages