We are going to submit our C++ project on a game, which resembles Classic VideoGame game "Battle Rush", by next week, but we don't know much more about how to make artificial intelligence of the enemy tanks. So do you have any idea that how to update and move those enemy tanks in our game......

How are the tanks represented? May I assume you have 2D grid of the Battle field with the tanks each having x and y coordinates, or something in that vein? (That's how I did it when I was learning c++. And I've never played battle rush). AI is a very …

Okay... let's look at the issues:

1) You say there are multiple enemy tanks to be controlled. Does this mean there is only one user, whom all the tanks attack? If so, the 'Tank target' in the code I gave you would simply be the user. If the tanks …

Okay, that helps a little. I assume the enemy tanks can attack either of the good tanks. Considering the tanks have x and y defined, target tracking should work pretty much exactly like the code I gave you. However - you must decide the goal of the tanks. I assumed …

## All 9 Replies

How are the tanks represented? May I assume you have 2D grid of the Battle field with the tanks each having x and y coordinates, or something in that vein? (That's how I did it when I was learning c++. And I've never played battle rush). AI is a very wide field, but since time does not seem to be on your side, (so learning algorithms and the like go out the window :() you might want to consider target tracking.

There are about a million links online that seem pretty much useless... lots of high Maths talk that confuses me. When I did this, I just had to figure out my own algorithm.

Haven't done c++ for a while, so excuse the syntax if its a little off. First move is to find the target closest to the tank you're working with at the moment. let xDist = the difference in x coordinates between the 2 tanks, and let yDist = the difference in y coordinates. Now consider 3 cases:

Case 1:
yDist >= 2 * xDist
if target is above me, move my tank UP, else move me down

Case 2:
xDist >= 2 * yDist
if target is to my right, move me right, else move me left

Case 3:
else:
Move me vertically and horizontally, in the appropriate direction

I'll try a little code, but like I said the syntax is probably off:

``````//The tank I'm moving right now
Tank me;
//My nearest target
Tank target;

//Get xDist and yDist
xDist = me.xCoord - target.xCoord;
yDist = me.yCoord - target.yCoord;

//Negatives will cause problems, get rid of them
//Use fabs: floating point absolute value
xDist = Math.fabs(xDist);
yDist = Math.fabs(yDist);

if (yDist >= (2 * xDist))
{
if (target.yCoord > me.yCoord) //Move up
me.yCoord++;
else //Move down
me.yCoord--;
}
else if (xCoord >= (2 * yCoord))
{
if (target.xCoord > me.xCoord) //Move right
me.xCoord++;
else //Move left
me.xCoord--;
}
else
{
if (target.yCoord > me.yCoord) //Move up
me.yCoord++;
else //Move down
me.yCoord--;
if (target.xCoord > me.xCoord) //Move right
me.xCoord++;
else //Move left
me.xCoord--;
}``````

That's basic target tracking. Hope that helps some :)
M

Thanks for all the suggestion. But I think there are more things to be considered.
1> There wiill be many enemy tanks to be controlled.
2> Each time everyones coordinate must be updated.
3> Whether or not a shooting is to be done should be determined, (this seems to be tricky to me)
4> Maintain the intelligence with each increase in level
.....and things like that.

I hope these are the points I wanna include. I would be grateful to have suggestions as above.

Okay... let's look at the issues:

1) You say there are multiple enemy tanks to be controlled. Does this mean there is only one user, whom all the tanks attack? If so, the 'Tank target' in the code I gave you would simply be the user. If the tanks can attack each other, then just stick to closest target. To control all the tanks, just perform a loop over all the enemy tanks and use the target tracking on each tank.

2)Could you gave me some code to show how each tank is represented? Otherwise it's a little hard to figure it out. A Tank class with x and y coordinates, as I assumed in the code I gave you, is an easy solution, but I don't know if you're allowed to do that... or if you've even been taught it. Took a while before I learnt about proper classes. But if you are using classes, the code I gave should do the trick. Otherwise I'll take a look at your code and do my best to find a way :)

3) Whether or not to shoot: Personally, I'd go with one of two possibilities here:

Case 1:
Limited range: Enemy tanks have a limited range, and can only shoot when they are a certain distance from the user (or closer). Eg if you set the range to five, an enemy tank can fire when they are 5 or less units from the user.

Case 2:
Limited targeting ability: Enemy tanks can only shoot straight (this is probably easier) so only if xDist or yDist is 0 can they shoot, and they shoot towards the user.

I'd say enemy tanks should shoot whenever the chosen case allows it. To ensure that they do not shoot a constant stream of bullets (which would be very difficult to play against), the moment a tanks shoots, set a counter for that tanks to x, say 5, and each turn reduce it by one. A tank can only shoot when it's counter is zero.

4) Difficulty: Assume there are 4 difficulty levels, say easy, medium hard and very hard. Keep track of this, as an int. So easy = 0, medium = 1 and so on.
Now, before a tanks moves, use a random number generator. Can't really remember how random numbers work in c++, so again forgive the syntax:

