# Coding enemy movement in game

• 04-09-2013, 04:53 PM
robgw3
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.
• 04-09-2013, 05:02 PM
robgw3
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.
• 04-10-2013, 11:21 AM
rnd me
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.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09875 seconds
• Memory Usage 2,366KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• friendlyurl_resolve_class
• friendlyurl_clean_fragment
• friendlyurl_geturl
• process_templates_complete