0% found this document useful (0 votes)
45 views21 pages

Message

The document describes a simulation of a Tefball game including Players, Ball, and PlayingField classes. It includes an abstract Player class and subclasses for Quarterback, Receiver, and Defender that define their attributes and behaviors. Methods are described for tracking location and movement of players and throwing/catching the ball.

Uploaded by

Benjamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views21 pages

Message

The document describes a simulation of a Tefball game including Players, Ball, and PlayingField classes. It includes an abstract Player class and subclasses for Quarterback, Receiver, and Defender that define their attributes and behaviors. Methods are described for tracking location and movement of players and throwing/catching the ball.

Uploaded by

Benjamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/**

* EECS 285 Project 2 - simulation of Tefball game.


* Contains the Players, Ball, and PlayingField of the game.
* See https://eecs285.github.io/p2-tefball/ for the specification.
*/

package eecs285.proj2.houd; // replace with your uniqname


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

/**
* Abstract Class for the Players in the game.
* Not explicitly used but the Quarterback, Receiver,
* and Defender class are derived from this class.
* Outlines the basic member variables and methods used in all the derived classes.
*/
abstract class Player {
private int playerInd;
private double startColumn;
private double startRow;
private double speed;
private double currColumn;
private double currRow;

//stores the type of player.


private char type;

/**
* Returns the player type.
* Valid player types:
* Q - quarterback
* R - Receiver
* D - defender
*
* @return player type
*/
public char getType() {
return type;
}

/**
* Sets the player type.
* Valid player types:
* Q - quarterback
* R - Receiver
* D - defender
*
* @param type char type that represents the player type
*/
public void setType(char type) {
this.type = type;
}

/**
* Returns the player's index in the ArrayList used in PlayingField
* Player indices are zero indexed.
*
* @return player index
*/
public int getPlayerInd(){
return playerInd;
}

/**
* Sets the member variable playerInd to the player index
* used in the ArrayList in PlayingField.
* Player indices are zero indexed.
*
* @param playerInd value that playerInd will be set to
*/
public void setPlayerInd(int playerInd){
this.playerInd = playerInd;
}

/**
* Returns the column in the PlayingField where the player starts.
* Columns are zero indexed.
*
* @return starting column
*/
public double getStartColumn(){
return startColumn;
}

/**
* Sets the member variable startColumn to the column
* in the PlayingField where the player is starting.
* Columns are zero indexed.
*
* @param startColumn player's starting column
*/
public void setStartColumn(double startColumn){
this.startColumn = startColumn;
}

/**
* Returns the row in the PlayingField where the player starts.
* Rows are zero indexed.
*
* @return starting row
*/
public double getStartRow(){
return startRow;
}

/**
* Sets the member variable startRow to the row number
* in the PlayingField where the player is starting.
* Rows are zero indexed.
*
* @param startRow player's starting column
*/
public void setStartRow(double startRow){
this.startRow = startRow;
}

/**
* Returns the value of the member variable speed
* which holds how fast the player runs.
* A speed of 1 indicates the player runs one unit in the PLayingField grid
* in 1 move (PerformMove).
*
* @return speed of player
*/
public double getSpeed(){
return speed;
}

/**
* Sets the value of the member variable speed to how fast the player runs.
* A speed of 1 indicates the player runs one unit in the PLayingField grid
* in 1 move (PerformMove).
*
* @param speed speed of player
*/
public void setSpeed(double speed){
this.speed = speed;
}

/**
* Returns the column in the PlayingField where the player currently is at.
* Columns are zero indexed.
*
* @return current column
*/
public double getCurrColumn() {
return currColumn;
}

/**
* Sets the member variable currColumnn to the column in the
* PlayingField for where the player current is at.
* Columnns are zero indexed.
*
* @param currColumn current column
*/
public void setCurrColumn(double currColumn) {
this.currColumn = currColumn;
}

/**
* Returns the row in the PlayingField where the player currently is at.
* Rows are zero indexed.
*
* @return current row
*/
public double getCurrRow() {
return currRow;
}

/**
* Sets the member variablecurrRow to the row in the PlayingField
* where the player currently is at.
* Rows are zero indexed.
*
* @param currRow current row
*/
public void setCurrRow(double currRow) {
this.currRow = currRow;
}

/**
* Abstract method
* Returns the row of the player's destination.
* This is the location where the player is moving towards.
* Relevant for quarterback and receiver.
* Rows are zero indexed.
*
* @return row of the player's destination
*/
abstract double getStopRow();

/**
* Abstract method
* Returns the column of the player's destination.
* This is the location where the player is moving towards.
* Relevant for quarterback and receiver.
* Columns are zero indexed.
*
* @return column of the player's destination
*/
abstract double getStopColumn();

/**
* Abstract Method
* Performs the player's move corresponding to its type.
* Returns the result of the game as GameResultEnum
*
* @param qb quarterback
* @param ball ball
* @return result of the game
*/
abstract GameResultEnum performMove(Player qb, Ball ball);

/**
* Default constructor for player class
*/
Player(){}

/**
* Alternative constructor for player class where values can be passed in.
*
* @param playerInd player index
* @param startColumn column of where the player starts
* @param startRow row of where the player stars
* @param speed speed of how fast the player runs
*/
Player(int playerInd, double startColumn, double startRow, double speed){
this.playerInd = playerInd;
this.startColumn = startColumn;
this.startRow = startRow;
this.speed = speed;
this.currColumn = startColumn;
this.currRow = startRow;
}
}

