An animated self-avoiding random walk built with JavaFX and Maven.
The walk starts at the center of a grid, travels along the borders of the cells, and never visits the same intersection twice. It stops when it reaches the border of the window or when no unvisited path remains.
The program can repeat the walk any number of times consecutively and then reports the observed probability that a walk reaches the border before it traps itself.
- JDK 27 (the project uses JavaFX 27)
- Maven 3.10 or newer
cd ~/Projects/CSC-190/AI_JavaFX
mvn clean javafx:runThe program asks for the grid size and the number of runs in the terminal before the window opens:
Enter grid size (cells per side, 2-100) [10]:
Enter number of runs (1-100) [1]:
Enter the number of cells on each side and the number of consecutive trials. The grid is built to that size and the first walk starts. If no terminal is attached (for example, when launched from an IDE), the defaults of 10 cells and 1 run are used.
The window is intentionally not resizable: the outer grid line is the window border where the walk ends.
mvn javafx:run -Djavafx.args="20 25"That runs 25 trials on a 20 x 20 grid. The named forms are also accepted:
mvn javafx:run -Djavafx.args="--size=20 --runs=25"| Key | Action |
|---|---|
SPACE |
Pause / resume the walk or the pause between trials |
R |
Repeat the whole session (same grid size and run count) |
ESC |
Quit |
Each finished walk prints a line like:
[Trial 3/25] Walk finished after 84 steps: reached the window border.
After every requested trial completes, the program prints a summary:
==================================================================
TRIAL SUMMARY - 20 x 20 grid, 25 runs
==================================================================
Reached the window border : 19 (76.0%)
Trapped by its own trail : 6 (24.0%)
Estimated probability of reaching the border : 76.0%
95% confidence interval (Wilson) : 56.6% to 88.5%
Average walk length : 141.6 steps
Shortest / longest walk : 12 / 412 steps
==================================================================
The estimated probability is simply the fraction of trials that reached the border. The 95% confidence interval (Wilson score interval) shows how much that estimate could vary with this sample size; more runs give a tighter interval. The first trial animates at a comfortable pace, while the remaining trials step quickly so a batch of runs finishes in reasonable time.
- A grid of
n x ncells has(n + 1) x (n + 1)intersections. - The walk starts at the center intersection and marks it visited.
- At every step it looks at the four neighboring intersections. Visited intersections are removed from the choices, and one of the remaining neighbors is chosen at random.
- The segment is drawn immediately, so the path unfolds step by step.
- Reaching an intersection on the outer edge of the grid ends the walk, because that is the border of the window.
- If all neighbors are already visited, the walk is trapped and ends there.
A Timeline advances the walk every 170 ms on the first run and every 12 ms on
later runs. The trail shifts from cyan to violet as it grows, and the head
marker turns amber with a ring when a walk finishes.
LEARNING_GUIDE.md is a guided tour of the main class. It
explains each JavaFX structure used (scene graph, panes, shapes, labels,
Timeline, PauseTransition, keyboard events), how those pieces interact, and
which design principles the code applies. It also suggests exercises for
extending the program.
AI_JavaFX/
├── pom.xml
├── README.md
├── LEARNING_GUIDE.md
└── src/main/java/csc190/aijavafx/AI_JavaFX.java