Question: Need help with Home subclass, how could i change method exist in super class. Player class import java.util.List; /** A Player in the game *

Need help with Home subclass, how could i change method exist in super class.

 Need help with Home subclass, how could i change method exist

Player class

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 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; } } }

World class

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; rfor(int c=0; clocations[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; } }

Empty location class

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); } }

Location class

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 class that i am working on

import java.util.List; public class Home extends Location { protected Home home; protected int peachesnum; // protected World world=new World();  // private List peachesofhelper;  // private RGB rgbofhelper=RGB.RED;  //protected final Player Helper=new Player(world,"helper",world.getHome(),peachesofhelper,100,rgbofhelper);  public Home(Position p,String description, Listpeople,Listpeaches){ super(p,description,people,peaches); } @Override public String getDescription() { return description; } @Override public void callForHelp(Player p, Location location) { super.callForHelp(p, location); /*  int healthneedtobehealed =10-p.getHealth();  for(int i=0;i peachesofhelper.add(p.getPeach());  }  p.setHealth(10);  peachesofhelper.clear(); */  } }

And i am trying to write a subclass of location class that create a home location and keep count number of players ,number of peaches they bring back, and a helper player that could help players when their health are really low.

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. 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

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!