import tester.*; import javalib.worldcanvas.WorldCanvas; import javalib.worldimages.*; import java.util.*; import java.awt.Color; // the basic data used by the ocean world game interface OceanWorldConstants{ // a random number generator public Random rand = new Random(); // the width of the world public int WIDTH = 400; // the height of the world public int HEIGHT = 400; // the color of the ocean public Color oceanColor = new Color(50, 150, 255); // the background image of the ocean public WorldImage oceanImage = new RectangleImage(new Posn(WIDTH/2, HEIGHT/2), WIDTH, HEIGHT, oceanColor); } //To represent a location (x,y) in graphics coordinates //-- adding methods to the library Posn class class CartPt extends Posn implements OceanWorldConstants{ // the standard constructor - invokes the one in the super class CartPt(int x, int y) { super(x, y); } /** TEMPLATE Fields: ... this.x ... -- int ... this.y ... -- int Methods: ... this.moveLeft(int) ... -- CartPt ... this.moveLeftRandom(int) ... -- CartPt ... this.distTo(CartPt) ... -- double */ // Move this CartPt left i units -- or back to the right, if out of bounds CartPt moveLeft(int i) { if (this.x - i < 0) return new CartPt(WIDTH, this.y); else return new CartPt(this.x-i, this.y); } // Move this CartPt left i units -- or back to the right, if out of bounds // also move randomly up or down -2 to 2 pixel CartPt moveLeftRandom(int i) { if (this.x - i < 0) return new CartPt(WIDTH, this.y); else // our tests did not catch that rand.nextInt(2) did not provide all desired values return new CartPt(this.x-i, this.y + rand.nextInt(5) - 2); } // Compute the distance from this point to the given one double distTo(CartPt that){ return Math.sqrt((this.x - that.x) * (this.x - that.x) + (this.y - that.y) * (this.y - that.y)); } } // A Shark swims in the ocean, looking for fish to eat class Shark implements OceanWorldConstants{ int y; // the horizontal coordinate int health; // the hunger level, when zero, the shark dies // the standard complete constructor Shark(int y, int health) { if (!this.outOfBounds(y)){ this.y = y; this.health = health; } } /** TEMPLATE Fields: ... this.y ... -- int ... this.health ... -- int Methods: ... this.move(int) ... -- CartPt ... this.sharkImage() ... -- WorldImage */ // move the shark up or down, on up-down key press Shark move(String ke){ if (ke.equals("up")) return new Shark(this.y - 3, this.health); else if (ke.equals("down")) return new Shark(this.y + 3, this.health); else return this; } // produce the image of this shark at its position WorldImage sharkImage(){ return new FromFileImage(new Posn(20, this.y), "small-shark.png"); } // throw an exception if the y-coordinate is out of bounds boolean outOfBounds(int y){ if (y < 0 || y > HEIGHT) throw new RuntimeException("Shark is out of bounds"); else return false; } } // Examples for the ocean game public class ExamplesSharkOutOfBounds implements OceanWorldConstants{ ExamplesSharkOutOfBounds() {} public static ExamplesSharkOutOfBounds examplesInstance = new ExamplesSharkOutOfBounds(); CartPt p1 = new CartPt(WIDTH, HEIGHT/2); CartPt pEnd = new CartPt(3, HEIGHT/2); CartPt pStart = new CartPt(WIDTH, HEIGHT/2); Shark s = new Shark(HEIGHT/2, 200); // test all methods in the class Shark public void testSharkMethods(Tester t){ t.checkExpect(this.s.move("up"), new Shark(HEIGHT/2 - 3, 200)); t.checkExpect(this.s.move("down"), new Shark(HEIGHT/2 + 3, 200)); t.checkExpect(this.s.move("left"), new Shark(HEIGHT/2, 200)); } // test the drawing of the ocean world image public void testDrawing(Tester t){ WorldCanvas canvas = new WorldCanvas(WIDTH, HEIGHT); canvas.drawImage(this.s.sharkImage()); canvas.show(); } // test the helper method outOfBounds public void testOutOfBounds(Tester t){ t.checkExpect(this.s.outOfBounds(50), false); t.checkException( new RuntimeException("Shark is out of bounds"), this.s, "outOfBounds", 420); t.checkException( new RuntimeException("Shark is out of bounds"), this.s, "outOfBounds", -5); t.checkConstructorException( new RuntimeException("Shark is out of bounds"), "Shark", 420, 20); t.checkConstructorException( new RuntimeException("Shark is out of bounds"), "Shark", -5, 20); } // run all tests, then run the game public static void main(String[] argv){ ExamplesSharkOutOfBounds e = new ExamplesSharkOutOfBounds(); Tester.runReport(e, false, false); } }