Left-click: pause; Right-click: change BG; Middle-click: reset
xxxxxxxxxx
/**
* Self Avoiding Walk II [With Backtracking] [Java/Pjs]
* Coding Challenge #162
* by The Coding Train / Daniel Shiffman
*
* https://TheCodingTrain.com/CodingChallenges/162-self-avoiding-walk.html
*
* Java Mode conversion by GoToLoop (2022/Feb/09) (v1.1.5)
*
* https://Discourse.Processing.org/t/
* converting-coding-challenge-self-avoiding-walk-backtracing-
* from-p5-js-to-processing-java/35047/5
*
* OpenProcessing.org/sketch/1475931
*/
import java.util.List;
final List<Spot> path = new ArrayList<Spot>();
Spot[][] grid;
Spot spot;
int matrix, removals;
color bg;
boolean removed, paused, finished;
void setup() {
size(650, 500);
noFill();
createGrid();
bg = randomColor();
}
void draw() {
if (finished) return;
if (removed) removeTailSpotIfTooOld();
if (!removed) getNextSpot();
removals += removed? 1 : -removals;
if (removals == Spot.MAX_STRAIGHT_REMOVALS) resetAllSpotsAges();
drawGridPath();
drawTrailPoint();
}
void mousePressed() {
if (mouseButton == LEFT)
if (paused ^= true) noLoop();
else loop();
else if (mouseButton == RIGHT) bg = randomColor();
else if (mouseButton == CENTER) createGrid();
else saveFrame();
}
color randomColor() {
return (int) random(Spot.ALL_COLORS);
}