SlideShare a Scribd company logo
question.(player, entity ,field and base.java codes are given)
Stopping Player
All players need to stay within the eld boundaries. This rst class you write will lay some of the
foundation for the logic needed in all your players. Create a class called Stopping that extends
the Player class. A Stopping player will initially move in a random direction and stops moving
when it reaches the border of the playing eld (without leaving the playing eld). The direction that
a player moves in is determined by its speed in each direction: speedX and speedY. You will add
this logic to manipulate the speed attributes for stopping in its play() method. Take care to notice
that the images of the players have some size to them. Your player should not “visually” leave
the eld. All parts of the image should be on or inside the eld boundaries.
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.
*
* 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 teammate 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
*
* 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
*
* 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
*
* 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);
}
}
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;
}
}
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
*
* In each direction, we have
* new_position = current_position + speed * time_elapsed
*
* 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)
*
* 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
*
* 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
*
* 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);
}
}
field.java
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Arrays;
/** This is the field that a game of Capture the Flag plays on.
*
* The field also acts as a moderator (umpire) for the game to
* try and prevent players from cheating.
*
* A field will have some width (x directions) and a height (y direction).
* The upper left-hand corner is the [0,0] coordinate.
*
* The values of x increase as you move to the right
* The values of y increase as you move down
*/
public class Field{
/** determines if players start at base or not */
public static boolean START_FROM_BASE = true;
/** constant scaling for moving */
protected static long SCALE = 30;
/** determines if the game has been won (completed) */
protected static boolean GAME_IS_WON = false;
/** distance between entities to be considered touching (from centre to centre) */
public static Double ARMS_LENGTH = 12.0;
/** max x-coordinate for entity on field */
final int maxX;
/** max y-coordinate for entity on field */
final int maxY;
/** min x-coordinate for entity on field */
final int minX;
/** min y-coordinate for entity on field */
final int minY;
/** the graphical view of the game */
final View view;
/* */
/* teams in the game */
/* */
/** the team for territory 1 */
protected ArrayList team1 = new ArrayList();
/** the team for territory 2 */
protected ArrayList team2 = new ArrayList();
/** getter for the players in team 1
*
* @return the players in team 1
*/
public ArrayList getTeam1(){ return this.team1; }
/** getter for the players in team 2
*
* @return the players in team 2
*/
public ArrayList getTeam2(){ return this.team2; }
/* */
/* basic entities that all games need */
/* */
/** A look-up table for players and their ids */
protected Hashtable getID = new Hashtable();
/** things for territory 1 */
protected final Entity flag1;
protected final Entity jail1;
protected final Entity base1;
/** things for territory 2 */
protected final Entity flag2;
protected final Entity jail2;
protected final Entity base2;
/** the flag for territory 2 */
/** the jail for territory 1 */
/** the jail for territory 2 */
/** the base for territory 1 */
/** the base for territory 2 */
/** holds the "things" in a game: flags, bases and jails */
ArrayList things = new ArrayList();
/** A look-up table that maps entity's to the team (territory) they belong to */
protected Hashtable getTeam = new Hashtable();
/** gets the x and y coordinates of flag 1
*
* @return [x,y], the x and y coordinates of the flag 1 on this field
*/
public int[] getFlag1Position(){ return new int[]{ flag1.getX(), flag1.getY() }; }
/** gets the x and y coordinates of flag 2
*
* @return [x,y], the x and y coordinates of the flag 2 on this field
*/
public int[] getFlag2Position(){ return new int[]{ flag2.getX(), flag2.getY() }; }
/** gets the x and y coordinates of jail 1
*
* @return [x,y], the x and y coordinates of the jail 1 on this field
*/
public int[] getJail1Position(){ return new int[]{ jail1.getX(), jail1.getY() }; }
/** gets the x and y coordinates of jail 2
*
* @return [x,y], the x and y coordinates of the jail 2 on this field
*/
public int[] getJail2Position(){ return new int[]{ jail2.getX(), jail2.getY() }; }
/** gets the x and y coordinates of base 1
*
* @return [x,y], the x and y coordinates of the base 1 on this field
*/
public int[] getBase1Position(){ return new int[]{ base1.getX(), base1.getY() }; }
/** gets the x and y coordinates of base 2
*
* @return [x,y], the x and y coordinates of the base 2 on this field
*/
public int[] getBase2Position(){ return new int[]{ base2.getX(), base2.getY() }; }
/** getter for all the "things" in the game: flags, bases and jails
*
* @return an arraylist containing both flags, bases and jails in this field
*/
public ArrayList getThings(){
/* hard-coded list of all things in the game. Change this if you want to add more things */
Entity[] allThings = new Entity[]{ flag1, flag2, base1, base2, jail1, jail2 };
return new ArrayList( Arrays.asList(allThings) );
}
/* */
/* methods that the game will use */
/* */
/** update all entities on the field
*
* update the position of all entity's on this field
*/
public void update(){
for(Entity en: team1){
try{
en.updatePosition(SCALE*1, this, getID.get(en));
}catch(EntityOutOfBoundsException e){
// add code here
}
}
for(Entity en: team2){
try{
en.updatePosition(SCALE*1, this, getID.get(en));
}catch(EntityOutOfBoundsException e){
// add code here
}
}
for(Entity en: things){
try{
en.updatePosition(SCALE*1, this, getID.get(en));
}catch(EntityOutOfBoundsException e){
// add code here
}
}
}
/** have all entities on this field "play" (perform their own logic) */
public void play(){
for(Entity e: team1){
e.play(this);
}
for(Entity e: team2){
e.play(this);
}
for(Entity e: things){
e.play(this);
}
}
/** draw the current state of the game */
public void draw(){
view.update();
view.draw(team1, team2, things);
}
/** Assigns a player to given territory on the field
*
* Registers a player to a side (territory) on the field. If START_FROM_BASE is
* true, a registered player will have its position set be the same as the base for
* territory they are being registered to.
*
* Side effects: registers a player with a territory. Assigns a sprite (image) to the player.
* Players position is set to their base if START_FROM_BASE is true.
*
* @param a is a player
* @param id is the player's id number
* @param territory is either 1 or 2 (representing a territory)
* @return true if a is not already registered, territory is 1 or 2,
* and the player's coordinates are not on the other territory. False, otherwise.
*/
public boolean registerPlayer(Player a, int id, int territory){
if( getTeam.containsKey(a) || territory < 1 || territory > 2){
return false;
}
if( territory == 1 ){
if (a.getX() > maxX/2){
/* player must be on the correct side of the field */
return false;
}
a.setSprite("sprites/blue.png", id);
if(START_FROM_BASE){
a.setX(base1.getX(), id);
a.setY(base1.getY(), id);
}
team1.add(a);
}else{
if (a.getX() < maxX/2){
/* player must be on the correct side of the field */
return false;
}
a.setSprite("sprites/red.png", id);
if(START_FROM_BASE){
a.setX(base2.getX(), id);
a.setY(base2.getY(), id);
}
team2.add(a);
}
getTeam.put(a, territory);
getID.put(a, id);
return true;
}
/** Assigns a non-player entity to given territory on the field
*
* Side effects: registers a non-player entity with a territory.
* Assigns a sprite (image) to the entity.
* Remembers the entity's id.
*
* @param a is an entity (non-player)
* @param id is the entity's id number
* @param territory is either 1 or 2 (representing a territory)
*/
public boolean registerThing(Entity a, int id, int territory){
if( territory < 1 || territory > 2){
return false;
}
// currently only remembers the thing's id
getID.put(a, id);
return true;
}
/* */
/* methods that players use */
/* */
/** Attempt to catch a player
*
* @param a is a player trying to catch player b<code>
* @param b is a player
* @return true if a and b on are on different teams
* and are within ARMS_LENGTH of each other. False otherwise.
*/
public boolean catchOpponent(Player a, Player b){
if( a.getTeam().equals(b.getTeam()) ){
return false;
}
if( Math.hypot( a.getX() - b.getX(), a.getY() - b.getY() ) <= ARMS_LENGTH ){
return true;
}
return false;
}
/** attempt to free a player from jail
*
* @param a is a player trying to free a teammate from jail
* @param b is a player
* @return true if a and b are on the same team,
* is in jail, and a is within ARMS_REACH of
* the jail where b is located. False otherwise.
*/
public boolean freeTeammate(Player a, Player b){
//
// need to add some code here
//
return false;
}
/** attempt to pick up a flag
*
* @param a is a player trying to pick up a flag
* @return true if a is within ARMS_REACH of
* the opposing team's flag and it is not being carried by anyone else.
* Returns false otherwise.
*/
public boolean pickUpFlag(Player a){
Entity b = flag1;
if( getTeam.get(a).equals(new Integer(1)) ){
b = flag2;
}
if( Math.hypot( a.getX() - b.getX(), a.getY() - b.getY() ) <= ARMS_LENGTH ){
return true;
}
return false;
}
/** attempt to win the game
*
* @param a is a player
* @return true if a is carrying its opponent's flag
* and is with ARMS_REACH of its own base. False otherwise.
*/
public boolean winGame(Player a){
Entity b = base1;
if( getTeam.get(this).equals(new Integer(1)) ){
b = base2;
}
//
// needs some code to determine if a is carrying the flag
//
if( Math.hypot( a.getX() - b.getX(), a.getY() - b.getY() ) <= ARMS_LENGTH ){
GAME_IS_WON = true;
return true;
}
return false;
}
/** Asks if the game is still running (no winner yet)
*
* @return true if the game is still running (the game has not been won) and false otherwise.
*/
public boolean gameStillRunning(){
return !GAME_IS_WON;
}
/** create the default playing field
*
* width of field = 800 pixels
* height of field = 600 pixels
*/
public Field(){
// initialize field dimensions (hardcoded for now)
maxX = 810; minX = 10;
maxY = 610; minY = 10;
// initialize all the "things" in the game
flag1 = new Flag(this, 1, 'f', 34+minX, 191+minY, "sprites/blueFlag.png");
jail1 = new Jail(this, 1, 'j', 29+minX, 399+minY, "sprites/jail.png");
base1 = new Base(this, 1, 'b', 29+minX, 199+minY, "sprites/blueBase.png");
getTeam.put(base1, 1); getTeam.put(jail1,1); getTeam.put(flag1, 1);
things.add(base1); things.add(jail1); things.add(flag1);
flag2 = new Flag(this, 2, 'F', 754+minX, 391+minY, "sprites/redFlag.png");
jail2 = new Jail(this, 2, 'J', 749+minX, 199+minY, "sprites/jail.png");
base2 = new Base(this, 2, 'B', 749+minX, 399+minY, "sprites/redBase.png");
getTeam.put(base2, 2); getTeam.put(jail2,2); getTeam.put(flag2, 2);
things.add(base2); things.add(jail2); things.add(flag2);
// initialize the view
view = new View(minX, minY, maxX, maxY);
}
}
Solution
public class Stopping extends Player{
@Override
public void update(Field field){}
@Override
public void play(Field field){
// Update the player's speed based on their proximity to the border of the field
if (this.x >= (field.maxX-16)){ //right
this.speedX = 0;
this.speedY = 0;
this.setX(field.maxX-16 ,id);
}else if (this.x <= (field.minX)){ //left
this.speedX = 0;
this.speedY = 0;
this.setX(field.minX,id);
}else if(this.y >= (field.maxY-16)){ //bottom
this.speedX = 0;
this.speedY = 0;
this.setX(field.maxY-50,id);
} else if (this.y <= (field.minY)){ //top
this.speedX = 0;
this.speedY = 0;
this.setX(field.minY+50,id);
}
// This logic slows down players that are too close to the wall
if (this.x >= (field.maxX+48)){
this.speedX = this.speedX/this.speedX;
this.speedY = this.speedY/this.speedY;
}else if (this.x <= (field.minX-48)){
this.speedX = this.speedX/this.speedX;
this.speedY = this.speedY/this.speedY;
}else if(this.y >= (field.maxY+48)){
this.speedX = this.speedX/this.speedX;
this.speedY = this.speedY/this.speedY;
} else if (this.y <= (field.minY-48)){
this.speedX = this.speedX/this.speedX;
this.speedY = this.speedY/this.speedY;
}
}
public Stopping(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 = -160 + Math.random()*(320);
this.speedY = -160 + Math.random()*(320);
}
}