class Quarterback extends Player{


private double stopColumn;
private double stopRow;
private double throwToColumn;
private double throwToRow;
private double throwSpeed;

/**
* Returns the column of the quarterback's destination.
* This is the location where the quarterback is moving towards.
* Relevant for quarterback and receiver.
* Columns are zero indexed.
*
* @return column of the quarterback's destination
*/
public double getStopColumn() {
return stopColumn;
}

/**
* Sets member variable stopColumn to the column of
* the quarterback's destination.
* This is the location where the quarterback is moving towards.
* Relevant for quarterback and receiver.
* Columns are zero indexed.
*
* @param stopColumn column of the quarterback's destination
*/
public void setStopColumn(double stopColumn) {
this.stopColumn = stopColumn;
}

/**
* Returns the row of the quarterback's destination.
* This is the location where the quarterback is moving towards.
* Relevant for quarterback and receiver.
* Rows are zero indexed.
*
* @return row of the player's destination
*/
public double getStopRow() {
return stopRow;
}

/**
* Sets member variable stopRow to the row of
* the quarterback's destination.
* This is the location where the quarterback is moving towards.
* Relevant for quarterback and receiver.
* rows are zero indexed.
*
* @param stopRow row of the player's destination
*/
public void setStopRow(int stopRow) {
this.stopRow = stopRow;
}
/**
* Returns the column of where the quarterback is throwing the ball.
* This is the location where the ball is moving towards.
* Columns are zero indexed.
*
* @return column of the ball's destination
*/
public double getThrowToColumn() {
return throwToColumn;
}

/**
* Sets member variable throwToColumn to the column of
* where the quarterback is throwing the ball.
* This is the location where the ball is moving towards.
* Columns are zero indexed.
*
* @param throwToColumn column of the ball's destination
*/
public void setThrowToColumn(int throwToColumn) {
this.throwToColumn = throwToColumn;
}

/**
* Returns the row of where the quarterback is throwing the ball.
* This is the location where the ball is moving towards.
* Rows are zero indexed.
*
* @return row of the ball's destination
*/
public double getThrowToRow() {
return throwToRow;
}

/**
* Sets member variable throwToRow to the row of
* where the quarterback is throwing the ball.
* This is the location where the ball is moving towards.
* Rows are zero indexed.
*
* @param throwToRow row of the ball's destination
*/
public void setThrowToRow(int throwToRow) {
this.throwToRow = throwToRow;
}

/**
* Returns the speed of which the quarterback throws the ball.
* This is the speed at which the ball is traveling.
*
* @return speed of the ball's movement
*/
public double getThrowSpeed() {
return throwSpeed;
}

/**
* Sets throwSpeed to the speed at which the quarterback throws the ball.
* This is the speed at which the ball is traveling.
*
* @param throwSpeed speed of the ball's movement
*/
public void setThrowSpeed(int throwSpeed) {
this.throwSpeed = throwSpeed;
}

/**
* Constructor for quarterback for which its member variables can be initialized
to.
*
* @param index player index
* @param startColumn column at which the quarterback starts
* @param startRow row at which the quarterback starts
* @param throwToColumn column at which the ball is thrown to
* @param throwToRow row at which the ball is thrown to
* @param speed speed at which the quarterback is traveling
* @param throwSpeed speed at which the ball is traveling
*/
public Quarterback(int index, double startColumn, double startRow, double
stopColumn,
double stopRow, double throwToColumn, double throwToRow,
double speed,
double throwSpeed){
setPlayerInd(index);
setStartColumn(startColumn);
setStartRow(startRow);
setSpeed(speed);
setCurrColumn(startColumn);
setCurrRow(startRow);
setType('Q');
this.stopColumn = stopColumn;
this.stopRow = stopRow;
this.throwToColumn = throwToColumn;
this.throwToRow = throwToRow;
this.throwSpeed = throwSpeed;
}

/**
* Performs the quarterback's move.
* Quarterback moves towards the final location. After the quarterback has
reached
* the final location, they wait a turn and then throw the ball. After throwing
the ball,
* the quarterback no longer moves. Always return GameResultEnum of ONGOING.
*
* @param qb quarterback
* @param ball ball
* @return result of the game (ONGOING)
*/
GameResultEnum performMove(Player qb, Ball ball){
// upon arrival, set thrown to true to update other players
if(getCurrColumn() == stopColumn && getCurrRow() == stopRow){
ball.setThrown(true);
}
// We performMove based on the speed and vector of travel
double vectorX = stopColumn - getStartColumn();
double vectorY = stopRow - getStartRow();
double length = Math.sqrt(vectorX * vectorX + vectorY * vectorY);
vectorX /= length;
vectorY /= length;
setCurrColumn(getCurrColumn() + vectorX * getSpeed());
setCurrRow(getCurrRow() + vectorY * getSpeed());
double distanceX = getCurrColumn() - getStartColumn();
double distanceY = getCurrRow() - getStartRow();
double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
//we check bounds to ensure overshooting does not occur
if (distance >= length) {
setCurrColumn(getStopColumn());
setCurrRow(getStopRow());
setSpeed(0);
}
//not there yet, continue
return GameResultEnum.ONGOING;
}
}

