www.webdeveloper.com
Results 1 to 3 of 3

Thread: Coding enemy movement in game

  1. #1
    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. #2
    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. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles