HW11 Assignment Specification

Slides:



Advertisements
Similar presentations
CS 112 Introduction to Programming
Advertisements

Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
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.
Copyright 2010 by Pearson Education 1 Assignment 11: Critters reading: HW11 assignment spec.
Copyright 2010 by Pearson Education 1 Assignment 11: Critters.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
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.
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.
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.
Chapter 4 - Finishing the Crab Game
Adapted from slides by Marty Stepp and Stuart Reges
CSC 211 Java I for loops and arrays.
REPETITION CONTROL STRUCTURE
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Introduction To Repetition The for loop
Weds, Nov. 26th Reading: Section Handout
Homework 8: Critters reading: HW8 spec.
Agenda Warmup AP Exam Review: Litvin A2
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
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Programming – Touch Sensors
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
While Loops BIS1523 – Lecture 12.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Introduction to Programming
Arrays, For loop While loop Do while loop
Lecture 8-3: Encapsulation, this
Critter exercise: Snake
File Handling Programming Guides.
© Akhilesh Bajaj, All rights reserved.
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
Let’s all Repeat Together
Topic 31 - inheritance.
Homework 8: Critters (cont.)
Building Java Programs
Building Java Programs
Homework 9: Critters (cont.)
Another Example Problem
Building Java Programs
Chapter 9 9-3: Polymorphism reading: 9.3
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Building Java Programs
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

HW11 Assignment Specification Assignment 11: Critters HW11 Assignment Specification

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

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.

Scoring Score for each species: For all animals of that species Number of animals alive Number of fights won Pieces of food eaten

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

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

Critter Class

Enums Critter class has two nested Enums for Direction of movement and how to fight

Nested Enums Direction.SOUTH, Attack.ROAR 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 descendants of Critter (like the ones you implement) do not have to use the Critter. it is implicit Direction.SOUTH, Attack.ROAR

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 an interesting and successful Longhorn

Critter exercise: Stone Write a critter class Stone(the dumbest of all critters): Method Behavior constructor public Stone() fight Always Attack.ROAR getColor Always Color.GRAY getMove Always Direction.CENTER toString "S" eat Always false

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?

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) { private int fights; // total times Critter has fought public int getMove() { if (fights % 2 == 0) { return Direction.WEST; } else {

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"

Critter exercise: Snake Method Behavior constructor public Snake(boolean northSnake) fight alternates between SCRATCH and POUNCE getColor Yellow getMove north bound 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"

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?