Copyright 2010 by Pearson Education 1 Assignment 11: Critters reading: HW11 assignment spec.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

CS 112 Introduction to Programming
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Critters; Subtype Polymorphism Reading: HW9 Handout, Chapter 9.2.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Copyright 2010 by Pearson Education 1 Assignment 11: Critters.
Games and Simulations O-O Programming in Java The Walker School
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
MOM! Phineas and Ferb are … Aims:
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Building Java Programs Chapter 8 Lecture 8-3: Object state; Homework 8 (Critters) reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Written by: Dr. JJ Shepherd
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Copyright 2010 by Pearson Education Building Java Programs Homework 8: Critters reading: Critters Assignment Spec.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
CS 112 Introduction to Programming Method Overriding; Object Hierarchy; Event-Driven Programming Yang (Richard) Yang Computer Science Department Yale University.
CS 112 Introduction to Programming Critters, Polymorphism Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Weds, Nov. 26th Reading: Section Handout
Homework 8: Critters reading: HW8 spec.
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Debugging and Random Numbers
Building Java Programs
HW11 Assignment Specification
Lecture 8-3: Encapsulation, this
Critter exercise: Snake
File Handling Programming Guides.
Organizing Memory in Java
Homework 8: Critters (cont.)
CSE 142 Critters (IPL) behavior: Ant Bird Hippo Vulture
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Module 4 Loops.
CS139 October 11, 2004.
Building Java Programs
Building Java Programs
Building Java Programs
Homework 8: Critters (cont.)
Building Java Programs
Building Java Programs
Homework 9: Critters (cont.)
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Building Java Programs
Presentation transcript:

Copyright 2010 by Pearson Education 1 Assignment 11: Critters reading: HW11 assignment spec

Copyright 2010 by Pearson Education 2 Critters A simulation world with animal objects with behavior: fight animal fighting getColor color to display getMove movement toString letter to display toString eat food? You must implement: Ant Bird Vulture Hippo Longhorn (Wins and Creative)

Copyright 2010 by Pearson Education 3

4 Food Simulator places food randomly around world Eating food increases score for species, but … Critters sleep after eating simulator (CritterMain) handles this A Critter that gets in a fight while sleeping always loses simulator handles this

Copyright 2010 by Pearson Education 5 A Critter class public class name extends Critter {... } extends Critter tells the simulator your class is a critter override methods from Critter based on Critter spec Critter has a number of methods not required by the 4 simple Critter classes (Ant, Bird, Vulture, Hippo) … but you should use them to create a killer Longhorn

Copyright 2010 by Pearson Education 6 Scoring Score for each species: For all animals of that species Number of animals alive Number of fights won Pieces of food eaten

Copyright 2010 by Pearson Education 7 Mating Two Critters of same species next to each other mate and produce a baby Critter Simulator handles this Critters not asked if they want to mate Critters vulnerable while mating (heart graphic indicates mating) automatically lose fight The Simulator handles all of this You don't write any code to deal with mating

Copyright 2010 by Pearson Education 8 Critter Class

Copyright 2010 by Pearson Education 9 Enums Critter class has two nested Enums for Direction of movement and how to fight

Copyright 2010 by Pearson Education 10 Nested Enums To access a Direction or Attack a class external to Critter would use the following syntax: Critter.Direction.NORTH Critter.ATTACK.POUNCE Classes that are desecdants of Critter (like the ones you implement) do not have to use the Critter. it is implicit Direction.SOUTH, Attack.ROAR

Copyright 2010 by Pearson Education 11 How the simulator works When you press "Go", the simulator enters a loop: move each animal once ( getMove ), in random order if the animal has moved onto an occupied square, fight ! Key concept: The simulator is in control, NOT your animal. Example: getMove can return only one move at a time. getMove can't use loops to return a sequence of moves. It wouldn't be fair to let one animal make many moves in one turn! Your animal must keep state (as fields, instance variables) so that it can make a single move, and know what moves to make later.

Copyright 2010 by Pearson Education 12 Critter exercise: Stone Write a critter class Stone (the dumbest of all critters): MethodBehavior constructor public Stone() fight Always ROAR getColor Always Color.GRAY getMove Always Direction.CENTER toString"S" eat Always false

Copyright 2010 by Pearson Education 13 Ideas for state You must not only have the right state, but update that state properly when relevant actions occur. Counting is helpful: How many total moves has this animal made? How many times has it fought? Remembering recent actions in fields is helpful: Which direction did the animal move last? How many times has it moved that way?

Copyright 2010 by Pearson Education 14 Keeping state How can a critter move west until it fights? public Direction getMove() { while (animal has not fought) { return Direction.EAST; } while (animal has not fought a second time) { return Direction.EAST; } } private int fights; // total times Critter has fought public int getMove() { if (fights % 2 == 0) { return Direction.WEST; } else { return Direction.EAST; } }

Copyright 2010 by Pearson Education 15 Testing critters Use the MiniMain to create String based on actions and print those out Focus on one specific critter of one specific type Only spawn 1 of each animal, for debugging Make sure your fields update properly Use println statements to see field values Look at the behavior one step at a time Use "Tick" rather than "Go"

Copyright 2010 by Pearson Education 16 Critter exercise: Snake MethodBehavior constructor public Snake(boolean northSnake) fight alternates between SCRATCH and POUNCE getColor Yellow getMove north snakes: 5 steps north, pause 5 ticks, 5 steps north, pause 5 ticks, … otherwise: 5 steps west, pause 5 ticks, 5 steps west, pause 5 ticks, … eat always eats toString"K"

Copyright 2010 by Pearson Education 17 Determining necessary fields Information required to decide what move to make? Direction to go in Length of current cycle Number of moves made in current cycle Remembering things you've done in the past: an int counter? a boolean flag?