Thread: Coding enemy movement in game

1. Registered User
Join Date
Mar 2013
Posts
4

Coding enemy movement in game

Can someone give me a hand with this? I am trying to make a pretty simple game, it takes place on a grid like a checker board. The object of the game is to get to the enemy side of the board before they get to yours. To do this you can place down up to 5 blocks to trap the enemy and destroy 1 block in case your trapped yourself. I have the player movement and block placement already done, but my problem is that i cant figure out how to make my enemy not, well, stupid. Currently i had it trying to move up towards my side and if it couldnt then it would move another way. The problem is though that it will move into a blocked in position and then move itself out, only to move itself back into that position immeadiately after.

Can someone give me an idea of how i would make the enemy move so it wont get itself stuck in an endless loop of getting blocked in, moving back, getting blocked in, moving back.

2. Registered User
Join Date
Mar 2013
Posts
4
Code:
```getRedPos();
if (mCounter % 2 != 0) {
if (blockArray[redPosX][redPosY - 1] == "w"){

if (redPosX <= 5) {
if ((blockArray[redPosX][redPosY - 1] == "w") && (moveDir != 4)) { // Enemy move up
blockArray[redPosX][redPosY - 1] = "r";
blockArray[redPosX][redPosY] = "w";
moveDir = 1;
keyStuff();
} else if ((blockArray[redPosX + 1][redPosY] == "w") && (moveDir != 2)) { // enemy move right
blockArray[redPosX + 1][redPosY] = "r";
blockArray[redPosX][redPosY] = "w";
moveDir = 3;
keyStuff();
} else {
blockArray[redPosX][redPosY + 1] = "r";
blockArray[redPosX][redPosY] = "w";
moveDir = 4;
keyStuff();
}
} else if (redPosX >= 6) {
if ((blockArray[redPosX][redPosY - 1] == "w") && (moveDir != 4)) { // Enemy move up
blockArray[redPosX][redPosY - 1] = "r";
blockArray[redPosX][redPosY] = "w";
moveDir = 1;
keyStuff();
} else if ((blockArray[redPosX - 1][redPosY] == "w") && (moveDir != 3)) { // Enemy move left
blockArray[redPosX - 1][redPosY] = "r";
blockArray[redPosX][redPosY] = "w";
moveDir = 2;
keyStuff();
} else {
blockArray[redPosX][redPosY + 1] = "r";
blockArray[redPosX][redPosY] = "w";
moveDir = 4;
keyStuff();
}
}
}```
This was the code i was using, the array held all the positions on the board and if they held a "w" then you could move there, anything else and you couldnt.

3. you need to somehow craft a few boolean-returning test functions to smarten the AI.

for example, isBlocked(row,col), isEventuallyWinable(row,col), etc.

once you get them working, you can test a potential move before you make it and discard any that are fruitless.