More Related Content

PDF
I really need some help if I have this right so far. Please Resub.pdf
PDF
I really need some help if I have this right so far. PLEASE CHANG.pdf
DOCX
3 1-1
PDF
Here are the instructions and then the code in a sec. Please R.pdf
PDF
Thanks so much for your help. Review the GameService class. Noti.pdf
PDF
The main class of the tictoe game looks like.public class Main {.pdf
PPTX
Game modeling
PDF
Please use java to write the program that simulates the card game!!! T (1).pdf
I really need some help if I have this right so far. Please Resub.pdf
I really need some help if I have this right so far. PLEASE CHANG.pdf
3 1-1
Here are the instructions and then the code in a sec. Please R.pdf
Thanks so much for your help. Review the GameService class. Noti.pdf
The main class of the tictoe game looks like.public class Main {.pdf
Game modeling
Please use java to write the program that simulates the card game!!! T (1).pdf

Similar to question.(player, entity ,field and base.java codes are given)Stop.pdf (20)

PDF
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
PDF
Here is the game description- Here is the sample game- Here is the req.pdf
PDF
This is Java,I am currently stumped on how to add a scoreboard for.pdf
PDF
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
PDF
Testing a 2D Platformer with Spock
PDF
P5
PPT
Gdc09 Minipong
DOCX
Java Helpa4.pdfASSIGNMENT 4 A game and an email system.docx
PDF
Darkonoid
PDF
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdf
PDF
This is for an homework assignment using Java code. Here is the home.pdf
PDF
Here is my javafx code to create a basic warehammer 40k game. I want.pdf
PPTX
98 374 Lesson 06-slides
PDF
package com.tictactoe; public class Main {public void play() {.pdf
PDF
Goal- Your goal in this assignment is to write a Java program that sim.pdf
DOCX
The task is to design and develop a prototype for one of the fea.docx
PDF
Please help with this. program must be written in C# .. All of the g.pdf
DOCX
Task(s)The task is to design and develop a prototype for one.docx
PDF
Missilecommand
PDF
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Here is the req.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Testing a 2D Platformer with Spock
P5
Gdc09 Minipong
Java Helpa4.pdfASSIGNMENT 4 A game and an email system.docx
Darkonoid
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdf
This is for an homework assignment using Java code. Here is the home.pdf
Here is my javafx code to create a basic warehammer 40k game. I want.pdf
98 374 Lesson 06-slides
package com.tictactoe; public class Main {public void play() {.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdf
The task is to design and develop a prototype for one of the fea.docx
Please help with this. program must be written in C# .. All of the g.pdf
Task(s)The task is to design and develop a prototype for one.docx
Missilecommand
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
Ad

More from shahidqamar17 (20)

PDF
At one time, the country of Aquilonia had no banks, but had curre.pdf
PDF
A certain element has a half life of 4.5 billion years. a. You find .pdf
PDF
A linear system is always time-invariant.SolutionNo its false. .pdf
PDF
Workforce Management also called Human Resource Management manages o.pdf
PDF
Where has healthcare information management been historically How h.pdf
PDF
What were the successes and failures of the Progressive Movement, an.pdf
PDF
What is the difference between private sector, nonprofit sector, and.pdf
PDF
What are Cubas entrepreneurial opportunities and the status of .pdf
PDF
The LabVIEW G programming is considered to be a data flow programmin.pdf
PDF
what is a bottom heap up construction with an example pleaseSol.pdf
PDF
What are the types of differences that exist between IFRS and U.S. G.pdf
PDF
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
PDF
Question 25 A Multilateral agreement in which countries signed to ban.pdf
PDF
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
PDF
1Write a Java program that calculates and displays the Fibonacciseri.pdf
PDF
need an example of a System Anaysis and design project must have 3 .pdf
PDF
Money laundering has become a mechanism for terrorist financing acti.pdf
PDF
Let x be any set and let lambda SolutionProof. Obviously .pdf
PDF
Java public cts Node t next ni next SolutionYou hav.pdf
PDF
1. What is the purpose of level numbers in COCOL records2.What ar.pdf
At one time, the country of Aquilonia had no banks, but had curre.pdf
A certain element has a half life of 4.5 billion years. a. You find .pdf
A linear system is always time-invariant.SolutionNo its false. .pdf
Workforce Management also called Human Resource Management manages o.pdf
Where has healthcare information management been historically How h.pdf
What were the successes and failures of the Progressive Movement, an.pdf
What is the difference between private sector, nonprofit sector, and.pdf
What are Cubas entrepreneurial opportunities and the status of .pdf
The LabVIEW G programming is considered to be a data flow programmin.pdf
what is a bottom heap up construction with an example pleaseSol.pdf
What are the types of differences that exist between IFRS and U.S. G.pdf
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
Question 25 A Multilateral agreement in which countries signed to ban.pdf
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
1Write a Java program that calculates and displays the Fibonacciseri.pdf
need an example of a System Anaysis and design project must have 3 .pdf
Money laundering has become a mechanism for terrorist financing acti.pdf
Let x be any set and let lambda SolutionProof. Obviously .pdf
Java public cts Node t next ni next SolutionYou hav.pdf
1. What is the purpose of level numbers in COCOL records2.What ar.pdf
Ad

Recently uploaded (20)

PPTX
master seminar digital applications in india
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Basic Mud Logging Guide for educational purpose
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
master seminar digital applications in india
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Insiders guide to clinical Medicine.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
PPH.pptx obstetrics and gynecology in nursing
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
01-Introduction-to-Information-Management.pdf
GDM (1) (1).pptx small presentation for students
Basic Mud Logging Guide for educational purpose
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH

question.(player, entity ,field and base.java codes are given)Stop.pdf

  • 1. question.(player, entity ,field and base.java codes are given) Stopping Player All players need to stay within the eld boundaries. This rst class you write will lay some of the foundation for the logic needed in all your players. Create a class called Stopping that extends the Player class. A Stopping player will initially move in a random direction and stops moving when it reaches the border of the playing eld (without leaving the playing eld). The direction that a player moves in is determined by its speed in each direction: speedX and speedY. You will add this logic to manipulate the speed attributes for stopping in its play() method. Take care to notice that the images of the players have some size to them. Your player should not “visually” leave the eld. All parts of the image should be on or inside the eld boundaries. 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 */
  • 2. 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); }
  • 3. /** Informs this player that they have been caught by another player. * * 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 teammate 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 * * 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 */
  • 4. 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 * * 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
  • 5. * * 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); } } Base.java public class Base extends Entity{ @Override public void play(Field field){} @Override
  • 6. 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; } } 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 */
  • 7. 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 */
  • 8. 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
  • 9. * * In each direction, we have * new_position = current_position + speed * time_elapsed * * 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
  • 10. } /** logic for this entity (change direction/speed) * * 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 * * 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 * * 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;}
  • 11. 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");
  • 12. } 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;
  • 13. 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); } } field.java import java.util.ArrayList; import java.util.Hashtable; import java.util.Arrays; /** This is the field that a game of Capture the Flag plays on. * * The field also acts as a moderator (umpire) for the game to * try and prevent players from cheating. * * A field will have some width (x directions) and a height (y direction). * The upper left-hand corner is the [0,0] coordinate. * * The values of x increase as you move to the right * The values of y increase as you move down */ public class Field{ /** determines if players start at base or not */ public static boolean START_FROM_BASE = true;
  • 14. /** constant scaling for moving */ protected static long SCALE = 30; /** determines if the game has been won (completed) */ protected static boolean GAME_IS_WON = false; /** distance between entities to be considered touching (from centre to centre) */ public static Double ARMS_LENGTH = 12.0; /** max x-coordinate for entity on field */ final int maxX; /** max y-coordinate for entity on field */ final int maxY; /** min x-coordinate for entity on field */ final int minX; /** min y-coordinate for entity on field */ final int minY; /** the graphical view of the game */ final View view; /* */ /* teams in the game */ /* */ /** the team for territory 1 */ protected ArrayList team1 = new ArrayList();
  • 15. /** the team for territory 2 */ protected ArrayList team2 = new ArrayList(); /** getter for the players in team 1 * * @return the players in team 1 */ public ArrayList getTeam1(){ return this.team1; } /** getter for the players in team 2 * * @return the players in team 2 */ public ArrayList getTeam2(){ return this.team2; } /* */ /* basic entities that all games need */ /* */ /** A look-up table for players and their ids */ protected Hashtable getID = new Hashtable(); /** things for territory 1 */ protected final Entity flag1; protected final Entity jail1; protected final Entity base1; /** things for territory 2 */ protected final Entity flag2; protected final Entity jail2; protected final Entity base2; /** the flag for territory 2 */
  • 16. /** the jail for territory 1 */ /** the jail for territory 2 */ /** the base for territory 1 */ /** the base for territory 2 */ /** holds the "things" in a game: flags, bases and jails */ ArrayList things = new ArrayList(); /** A look-up table that maps entity's to the team (territory) they belong to */ protected Hashtable getTeam = new Hashtable(); /** gets the x and y coordinates of flag 1 * * @return [x,y], the x and y coordinates of the flag 1 on this field */ public int[] getFlag1Position(){ return new int[]{ flag1.getX(), flag1.getY() }; } /** gets the x and y coordinates of flag 2 * * @return [x,y], the x and y coordinates of the flag 2 on this field */ public int[] getFlag2Position(){ return new int[]{ flag2.getX(), flag2.getY() }; } /** gets the x and y coordinates of jail 1 * * @return [x,y], the x and y coordinates of the jail 1 on this field */ public int[] getJail1Position(){ return new int[]{ jail1.getX(), jail1.getY() }; } /** gets the x and y coordinates of jail 2 * * @return [x,y], the x and y coordinates of the jail 2 on this field */ public int[] getJail2Position(){ return new int[]{ jail2.getX(), jail2.getY() }; }
  • 17. /** gets the x and y coordinates of base 1 * * @return [x,y], the x and y coordinates of the base 1 on this field */ public int[] getBase1Position(){ return new int[]{ base1.getX(), base1.getY() }; } /** gets the x and y coordinates of base 2 * * @return [x,y], the x and y coordinates of the base 2 on this field */ public int[] getBase2Position(){ return new int[]{ base2.getX(), base2.getY() }; } /** getter for all the "things" in the game: flags, bases and jails * * @return an arraylist containing both flags, bases and jails in this field */ public ArrayList getThings(){ /* hard-coded list of all things in the game. Change this if you want to add more things */ Entity[] allThings = new Entity[]{ flag1, flag2, base1, base2, jail1, jail2 }; return new ArrayList( Arrays.asList(allThings) ); } /* */ /* methods that the game will use */ /* */ /** update all entities on the field * * update the position of all entity's on this field */
  • 18. public void update(){ for(Entity en: team1){ try{ en.updatePosition(SCALE*1, this, getID.get(en)); }catch(EntityOutOfBoundsException e){ // add code here } } for(Entity en: team2){ try{ en.updatePosition(SCALE*1, this, getID.get(en)); }catch(EntityOutOfBoundsException e){ // add code here } } for(Entity en: things){ try{ en.updatePosition(SCALE*1, this, getID.get(en)); }catch(EntityOutOfBoundsException e){ // add code here } } } /** have all entities on this field "play" (perform their own logic) */ public void play(){ for(Entity e: team1){ e.play(this); } for(Entity e: team2){ e.play(this); } for(Entity e: things){ e.play(this); } }
  • 19. /** draw the current state of the game */ public void draw(){ view.update(); view.draw(team1, team2, things); } /** Assigns a player to given territory on the field * * Registers a player to a side (territory) on the field. If START_FROM_BASE is * true, a registered player will have its position set be the same as the base for * territory they are being registered to. * * Side effects: registers a player with a territory. Assigns a sprite (image) to the player. * Players position is set to their base if START_FROM_BASE is true. * * @param a is a player * @param id is the player's id number * @param territory is either 1 or 2 (representing a territory) * @return true if a is not already registered, territory is 1 or 2, * and the player's coordinates are not on the other territory. False, otherwise. */ public boolean registerPlayer(Player a, int id, int territory){ if( getTeam.containsKey(a) || territory < 1 || territory > 2){ return false; } if( territory == 1 ){ if (a.getX() > maxX/2){ /* player must be on the correct side of the field */ return false; } a.setSprite("sprites/blue.png", id); if(START_FROM_BASE){
  • 20. a.setX(base1.getX(), id); a.setY(base1.getY(), id); } team1.add(a); }else{ if (a.getX() < maxX/2){ /* player must be on the correct side of the field */ return false; } a.setSprite("sprites/red.png", id); if(START_FROM_BASE){ a.setX(base2.getX(), id); a.setY(base2.getY(), id); } team2.add(a); } getTeam.put(a, territory); getID.put(a, id); return true; } /** Assigns a non-player entity to given territory on the field * * Side effects: registers a non-player entity with a territory. * Assigns a sprite (image) to the entity. * Remembers the entity's id. * * @param a is an entity (non-player) * @param id is the entity's id number * @param territory is either 1 or 2 (representing a territory) */ public boolean registerThing(Entity a, int id, int territory){ if( territory < 1 || territory > 2){ return false;
  • 21. } // currently only remembers the thing's id getID.put(a, id); return true; } /* */ /* methods that players use */ /* */ /** Attempt to catch a player * * @param a is a player trying to catch player b<code> * @param b is a player * @return true if a and b on are on different teams * and are within ARMS_LENGTH of each other. False otherwise. */ public boolean catchOpponent(Player a, Player b){ if( a.getTeam().equals(b.getTeam()) ){ return false; } if( Math.hypot( a.getX() - b.getX(), a.getY() - b.getY() ) <= ARMS_LENGTH ){ return true; } return false; } /** attempt to free a player from jail * * @param a is a player trying to free a teammate from jail * @param b is a player * @return true if a and b are on the same team, * is in jail, and a is within ARMS_REACH of * the jail where b is located. False otherwise.
  • 22. */ public boolean freeTeammate(Player a, Player b){ // // need to add some code here // return false; } /** attempt to pick up a flag * * @param a is a player trying to pick up a flag * @return true if a is within ARMS_REACH of * the opposing team's flag and it is not being carried by anyone else. * Returns false otherwise. */ public boolean pickUpFlag(Player a){ Entity b = flag1; if( getTeam.get(a).equals(new Integer(1)) ){ b = flag2; } if( Math.hypot( a.getX() - b.getX(), a.getY() - b.getY() ) <= ARMS_LENGTH ){ return true; } return false; } /** attempt to win the game * * @param a is a player * @return true if a is carrying its opponent's flag * and is with ARMS_REACH of its own base. False otherwise. */ public boolean winGame(Player a){ Entity b = base1; if( getTeam.get(this).equals(new Integer(1)) ){
  • 23. b = base2; } // // needs some code to determine if a is carrying the flag // if( Math.hypot( a.getX() - b.getX(), a.getY() - b.getY() ) <= ARMS_LENGTH ){ GAME_IS_WON = true; return true; } return false; } /** Asks if the game is still running (no winner yet) * * @return true if the game is still running (the game has not been won) and false otherwise. */ public boolean gameStillRunning(){ return !GAME_IS_WON; } /** create the default playing field * * width of field = 800 pixels * height of field = 600 pixels */ public Field(){ // initialize field dimensions (hardcoded for now) maxX = 810; minX = 10; maxY = 610; minY = 10; // initialize all the "things" in the game flag1 = new Flag(this, 1, 'f', 34+minX, 191+minY, "sprites/blueFlag.png");
  • 24. jail1 = new Jail(this, 1, 'j', 29+minX, 399+minY, "sprites/jail.png"); base1 = new Base(this, 1, 'b', 29+minX, 199+minY, "sprites/blueBase.png"); getTeam.put(base1, 1); getTeam.put(jail1,1); getTeam.put(flag1, 1); things.add(base1); things.add(jail1); things.add(flag1); flag2 = new Flag(this, 2, 'F', 754+minX, 391+minY, "sprites/redFlag.png"); jail2 = new Jail(this, 2, 'J', 749+minX, 199+minY, "sprites/jail.png"); base2 = new Base(this, 2, 'B', 749+minX, 399+minY, "sprites/redBase.png"); getTeam.put(base2, 2); getTeam.put(jail2,2); getTeam.put(flag2, 2); things.add(base2); things.add(jail2); things.add(flag2); // initialize the view view = new View(minX, minY, maxX, maxY); } } Solution public class Stopping extends Player{ @Override public void update(Field field){} @Override public void play(Field field){ // Update the player's speed based on their proximity to the border of the field if (this.x >= (field.maxX-16)){ //right this.speedX = 0; this.speedY = 0; this.setX(field.maxX-16 ,id); }else if (this.x <= (field.minX)){ //left
  • 25. this.speedX = 0; this.speedY = 0; this.setX(field.minX,id); }else if(this.y >= (field.maxY-16)){ //bottom this.speedX = 0; this.speedY = 0; this.setX(field.maxY-50,id); } else if (this.y <= (field.minY)){ //top this.speedX = 0; this.speedY = 0; this.setX(field.minY+50,id); } // This logic slows down players that are too close to the wall if (this.x >= (field.maxX+48)){ this.speedX = this.speedX/this.speedX; this.speedY = this.speedY/this.speedY; }else if (this.x <= (field.minX-48)){ this.speedX = this.speedX/this.speedX; this.speedY = this.speedY/this.speedY; }else if(this.y >= (field.maxY+48)){ this.speedX = this.speedX/this.speedX; this.speedY = this.speedY/this.speedY; } else if (this.y <= (field.minY-48)){ this.speedX = this.speedX/this.speedX; this.speedY = this.speedY/this.speedY; } } public Stopping(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 = -160 + Math.random()*(320); this.speedY = -160 + Math.random()*(320); } }