Question: We need help with the SmartyPants subclass(please include comments for me to understand) . We are having some difficulty working on this. We appreciate all

We need help with the SmartyPants subclass(please include comments for me to understand) . We are having some difficulty working on this. We appreciate all the help we can get !

Also, the home subclass, PeachGrove, and PeachHunter does not compile(we believe the main problem is the Home subclass because there are some methods that aren't defined). All other subclasses compile. Also, please include some comments in the Home subclass and the Helper subclass. We were helped by another person with those subclasses but he/she didn't include any comments so we it find hard to understand what they did. We have all the subclasses except the Smartypants sublcass. I have put together all the code that our team has built so far.

You will (partially) build a game about peaches. The game has players moving about in a world trying to find, eat and store peaches.

Each member in your group will have to implement two classes (a Location subclass and a Playersubclass). Your team is free to add more to the game if you wish but that will not be necessary.

Locations

The world that the game takes place in consists of different locations. A location can have zero or more players in it, zero or more peaches in it, and it may have some behaviour of its own.

Each member will implement a location subclass.

1) A Home is the home location. There is only one home in each world. This is where several of the players will begin the game. Is is a place for players to drop off (to stockpile) peaches. The home keeps track of how many peaches each player brings back.

The home will also create a new Helper player to go help another player in distress (low health). It will give the helper the location of the player in distress and some peaches to give to the distressed player to eat.

2) A PeachGrove has a bunch of peach trees, zero or more peaches (from the trees) and perhaps some bees (that may sting a player). When a player enters a peach grove for the n-th time then n bees might sting that person each with a 50% probability. Each sting will result in 5 health points being lost to the player. If the player doesn't die from bee stings then they can eat and take away as many peaches as they are able (if there are that many available). A grove does not grow more peaches as the game progresses.

3) A PeachPit is a location with a large hole. When a player enters (falls into) the pit, they lose 1/2 of their health points. (Just use integer division here.)

A peach pit also remembers who has already fallen into the pit. When a player falls in for the second (or more) time, they are instantly transported to the Home location, lose 1/2 their health points and their turn ends.

There may initially be several peaches in the pit. A player that falls in for the first time may take them (to eat or carry).

4) A BearsDen has a bear in it. If a player offers a bear two or more peaches then the bear will leave the

player alone (do nothing). If the player does not give two or more peaches to the bear then the bear will attack the player resulting in 25 health points being deducted. The bear will remember each player that gave it peaches and will not take peaches again or attack that player again.

Players

Each group member will implement a player subclass.

1) A PeachHunter searches for peaches to bring back to the home location. A peach hunter can carry lots of peaches (as many as 100). The peach hunter should return to home to deposit its peaches once they have 50 or more peaches. A peach hunter will remember where peach groves are when they find them and go back to them to get more peaches until the grove runs out. If a hunter's health is less than 50 then it can only carry 25 peaches. It must then put any excess peaches in the current location if this happens.

2) A PitFinder looks for peach pits and remembers where they are (locations). Each time a piut finder finds a new pit it will go back home and report this to the home (when the player enters home). When a pit finder and peach hunter interact, a finder will reveal the location of a pit to the finder in exchange of 5 peaches. A finder with health less then 30 can only carry 10 peaches. If their health drops down to this (if they fall in a pit or attacked by a bear for example) they must drop any excess peaches in the current location.

3) A SmartyPants just wanders around the world trying to sell advice to others. A smarty pants is given knowledge of all peach pits, bear dens and peach groves in the world. When it interacts with peach hunters they will reveal a grove's location for 7 peaches; when they interact with pit finders they will reveal a pit or bears den for 6 peaches; and when they interact with a thief they will steal all the theirs peaches (instead of the thief stealing theirs). There is only one smarty pants in the world. If a hunter or finder do not have enought peaches then no transaction is made.

A smarty pants is fair to peach hunters and pit finders. They will not reveal the same location twice to the same player (unless they already revealed all of the locations).

