/* --- CSU213 Fall 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 12: October 2, 2006 Part 1: Using Libraries */ import draw.*; import colors.*; import geometry.*; import java.util.Random; /* +------------------------+ | LoStars |<------------------+ +------------------------+ | +------------------------+ | | LoStars moveFixed() | | | LoStars move() | | | boolean draw(Canvas x) | | +------------------------+ | | | / \ | --- | | | ------------------------------- | | | | +------------------------+ +------------------------+ | | MTLoStars | | ConsLoStars | | +------------------------+ +------------------------+ | +------------------------+ +-| Star first | | | LoStars moveFixed() | | | LoStars rest |-+ | | LoStars move() | | +------------------------+ | | | boolean draw(Canvas x) | | | LoStars moveFixed() | | | +------------------------+ | | LoStars move() | | | | | boolean draw(Canvas x) | | | | +------------------------+ | | | | | | +--+ v +------------------------+ | Star | +------------------------+ | Posn loc | | int lifespan | +------------------------+ | Star moveFixed() | | Star move() | | boolean draw(Canvas c) | | int randomX() | | int randomY() | +------------------------+ */ //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent a shooting star class Star { Posn loc; int lifespan; Star(Posn loc, int lifespan) { this.loc = loc; this.lifespan = lifespan; } // move this star down 3 pixels if alive, 4 pixels if burned Star moveFixed(){ if (lifespan > 0) return new Star(new Posn(this.loc.x, this.loc.y + 3), this.lifespan - 1); else return new Star(new Posn(this.loc.x, this.loc.y + 4), this.lifespan); } // move this star down Star move(){ if (lifespan > 0) return new Star(new Posn(this.loc.x + this.randomX(), this.loc.y + this.randomY()), this.lifespan - 1); else return new Star(new Posn(this.loc.x, this.loc.y + 4), this.lifespan); } // draw this star on the given canvas boolean draw(Canvas c){ if (this.lifespan > 0) return c.drawDisk(this.loc, 4, new White()); else return c.drawDisk(this.loc, 3, new Black()); } // produce a random distance to move left or right int randomX(){ return 2 - new Random().nextInt() % 5; } // produce a random distance to fall down int randomY(){ return new Random().nextInt() % 5; } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent shooting stars in our world interface LoStars { // move the stars in this list down a fixed distance LoStars moveFixed(); // move the stars in this list down LoStars move(); // draw the stars in this list boolean draw(Canvas x); } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent an empty list of stars class MTLoStars implements LoStars { MTLoStars() { } // move the stars in this list down a fixed distance LoStars moveFixed() { return this; } // move the stars in this list down LoStars move() { return this; } // draw the stars in this list boolean draw(Canvas x) { return true; } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent a nonempty list of stars class ConsLoStars implements LoStars { Star first; LoStars rest; ConsLoStars(Star first, LoStars rest) { this.first = first; this.rest = rest; } /* // ** DRAFT TEMPLATE ** Edit as needed. LoStars move() { ... this.first ... ... this.rest.move(...) ... ... this.rest.draw(...) ... } */ // move the stars in this list down a fixed distance LoStars moveFixed() { return new ConsLoStars(this.first.moveFixed(), this.rest.moveFixed()); } // move the stars in this list down LoStars move() { return new ConsLoStars(this.first.move(), this.rest.move()); } // draw the stars in this list boolean draw(Canvas c) { return this.first.draw(c) && this.rest.draw(c); } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // the world of shooting stars class StarWorld extends World{ LoStars stars; StarWorld(LoStars stars){ this.stars = stars; } World onTick(){ return new StarWorld(this.stars.move()); } World onKeyEvent(String ke){ return this; } boolean draw(){ return theCanvas.drawRect(new Posn(0, 0), 200, 300, new Blue()) && this.stars.draw(this.theCanvas); } boolean erase(){ return theCanvas.drawRect(new Posn(0, 0), 200, 300, new Blue()); } } class Examples{ Examples() {} Star s1 = new Star(new Posn(30, 0), 30); Star s2 = new Star(new Posn(40, 10), 20); Star s3 = new Star(new Posn(70, 20), 40); Star s4 = new Star(new Posn(50, 60), 20); Star s5 = new Star(new Posn(80, 30), 40); Star s6 = new Star(new Posn(20, 20), 50); Star s7 = new Star(new Posn(40, 40), 50); Star s8 = new Star(new Posn(70, 30), 30); Star s9 = new Star(new Posn(10, 40), 10); Star s10 = new Star(new Posn(60, 40), 20); Star burned = new Star(new Posn(60, 40), 0); LoStars mtstars = new MTLoStars(); LoStars stars = new ConsLoStars(this.s1, new ConsLoStars(this.s2, new ConsLoStars(this.s3, new ConsLoStars(this.s4, new ConsLoStars(this.s5, new ConsLoStars(this.s6, new ConsLoStars(this.s7, new ConsLoStars(this.s8, new ConsLoStars(this.s9, new ConsLoStars(this.s10, this.mtstars)))))))))); LoStars starsx = new ConsLoStars(this.s1, new ConsLoStars(this.burned, new ConsLoStars(this.s3, this.mtstars))); // test the method move in the class Star boolean testMoveFixedStar = (check this.s1.moveFixed() expect new Star(new Posn(30, 3), 29)) && (check this.burned.moveFixed() expect new Star(new Posn(60, 44), 0)); // test the method move in the class LoStars boolean testMoveFixedLoStars = (check this.mtstars.moveFixed() expect this.mtstars) && (check this.starsx.moveFixed() expect new ConsLoStars(new Star(new Posn(30, 3), 29), new ConsLoStars(new Star(new Posn(60, 44), 0), new ConsLoStars(new Star(new Posn(70, 23), 39), this.mtstars)))); StarWorld sw = new StarWorld(this.stars); boolean go = sw.bigBang(200, 300, 0.1); }