Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Catcher Player(player, jail, entity and base.java given) Create a class called C

ID: 3776037 • Letter: C

Question

Catcher Player(player, jail, entity and base.java given)
Create a class called Catcher that extends the Player class. The goal of your Catcher player is to try and catch opposing players on the eld.your player should not chase after players on its own team. When player A catches an opponent (the catchOpponent method lets you know when you catch an opponent), say player B, player A will take player B to A’s jail. After your player catches and brings an opponent to jail, it then looks for a dierent opponent to catch if there are any. If there is only one opponent it will try and catch the same opponent again. When the caught players gets to jail, you should teleport it back to its own base where it can be free to walk around again.

Player class;

public abstract class Player extends Entity{


/** this player's team name */
protected String team;

/** this player's name */
protected String name;

/** this player's number */
protected int number;

  
/** gets this player's team name
    *
    * @return the team name that this player is on
    */
public final String getTeam(){ return this.team; }


/** gets this player's name
    *
    * @return the name of this player
    */
public final String getName(){ return this.name; }

/** gets this player's number
    *
    * @return the number of this player
    */
public final int getNumber(){ return this.number; }


/** creates a player with specified symbol at specified position
    *
    * @param f is the field the player will be playing on
    * @param side is the side of the field the player will play on
    * @param name is this name of the player
    * @param number is this player's number
    * @param team is this player's team name
    * @param symbol is a character (char) representation of this player
    * @param x is the x-coordinate of this player
    * @param y is the y-coordinate of this player
    */
public Player(Field f, int side, String name, int number, String team, char symbol, double x, double y){
    super(symbol, x, y);
    this.name = name;
    this.number = number;
    this.team = team;
    f.registerPlayer(this, this.id, side); // register the player on the field
}

/** attempt to catch an opponent player
    *
    * @param opponent a player on the opponent's team that you are trying to catch
    * @param field is the field the game is being played on
    * @return true if this player successfully catches the opponent player, false otherwise
    */
public final boolean catchOpponent(Player opponent, Field field){
    return field.catchOpponent(this, opponent);
}


/** Informs this player that they have been caught by another player.
    * <p>
    * This method should only be called from within the Field class.
    *
    * @param opponent is the player that caught this player
    * @param id should be the id of the this player
    */
public void beenCaught(Player opponent, int id){
    /* check if the caller knows this entity's id */
    if( this.id != id ){
      throw new SecurityException("Unauthorized attempt to call beenCaught ");
    }
   
}
   
/** attempt to free a teammate from jail
    *
    * @param teammate is another player on this player's team
    * @param field is the field the game is being played on
    * @return true if the <code>teammate</code> is successfully freed from jail, false otherwise
    */
public final boolean freeTeammate(Player teammate, Field field){
    return field.freeTeammate(this, teammate);
}
   
/** Informs this player that they have been freed by a teammate
    * <p>
    * This method should only be called from within the Field class.
    *
    * @param teammate is the player that caught this player
    * @param id should be the id of the this player
    */
public void hasBeenFreed(Player teammate, int id){
    /* check if the caller knows this entity's id */
    if( this.id != id){
      throw new SecurityException("Unauthorized attempt to call hasBeenFreed ");
    }
   
}



/** attempt to pick up the opponent's flag
    *
    * @param field is the field the game is being played on
    * @return true if this player successfully picked up the opponent's flag, false otherwise
    */
public final boolean pickUpFlag(Field field){
    return field.pickUpFlag(this);
}


/** Informs this player that they have picked up the flag
    * <p>
    * This method should only be called from with the Field class.
    *
    * @param id should be the id of the this player
    */
public void hasPickedUpFlag(int id){
    /* check if the caller knows this entity's id */
    if( this.id != id ){
      throw new SecurityException("Unauthorized attempt to call hasPickedUpFlag ");
    }
   
}

/** Informs this player that they have dropped the flag
    * <p>
    * This method should only be called from within the Field class.
    *
    * @param id should be the id of the this player
    */
public void hasDroppedFlag(int id){
    /* check if the caller knows this entity's id */
    if( this.id != id ){
      throw new SecurityException("Unauthorized attempt to call hasDroppedFlag ");
    }
   
}


/** attempt to win the game
    *
    * @param field is the field the game is being played on
    * @return true if this player successfully brings the opponent's flag back to this player's base, false otherwise
    */
public final boolean winGame(Field field){
    return field.winGame(this);
}

  


}

jail.java

public class Jail extends Entity{

/* no logic for the jail yet */
@Override
public void play(Field field){}

/* jails does not move yet */
@Override
public void update(Field field){}


@Override
public boolean equals(Object o){
    if(this==o){return true;}

    if(o == null) return false;
   
    /* jails are the same if they have the same symbol */
    if(o instanceof Jail && this.getSymbol() == ((Jail)o).getSymbol()){
      return true;
    }
   
    return false;
}

public Jail(char symbol, int x, int y){
    super(symbol, x, y);
    speedX = speedY = 0;
}

public Jail(Field f, int side, char symbol, int x, int y, String ref){
    super(f, side, symbol, x, y, ref);
    speedX = speedY = 0;
}

}