4) A Helper is created and dispatched from the home location whenever a player (pit finder or peach hunter) asks for help. They bring a bunch of peaches to the player (giving them to the player when they interact with them). Once a helper helps someone they go back home (and do nothing for the rest of the game). A helper will not give any peaches to a bear.

5) A PeachThief that will try to steal peaches from other players, as well as pick up peaches it finds along the way. When it interacts with a hunter or pit finder, it will repeatedly steal peaches (with probability 75%) until it fails to steal one (or the other player runs out of peaches). A thief will immediately eat the first peach successfully stolen in any interaction.

At any time (during the play method) a player can eat a peach they have. This will rejuvenate them by adding

some health equal to the ripeness of the peach. So if a peach has ripeness 10 then eating it will give the player 10 health points. However, if the peach is bad, then it will deduct the value of the ripeness from their health instead.

If a player's health ever drops below 10 they can call home for help. The Home loction will dispatch aHelper player to send some peaches to eat.

Peaches Adventure

The provided PeachesGame program is a simple starting point for the game. Essentially, it lets each player in the world have their turn (play method) and repeats until the home location has accumulated enuogh peaches.

Additions

You are free to make additions to the "game". For example, you can add a human player if you wish.

Project

The project aspect of this work will involve presenting demos of the code. Each team should come prepared with several "programs" that each demonstrate one small aspect of the game.

For example, one demo might just have a small world with a single player to illustarte that the player behaves as designed. You might start with a peach hunter and a peach grove and demo how it finds the grove, takes peaches back to home and then goes back to the grove. You might then create a smarty pants player exactly where the hunter is to show that they interact woth each other properly. You have the freedom to construct and manipulate the state of the game to illustrate the desired proprty. Each demo should illustarte one or two behavours (bit not much more).

Hints

Look at the specification and incrementally add behaviour to your classes. You do not need to get everything working at once. Build up your classes one behaviour at a time.

Read carefully:

// Location classes

Location.java import java.util.List;

public class Location{ protected Position position; protected String description = "Nothing special about this location."; protected List peopleAtLocation = null; protected List peachesAtLocation = null; public Location(Position p, String description, List people, List peaches){ this.position = p; this.description = description; this.peopleAtLocation = people; this.peachesAtLocation = peaches; } /** getter for position */ public Position getPosition(){ return position; } /** getter for description */ public String getDescription(){ return description; } /** getter for players */ public List getPlayers(){ return peopleAtLocation; } /** getter for a Peach */ public Peach getPeach(){return peachesAtLocation.remove(0); } /** check number pf peaches in location */ public int numberOfPeaches(){ return peachesAtLocation == null ? 0 : peachesAtLocation.size(); } /** adding a peach to the location */ public void addPeach(Peach p){ peachesAtLocation.add(p); } /** allows the location to do something to a player when entering the location */ public void enter(Player p){ p.setLocation(this); peopleAtLocation.add(p); System.out.println(p.getName() + " just entered location " + position); } /** remove a player from a room */ public void exit(Player p){ peopleAtLocation.remove(p); System.out.println(p.getName() + " just left location " + position); } /* ONLY for Home subclass */ public void callForHelp(Player p, Location location){ } @Override public String toString(){ return description + position.toString(); } } ------------------------------------------------------------------------------------------------------- Home.java import java.util.*;