class Receiver extends Player {


private double intermedColumn;
private double intermedRow;
private double stopColumn;
private double stopRow;
private boolean reachedIntermediate;

/**
* Constructor for receiver for which its member variables can be initialized to.
*
* @param playerInd player index
* @param startCol column at which the receiver starts
* @param startRow row at which the receiver starts
* @param speed speed at which the receiver is traveling
* @param intermedColumn column at which the receiver starts moving towards first
* @param intermedRow row at which the receiver starts moving towards first
* @param stopColumn column at which the receiver starts moving towards
* after reaching the intermediate location
* @param stopRow row at which the receiver starts moving towards
* after reaching the intermediate location
*/
Receiver(int playerInd, double startCol, double startRow, double speed, double
intermedColumn,
double intermedRow, double stopColumn, double stopRow) {
setPlayerInd(playerInd);
setStartColumn(startCol);
setStartRow(startRow);
setCurrRow(startRow);
setCurrColumn(startCol);
setSpeed(speed);
setType('R');
this.intermedColumn = intermedColumn;
this.intermedRow = intermedRow;
this.stopColumn = stopColumn;
this.stopRow = stopRow;
this.reachedIntermediate = false;
}

/**
* Returns the column of the receiver's first destination.
* This is the location where the receiver is moving towards first.
* Columns are zero indexed.
*
* @return column of the receiver's destination
*/
public double getIntermedColumn(){
return intermedColumn;
}

/**
* Sets member variable intermedColumn to the column of
* the receiver's first destination.
* This is the location where the receiver is moving towards first.
* Columns are zero indexed.
*
* @param intermedColumn column of the player's destination
*/
public void setIntermedColumn(double intermedColumn){
this.intermedColumn = intermedColumn;
}

/**
* Returns the row of the receiver's first destination.
* This is the location where the receiver is moving towards first.
* Rows are zero indexed.
*
* @return row of the receiver's destination
*/
public double getIntermedRow(){
return intermedRow;
}

/**
* Sets member variable intermedRow to the row of
* the receiver's first destination.
* This is the location where the receiver is moving towards first.
* Rows are zero indexed.
*
* @param intermedRow row of the receiver's destination
*/
public void setIntermedRow(double intermedRow){
this.intermedRow = intermedRow;
}

/**
* Returns the column of the receiver's destination.
* This is the location where the receiver is moving towards.
* Columns are zero indexed.
*
* @return column of the receiver's destination
*/
public double getStopColumn(){
return stopColumn;
}

/**
* Sets member variable stopColumn to the column of
* the receiver's destination.
* This is the location where the receiver is moving towards.
* Columns are zero indexed.
*
* @param stopColumn column of the receiver's destination
*/
public void setStopColumn(double stopColumn){
this.stopColumn = stopColumn;
}

/**
* Returns the row of the receiver's destination.
* This is the location where the receiver is moving towards.
* Rows are zero indexed.
*
* @return row of the receiver's destination
*/
public double getStopRow(){
return stopRow;
}

/**
* Sets member variable stopRow to the row of
* the receiver's destination.
* This is the location where the receiver is moving towards.
* rows are zero indexed.
*
* @param stopRow row of the player's destination
*/
public void setStopRow(double stopRow){
this.stopRow = stopRow;
}

/**
* Performs the receivers's move.
* Receiver first moves to intermediate position. After reaching the intermediate
* position, the receiver starts moving towards the final location. When the
* receiver has reached their, they wait and no longer move. If the ball is at
the
* same location as receiver, then a GameResultEnum of RECEPTION is returned.
Otherwise
* a GameResultEnum of ONGOING is returned.
*
* @param qb quarterback
* @param ball ball
* @return result of the game (ONGOING or RECEPTION)
*/
GameResultEnum performMove(Player qb, Ball ball) {
//check if we have already reached the intermediate previously
if (reachedIntermediate) {
//check to see if we are at the stop already (in the case of starting there)
if(getCurrColumn()==getStopColumn() && getCurrRow()==getStopRow()){
setSpeed(0);
setCurrRow(stopRow);
setCurrColumn(stopColumn);
}
//otherwise, perform move as all players do
else{
double changeRow = stopRow - getIntermedRow();
double changeColumn = stopColumn - getIntermedColumn();
double magnitude = Math.sqrt(changeRow * changeRow + changeColumn *
changeColumn);
changeRow /= magnitude;
changeColumn /= magnitude;

changeRow *= getSpeed();
changeColumn *= getSpeed();

setCurrRow(getCurrRow() + changeRow);
setCurrColumn(getCurrColumn() + changeColumn);

double distanceX = getCurrColumn() - getIntermedColumn();


double distanceY = getCurrRow() - getIntermedRow();
double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
//check to see if we have overshot the stop
if (distance >= magnitude) {
setSpeed(0);
setCurrRow(stopRow);
setCurrColumn(stopColumn);
}
}
}
//otherwise, traveling from start to intermed
else {
//check to see if we start on intermed
if(getCurrColumn()==getIntermedColumn() && getCurrRow()==getIntermedRow()){
setSpeed(0);
setCurrRow(intermedRow);
setCurrColumn(intermedColumn);
}
//otherwise move as normal
else {
double changeRow = intermedRow - getStartRow();
double changeColumn = intermedColumn - getStartColumn();

double magnitude = Math.sqrt(changeRow * changeRow + changeColumn *


changeColumn);
changeRow /= magnitude;
changeColumn /= magnitude;

changeRow *= getSpeed();
changeColumn *= getSpeed();

setCurrRow(getCurrRow() + changeRow);
setCurrColumn(getCurrColumn() + changeColumn);

double distanceX = getCurrColumn() - getStartColumn();


double distanceY = getCurrRow() - getStartRow();

double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);


//check to see if we have overshot intermed
if (distance >= magnitude) {
reachedIntermediate = true;
setCurrRow(intermedRow);
setCurrColumn(intermedColumn);
}
}
}
//check to see if we have ended our turn on the ball
if (ball.getThrown() && (Math.round(getCurrRow()) ==
Math.round(ball.getCurrRow())) &&
(Math.round(getCurrColumn()) == Math.round(ball.getCurrColumn()))) {
return GameResultEnum.RECEPTION;
}
//otherwise, continue
return GameResultEnum.ONGOING;
}
}