Entity.java

/**
* Entity class to represent "things" in the game: players, flags, jails, bases, etc.
*/
public abstract class Entity{
/** time scaling factor. Will be more important when View is graphics based */
public static double TIME_SCALE = 50;    
  
/** character representation of this entity */
protected char symbol;

/** current x-coordinate of this entity */
protected double x;
/** current y-coordinate of this entity */
protected double y;

/** current speed in x-direction of this entity */
protected double speedX;
/** current speed in y-direction of this entity */
protected double speedY;


/** unique ID for entity */
protected int id;

protected static int ID = 0; // used to generate unique id's


/** maximum speed for a player for use in tournament */
public static final int MAX_SPEED = 10;


/** creates an entity (for players) with specified symbol at specified position
    *
    * @param symbol is a character (char) representation of the entity
    * @param x is the x-coordinate of the entity
    * @param y is the y-coordinate of the entity
    */
public Entity(char symbol, double x, double y){
    this.symbol = symbol;
    this.x = x;
    this.y = y;
    this.id = ID; // set the ID for the entity
    ID +=1;        // update the ID counter for the next entity created
}
  

/** constructor for non-player things
    *
    * @param symbol is a character (char) representation of the entity
    * @param x is the x-coordinate of the entity
    * @param y is the y-coordinate of the entity
    */
public Entity(Field f, int side, char symbol, double x, double y, String ref){
   this(symbol, x, y);
    f.registerThing(this, this.id, side); // register with the field
    this.sprite = SpriteStore.get().getSprite(ref);
}


/** gets this entity's symbol
    *
    * @return the symbol of this entity (char)
    */
public char getSymbol(){ return this.symbol; }

/** gets the current speed in the x-direction of this entity
    *
    * @return the current x-direction speed of this entity
    */
public double getSpeedX(){ return this.speedX; }

/** gets the current speed in the y-direction of this entity
    *
    * @return the current y-direction speed of this entity
    */
public double getSpeedY(){ return this.speedY; }

/** gets the current x-coordinate of this entity
    *
    * @return the current x-coordinate of this entity
    */
public int getX() { return (int) this.x; }

/** gets the current y-coordinate of this entity
    *
    * @return the current y-coordinate of this entity
    */
public int getY() { return (int) this.y; }

/** update the position of this entity using basic physics
    * <p>
    * In each direction, we have
    * new_position = current_position + speed * time_elapsed
    * <p>
    * Note: TIME_SCALE will need to be manually adjusted to a value that allows good game play
    *
    * @param time is a length of time
    * @param field is the current field
    * @param id is supposed to the entity's id value
    */
public final void updatePosition(long time, Field field, int id)
          throws SecurityException, EntityOutOfBoundsException
    {
    if( id != this.id ){
      throw new SecurityException("Unauthorized change of position");
    }

    /* need to add logic of when to actually throw this */
    if(false){
      throw new EntityOutOfBoundsException("out of bounds");
    }

    this.x += (time * this.speedX) / TIME_SCALE;
    this.y += (time * this.speedY) / TIME_SCALE;
    checkCoordinates(field);
}

/** checks that this player is within the field boundaries
    *
    * @param field is the field that this entity is on
    */
protected void checkCoordinates(Field field) throws EntityOutOfBoundsException{
    // check if this entity is out of the playing field
    // if they are throw the exception
    // if they are not, do nothing
}


/** logic for this entity (change direction/speed)
    * <p>
    * logic is based on current playing field (which holds information about
    * all entities on the field) and possibly state of this entity. Do NOT
    * update the entity's position here, just update speed.
    *
    * @param field is the current playing field
    */
public abstract void play(Field field);

/** update this entity for any changes to it
    * <p>
    * For example, if this entity is moved by another entity, this entity's
    * positions need to be updated.
    *
    * @param field is the current playing field
    */
public abstract void update(Field field);


/** override the equals method from Object
    * <p>
    * This needs to be implemented in a child class
    *
    * @param o is an object to be tested for equality with this
    * @return throws an exception
    */
@Override
public boolean equals(Object o){
   if(this == o){return true;}
   if(o==null){return false;}
   if( o instanceof Entity){
    return this.id == ((Entity)o).id;
   }
   return false;
}


/* Setter methods
   *
   * We don't want a player of one team to be able to change the
   * position or movement of a player on another team.
   * The caller needs to know the entity's id number.
   */

/** sets the x-coordinate of this entity
    *
    * @param x is the new x-coordinate
    * @param id should be the entity's id
    */
protected final void setX(double x, int id){
    /* check if the caller knows this entity's id */
    if( id != this.id ){
      throw new SecurityException("Unauthorized change of entity x coordinate");
    }
    this.x = x;
}

/** sets the y-coordinate of this entity
    *
    * @param y is the new y-coordinate
    * @param id should be the entity's id
    */
protected final void setY(double y, int id){
    /* check if the caller knows this entity's id */
      if( id != this.id ){
      throw new SecurityException("Unauthorized change of entity y coordinate");
    }
    this.y = y;
}

/** sets the speed in the x-direction of this entity
    *
    * @param speedX is the new x-direction speed
    * @param id should be the entity's id
    */
protected final void setSpeedX(double speedX, int id){
    /* check if the caller knows this entity's id */
      if( id != this.id ){
      throw new SecurityException("Unauthorized change of entity x-direction speed");
    }
    this.speedX = speedX;
}

/** sets the current speed in the y-direction of this entity
    *
    * @param speedY is the new y-direction speed
    * @param id should be the entity's id
    */
protected final void setSpeedY(double speedY, int id){
    /* check if the caller knows this entity's id */
    if( id != this.id ){
      throw new SecurityException("Unauthorized change of entity y-direction speed");
    }
    this.speedY = speedY;
}


//
// this is used for graphical representations
//

/** the sprite that will represent this entity */
protected Sprite sprite;

public Sprite getSprite(){ return this.sprite; }

public void setSprite(String ref, int id){
    /* check if the caller knows this entity's id */
    if( id != this.id ){
      throw new SecurityException("Unauthorized change of entity's sprite");
    }
    this.sprite = SpriteStore.get().getSprite(ref);
}



}

