# simple ai in a 2d array

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, i have a random matrix (2D) array with 1 and 0 like this: 0110001001 0011000101 0011000101 ... ... .. and i want to go to position(last,last) from position(0,0) the one s are not passable. i mean player will go on the zero s and i can go 4 way: right, left, up, down. i use c++ and here is my algrithm: i am using a function FindWays(x,y,) to find the ways for x,y positions. like this: left = matrix[x-1][y]; //finds the left and using a function to move: ChangePositionFrom(x,y) my question is about this function. i use it like this: if(left == 0 && prev_pos != right) x -= 1; ... ... my control order is: right, down, left and up in ChangePositionFrom(x,y) function. but this technic does not solve lots of matrix. i mean it is not a good algorithm(i found it myself). can i improve it easily or do i have to change all system? (i do not need expert ai) Thank you very much..

##### Share on other sites
Look up for A*(A-star) in the Articles here in Gamedev in the AI section, or if you preffer use a search engine. It's a better algorithm.

##### Share on other sites
i know A* is better but it have to be simple. :D
i use this for right:
if(right== 0 && (prev_pos != RIGHT|| (left!= 0 && down!= 0 && up!= 0))) //can go right
{
//go right
}
...//else if s for down, left, up..
//else printf("cannot find the way");
/bla bla

this works for this:
00110
10011
01011
00000
11100
but not for this:
00011
11011
00011
01101
00000

because it says : up up down down up up down down... the turning back funtions does not works fine..

any suggestions ?

##### Share on other sites
How about the Min Max algorithm...

Hmmmm. on second thoughts its maybe not quite what you want.. But its worth a look anyway

##### Share on other sites
how can i go straight forward ? i dont want up up down down.

asiest method to solve this making a prev_pos_x,y variable and check it. but if i use that model wont solve this

000001
101111
000000
000000
000000

because it will go straight forward right and cant turn back. i controlled left&&up&&down but it still does not make any sense... are there any other suggsetions?

thank you...

##### Share on other sites
A* is the way to go, and it is not a complex algorithm.... it is very simple

##### Share on other sites
thank you, i will chek it out.

##### Share on other sites
anyway, i improved my own algorithm and now it works very well.
i added some parameters to change position function and some checks to know is the player on the edge. and added some functions for previous position vector. i am very happy with this. [smile]
if anyone wants to look up my code, i may send it.

and again : Thanks for replies ..

##### Share on other sites
Simplest algorithm (pseudocode):
while(not at goal)
{
direction = randint(0,3);
if(walkable(direction))walk();
}

Doesn't find optimal paths though. Isn't fast either.

##### Share on other sites
Quote:
 Original post by TrapSimplest algorithm (pseudocode):while(not at goal){direction = randint(0,3);if(walkable(direction))walk();}Doesn't find optimal paths though. Isn't fast either.

hey thanks but, that is soo much easy :D
here is my code:
if(done)
//done
else
if((right == 0) && ((prev!= RIGHT) || (left!= 0 && down != 0 && up!= 0)))
//go right

... //and the other ways

and i make a variable to find how many movements done. if it reachs 40 it means that player cannot find way, so no solve for the random matrix.

edit: : this algorith is not perfect but can solve this:
0000001
1101111
0001111
0111111
0100011
0101000
0001100

Thank you..

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 23
• 9
• 49
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007588
• ### Who's Online (See full list)

There are no registered users currently online

×