import geometry.*; import colors.*; import draw.*; import java.util.Random; // shark class class Shark { int y; int size; Shark(int y, int size) { this.y = y; this.size = size; } // returns a new shark for a givin key input /* ...this.y... -- int ...keyInput... --string ...this.size... --int */ Shark onKeyEvent(String input){ if (input.equals("up")) return new Shark(this.y - 5, this.size); else if (input.equals("down")) return new Shark(this.y + 5, this.size); else return this; } // Draws the shark on a givin canvas /* ...this.y... -- int ...canvas... -- canvas ...this.size... -- int */ boolean drawShark(Canvas c){ return c.show() && c.drawCircle(new Posn(50, this.y), this.size, new Black()); } // makes the shark hungrier as the game proceeds /* ...this.y... -- int ...this.size... -- int */ Shark onTick(){ if (this.size == 4) return new Shark(this.y, 4); else return new Shark(this.y, (this.size - 1)); } // when the shark eats a fish it gets bigger /* ...this.y... -- int ...this.size... -- int */ Shark eatFish(){ return new Shark(this.y, (this.size + 25)); } } // Fish Class class Fish { int x; int y; Fish(int x, int y) { this.x = x; this.y = y; } // ticks the fish forword along the canvas /* ...this.x... -int ...this.y... -int */ Fish onTick(){ return new Fish((this.x - 5), this.y); } // returns true if a fish has escaped the shark /* ...this.x... -int ...this.y... -int */ boolean escaped(){ if (this.x < 0) return true; else return false; } // Draws a fish on the canvas /* ...this.x... -int ...this.y... -int ...canvas... -canvas */ boolean DrawFish(Canvas c){ return c.show() && c.drawCircle(new Posn(this.x, this.y), 3, new Red()); } // shark found fish /* ...this.x... -int ...this.y... -int ...shark.y... -int ...shark.size... -int */ boolean sharkFoundFish(Shark shark){ return Math.sqrt(Math.pow((this.x - 50), 2) + Math.pow((this.y - shark.y), 2)) <= shark.size; } } // Ocean world class OceanWorld extends World{ Shark shark; Fish fish1; Fish fish2; Fish fish3; Canvas oceanScene; OceanWorld(Shark shark, Fish fish1, Fish fish2, Fish fish3, Canvas oceanScene) { this.shark = shark; this.fish1 = fish1; this.fish2 = fish2; this.fish3 = fish3; this.oceanScene = oceanScene; } World onTick(){ if (this.fish1.escaped()) return new OceanWorld(this.shark, new Fish(300, (new Random().nextInt() % 290)), this.fish2, this.fish3, this.oceanScene); else if (this.fish2.escaped()) return new OceanWorld(this.shark, this.fish1, new Fish(300, (new Random().nextInt() % 290)), this.fish3, this.oceanScene); else if (this.fish3.escaped()) return new OceanWorld(this.shark, this.fish1, this.fish2, new Fish(300, (new Random().nextInt() % 290)), this.oceanScene); else if (this.fish1.sharkFoundFish(this.shark)) return new OceanWorld(this.shark.eatFish(), new Fish(300, (new Random().nextInt() % 290)), this.fish2, this.fish3, this.oceanScene); else if (this.fish2.sharkFoundFish(this.shark)) return new OceanWorld(this.shark.eatFish(), this.fish1, new Fish(300, (new Random().nextInt() % 290)), this.fish3, this.oceanScene); else if (this.fish3.sharkFoundFish(this.shark)) return new OceanWorld(this.shark.eatFish(), this.fish1, this.fish2, new Fish(300, (new Random().nextInt() % 290)), this.oceanScene); else if (this.shark.size == 0) return this.endOfWorld("you lose"); else return new OceanWorld(this.shark.onTick(), this.fish1.onTick(), this.fish2.onTick(), this.fish3.onTick(), this.oceanScene); } World onKeyEvent(String input){ return new OceanWorld(this.shark.onKeyEvent(input), this.fish1, this.fish2, this.fish3, this.oceanScene); } boolean draw(){ return (this.theCanvas.show()) && (this.theCanvas.drawRect(new Posn(0, 0), 300, 200, new Blue())) && (this.fish1.DrawFish(this.theCanvas)) && (this.fish2.DrawFish(this.theCanvas)) && (this.fish3.DrawFish(this.theCanvas)) && (this.shark.drawShark(this.theCanvas)); } // start the world and the timer boolean go() { return this.bigBang(300,200,.05); } } class Example{ Example(){} Shark intShark = new Shark(100, 75); Fish fish1 = new Fish (300, 10); Fish fish2 = new Fish (300, 100); Fish fish3 = new Fish (300, 140); Canvas canvas = new Canvas(300, 200); OceanWorld ocean1 = new OceanWorld(this.intShark, this.fish1, this.fish2, this.fish3, this.canvas); /// SHARK /* boolean onkey = (check this.intShark.onKeyEvent("down") expect new Shark(105, 10)) && (check this.intShark.onKeyEvent("up") expect new Shark(95, 10)); boolean ontick = check this.intShark.onTick() expect new Shark(100, 9); boolean eatfish = check this.intShark.eatFish() expect new Shark(100, 14); /// FISH boolean ontick2 = check this.afish.onTick() expect new Fish(299, 50); boolean escape = check this.afish.escaped() expect false; */ /// OceanWorld boolean draw = this.ocean1.draw(); OceanWorld sfw = this.ocean1; boolean start = this.sfw.go(); }