public class Home extends Location {

protected Map> peaches;

public Home(World w, Position p, String description, List people, List peaches){ super(w, p, description, people, peaches); this.position = new Position(0,0); // ensures there will never be more than 1 home created this.description = "A Home is the home location. There is only one home in each world. " + "This is where several of the players will begin the game. " + "It is a place for players to drop off (to stockpile) peaches. The home keeps track of how " + "many peaches each player brings back. " + "The home will also create a new Helper player to go help another player in distress (low health). " + "It will give the helper the location of the player in distress and some peaches to give to the " + "distressed player to eat. "; this.peaches = new HashMap>(); }

public void callForHelp(Player p, Location l){ Helper helper = new Helper(this.world,null, this, new ArrayList(), 1000000, null); for (int i = 0; i < 10; i++) { helper.peaches.add(new Peach(10, false)); } helper.setLocation(l); helper.interact(p); helper.setLocation(this); }

public static void main(String[] args) { World w = new World(); Player p = new Player(w, "cat", w.home, new ArrayList(), 50, RGB.YELLOW); Player q = new Player(w, "dog", w.getLocations().get(1), new ArrayList(), 9, RGB.BLUE); w.addPlayer(p).addPlayer(q);

System.out.println(w.getHome().description); System.out.println(w.getHome().position); System.out.println(w.getHome().peachesAtLocation); System.out.println(w.getHome().peopleAtLocation);

p.peaches.add(new Peach(10, false)); p.peaches.add(new Peach(9, false)); p.peaches.add(new Peach(1, true)); System.out.println(Arrays.toString(p.peaches.toArray()));

p.storeAtHome(2);

System.out.println(w.home.peaches.toString()); System.out.println(p.peaches.toString()); q.play(); System.out.println(q.peaches.toString()); System.out.println(w.getHome().peopleAtLocation); } } ------------------------------------------------------------------------------------------------------- PeachGrove.java import java.util.List; import java.util.HashMap; import java.util.Map; import java.util.Iterator;

// This class creates the Peach Grove Location that contains peaches, as well as a bee. // It also contains who can get the peaches and determines who can trade their health with a peach. public class PeachGrove extends Location{ public static final int START_STING = 3; public static final int HEALTH_POINTS = 5; public static final int PLAYER_DEAD = -1; public static final int PLAYER_STUNG = 0; public static final int NO_STING = 1; public static final double STING_LOST_HEALTH_POINT = 0.5 * HEALTH_POINTS; private List peaches; // This class contains a list of peaches private Map swarmLookup; // This maps the player to how many times they enter this Peach grove. public PeachGrove(Position p, String description, List players, List peaches){ super(p, description, players, peaches); this.peaches = peaches; this.swarmLookup = new HashMap(); } /** * This method determines if the player has entered the peach grove at the first time or not. * If he/she is not the first time to enter he/she will lose health points. Otherwise they don't lose any health points. */ public int beeSting(Player p){ if (isFirstEnter(p) == true){ // first time swarmLookup.put(p, new Integer(1)); return NO_STING; } else{ // not first time Integer intg = swarmLookup.get(p); int lost_health = (int)(intg.intValue() * STING_LOST_HEALTH_POINT); if (p.getHealth() - lost_health > 0){ p.setHealth(p.getHealth() - lost_health); return PLAYER_STUNG; } else{ // dead swarmLookup.remove(p); return PLAYER_DEAD; } } } /** * This method allows the player p to take peaches from the peach grove. */ public int takePeach(Player p, int num){ int count = 0; if (peaches.size() >= num){ count = num; } else { count = peaches.size(); } int total = 0; // total number of peaches can be given Iterator it = peaches.iterator(); while(it.hasNext() && total <= count){ Peach ps= it.next(); total ++; // If the player is a PeachHunter, you can get the list of peaches the peach hunter has. // Then you add the peaches to the peach hunter's list of peaches if (p instanceof PeachHunter) { List peaches = ((PeachHunter) p).getCollectedPeaches(); peaches.add(ps); } } return total; } /** * The method checks if the specified player enters the peach grove first. */ private boolean isFirstEnter(Player p){ if(swarmLookup.isEmpty()){ return true; } else{ Integer swarm = swarmLookup.get(p); if (swarm != null){ return false; } else { return true; } } } } ------------------------------------------------------------------------------------------------------- PeachPit.java import java.util.List; public class PeachPit extends Location{ public PeachPit(Position p, List people, List peaches){ super(p,"PeachPit",people,peaches); } @Override public void enter(Player p) { p.setHealth(p.getHealth()/2); peopleAtLocation.add(p); System.out.println(p.getName() + " just entered peach pit " + position);

}

} ------------------------------------------------------------------------------------------------------- BearsDen.java import java.util.List; import java.util.HashMap; import java.util.Map; public class BearsDen extends Location{ private Map> lookup; public static final int PLAYER_DEAD = -1; public static final int COMPLETE_ATTACK = 1; public static final int NO_ATTACK = 0; public BearsDen(Position p, String description, List players, List peaches){ super(p, description, players, peaches); lookup = new HashMap>(); } /** * This method checks the lookup map to see if the player needs to be attacked by the bear or not. */ private boolean isAttack(Player p){ if ( lookup.isEmpty() ) { return true; } else { List ps = lookup.get(p); if (ps != null){ return false; } else { return true; } } } public int attack(Player p ){ if(isAttack(p) == true){ if(p.getHealth() <= 25){ p.setHealth(0); return PLAYER_DEAD; } else{ int health = p.getHealth(); p.setHealth (health - 25); return COMPLETE_ATTACK; } } else{ return NO_ATTACK; } } /** * This method adds the player and peaches into the lookup table. Whenever the player has entry * in the lookup table, the bear won't attack the player. Otherwise it will. */ public void payPeaches(Player p, List ps) { if (lookup.containsKey(p) == false) { lookup.put(p, ps); } } }

