Need Criticism on coding style of my AI code

Started by
14 comments, last by Nicholas Kong 10 years, 6 months ago

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;
                        
                    
                }
                
            }
}
Advertisement

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

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

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);

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?

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?

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?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.


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.


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.

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.

This topic is closed to new replies.

Advertisement