class Defender extends Player{

/**
* Constructor for defender for which its member variables can be initialized to.
*
* @param index player index
* @param startColumn column at which the defender starts
* @param startRow row at which the defender starts
* @param speed speed at which the defender is traveling
*/
public Defender(int index, double startColumn, double startRow, double speed) {
setPlayerInd(index);
setStartColumn(startColumn);
setStartRow(startRow);
setSpeed(speed);
setCurrColumn(startColumn);
setCurrRow(startRow);
setType('D');
}

/**
* Returns the row of the defender's destination.
* This is the location where the defender is moving towards.
* Relevant for quarterback and receiver.
* Not relevant for the defender class.
* Rows are zero indexed.
*
* @return row of the defender's destination
*/
public double getStopRow() {
return 0;
}

/**
* Returns the column of the defender's destination.
* This is the location where the defender is moving towards.
* Relevant for quarterback and receiver.
* Not relevant for the defender.
* Columns are zero indexed.
*
* @return column of the defender's destination
*/
public double getStopColumn() {
return 0;
}

/**
* Performs the defender's move.
* Defender moves toward quarterback. After the quarterback has thrown the ball,
the
* quarterback starts moving towards the location where the ball is thrown to.
* Returns GameResultEnum status SACK when the quarterback hasn't thrown ball,
* and the defender is at the quarterback position. Returns GameResultEnum status
* INTERCEPTION when the quarterback has thrown ball, and the defender is at the
* ball's position. Otherwise, performMove returns a GameResultEnum status of
* ONGOING
*
* @param qb quarterback
* @param ball ball
* @return result of the game (ONGOING, SACK, INTERCEPTION)
*/
GameResultEnum performMove(Player qb, Ball ball) {
//check to see if ball has been thrown yet
if (ball.getThrown()) {
//if so, move towards ball
double vectorX = ball.getStopColumn() - getCurrColumn();
double vectorY = ball.getStopRow() - getCurrRow();
double length = Math.sqrt(vectorX * vectorX + vectorY * vectorY);
//check to see if we are in range of our stop
if ((length <= getSpeed()) && (ball.getThrown())) {
setCurrColumn(ball.getStopColumn());
setCurrRow(ball.getStopRow());
setSpeed(0);
}
//otherwise, move as normal
else {
vectorX /= length;
vectorY /= length;
setCurrColumn(getCurrColumn() + vectorX * getSpeed());
setCurrRow(getCurrRow() + vectorY * getSpeed());
}
}
//otherwise, we are moving to qb
else {
double vectorX = qb.getCurrColumn() - getCurrColumn();
double vectorY = qb.getCurrRow() - getCurrRow();
double length = Math.sqrt(vectorX * vectorX + vectorY * vectorY);
//check to see if we are in range of the qb
if ((length <= getSpeed()) && (!ball.getThrown())){
setCurrColumn(qb.getCurrColumn());
setCurrRow(ball.getCurrRow());
setSpeed(0);
}
//otherewise move as normal
else {
vectorX /= length;
vectorY /= length;
setCurrColumn(getCurrColumn() + vectorX * getSpeed());
setCurrRow(getCurrRow() + vectorY * getSpeed());
}
}
//check to see if we end up on qb and ball is not yet thrown
if ((!ball.getThrown()) && (Math.round(getCurrRow()) ==
Math.round(qb.getCurrRow())) &&
(Math.round(getCurrColumn()) == Math.round(qb.getCurrColumn()))) {
return GameResultEnum.SACK;
}
//check to see if we are at same position as ball and the ball has been thrown
if (ball.getThrown() && (Math.round(getCurrRow()) ==
Math.round(ball.getCurrRow())) &&
(Math.round(getCurrColumn()) == Math.round(ball.getCurrColumn()))) {
return GameResultEnum.INTERCEPTION;
}
//otherwise, continue
return GameResultEnum.ONGOING;
}
}

