Drunk Snail

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
hi i have another problem about a program i'm doing recently:

our teacher instructed us to create a program that simulates drunk snail, which moves to places randomly. Also we have to make the program so that it'll do a self-avoid walk, which means that the snail can't go back to the spot where it already had been. So i constructed a program that does random walk, but i can't get the self avoid walk thingie to work. My method's writing all the coordinates to a .txt file and use two variables to check the x-coordinate and the y-coordinate of where the snail had been. if i find a match, the program would go back and reproduce another two random number for x and y coordinate, but i can't get it to work. It still steps on the spot it had already been, the source code is here, please help me, i have absolutely no idea why its not working. I've tried so many ways, and it still doesn't work, maybe I forgot to do something or i did something wrong, but i just can't figure out what is it...

thank you for any suggestions to make the program work :D
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
Update: I just updated my source code, it should make more sense, but somehow the checking process wouldn't work....... help!
 

esung

Golden Member
Oct 13, 1999
1,063
0
0
instead of implementing a TXT file and do lots of read/write, why don't you just dump the coordinates to a 2D array or struct?

one way to see if the checking/reading file is doing correctly is putting manual flag/probes inside the loop. See if your program is actually reading the the coordinates from the file (how do you know? insert a line after the read and print it to the screen..) and see if
the program is actually checking the cooridates (again, print the result to the screen so you'll see it)

just some suggestions...
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
esung: yah but i haven't learned matrix yet, so i don't know how to use it. And yah, i know it's reading because when i debugged the program, i added two lines so it would cout all the coordinates from the .txt, and it did.... I thought it was the problem of the reading process also, but it wasn't that. Thx for your suggestion anyways :)
 

Turkey

Senior member
Jan 10, 2000
839
0
0
Given the way you're storing the moves, it seems to me you need three loops: one that keeps track of the current discretized time (how many moves the snail has taken), one that doesn't stop until a valid move is found, and one that looks thru the single-dimensional arrays to find a valid move. So, you would have something like this:

while (the snail has more moves to make) {
do {
generate next x coord
generate next y coord
} while (x coord and y coord match an x coord and y coord of a previous move) {

move the snail, store the move, and decrement the number of moves the snail has left to take
}

the "x coord and y coord match an x coord and y coord of a previous move" would be done in a function like isValidMove(x, y, previous X's, previous Y's, X & Y array size, number of moves taken) that would return true if the move is valid and false if not. In the function, all it would do is look through all the previous X's and previous Y's (using a loop) to see if this X,Y pair has been moved to before.

Seems like this has many more subtle angles than just determining if the square has been moved to before... if the snail can't backtrack, it may get stuck in a "hole" where it walked into a self-made wall (a la Tron). But, those are really hard to detect generally. The above algorithm would just hang because it would be stuck in an infinite loop. One thing you could do is prevent the snail from moving to a square that is directly between two squares that have already been moved to.

Problem: if the snail has moved in this path:
*****
*
****S

and it turns left then left, there is no way out. But, if you prevent the snail from turning left in the first place (because it is a square that is directly between two squares that have already been moved to) then it won't get stuck.

Good luck!
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
i think i do have those three loops, first I have a do while loop that says do { }while(x<26), which the program will 26 times. Which stored into x that tells how many moves the snail has taken. then i have two loops followed by 2 if statements. Which basically says &quot;run every cell in the X coordinate variable, if find a match, compare the corresponding cell to see if they match. If they match(x and y coordinates), call the function MakeTheMove to suggest another move, if the move passed back is equal to the coordinates that match the coordinates that match the first pair of coordinates, loop it to make another move and redo the process&quot; it'll do the steps for 26 times (which means it'll check the move everytime before it actually goes there), and for the problem you suggested, i'll just say if it moves 8 times and it still looping, then cout a statement that says &quot;i can't move anywhere else, i quit&quot; and program ends :D

correct me if im wrong, thank you for your inputs!
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
ok i went back to my first idea(writing/reading a file) and i added one line, it WORKED!!!:Q:Q:Q!

but still, thank you all who've helped me.