/* maze.java Tuesday pm lecture - Part 3: Methods for Unions and Self-Referential Data The following class diagram represents the data for a simple maze game that consists of three different kinds of rooms: Token rooms, Gold rooms, and Pits. The player starts the game in one room. In each token room the player add to her fortune the number of tokens found in the token room and chooses to go to the next room through either the left or the right door. When the player reaches the Pit, the game ends with player loosing everything. Each Gold room contains a key in the shape of a number. When the player reaches the Gold room each token changes into as many gold nuggets as the number on the key, and the game ends. The maze is design in such way that the player cannot visit the same room twice. +------------------+ | interface: | | Room |<----------------------------------------+ +------------------+ | | int countPits() | | | int mostTokens() | | +------------------+ | / \ | | | - - - - - - - - - - - - - - - - - - - - - - - - - - - | | | | | +------------------+ +------------------+ +------------------------+ | | Pit | | Gold | | Token | | +------------------+ +------------------+ +------------------------+ | | int countPits() | | int factor | | int num | | | int mostTokens() | +------------------+ | Room left |--+ +------------------+ | int countPits() | | Room right |--+ | int mostTokens() | +------------------------+ +------------------+ | int countPits() | | int mostTokens() | | int maxValue(int, int) | +------------------------+ Here is an example of such maze: +---------+ | start | | TOKEN 5 | +---------+ / \ / \ +-----+ +---------+ | PIT | | token3 | +-----+ | TOKEN 3 | +---------+ / \ / \ +-----+ +--------+ | PIT | | gold | +-----+ | GOLD 2 | +--------+ The player starts in the 'Start' room and adds 5 tokens to it's token collection. If he chooses to go left, he ends up in a Pit and the game ends with no winnings. If the chooses to go right, he goes to the TokenA roon and adds 3 tokens to the token collection. After that, going to the left again leads to a Pit, but going to the right ends the game with 16 gold nuggets - as each of the eight tokens in the player's possession turns into tow gold nuggets. */ // to represent a room in a maze game interface Room { // 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 Room { Pit() { } /* TEMPLATE ... this.countPits() ... -- int ... this.mostTokens() ... -- int */ // count the number of pits in the maze starting at this pit room int countPits(){ return 1; } // find the most number of tokens a player can collect starting in this room int mostTokens(){ return 0; } } // to represent a gold room in a maze game class Gold implements Room { int factor; Gold(int factor) { this.factor = factor; } /* TEMPLATE ... this.factor ... -- int ... this.countPits() ... -- int ... this.mostTokens() ... -- int */ // count the number of pits in the maze starting at this gold room int countPits(){ return 0; } // find the most number of tokens a player can collect starting in this room int mostTokens(){ return 0; } } // to represent a token room in a maze game class Token implements Room { int num; Room left; Room right; Token(int num, Room left, Room right) { this.num = num; this.left = left; this.right = right; } /* TEMPLATE ... this.num ... -- int ... this.left.mmm() ... -- Room ... this.right.mmm() ... -- Room ... 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 int countPits(){ return this.left.countPits() + this.right.countPits(); } // find the most number of tokens a player can collect starting in this room 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; } } } class Examples { Examples () {} Room pit = new Pit(); Room gold = new Gold(2); Room token3 = new Token(3, this.pit, this.gold); Room start = new Token(5, this.pit, this.token3); boolean testCountPits1 = check this.pit.countPits() expect 1; boolean testCountPits2 = check this.gold.countPits() expect 0; boolean testCountPits3 = check this.token3.countPits() expect 1; boolean testCountPits4 = check this.start.countPits() expect 2; boolean testMostTokens1 = check this.pit.mostTokens() expect 0; boolean testMostTokens2 = check this.gold.mostTokens() expect 0; boolean testMostTokens3 = check this.token3.mostTokens() expect 3; boolean testMostTokens4 = check this.start.mostTokens() expect 8; }