karel j robot selected advanced topics chapter 7 and 8

15
Karel J Robot Selected Advanced Topics Chapter 7 and 8

Upload: jaylyn-shade

Post on 01-Apr-2015

228 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Karel J Robot Selected Advanced Topics Chapter 7 and 8

Karel J RobotSelected Advanced TopicsChapter 7 and 8

Page 2: Karel J Robot Selected Advanced Topics Chapter 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();}}

Page 3: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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

Page 4: Karel J Robot Selected Advanced Topics Chapter 7 and 8

Solution public static int factorial(int x)

{ if(x<=1) { return 1; } return x*factorial(x-1); }

Page 5: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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

Page 6: Karel J Robot Selected Advanced Topics Chapter 7 and 8

HomeworkRead chapter 7

Page 7: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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!

Page 8: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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!

Page 9: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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!

Page 10: Karel J Robot Selected Advanced Topics Chapter 7 and 8

2 different threadsConcurrent threads sometimes

pose issuesHomeworkRead chapter 8 – program the

racers on 212 and the philosophers on 214

Page 11: Karel J Robot Selected Advanced Topics Chapter 7 and 8

Problem setCh 7, #1(Solve just #16

recursively), 18Ch 8, #1 (requires editing

steeplechase program)PACMAN

Page 12: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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!

Page 13: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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

Page 14: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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

Page 15: Karel J Robot Selected Advanced Topics Chapter 7 and 8

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)