#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    3

    Messy for loop/if statement


    The following code works correctly. But I have to include a another if statement. The problem is that I always ruin the logic.

    Current code:

    Code:
        public void moveGhost(Tiles target) {
        if(specialIntersections()){
        direction = direction; //keeps going in the same direction
        }
        else{
        int oppDir;
        if(direction == UP){
        oppDir = DOWN;
        }
        else if(direction == DOWN){
        oppDir = UP;
        }
        else if(direction == LEFT){
        oppDir = RIGHT;
        }
        else{
        oppDir=LEFT;
        }
    
        double minDist = 10000.0;
        Tiles potentialNext;
    
        for(int i=0; i<4; i++){
            if(i!=oppDir){
             potentialNext = maze.nextTile(getCurrentPos(), i);
                  if(!(potentialNext.wall()) && check(potentialNext)){
                        if(calculateDistance(target, potentialNext) < minDist){
                           minDist = calculateDistance(target, potentialNext);
                           futureDirection = i;
                          }
                       }
                    }
                 }
              }
    
    
              changeDirection();
              timer++;
              increment();
    
              x += xinc;
              y += yinc;
              tunnel();
              }
    Another check I need to include:
    Code:
          //if the door is a wall (closed) the object cannot go through it
          if(DoorIsWall()){
    	if(!(potentialNext.wall()) && !(potentialNext.door())  && check(potentialNext)){
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please edit your post and format the code. The statements should not all start in the same column. That makes it hard to read and understand the logic. Indent the code 3-4 spaces that are inside of {}s.

    The last bit of code looks ok, the first part is not readable and needs to be reformatted.
  4. #3
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    If statement.


    Why would the additional condition check ruin the logic?

    Start analysing the logical rules coded in the method. Then, "mock run" the codes in your brain, imagining yourself as the subject/character in the game that is governed by the rules implemented in the method.

    Now, find out where is the most suitable place where DoorIsWall() method should be executed. Since we do not know what is inside your DoorIsWall() method body, and you did not explicitly tell us why and how does adding this extra check ruin the game logic, we cannot tell much to help.
    When the programming world turns decent, the real world will turn upside down.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    38
    Rep Power
    9
    What does your code is suposed to do with all those checks after the "DoorIsWall()"
    You only show the if it is, if it isn't what's suposed to do?

IMN logo majestic logo threadwatch logo seochat tools logo