Class Game

java.lang.Object
  extended by Game

public class Game
extends java.lang.Object

This class represents the gorilla game. It keeps track of buildings, players and the banana. This class handles all the rules of the game.

Field Summary
static int AIMING
          Indicates that a gorilla is aiming.
static int BANANA_SPLATTING
          Indicates that a banana hit a gorilla or a building and the shock wave is now expanding.
static int BANANA_THROWN
          Indicates that a gorilla threw the banana and the banana has not hit a gorilla or a building yet.
 double GRAVITY
          The gravity constant used in the calculations of a banana's movement.
 
Constructor Summary
Game(int numRounds, int numOvertimeRounds, java.lang.String[] playerNames, int height, int width, double gravity)
          Makes a new game with the characteristics specified by the parameter values.
Game(int numRounds, int numOvertimeRounds, java.lang.String[] playerNames, int height, int width, double gravity, int ticksPerTurn)
          2nd constructor - Makes a new game with the characteristics specified by the parameter values including ticks per turn.
 
Method Summary
static boolean circleIntersectsRectangle(double x, double y, double radius, int left, int bottom, int width, int height)
          This method checks whether a circle and a rectangle intersect.
 Banana getBanana()
          Returns a reference to the banana
 java.util.Iterator<Building> getBuildingIterator()
          Returns an iterator that lets one go through the list of all buildings.
 Gorilla getCurrentGorilla()
          Returns a reference to the gorilla whose turn it is.
 java.util.Iterator<Gorilla> getGorillaIterator()
          Returns an iterator that lets one go through the list of all gorillas.
 int getHeight()
          Returns the height of the game area
 int getNumGorillas()
          Returns the number of gorillas playing the game, including those that were hit or are not participating in overtime.
 int getNumOvertimeRounds()
          Returns how many rounds an overtime consists of.
 int getNumRounds()
          Returns the number of rounds.
 int getOvertimeNumber()
          Returns the number of overtimes.
 int getRound()
          Returns the number of the current round.
 int getTicksLeftThisTurn()
          Returns how many ticks the gorilla whose turn it is right now has left to aim before it loses its turn
 int getTicksPerTurn()
          Returns how many ticks a gorilla can spend aiming before it loses its turn.
 int getTurn()
          Returns whose turn it is.
 int getTurnState()
          Returns which part of the current turn the game is in
 int getWidth()
          Returns the width of the game area
 double getWindSpeed()
          Returns the wind speed in the game area
 java.util.ArrayList<java.lang.Integer> getWinningGorillas()
          Returns the numbers of gorillas that have the highest score.
 boolean isGameOver()
          Returns whether the game is over
 boolean isItOvertime()
          Returns whether it's overtime
 void splatMidAir()
          If a banana is flying and its x coordinate and the x coordinate of the middle of the gorilla who tossed it differ by at least half the gorilla's width plus the banana's shock wave radius, the banana splats, stops moving, and its shock wave starts expanding.
 void startNewGame()
          Starts a new game.
 void tick(double time)
          You will need to modify this method for extra credit This method runs one tick of the game.
 void tossBanana(double angle, double velocity)
          Makes the gorilla whose turn it is toss a banana, but only if the game is in the aiming state.
 java.lang.String toString()
          Returns information about the game as a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

GRAVITY

public final double GRAVITY
The gravity constant used in the calculations of a banana's movement.


AIMING

public static final int AIMING
Indicates that a gorilla is aiming.

BANANA_THROWN

public static final int BANANA_THROWN
Inidcates that a gorilla threw the banana and the banana has not hit a gorilla or a building yet.

BANANA_SPLATTING

public static final int BANANA_SPLATTING
Inidcates that a banana hit a gorilla or a building and the shock wave is now expanding.
Constructor Detail

Game

public Game(int numRounds,
              int numOvertimeRounds,
              java.lang.String[] playerNames,
              int height,
              int width,
              double gravity)
Makes a new game with the characteristics specified by the parameter values. This constructor should also start the first round of the game.

Parameters:
numRounds - The number of rounds this game will run for.
numOvertimeRounds - The number of rounds each overtime will consist of.
playerNames - The names of the players. This array will have between 2 and 4 elements in it.
height - The height of the game area
width - The width of the game area
gravity - Gravitational constant for the purpose of computing how the banana flies.

Game

public Game(int numRounds,
              int numOvertimeRounds,
              java.lang.String[] playerNames,
              int height,
              int width,
              double gravity,
              int ticksPerTurn)
For turn limit: - Makes a new game with the characteristics specified by the parameter values including restricting how long a gorilla can aim. This constructor should also start the first round of the game.

Parameters:
numRounds - The number of rounds this game will run for.
numOvertimeRounds - The number of rounds each overtime will consist of.
playerNames - The names of the players. This array will have between 2 and 4 elements in it.
height - The height of the game area
width - The width of the game area
gravity - Gravitational constant for the purpose of computing how the banana flies.
ticksPerTurn - How many ticks a gorilla can spend aiming before it loses its turn. A value of 0 means that there is no limit.
Method Detail

