import tester.*; /** * HtDC Lectures * Lecture 6: Methods for self-referential unions of classes * * Copyright 2013 Viera K. Proulx * This program is distributed under the terms of the * GNU Lesser General Public License (LGPL) * * @since 13 September 2013 */ /* +-------------------------------------------------+ | +---------------------------------------------+ | | | | | v v | | +------------------+ | | | IRoom | | | +------------------+ | | | int countPits() | | | | int mostTokens() | | | +------------------+ | | / \ | | | | | - - - - - - - - - - - - - - - - - - - - - - - - - - - | | | | | | | +------------------+ +------------------+ +------------------------+ | | | Pit | | Gold | | Token | | | +------------------+ +------------------+ +------------------------+ | | | int countPits() | | int factor | | int num | | | | int mostTokens() | +------------------+ | IRoom left |-+ | +------------------+ | int countPits() | | IRoom right |---+ | int mostTokens() | +------------------------+ +------------------+ | int countPits() | | int mostTokens() | | int maxValue(int, int) | +------------------------+ */ // to represent a room in a maze game interface IRoom { // count the number of pits in the maze starting at this room int countPits(); // find the most number of tokens a player can collect starting in this room int mostTokens(); } // to represent a pit room in a maze game class Pit implements IRoom { Pit() { } /*TEMPLATE Methods: ... this.countPits() ... -- int ... this.mostTokens() ... -- int */ // count the number of pits in the maze starting at this pit room public int countPits(){ return 1; } // find the most number of tokens a player can collect starting in this room public int mostTokens(){ return 0; } } // to represent a gold room in a maze game class Gold implements IRoom { int factor; Gold(int factor) { this.factor = factor; } /*TEMPLATE Fields: ... this.factor ... -- int Methods: ... this.countPits() ... -- int ... this.mostTokens() ... -- int */ // count the number of pits in the maze starting at this gold room public int countPits(){ return 0; } // find the most number of tokens a player can collect starting in this room public int mostTokens(){ return 0; } } // to represent a token room in a maze game class Token implements IRoom { int num; IRoom left; IRoom right; Token(int num, IRoom left, IRoom right) { this.num = num; this.left = left; this.right = right; } /*TEMPLATE Fields: ... this.num ... -- int ... this.left.mmm() ... -- IRoom ... this.right.mmm() ... -- IRoom Methods: ... this.countPits() ... -- int ... this.mostTokens() ... -- int Methods for Fields: ... this.left.countPits() -- int ... this.right.countPits() -- int ... this.left.mostTokens() -- int ... this.right.mostTokens() -- int */ // count the number of pits in the maze starting at this token room public int countPits(){ return this.left.countPits() + this.right.countPits(); } // find the most number of tokens a player can collect starting in this room public int mostTokens(){ return this.num + this.maxValue(this.left.mostTokens(), this.right.mostTokens()); } // find the maximum of the two given numbers int maxValue(int a, int b){ if (a > b) return a; else return b; } } // to represent examples and tests for a maze game class ExamplesMaze { ExamplesMaze () {} // sample maze rooms: IRoom pit = new Pit(); IRoom gold = new Gold(2); IRoom token3 = new Token(3, this.pit, this.gold); IRoom start = new Token(5, this.pit, this.token3); // test the method countPits for the classes that represent a maze boolean testCountPits(Tester t) { return t.checkExpect(this.pit.countPits(), 1) && t.checkExpect(this.gold.countPits(), 0) && t.checkExpect(this.token3.countPits(), 1) && t.checkExpect(this.start.countPits(), 2); } boolean testMostTokens(Tester t) { return t.checkExpect(this.pit.mostTokens(), 0) && t.checkExpect(this.gold.mostTokens(), 0) && t.checkExpect(this.token3.mostTokens(), 3) && t.checkExpect(this.start.mostTokens(), 8); } }