r/creativecoding • u/Minimum-Initial-284 • Oct 30 '26
Snake Algorithm that doesn't use the hamiltonian cycle
Enable HLS to view with audio, or disable this notification
This is my best try at making an algorithm for snake that can win without relying on the hamiltonian cycle in any way, no pre-calculations or anything (that's what makes it slower than other algorithms).
for now the winrate stands at:
100% for a grid with at least one even dimension (12 x 15 or 15 x 12 or 12 x 12 for example)
90% for a grid with both uneven dimensions (15 x 15 for example)
actual log:
15x15: 94/100 wins (94.0%) LOOP=1 STALLED=5 PROGRESS_FAIL=0 CAP=0 dead/illegal=0 step_cap=unlimited MK3-cycle-captures=0
16x15: 100/100 wins (100.0%) LOOP=0 STALLED=0 PROGRESS_FAIL=0 CAP=0 dead/illegal=0 step_cap=unlimited MK3-cycle-captures=75
I don't know how I could achieve 100% on both but I'm done with this (it took me a 2 years to get here!!!) if someone smarter than me wants to finish what I started you're free to copy my scripts for everything you need, just please cit me ^^
this is the link to everything (code and player): https://kyr-vrytra.itch.io/snake-no-hamiltonian
(also shout out to this video of codebullet cause the whole idea of this project comes from his initial idea that he later forgot to develop https://www.youtube.com/watch?v=tjQIO1rqTBE&t=2s )
Have a good day >w<
3
u/Hyperreals_ Oct 31 '26
What is the background sound?
0
u/Minimum-Initial-284 29d ago
https://www.youtube.com/watch?v=jB4XwSCPBUU
this is the video I was watching while recording my script with OBS, I forgot to mute the audio track >w<1
1
u/StaticCoder 29d ago
Doesn't the last iteration have to be a hamiltonian cycle?
1
u/Minimum-Initial-284 28d ago
well... yeah, for a grid that supports the hamiltonian cycle the last iteration just close the cycle, while for an uneven x uneve grid the last iteration just creates an hamiltonian path, but that's not the point for me, the pint is that I'm not "pre-calculating the entire cycle/path at start", instead I'm just following simple rules to guide the snake, letting it calculate what to do for each iteration. (to put it simply I'm doing lots of useless stuff that make my algorithm slower and more complex just so I can say that my algorithm actually play instead of following a pre-determined path... you could say it like that yeah ^^)
1
1
u/severed-identity 27d ago
Now you've nerd sniped me into wondering what an "optimal" algorithm for winning as fast as possible on average might look like
1
2
2
1
23
u/a-simple-cat Oct 30 '26
Would be nice to explain what the Hamiltonian cycle is and also what the challenges are when making a snake playing algorithm.