getWidth

public int getWidth()
Returns the width of the game area

Returns:
the width of the game area

getHeight

public int getHeight()
Returns the height of the game area

Returns:
the height of the game area

getNumGorillas

public int getNumGorillas()
Returns the number of gorillas playing the game, including those that were hit or are not participating in overtime.

Returns:
the number of gorillas playing the game

getRound

public int getRound()
Returns the number of the current round. The value should be an integer between 1 and the number of rounds (or number of overtime rounds if it is overtime).

Returns:
the number of the current round

getNumRounds

public int getNumRounds()
Returns the number of rounds.

Returns:
the number of rounds

isItOvertime

public boolean isItOvertime()
Returns whether it's overtime

Returns:
true if it's overtime, false otherwise

getOvertimeNumber

public int getOvertimeNumber()
Returns the number of overtimes.

Returns:
0 if it's not overtime, otherwise the overtime number

getNumOvertimeRounds

public int getNumOvertimeRounds()
Returns how many rounds an overtime consists of.

Returns:
how many rounds an overtime consists of

getTurn

public int getTurn()
Returns whose turn it is.

Returns:
a number between 0 and the number of players minus one.

getTurnState

public int getTurnState()
Returns which part of the current turn the game is in

Returns:
one of Game.AIMING, Game.BANANA_THROWN and BANANA_SPLATTING.

isGameOver

public boolean isGameOver()
Returns whether the game is over

Returns:
true if the game is over, false otherwise

getBuildingIterator

public java.util.Iterator<Building> getBuildingIterator()
Returns an iterator that lets one go through the list of all buildings.

Returns:
an iterator into the list of all buildings

getGorillaIterator

public java.util.Iterator<Gorilla> getGorillaIterator()
Returns an iterator that lets one go through the list of all gorillas.

Returns:
an iterator into the list of all gorillas

getBanana

public Banana getBanana()
Returns a reference to the banana

Returns:
null if a player is aiming, otherwise a reference to the banana that's flying or whose shock wave is spreading through the game area

getCurrentGorilla

public Gorilla getCurrentGorilla()
Returns a reference to the gorilla whose turn it is.

Returns:
a reference to the gorilla whose turn it is

getWindSpeed

public double getWindSpeed()
Returns the wind speed in the game area

Returns:
the wind speed in the game area

getWinningGorillas

public java.util.ArrayList<java.lang.Integer> getWinningGorillas()
Returns the numbers of gorillas that have the highest score. For example, it should return an ArrayList whose two elements are 0 and 3 if there are four gorillas whose scores are 7, 3, -2 and 7, respectively.

Returns:
an ArrayList with numbers of the players sharing the highest score

startNewGame

public void startNewGame()
Starts a new game.


tossBanana

public void tossBanana(double angle,
                       double velocity)
Makes the gorilla whose turn it is toss a banana, but only if the game is in the aiming state.

Parameters:
angle - The angle at which the gorilla should toss the banana. The value is in radians and is between -Math.PI/2 and Math.PI/2
velocity - The velocity at which the gorilla should toss the banana. The value is a non-negative floating point number.

splatMidAir

public void splatMidAir()
If a banana is flying and its x coordinate and the x coordinate of the middle of the gorilla who tossed it differ by at least half the gorilla's width plus the banana's shock wave radius, the banana splats, stops moving, and its shock wave starts expanding.


tick

public void tick(double time)
Runs a tick of the game.

Parameters:
time - The tick length

toString

public java.lang.String toString()
Returns information about the game as a string. This will be useful for debugging.

Overrides:
toString in class java.lang.Object

getTicksPerTurn

public int getTicksPerTurn()
Returns how many ticks a gorilla can spend aiming before it loses its turn.

Returns:
A positive integer indicating the number of ticks or zero if there is no limit on the number of ticks a gorilla can spend aiming.

getTicksLeftThisTurn

public int getTicksLeftThisTurn()
Returns how many ticks the gorilla whose turn it is right now has left to aim before it loses its turn

Returns:
A non-negative integer. A returned value of zero may mean either that the gorilla ran out of time or that there is no limit. You should consult the output of getTicksPerTurn() to find which one it is.

circleIntersectsRectangle

public static boolean circleIntersectsRectangle(double x,
                                                double y,
                                                double radius,
                                                int left,
                                                int bottom,
                                                int width,
                                                int height)
This method checks whether a circle and a rectangle intersect. This is useful for detecting whether a banana hit a gorilla or a building

Parameters:
x - The x coordinate of the center of the circle.
y - The y coordinate of the center of the circle.
radius - The radius of the circle.
left - The x coordinate of the bottom left corner of the rectangle
bottom - The y coordinate of the bottom left corner of the rectangle
width - The height of the rectangle
height - The width of the rectangle
Returns:
true if the circle and the rectangle intersect, false otherwise