``````//There are four difficulty levels: store as an into between 0 and 3
int difficulty
//Now determine how the tank will move:
//Case 1: Intelligent
if (random.nextInt(5) <= difficulty)
{
//Target tracking
}
else //Case 2: random
{
//Move randomly
}``````

Take note of the following: The difficulties are in the range [0..3] while the number generated is in the range [0..4]. This ensures that even on the highest difficulty level, sometimes the enemy makes mistakes.

Hope that helps. If you have any other questions I'll do my best to answer. I enjoy AI (probably gonna study it next year) Just wish you had more time for this... we could try out some learning algorithms... *sigh* always next time.

And don't forget to let me know how you represent your tanks so I can answer question 2 :)

M

Lets see how I have started the game.

There is a class called Tank.
From it I have derived two classes for good tank and enemy tanks(because they used various common functions such as blit and so on).
As you guessed, i have used a 2D array grid to represent the map.
I also have designed the map creator(its relatively simple).
The game loads the map at the starting, there are various types of blocks. Our target is to save THE EAGLE(a block to be protected by us).

And about the tanks, In the class derived from tank base, x and y have been defined.
So for two good players, two objects from the goodtank classes are created. And for enemies, an array of the badtank objects.
If any suggestions It would be great.

`And about the algorithms, what are those. I also am interested in things like these.

Okay, that helps a little. I assume the enemy tanks can attack either of the good tanks. Considering the tanks have x and y defined, target tracking should work pretty much exactly like the code I gave you. However - you must decide the goal of the tanks. I assumed the enemy tanks had the purpose of destroying the good tanks. But by the sounds of it, their purpose is to destroy the eagle... if that is the case, then the eagle will be your target as opposed to a good tank. But that's a minor point.

The second question you asked in your previous post was updating everyone's coordinates - I don't see that this is a problem, since you have x and y coordinates defined, and the target tracking changes those based on its "intelligent" decision... Maybe elaborate on what you don't understand there, cos I don't quite get the problem.

The way you've started seems good to me. Seems like the way I would've started anyway.

Learning algorithms are a very interesting field. I only have a basic understanding, but the basic concept is that instead of "hard coded" decisions like in the code I gave you, the programme learns for itself what a good decision is.

So, in this example, each tank would have associated with it a "list" of moves. Each time a tank has to move, it will either choose a move from the list, or move randomly.

Now here is where things get interesting. The tanks are given a way to decide if a move is "good" or "bad". In this case, a move that increases distance from the target is bad, one the reduces it is good. When the tank chooses a move from its list, it chooses the best move from the list. If it moves randomly, it evaluates that move afterwards, and places it in the list.

Random moves should only be chosen from those moves not yet evaluated. In the case of the tanks, they can only move in one of 8 ways (up, down, left, right and the 4 diagonals). Eventually, no more random moves will be available, since all decisions have already been evaluated, and the tanks will have "learnt" the best possible decision to make. :) Hope that quick explanation helps.

One problem sometimes is that a good decision in one situation is not good in another. For example, when the target is directly to the tank's right, moving right is the best decision. However, when the target is directly to your left, this is the worst decision. So you need a number of lists to allow for every possible situation...

But what if you do not foresee a particular situation. One solution to this, I suppose, is to allow your intelligent class to make a new list for itself if it cannot find a current list that matches the situation. But this can be very tricky... and considering you have less than a week left if I correctly remember your first post, it's utterly unnecessary for now.

Let me know how it goes, and please ask any question you have. I enjoy thinking about AI :) And If you do have some extra time, consider attempting the learning algorithms ... the situation is simple enough that each tank would only need 8 lists as far as I see it.

And when you finish this game please send it to me, I'd love to see it. :)

M

We think ,you haven't understand our game completely rather partially. Although your logic and the concept of AI is very useful to us. So thanks for sharing and spending time for us.
In our game,there are two (or one) good tanks which are controlled by the users or game players, and saveral enemy tanks which are controlled by our logic ( so called AI or Computer). All gameplay completed in a map. A map consist of different types of blocks(Some Breakable, some transparent, some grass). If the bullets, fired by the good tanks or enemy tanks, strikes the breakable block, certain part of the field contained by that block get emptied(filled 0 in the specified position of field array). Tanks can move on the grass blocks. A special type of block is designed as home of the good tanks, whose position is constant for every map. Good tanks have to save the home from the enemies.
So the target of the enemy tanks will be home as well as good tanks.
In this way our game will be completed....

So now the problem is when will the enemy tanks shoot at the breakable blocks.

Okay... I think I'm starting to get a better idea of your game.

Is it "good" for a tank to shoot at a breakable block. Will it help them if they shoot breakable blocks. If so, then they should shoot the breakable blocks whenever they can.

However, if shooting the breakable blocks makes it harder for them, then they should avoid the breakable blocks. Of course build in randomness and the like. But I don't know if I can really help much more.

Hope you manage it :)

Can you help us about target tracking..!!, How good tanks can be tracked by the enemy tanks..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.