class Ball {
private double startColumn;
private double startRow;
private double currColumn;
private double currRow;
private double stopColumn;
private double stopRow;
private double speed;
private boolean thrown;

/**
* Returns whether the ball has been thrown.
*
* @return status of the ball (thrown or not thrown)
*/
public boolean getThrown() {
return thrown;
}

/**
* Sets member variable thrown to whether the ball has been thrown.
*
* @param thrown status of the ball (thrown or not thrown)
*/
public void setThrown(boolean thrown) {
this.thrown = thrown;
}

/**
* Returns the column of the ball's initial position.
* This is the location where the ball starts.
* Columns are zero indexed.
*
* @return column of the ball's starting location
*/
public double getStartColumn(){
return startColumn;
}

/**
* Sets member variable startColumn to the column of the ball's starting
location.
* This is the location where the ball initially starts.
* Columns are zero indexed.
*
* @param startColumn column of ball's starting position
*/
public void setStartColumn(double startColumn){
this.startColumn = startColumn;
}

/**
* Returns the row of the ball's initial position.
* This is the location where the ball starts.
* Rows are zero indexed.
*
* @return row of the ball's starting location
*/
public double getStartRow(){
return startRow;
}

/**
* Sets member variable startRow to the row of the ball's starting location.
* This is the location where the ball initially starts.
* Rows are zero indexed.
*
* @param startRow row of ball's starting position
*/
public void setStartRow(double startRow){
this.startRow = startRow;
}

/**
* Returns the column of the ball's current position.
* This is the location where the ball is currently at.
* Columns are zero indexed.
*
* @return column of the ball's current location
*/
public double getCurrColumn(){
return currColumn;
}

/**
* Sets member variable currColumn to the column of the ball's current location.
* This is the location where the ball currently is at.
* Columns are zero indexed.
*
* @param curColumn column of ball's current position
*/
public void setCurrColumn(double curColumn){
this.currColumn = currColumn;
}

/**
* Returns the row of the ball's current position.
* This is the location where the ball is currently at.
* Rows are zero indexed.
*
* @return row of the ball's current location
*/
public double getCurrRow(){
return currRow;
}

/**
* Sets member variable currRow to the row of the ball's current location.
* This is the location where the ball currently is at.
* Rows are zero indexed.
*
* @param curRow row of ball's current position
*/
public void setCurrRow(double curRow){
this.currRow = currRow;
}

/**
* Returns the column of the ball's destination (where its thrown).
* This is the location where the ball is moving towards.
* Columns are zero indexed.
*
* @return column of the ball's destination
*/
public double getStopColumn(){
return stopColumn;
}

/**
* Sets member variable stopColumn to the column of
* the ball's destination.
* This is the location where the ball is moving towards.
* Columns are zero indexed.
*
* @param stopColumn column of the ball's destination
*/
public void setStopColumn(double stopColumn){
this.stopColumn = stopColumn;
}

/**
* Returns the row of the ball's ending position.
* This is the location of the ball's destination.
* Rows are zero indexed.
*
* @return row of the ball's destination
*/
public double getStopRow(){
return stopRow;
}

/**
* Sets member variable stopRow to the row of
* the ball's destination.
* This is the location where the ball is moving towards.
* rows are zero indexed.
*
* @param stopRow row of the ball's destination
*/
public void setStopRow(double stopRow){
this.stopRow = stopRow;
}

/**
* Returns the speed at which the ball travels.
*
* @return returns speed at which the ball travels.
*/
public double getSpeed(){
return speed;
}

/**
* Sets speed to the speed at which the ball travels.
*
* @param speed speed at which the ball travels.
*/
public void setSpeed(double speed){
this.speed = speed;
}

/**
* Constructor for ball so that its member varibles can be initialized
*
* @param startColumn column at which the ball starts
* @param startRow row at which the ball starts
* @param currColumn column at which the ball is currently at
* @param currRow row at which the ball starts
* @param stopColumn column at which the ball moves toward
* @param stopRow row at which the ball moves toward
* @param speed speed at which the ball travels
*/
Ball(double startColumn, double startRow, double currColumn, double currRow,
double stopColumn, double stopRow,
double speed) {
this.startColumn = startColumn;
this.startRow = startRow;
this.currColumn = currColumn;
this.currRow = currRow;
this.stopColumn = stopColumn;
this.stopRow = stopRow;
this.speed = speed;
this.thrown = false;
}

public GameResultEnum performMove(){


//move as all other players move
double vectorX = stopColumn - startColumn;
double vectorY = stopRow - startRow;
double length = Math.sqrt(vectorX*vectorX + vectorY*vectorY);
vectorX/=length;
vectorY/=length;
currColumn = currColumn+vectorX*speed;
currRow = currRow+vectorY*speed;
//check to see if we overshot our stop
//if so, jump back to stop
//set speed to 0
if((stopRow >= startRow)&&(currRow >= stopRow)){
currRow = stopRow;
currColumn = stopColumn;
speed = 0;
}
else if((stopRow <= startRow)&&(currRow <= stopRow)){
currRow = stopRow;
currColumn = stopColumn;
speed = 0;
}
//ongoing
return GameResultEnum.ONGOING;
}
}

