java - "GridWorld" for ThinkJava Exercise 5.1 -
newbie completeing thinkjava book , trying figure out 1 of answers exercises. calls me download "gridworld" files , complete following steps:
- write method named movebug takes bug parameter , invokes move. test method calling main.
- modify movebug invokes canmove , moves bug if can.
- modify movebug takes integer, n, parameter, , moves bug n times (if can).
- modify movebug if bug can’t move, invokes turn instead.
i stuck on number 3, can not figure out how pass n "move() method"
-please newbie
my code:
import info.gridworld.actor.actorworld; import info.gridworld.actor.bug; import info.gridworld.actor.rock; public class bugrunner { public static void main(string[] args) { actorworld world = new actorworld(); bug redbug = new bug(); world.add(redbug); world.add(new rock()); world.show(); movebug(redbug,5); system.out.println(redbug.getlocation()); } public static void movebug(bug abug, int n){ if(abug.canmove() == true){ abug.move(); } else { abug.turn(); } } }
you mean you're stuck on number 3:
- modify movebug takes integer, n, parameter, , moves bug n times (if can).
this means write loop, looping n
times, calling move()
once per iteration, if canmove()
returns true.
btw: if (canmove() == true) {...}
long way if (canmove()) {...}
.
and fix indentation of if
statement.
Comments
Post a Comment