# How to devise an algorithm for a person being on a walk?

The game is an isometric RPG. The map is tile based.

I'm creating a town for this game. And I want one of the characters to roam the town randomly, because she is on a walk with her children.

I tagged the tiles she can go on. And I created a simplistic algorithm that I, in my naiveness, thought would be enough. Here it is:

If the character is blocked out and can't move in any direction she waits 1s before checking this again. If she can move in only one of the 4 directions, she moves that direction. If there is more than one possibility: If she can go forward, she always has 50% chance of going forward; if she can go both to her left and to her right there is an equal chance she chooses either of those two directions; and finally, if she can turn back, that chance is 2x smaller than turning to her left and 2x smaller than turning to her right. So, if she can choose from all 4 directions, she will have 50% chance to go forward, 20% chance to go to her left, 20% chance to go to her right, and 10% chance to go back. Repeat the above every step.

This algorithm fails beautifully. The character mostly moves randomly in one place. If she gets on a square she almost can't leave it.

I thought the solution would be to increase the chance of her moving forward. So I made the chance to go forward 5 times bigger than doing anything else. But this proved to hardly improve things.

COuld you give me some hints, point me to the correct direction? Or at least tell me that this is not worth it and I should instead resort to giving her a predefined path? (Are there no algorithms to make a character roam a given set of walkable tiles?)

• Can't you just make a graph of connected tiles, select a random tile and make her go there via an A* path planing/finding? Sep 26, 2017 at 18:02
• This sounds like a fine approach. Perhaps it's the implementation that's failing? Have it run for a bit, sum up each choice, combine all the sums to see what percentages you're actually getting. Otherwise, I'd say you're changing directions too often. You'd more likely want to pick a direction and go forward for X tiles, then choose to continue, turn or go back with your percentages.
– House
Sep 26, 2017 at 18:02
• @MichaelHouse My current guess is that the complicated, full of obstacles structure of a town is the culprit. If she's on a square then only few tiles in comparision to the area of the square can give her any hopes of leaving the square (since the square's edge is much longer than the width of a street attached to this square). And if she gets to the park then the trees force her to change directions often, and as a result she basically runs around in circles. Sep 26, 2017 at 18:13
• @MichaelHouse And if she's on a street then let's hope the chances of her keeping the direction are large enough for her to walk to the end of the street, or else she'll be going this street back and forth. And what if we make her change the directions not so often? Then she'll always walk to the end of the street and never change her direction on junctions. Sep 26, 2017 at 18:16
• @gaazkam Then take Alexandre's suggestion. Provide her destinations that she navigates to. Go to the park, then the square, then B street, down to 6th Ave and back to the park. Choose the locations randomly, have her kids lead sometimes (as they will). It'll be far more natural looking than the random walk algorithm you have now.
– House
Sep 26, 2017 at 18:21