the move function. first kudos. gratz on pumping out a prototype. now if your serious about making it better. heres an idea.
consider: G being goal, E being Enemy (the thing were moving), X being blocks
E will move one up. right. then next time around it will move one back. then move one up. then move one back. with your code that is.
So how do you fix that. well its a common prob its worth fixing for the _experience_. youll be glad you did cause youll get it again.
A*... or for us a conceptual algo with similiarites otherwsie known as "I kick butt ill make the algo up as i go" Which i recommend. nothing like reinventing the wheel to learn why the wheel is round and not square. or in most cases nothing like making a square wheel to make you appreciate the beauty of a round one.
heres a tut. he gives source code that might be tough to read so ill talk you through it.
http://www.geocities.com/jheyesjones/astar.html
its pretty general. but its also adapatable =) but i wouldnt adapt i would learn.
on to our square wheel.
Basically you just keep trying to move till you get to the goal. without moving. you have all the game logic there at your disposal.
now the data structure we use to keep track of stuff is debatable. im going to say priority_queue for simplicity. but i think i can find lots of room to make it go faster there.
we need two of those. one called "open" one called "closed"
on open we hold squares were still thinking bout. on closed we put squares that were done with.
now we start by adding our start square to a node. then we give it a value in the node by detirmining the distance to the goal. set a "path cost" to 0. then we put the start node in "open" then we kick into the loop.
for the loop we pop off our best node. which is easy cause priority_queue inserts ordered. for that to work though we have to define "operator< (Node& n)" in node to return this->value < n.value; having our best node we form successor nodes. these are nodes that i can get to from this node. we value all those by distance to goal. and increment the path cost. handle the parent pointers etc. now we got data for each potential move we look to see if we have moved to this square via another way before.
consider the above case i gave in source. lets say im at the U shape stuck. thats my node. i generate all my succesor nodes. i only get moving back. now im at the step where i look for matching nodes.
i check both open and closed list for my current position. via overloading operator==(Node& n) to compare location. what would i find? well i was there once. i would find on closed via a search my original start node. Now having that node i compare my new path cost. to the old path cost. Well the start node had a path cost of 0. and my new node has a path cost of 3. so i was there in one search once but i had gotten there with less steps. So i dont want to replace my new path with that one. that would be taking the long way. so i leave that node alone. and garbage my new one as useless.
ok back to where i was in describing the algo. were looking for matching nodes in general having just generated succesors to the best square we had so far. We check both open and closed. if its on either we check to see if ours took less steps. if it did take less steps we replace the DATA by copying into that node on the closed list. why? cuase the childs (succesors) of that one are already on the open list and pointing at that node in memory with their "parent" value. if we replace it we loose that link.
ok now suppose we dont find this node on either open or closed. then we put it on open. err insert it cause our open is ordered by "distance to goal"
now remember our original node that we popped off the list to generate succesors. we move it to closed.
go to top of loop popping off the node closest to goal. when finally we have a successor that is the goal. what then?
we use the parent values to reverse the trip. valla your shortest path that wont have the behavior that i mentioned to start this off.
its really alot easier then it sounds.