Trying to modify or add a method so that every time a wombat moves 24 blocks it uses up one leaf.


1 2 3 4 5 6 7 | pulbic void act() { moves++; if (moves >= 24 ) { moves = 0 ; leafesEaten--; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void act() { if (foundLeaf() && leavesEaten < 5 ) { eatLeaf(); } else if (canMove()) { move(); } else { turnLeft(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | public class Wombat extends Actor { private static final int EAST = 0 ; private static final int WEST = 1 ; private static final int NORTH = 2 ; private static final int SOUTH = 3 ; private int direction; private int leavesEaten; public Wombat() { setDirection(EAST); leavesEaten = 0 ; } /** * Do whatever the wombat likes to to just now. */ public void act() { if (foundLeaf() && leavesEaten < 5 ) { eatLeaf(); } else if (canMove()) { move(); } else { turnLeft(); } } /** * Check whether there is a leaf in the same cell as we are. */ public boolean foundLeaf() { Actor leaf = getOneObjectAtOffset( 0 , 0 , Leaf. class ); if (leaf != null ) { return true ; } else { return false ; } } /** * Eat a leaf. */ public void eatLeaf() { Actor leaf = getOneObjectAtOffset( 0 , 0 , Leaf. class ); if (leaf != null ) { // eat the leaf... getWorld().removeObject(leaf); leavesEaten = leavesEaten + 1 ; } } /** * Move one cell forward in the current direction. */ public void move() { if (!canMove()) { return ; } switch (direction) { case SOUTH : setLocation(getX(), getY() + 1 ); break ; case EAST : setLocation(getX() + 1 , getY()); break ; case NORTH : setLocation(getX(), getY() - 1 ); break ; case WEST : setLocation(getX() - 1 , getY()); break ; } } /** * Test if we can move forward. Return true if we can, false otherwise. */ public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch (direction) { case SOUTH : y++; break ; case EAST : x++; break ; case NORTH : y--; break ; case WEST : x--; break ; } // test for outside border if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { return false ; } else if (x < 0 || y < 0 ) { return false ; } return true ; } /** * Turns towards the left. */ public void turnLeft() { switch (direction) { case SOUTH : setDirection(EAST); break ; case EAST : setDirection(NORTH); break ; case NORTH : setDirection(WEST); break ; case WEST : setDirection(SOUTH); break ; } } /** * Sets the direction we're facing. The 'direction' parameter must * be in the range [0..3]. */ public void setDirection( int direction) { if ((direction >= 0 ) && (direction <= 3 )) { this .direction = direction; } switch (direction) { case SOUTH : setRotation( 90 ); break ; case EAST : setRotation( 0 ); break ; case NORTH : setRotation( 270 ); break ; case WEST : setRotation( 180 ); break ; default : break ; } } /** * Tell how many leaves we have eaten. */ public int getLeavesEaten() { return leavesEaten; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // insert at line 10 private int moves; // insert at line 89 (after the 'switch' block moves++; if (moves == 24 ) { moves = 0 ; if (leavesEaten > 0 ) { leavesEaten--; } else { // die? } } |