-------------------------------------------------------------------------------------------------------

//PlayerClasses

PeacHunter.java import java.util.List; import java.util.ArrayList; import java.util.Iterator;

public class PeachHunter extends Player { private List collectedPeaches; private List peachGroves; public static String LOC_DES_PEACH_GROVE = "Peach Grove"; public PeachHunter(World w, String name, Location location, List peaches, int health, RGB rgb){ super(w, name, location, peaches, health, rgb); this.world = w; this.name = name; this.collectedPeaches = peaches; this.health = health; this.location = location; this.colour = rgb; // To remember all the peach groves Location [][] locs = w.getWorld(); int rows = locs.length; int cols = locs[0].length; peachGroves = new ArrayList(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (locs[i][j].getDescription().equalsIgnoreCase(LOC_DES_PEACH_GROVE)) { //if (locs[i][j] instanceof PeachGrove){ peachGroves.add(locs[i][j]); } } } } public List getCollectedPeaches(){ return collectedPeaches; } /** * This method lets the Peach Hunter collect the peaches from the Peach Groves. */ public int collect(Player p){ if(peachGroves.size() == 0) { return 0; } int totalPeaches = 0; Iterator it = peachGroves.iterator(); PeachGrove peachG = null; boolean continues = true; while (it.hasNext() && continues) { if ( it.next() instanceof PeachGrove ) { peachG = (PeachGrove) it.next(); // When hunter enters the Peach Grove, his/her health may decline. // This will check to see if the player dies or not. if (peachG.beeSting(this) != PeachGrove.PLAYER_DEAD) { // if health is less than 50, the player can only take no more than 25 peaches if (health < 50 ) { totalPeaches += peachG.takePeach(this, 25); } // Otherwise, the player can carry up to 50 peaches. else { totalPeaches += peachG.takePeach(this, 50); } } else { // If the player is dead continues = false; } } // end of it.next() instanceof PeachGrove // If the player collects more than 100 peaches, send the player back home. if (this.collectedPeaches.size() > 100) { // This sends the player back home. Location home = world.getHome(); this.location = home; // The player enters home home.enter(p); // This deposits all the peaches from the player's hand for (int k = 0; k < collectedPeaches.size(); k++) { home.addPeach(collectedPeaches.remove(k)); } continues = false; } } // end of while return totalPeaches; } } ------------------------------------------------------------------------------------------------------- PitFinder.java

import java.lang.Math; import java.util.ArrayList; import java.util.List;

public class PitFinder extends Player { private String status; private Position pitPosition = null;

public PitFinder(World w, Location location, RGB rgb) { super(w, "PitFinder", location, new ArrayList(), 100, rgb); this.status = "EXPLORE"; }

@Override public void play() { System.out.println(this.status); if(this.status.equals("DONE")) return;

//move to a new location int direction = this.status.equals("EXPLORE") ? this.decideDirection() : this.homeDirection(); this.move(direction);

//take optional injury from new location

//what is this location about? this.knowNewLocation();

//interact with other players in new location Player peachHunter = this.findPeachHunter(); if(peachHunter != null) { //the PeachHunter should call getPitPosition of PitFinder to get pit position peachHunter.interact(this); }

//deal with peach if(this.health < 30 ) { int size = this.peaches.size(); for(int i = size - 1;i > 9;i--) { this.location.addPeach(this.peaches.remove(0)); } } //should i call Helper?

}

private int decideDirection() { int index_X = getLocation().getPosition().getX(); int index_Y = getLocation().getPosition().getY(); double random = Math.random(); int result = 0; if (this.status == "EXPLORE") { if (index_X == 0) { if (index_Y == 0) { result = random * 2 > 1 ? Directions.DOWN : Directions.RIGHT; } else if (index_Y == 1) { // san deng fen result = random * 3 > 1 ? (random * 3 > 2 ? Directions.DOWN : Directions.LEFT) : Directions.RIGHT;

} else { result = random * 2 > 1 ? Directions.DOWN : Directions.LEFT; }

} else if (index_X == 1) { if (index_Y == 0) { result = random * 3 > 1 ? (random * 3 > 2 ? Directions.DOWN : Directions.UP) : Directions.RIGHT; } else if (index_Y == 1) { double seed = random * 4; result = seed > 2 ? (seed > 1 ? Directions.UP : Directions.DOWN) : (seed > 3 ? Directions.LEFT : Directions.RIGHT); } else { result = random * 3 > 1 ? (random * 3 > 2 ? Directions.DOWN : Directions.LEFT) : Directions.UP; }

} else { if (index_Y == 0) { result = random * 2 > 1 ? Directions.UP : Directions.RIGHT; } else if (index_Y == 1) { result = random * 3 > 1 ? (random * 3 > 2 ? Directions.UP : Directions.LEFT) : Directions.RIGHT; } else { result = random * 2 > 1 ? Directions.DOWN : Directions.LEFT; } } } return result; }

private int homeDirection() { int index_X = getLocation().getPosition().getX(); int index_Y = getLocation().getPosition().getY(); int result = 0; if(index_X > 0) { result = Directions.UP; } else if (index_Y > 0) { result = Directions.LEFT; } return result; }

private void knowNewLocation() { //if this is a PeachPit if(this.location.getDescription().equals("Pit")) { this.status = "GO_HOME"; this.pitPosition = this.location.getPosition(); } else if(this.location.getDescription().equals("Home")) { if(this.pitPosition != null) this.status = "DONE"; } }

private Player findPeachHunter() { List players = location.getPlayers(); Player p = null; int size = players.size(); for(int i = 0; i

@Override public void interact(Player p) { //this function is call by counter part String name = p.getName(); switch(name) { case "PitHunter": int count = 5; while(count > 0) { peaches.add(p.getPeach()); count--; } break; }

} public Position getPitPosition() { return pitPosition; }

public static void main(String[] args) { World w = new World(); PitFinder p = new PitFinder(w, w.home, RGB.BLUE); for(int i = 0;i<100;i++) { System.out.println(p.decideDirection()); }

}

}

------------------------------------------------------------------------------------------------------- SmartyPants.java // We have not created it ------------------------------------------------------------------------------------------------------- Helper.java import java.util.List;

public class Helper extends Player {

public Helper(World w, String name, Location location, List peaches, int health, RGB rgb){ super(w, name, location, peaches, health, rgb); this.location = w.home; }

public void interact(Player p){ for (int i = 0; i < this.peaches.size(); i++) { p.peaches.add(this.getPeach()); } } } ------------------------------------------------------------------------------------------------------- PeachThief.java import java.util.List;

public class PeachThief extends Player{ protected int FirstPeachEatten=0;

protected PeachThief(World w, String name, Location location, List peaches, int health, RGB rgb){ super(w,name,location,peaches,health,rgb); }

public void play() { super.play(); if(location.peopleAtLocation.size()!=0){ for(int i=0; i (double) 75 / 100) { peaches.add(location.peopleAtLocation.get(i).peaches.get(0)); location.peopleAtLocation.get(i).peaches.remove(0); if(FirstPeachEatten==0){ health=health+this.peaches.get(0).ripeness; FirstPeachEatten++; } }else{ break; } } } } } }

}

------------------------------------------------------------------------------------------------------- Directions.java public class Directions{ public static final int UP = 0; public static final int DOWN = 1; public static final int LEFT = 2; public static final int RIGHT = 3; } ------------------------------------------------------------------------------------------------------- EmptyLocation.java import java.util.ArrayList;

public class EmptyLocation extends Location{ public EmptyLocation(Position p, String description){ super(p, description, new ArrayList(), new ArrayList()); } /** getter for a Peach */ public Peach getPeach(){ return null; } /** remove a player from a room */ public void remove(Player p){ peopleAtLocation.remove(p); } } ------------------------------------------------------------------------------------------------------- Player.java import java.util.List;

/** A Player in the game * * Each member of your team should implement their own * unique Player subtype. Your group should also have a human player. */

public class Player{ protected World world; // world that player lives in protected String name; // name of player protected Location location; // where in the world the player is protected List peaches; // peaches protected int health; // health of player protected RGB colour; // colour of player (if graphics is used) /** Creates a player in the game * * @param world is the world that the player lives in * @param name is the name of the player * @param location is where in the world the player is * @param peaches is a list of peaches the player starts with * @param health is the health of the player (which may or may not be relevant in your game) * @param RGB is the colour of the player */ public Player(World w, String name, Location location, List peaches, int health, RGB rgb){ this.world = w; this.name = name; this.location = location; location.getPlayers().add(this); this.peaches = peaches; this.health = health; this.colour = rgb; } /** Getter for a player's world */ public World getWorld(){ return world; }

/** Getter for a player's name */ public String getName(){ return name; } /** Getter for a player's location */ public Location getLocation(){ return location; } /** Getter for a player's peach */ public Peach getPeach(){ return peaches == null ? null : peaches.remove(0); } /** Getter for a player's health */ public int getHealth(){ return health; } /** This is the logic of the player. * It defines what they should do when given a chance to do somerthing */ public void play(){ if( health < 10 ){ getHelp(); return; } } /** Moves a player from one location to a new location * * @param newLocation is the new location that the player will be moved to * @return true if the move was successful and false otherwise (e.g. when trying to move from one * location to another that are not connected) */ public boolean move(int direction){ // move from current location to new location (if possible) world.move(this, direction); return false; } /** sets a player's current location * * @param location is the Location to set for the player */ public void setLocation(Location location){ this.location = location; }

/** Setter for a player's health * * @param h is the new health of the player */ public void setHealth(int h){ this.health = h; } /** Allows for interaction with this player and another player * (presumably called from within the play method) * * @param p is a player that is interacting with this player */ public void interact(Player p){ // allows for some interaction with a player } /** ask for help when they need it */ public void getHelp(){ world.getHome().callForHelp(this, location); } @Override public String toString(){ return name; } /** Two players are the same if they have the same name, location and health. */ @Override public boolean equals(Object o){ if( o instanceof Player){ return this.name.equals( ((Player)o).name ) && this.location.equals( ((Player)o).location ) && this.health == ((Player)o).health; }else{ return false; } }

} ------------------------------------------------------------------------------------------------------- Peach.java public class Peach implements Comparable{ protected int ripeness; protected boolean bad;

public Peach(int ripeness, boolean bad){ this.ripeness = ripeness; this.bad = bad; } public Peach(int ripeness){ this(ripeness, false); } public int getRipeness(){ return ripeness; } /** ages a peach in some way */ public void age(){} @Override public int compareTo(Peach other){ return 0; } } -------------------------------------------------------------------------------------------------------

Position.java public class Position{ protected int x; protected int y; public Position(int x, int y){ this.x = x; this.y = y; } public int getX(){ return x; } public int getY(){ return y; } @Override public String toString(){ return "["+x+","+y+"]"; } } ------------------------------------------------------------------------------------------------------- RGB.java public class RGB{ public static RGB RED = new RGB(255,0,0); public static RGB GREEN = new RGB(0,255,0); public static RGB BLUE = new RGB(0,0,255); public static RGB YELLOW = new RGB(255,255,0); public static RGB MAGENTA = new RGB(255,0,255); public static RGB CYAN = new RGB(0,255,255); protected int red; protected int green; protected int blue; public RGB(int red, int green, int blue){ this.red = red; this.green = green; this.blue = blue; } public RGB(int gray){ this(gray,gray,gray); } } ------------------------------------------------------------------------------------------------------- World.java import java.util.List; import java.util.ArrayList; import java.util.Arrays;

public class World{ protected Location[][] locations; protected Location home; protected List players; // all players in the world public World(){ locations = new Location[3][3]; for(int r=0; r<3; r+=1){ for(int c=0; c<3; c+=1){ locations[r][c] = new EmptyLocation(new Position(r,c), "Nothing here to see."); } } home = locations[0][0]; players = new ArrayList(); } public List getPlayers(){ return players; } public Location[][] getWorld(){ return locations; } public Location getHome(){ return home; } public List getLocations(){ List list = new ArrayList(locations.length*locations[0].length); for (Location[] array : locations) { list.addAll(Arrays.asList(array)); } return list; } /* keep a list of all players in the world. Each time a helper is created be * sure to add them to this list */ public World addPlayer(Player p){ players.add(p); return this; } public boolean move(Player p, int direction){ Location loc = p.getLocation(); // player's current location int x = loc.getPosition().getX(); int y = loc.getPosition().getY(); Location newLocation = null; // switch(direction){ case Directions.UP: newLocation = locations[x-1][y]; break; case Directions.DOWN: newLocation = locations[x+1][y]; break; case Directions.LEFT: newLocation = locations[x][y-1]; break; case Directions.RIGHT: newLocation = locations[x][y+1]; break; default: break; } loc.exit(p); newLocation.enter(p); return true; } }

//PeachesAdventureClass PeachesGame.java import java.util.ArrayList;

public class PeachesGame{ public static void main(String[] args){ World w = new World(); Player p = new Player(w, "cat", w.home, new ArrayList(), 50, RGB.YELLOW); Player q = new Player(w, "dog", w.home, new ArrayList(), 100, RGB.BLUE); w.addPlayer(p).addPlayer(q); System.out.println("Home : " + w.getHome()); System.out.println(" Players at Home : " + w.getHome().getPlayers()); System.out.println("Location of all players in world"); for(Player pp: w.getPlayers()){ System.out.println(pp.getLocation()); System.out.println(pp.getLocation().getPlayers()); } System.out.println("Move some players in world"); p.move(Directions.DOWN); q.move(Directions.RIGHT); System.out.println("Location of all players in world"); for(Player pp: w.getPlayers()){ System.out.println(pp.getLocation()); System.out.println(pp.getLocation().getPlayers()); } // what the game might look like... while( w.getHome().numberOfPeaches() <= 100 ){ // iterate over all locations in the world for(Location location: w.getLocations()){ // iterate over all players in the current location for(Player player: location.getPlayers() ){ System.out.println(player.getName() + ", " + player.getLocation()); player.play(); } } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!