Base.java

public class Base extends Entity{

@Override
public void play(Field field){}

@Override
public void update(Field field){}

@Override
public boolean equals(Object o){
    if(o == null) return false;
    if(o instanceof Base && this.getSymbol() == ((Base)o).getSymbol()){
      return true;
    }
    return false;
}


public Base(char symbol, int x, int y){
    super(symbol, x, y);
    speedX = speedY = 0;
}

   public Base(Field f, int side, char symbol, int x, int y, String ref){
    super(f, side, symbol, x, y, ref);
    speedX = speedY = 0;
}
}

Explanation / Answer


class Catcher extends Player{

  
   @Override
public void play(Field field){
     setSpeed(field);
     
     if( pickUpFlag(field) == true){
       this.speedX = 0;
       this.speedY = 0;
     }
     if(( (x <= field.maxX-15) && (y <= field.maxY-15) ) && ( (x >= field.minX) && (y >= field.minY) )){
     
     }
     else{
       this.speedX = 0;
       this.speedY = 0;



     }
   }


@Override
public void update(Field field){}


public Catcher(Field f, int side, String name, int number, String team,char symbol, double x, double y){
    super(f, side, name, number, team, symbol, x, y);
    //this.speedX = Math.random()*4-2;
    //this.speedY = Math.random()*4-2;
  
}
double speed = Math.random()*6;
double multiplier = 0;
  
double directionX = 0;
double directionY = 0;
private void setSpeed(Field field){

  
    if( this.team.equals("reds") ){
    
    
  
      directionX = field.team1.get(0).x-x;
      directionY = field.team1.get(0).y-y;
      //if catch player take to jail
      if(field.catchOpponent(this ,(Player) field.team1.get(0))){
      
        directionX = field.getJail2Position()[0]-x;
        directionY = field.getJail2Position()[1]-y;
        field.team1.get(0).x = this.x;
        field.team1.get(0).y = this.y;
        System.out.println("1");
      
        if(Math.hypot(this.x - field.getJail2Position()[0], this.y - field.getJail2Position()[1]) <= field.ARMS_LENGTH){
          field.team1.get(0).x = field.getBase1Position()[0];
          field.team1.get(0).y = field.getBase1Position()[1];
        }
      
      
      }
  
    }
    else{
    
      directionX = field.team2.get(0).x-x;
      directionY = field.team2.get(0).y-y;
   
      if(field.catchOpponent(this ,(Player) field.team2.get(0))){
        directionX = field.getJail1Position()[0]-x;
        directionY = field.getJail1Position()[1]-y;
        field.team2.get(0).x = this.x;
        field.team2.get(0).y = this.y;
        System.out.println("2");
      
        if(Math.hypot(this.x - field.getJail1Position()[0], this.y - field.getJail1Position()[1]) <= field.ARMS_LENGTH){
          field.team2.get(0).x = field.getBase2Position()[0];
          field.team2.get(0).y = field.getBase2Position()[1];
        }
      
      
      }
  
    }
    multiplier = Math.sqrt(Math.pow(speed,2)/(Math.pow(directionX,2)+Math.pow(directionY,2)));
    speedX = directionX*multiplier;
    speedY = directionY*multiplier;
  
    }
      
  
}
  





















Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote