Page 1 of 2

How does on AI work?

PostPosted: Sun Feb 03, 2008 7:24 pm
by Thanx
Hi every1!
In my latest games I wanted to have more than 1 player, since this would be a turn-based strategy type of game. But I don't know how to construct an Artificial Intelligence... Small problem... :) Here is how this works: You have a map, which is made of squares. Each square is what I call a territory, and you have to control all to win. The squares have attack and defence. In a turn you can do 2 different things with a square: attack an adjent enemy square, or upgrade 1 of these skills (increment). In each turn you can only do 1 of these with a square, and you can do 5 of these actions. Hoq do I construct an AI for this? I know it's rather complicated, so feel free to dump something complicated on my head.
Thanx a lot!

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 9:01 am
by Bee-Ant

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:05 am
by Thanx
Thanx, but it's not what I need.... That's for gifhting games, for the enemy. But this is a turn based strategy game. It's a bit different.
Thanx anywat, might use it in a leter game...

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:07 am
by Bee-Ant
Use random timers...
AI is the most difficult thing

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:09 am
by Thanx
TURN BASED!!!
NO TIMING start turn-end turn Have you played a board game? That's what it is like...

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:20 am
by Bee-Ant
If you post the demo, I could fix it...maybe

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:23 am
by Thanx
Not now, Not on that PC, (in school) I'll post it later...

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:26 am
by Bee-Ant
OK, I'll wait

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 11:31 am
by DilloDude
I would do something like this: For convenience, have each territory store whether it's adjacent to an enemy territory. When a territory changes control, it would update the territories around it.
On the AI turn:
Create a list of all the territories you might want to do something with, and give each a priority. For this example, you might just use all territories that are either adjacent to an enemy territory, or adjacent to one that is. Loop through all these territories and set a priority value for them, then move it into a list and sort it according to priority as you go.
A territory adjacent to an enemy territory might start out with a priority of 2. If it has relatively low defense, you might decide to increase defense. Give it a priority based on just how low its defense is compared with the attack of enemy territories next to it. On the other hand, if attack is low and defense isn't too bad, you would want to increase attack. If adjacent enemy territories look conquerable, then attack. Give it a priority based on the relation to the enemy's defense and your attack.
A territory not adjacent to an enemy (but adjacent to one that is) would not start out with the priority bonus. If defense is too much lower than attack, increase defense, otherwise increase attack. Based on how low the value to increase is, set the priority.
You may want to add a bit of randomness to the priority, to make it more unpredictable - say add rand(3) to each value (this may need to be adjusted).
As well as priority, store what you decided to do with that territory (you can just use an integer: 0 - 3 for attack right, up, left, down, 4 and 5 for improve defense, improve attack). Now loook through the list and find where it fits in in the priority range. Insert it in there.
When you've gone through each territory, look down through the first five territories in the list, starting with the highest priority. Do the action you decided there. The turn is now over.
You may want to add a few different methods of calculating priority and moves, to make a few different AI algorithms. So one might be more defensive, while another would be more offensive in strategy.
Hope this gives you something to think about, although there are probably other ways. If you need help, just ask.

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 11:45 am
by Bee-Ant
Dill...too long of text

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 5:00 pm
by Thanx
Thanx Dill! Bee-Ant just has big-text-fobia. I've experienced that. Thanx again, I'm just somehow thinking on making the map by using arrays. Maybe an array just to tell where enemy and friendly territories are, one which will contain the attack levels, and one that contains the defence levels. Yeah, that should work I guess, what's your opinion? Thanx again, + 1 point for you! (Yes Bee-Ant, even though it's a bit long...) :D :D :D

Re: How does on AI work?

PostPosted: Mon Feb 04, 2008 10:55 pm
by DilloDude
It depends if you want to save it. If you aren't saving it, I would make a struct, which would be a territory. Each territory would have variables for attack and defense, which side owns it, and whether it is adjacent to an enemy territory.
If you want to save it, it would be simpler to use separate arrays, as you can pack them in save groups, but you could make your own function to save an array of structs. You'd want an array for the whole board - if you use save groups, id'd have to be a 1d array, otherwise you could use a 2d one. It depends.
When using structs, you can make any other references to them a pointer to the struct (for storing them in temporary lists and such). With separate arrays, you'd store an integer which is the index into the array.

Re: How does on AI work?

PostPosted: Tue Feb 05, 2008 4:41 pm
by Thanx
Could you please tell me a little more about structs? I haven't really heard about them. How do you create them?

Re: How does on AI work?

PostPosted: Wed Feb 06, 2008 3:57 am
by Bee-Ant
Btw, I'm learning about struct in school this day... :D

Re: How does on AI work?

PostPosted: Wed Feb 06, 2008 9:25 am
by DilloDude
A struct is a collection of related variables. You can define a struct as a variable type. Then you can make as many of those as you want. A struct's variables are accessed with a '.' after the struct name. If you have a pointer, use '->' instead. This is exactly what an Actor is. When you just use 'x' or 'textNumber', for example, you access the event actor's variables for those values. Bu when you access another actor's variables, you use 'actor.y' or 'actor.angle'. In this case, 'actor', the name of the Actor in question, is a struct. Using '.', you are accessing it's values.
Some functions in GE (getclone, CreateActor, getactor etc.) return a pointer to an Actor (Actor *). This means you can store a reference to the original Actor, and access it's current values, and change them. To do that, use '->' (eg. actor->animpos = 36). To set an actor (or any other variable) as a pointer, use '&'. At times it can get confusing what you're working with, and whether you need to use '*' or '&'. When a pointer, use '*'. When assigning a pointer, use '&'. WHen changing a pointer, use '*'. For instance:
Code: Select all
Actor *pActor = &ball.
pActor->xvelocity *= -1;

or with an int:
Code: Select all
int *pInt = &yscreen;
*pInt+= 50;

Like this, it is pointless. It comes handy when you want a parameter in a function, or if you want to store information in another location. In the above example, if I were to just make a separate int, and set it to yscreen, when I changed it, it would not change yscreen.
To define a struct, use:
Code: Select all
typedef struct
{
    int x;
    int y;
}coordinate;

In this way, you can make other useful things. For the territory example, you might do this:
Code: Select all
typedef struct
{
    int attack;
    int defense;
    char owner;
    char border;
    int x;//the x coordinate in the board array
    int y;//the y coordinate in the board array
}territory;

You can then make a 2d array of territories:
Code: Select all
territory board[100][100];

If you wanted to store a territory (such as in a temporary list) you would make a pointer:
Code: Select all
territory *tempList[10000];
int i;
int j;
int lPos = 0;
for (j = 0; j < 100; j ++)
{
    for (i = 0; i < 100; i ++)
    {
        if (board[i][j].border)
        {
            tempList[lPos] = &board[i][j];
            lPos ++;
        }
        else if//check adjacent territories...
    }
}

In this case, you might be able to combine both if statements. You'd then need somewhere to store priority and action. You could even make another struct which contains a territory:
Code: Select all
typedef struct
{
    int priority;
    char moveType;
    territory *location;
}move;

Then in the above example you'd change it to:
Code: Select all
move tempList[10000];
//...
            tempList[lPos].location = &board[i][j];
//...
\

to get information from the territory, you'd do something like this:
Code: Select all
tempList[i].location->defense

Of course, 'i' would be changed to whatever index you want.

Hope that helps a bit. If you have any further questions, ask them, I won't complain (I might get mad and not answer, but I won't complain :wink: ).