public class PlayingField {


private Player qb;
private Ball ball;
private java.util.ArrayList<Player> players;
private int fieldWidth;
private int fieldHeight;
private int numPlayers;

/**
* Creates a PlayingField object.
*
* @param numPlayers the number of players in the game
* @param fieldWidth the width of the playing field
* @param fieldHeight the height of the playing field
*/
PlayingField(int numPlayers,
int fieldWidth,
int fieldHeight) {
this.numPlayers = numPlayers;
this.fieldHeight = fieldHeight;
this.fieldWidth = fieldWidth;
players = new ArrayList<Player>();
for(int i = 0; i < numPlayers; i++) {
players.add(null);
}
}

/**
* Adds a quarterback to the field.
*
* @param playerIndex 0-based index that determines when the player
* will move in each turn
* @param startColumn the column of the player's starting position
* @param startRow the row of the player's starting position
* @param stopColumn the column of the player's stopping position
* @param stopRow the row of the player's stopping position
* @param throwToColumn the column of the location to throw the ball
* @param throwToRow the row of the location to throw the ball
* @param speed the speed at which the player moves
* @param throwSpeed the speed at which the ball moves once it is
* thrown
*/
void addQuarterback(int playerIndex, double startColumn, double startRow, double
stopColumn,
double stopRow, double throwToColumn, double throwToRow,
double speed,
double throwSpeed) {
qb = new Quarterback(playerIndex, startColumn, startRow, stopColumn, stopRow,
throwToColumn, throwToRow, speed, throwSpeed);
ball = new Ball(stopColumn, stopRow, stopColumn, stopRow, throwToColumn,
throwToRow, throwSpeed);
players.set(playerIndex, qb);
}

/**
* Adds a receiver to the field.
*
* @param playerIndex 0-based index that determines when the player
* will move in each turn
* @param startColumn the column of the player's starting position
* @param startRow the row of the player's starting position
* @param intermediateColumn the column of the player's
* intermediate destination
* @param intermediateRow the row of the player's intermediate
* destination
* @param stopColumn the column of the player's final destination
* @param stopRow the row of the player's final destination
* @param speed the speed at which the player moves
*/
void addReceiver(int playerIndex,
double startColumn,
double startRow,
double intermediateColumn,
double intermediateRow,
double stopColumn,
double stopRow,
double speed) {
// your code here
Player receiver = new Receiver(playerIndex, startColumn, startRow, speed,
intermediateColumn,
intermediateRow, stopColumn, stopRow);
players.set(playerIndex, receiver);
}

/**
* Adds a defender to the field.
*
* @param playerIndex 0-based index that determines when the player
* will move in each turn
* @param startColumn the column of the player's starting position
* @param startRow the row of the player's starting position
* @param speed the speed at which the player moves
*/
void addDefender(int playerIndex,
double startColumn,
double startRow,
double speed) {
// your code here
Player defender = new Defender(playerIndex, startColumn, startRow, speed);
players.set(playerIndex, defender);
}

/**
* Determines whether or not the game setup is valid.
*
* A valid game has a single quarterback and a player added to each
* expected index.
*
* @return whether or not the game is valid
*/
boolean checkIsValidGame() {
if(numPlayers!=players.size()) return false;
int counter = 0;
for(int i = 0; i < numPlayers; i++){
if(players.get(i) == null) return false;
if(players.get(i).getType()=='Q') counter++;
}
if(counter != 1) return false;
return true;
}

private void printField() {


System.out.print(" ");
for(int i = 0; i <= fieldWidth; i++) {
System.out.print(i % 10);
}
System.out.println();

ArrayList<ArrayList<Character>> field = new ArrayList<ArrayList<Character>>();


for(int i = 0; i <= fieldHeight; i++) {
field.add(new ArrayList<Character>());
}
for(int i = 0; i <= fieldHeight; i++) {
for(int j = 0; j <= fieldWidth; j++) {
field.get(i).add('-');
}
}

if(ball.getThrown()) {
Long row = Math.round(ball.getCurrRow());
Long col = Math.round(ball.getCurrColumn());
field.get(row.intValue()).set(col.intValue(), 'B');
}

for(int i = 0; i < numPlayers; i++) {


Long row = Math.round(players.get(i).getCurrRow());
Long col = Math.round(players.get(i).getCurrColumn());
field.get(row.intValue()).set(col.intValue(), players.get(i).getType());
}

for(int i = 0; i <= fieldHeight; i++) {


System.out.print(i % 10);
for(int j = 0; j <= fieldWidth; j++) {
System.out.print(field.get(i).get(j));
}
System.out.println();
}
}
/**
* Runs the game simulation to completion, returning the result.
*
* Simulates turns until the game ends. A turn involves the ball
* moving first, if it has been thrown, and then each player in turn
* by index. If a game-ending event occurs during a turn (i.e. a
* sack, interception, reception, or incompletion), returns with the
* corresponding enum value. Otherwise, continues to simulate turns
* until the game ends with one of those events.
*
* Note: a turn may not complete - if a game-ending event occurs,
* the game ends immediately without the remaining players moving.
*
* @return enum value corresponding to the result of the game
*/
GameResultEnum playBall() {
GameResultEnum status = GameResultEnum.ONGOING;
while(status == GameResultEnum.ONGOING) {
printField();
if(ball.getThrown()) {
ball.performMove();
}

for(int i = 0; i < numPlayers; i++) {


status = players.get(i).performMove(qb, ball);
if(status != GameResultEnum.ONGOING) {
return status;
}
}

if((ball.getCurrColumn() == ball.getStopColumn())&&
(ball.getCurrRow() == ball.getStopRow())) {
return GameResultEnum.INCOMPLETION;
}
}
return status; // replace with your solution
}

You might also like