# Need Criticism on coding style of my AI code

This topic is 2135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here is the AI code for the monster that moves left and right in a game.

Note: movementRightFlag and movementLeftFlag variable gets turned on and off at different circumstances.

I am trying to get into better design habits mainly because program design was not taught in college.

I need criticisms on the code. I welcome all feedback.

Code is in Java.

public BatSprite(double x, double y) {
runOnce = true;
}

public void update(long milliseconds)
{
if(movementLeftFlag)
{

if(position.getY() > leastXPositionThreshold)
{

position.setX(position.getX() - 1);

if(position.getX() == leastXPositionThreshold)
{
runOnce = false;

movementRightFlag = true;

}

}

}

if(movementRightFlag)
{
movementLeftFlag = false;
position.setX(position.getX() + 1);

if(position.getX() > mostXPositionThreshold)
{

runOnce = true;
movementRightFlag = false;

}

}

// extra logic that changes the state of the monster direction
if(position.getY() == 50)
{
direction = "down";

if(runOnce)
{

movementLeftFlag = true;

}

}
}

Edited by warnexus

##### Share on other sites

Make your spacing perfectly consistent.  Maybe that was something that got messed up in cut/paste.

##### Share on other sites

You dont need that  movementLeftFlag and the other one...

just declare single variable for direction (could be vector2, dont use x,y but vector - this will give you easier writing algorithms)

if(

(direction >0 && position.getX() > mostXPositionThreshold) ||

(direction <0 && position.getX() > leastXPositionThreshold)

) direction=-direction;

position.setX(position.getX() + direction);

offcouse it can be buggy if speed of your AI element variates, but the code could be something similar to this....
or:

if(direction >0 && position.getX() > mostXPositionThreshold)direction=-speed;else

if(direction <0 && position.getX() > leastXPositionThreshold)direction=speed;

position.setX(position.getX() + direction);

Edited by serumas

##### Share on other sites

You dont need that  movementLeftFlag and the other one...

just declare single variable for direction (could be vector2, dont use x,y but vector - this will give you easier writing algorithms)

if(

(direction >0 && position.getX() > mostXPositionThreshold) ||

(direction <0 && position.getX() > leastXPositionThreshold)

) direction=-direction;

position.setX(position.getX() + direction);

offcouse it can be buggy if speed of your AI element variates, but the code could be something similar to this....
or:

if(direction >0 && position.getX() > mostXPositionThreshold)direction=-speed;else

if(direction <0 && position.getX() > leastXPositionThreshold)direction=speed;

position.setX(position.getX() + direction);

I don't think direction should be a numeric value. Why are you making direction a numeric value?

##### Share on other sites

Make your spacing perfectly consistent.  Maybe that was something that got messed up in cut/paste.

True. I thought there might have a comment about the flag variables. But I guess those are fine then?

##### Share on other sites

I don't think direction should be a numeric value. Why are you making direction a numeric value?

Uh... encoding directions/offsets/positions as vectors (numeric coordinates) is a very, very standard practice. Why do you think it's wrong?

##### Share on other sites

I don't think direction should be a numeric value. Why are you making direction a numeric value?

Uh... encoding directions/offsets/positions as vectors (numeric coordinates) is a very, very standard practice. Why do you think it's wrong?

I usually see direction implemented using enums and I coded mine using strings for my own convenience and it makes it more readable for me. I do not know the standard practice because I was never exposed to it. I am a CS undergraduate.

It seems weird to think of direction as a number. I was always thought of them as up,down,left and right. How can one say "up is 10, 350 or 800"?

Is there a link anyone can refer to me in regards to "standard practices"? It would surely broaden my horizon and make me a better programmer.

Edited by warnexus

##### Share on other sites

I don't think direction should be a numeric value. Why are you making direction a numeric value?

Direction is generally represented by a vector or an angle in games. Or rather a character's movement is represented by its velocity which is represented by a vector. So each update you can simply do:

position += velocity; To move your character.

Then if you wanted to change directions you could simply do:

velocity *= -1.0f; to flip it the other direction.

You can represent direction in another way if you want, but generally it introduces branching (if statements) which adds complexity to code. For example, each time you have an if/else block you introduce another code path, which increases the complexity of your program and requires additional testing.

##### Share on other sites

I don't think direction should be a numeric value. Why are you making direction a numeric value?

Direction is generally represented by a vector or an angle in games. Or rather a character's movement is represented by its velocity which is represented by a vector. So each update you can simply do:

position += velocity; To move your character.

Then if you wanted to change directions you could simply do:

velocity *= -1.0f; to flip it the other direction.

You can represent direction in another way if you want, but generally it introduces branching (if statements) which adds complexity to code. For example, each time you have an if/else block you introduce another code path, which increases the complexity of your program and requires additional testing.

Ah I see. Good point. I notice the branching in my own code too. It would seem complicated in the future like you said. Okay I will change direction into a vector. Thanks.

##### Share on other sites

Also, in general it's good to think about character movement in mathematical terms. We can mathematically traverse the coordinate space using vectors (velocities and  positions at the very basic).

When you get to a little more complex movement (such as steering) it would be ridiculous to write code than modifies the X and Y motion by hand rather than simply rotating the velocity vector.

• 11
• 13
• 9
• 12
• 15