Karel J Robot
description
Transcript of Karel J Robot
Karel J RobotSelected Advanced TopicsChapter 7 and 8
RecursionRecursion – where the function
being defined is applied within it’s own definition
Ex)(p185) public void findBeeper(){ if(!nextToABeeper){ move(); findBeeper();}}
RecursionRecursive functions do not use loops!Fun Practice:What is a factorial?Create the following method:public static int factorial(int x)Hints: what does a factorial do?Takes the number… multiply’s it by the
next smallest integerStops at 1Your method should evaluate whether or
not you’ve reached 1
Solution public static int factorial(int x)
{ if(x<=1) { return 1; } return x*factorial(x-1); }
challengeCreate the following method:int sumOfDigits(int x)If x is 39, sumOfDigits should
return 12Hints: base case is a number
from 0-9For the recursive call, consider
how x/10 or x%10 will help% - modulus – returns the
remainder/ - division – doesn’t round:
9/2 = 4
HomeworkRead chapter 7
Thread creationIn the main, we have typically seen the
following:MileMover karel = new
MileMover(1,1,East,0);karel.move(); Having a robot set up his own thread, we can
do the following:MileMover karel = new MileMover(1,1,East,
0);He will do the same thing as above without
calling any methods if we have it set up its own thread!
Thread creationImplement the Runnable class //this is a java
class that sets up threadsIn constructor:World.setupThread(this) //this tells the robot
to run itselfNEED: public void run()Inside of the run method will be the code that
will automatically runjLastly – in the main, we need:World.setTrace(false);
World.showSpeedControl(true)Click resume!
Thread creationModify the path finding robot
from ch 6 pr set so that in the main, you only have the following:
new ch6pr14(2,2,East,0)//you don’t even have to give the
robot a name!
2 different threadsConcurrent threads sometimes
pose issuesHomeworkRead chapter 8 – program the
racers on 212 and the philosophers on 214
Problem setCh 7, #1(Solve just #16
recursively), 18Ch 8, #1 (requires editing
steeplechase program)PACMAN
pacmanOur final karel experience:
◦1st – set up the robot world to mimic a pacman world (karel images replaces with pacman, etc.)
◦2nd – create an abstract class called ghost
◦Look up the 4 different types of ghosts…. And program their behaviors into different types of classes!
Pacman3rd – the pacman class
◦We need to be able to control him◦Eventually we will work with
keylisteners for seemless play, but for now we’ll do an easier solution
- We need a ‘commandPacman()’ method
- Creates a local variable:- Scanner kbd = new
Scanner(System.in);
PacmancommandPacman() continued…Need a variable to hold keyboard input:char move = kb.next().charAt(0); Need a switch statement so 4 different keys control
movements:switch(move){ case ‘w’:
karel.faceNorth();karel.move();break;
case ‘s’: karel.faceSouth(); //… you can figure the rest!
Use a while loop so that move == ‘q’ makes karel turnOff
ProjectWork in pairs! (we will try and mimic
teamwork)1 person can work on pacman while
the other on ghosts10 points – working pacman class10 points – working ghost classes (i.e.
shut off pacman when they touch)2 points – accurate world3 points – bonus for best version
(class will vote)