Class CarGame
java.lang.Object
CarGame
public class CarGame
- extends java.lang.Object
This class represents the car game. The car game maintains information
about the highway where the game is happening as well as about the
player, the player's score, and how much time is left in the game.
It also defines a random number generator that is
used throughout the game and keeps track of the current tick number.
- Author:
- Dalibor Zeleny (dalibor@cs.wisc.edu)
static double |
DROP_CARS_DISTANCE
If the distance of any car is more than this constant's value from the
player's car, it will get removed from the highway. |
static long |
TICKS_PER_SECOND
The number of ticks in each second. |
Constructor Summary |
CarGame(int numLanes,
double tickLength,
java.lang.String playerName,
int ticksBeforeStart)
Creates a new CarGame object with the specified properties |
Method Summary |
private void |
addRandomCar()
Add one random car to the highway. |
private void |
addRandomCoin()
Add one random coin to the highway. |
private void |
checkForCrashes()
If there are any cars that crashed into each other (that is, they overlap),
this method should remove them from the highway. |
Highway |
getHighway()
Returns a reference to the highway where the game is happening. |
Driver |
getPlayer()
Returns a reference to an object that represents the player. |
java.util.Random |
getRNG()
Returns a reference to the random number generator used by this game. |
long |
getScore()
Returns the player's score. |
long |
getTickNumber()
Returns the current tick number. |
double |
getTimeLeft()
Returns how many seconds are remaining in the game. |
boolean |
isGameOver()
Indicates whether the game is over. |
private void |
moveEverything()
Every car and every coin should now move (tick) for
the amount of time specified by the game's tick length. |
private void |
notifyDrivers()
For each car on the highway, tell its driver about nearby cars by calling
the makeDecision() method. |
private void |
pickUpCoins()
For each coin on the highway, check if it overlaps
with the player's car. |
private void |
processKeysPressed()
This method processes key presses made before this tick. |
private void |
removeDistantCars()
To speed up the game, all cars whose distance from the player's car is
more than DROP_CARS_DISTANCE should be removed from the highway. |
private void |
removeExpiredCoins()
Remove all expired coins from the highway so that
the player cannot pick the up and get points/time for them. |
void |
reset()
Restarts the game. |
void |
setAddCarProbability(double addCarProbability)
Updates the probability with which another car should be added in each
tick using the parameter as the new value. |
void |
setAddCoinProbability(double addObjectProbability)
Updates the probability with which another object should be added in each
tick using the parameter as the new value. |
void |
tick()
Runs one tick of the game. |
java.lang.String |
toString()
Returns a string description of the game's state. |
void |
updateKeystrokes(boolean[] keystrokes)
Updates which keys were pressed before this tick. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
r
private java.util.Random r
keysPressed
private boolean[] keysPressed
TICKS_PER_SECOND
public static long TICKS_PER_SECOND
- The number of ticks in each second.
DROP_CARS_DISTANCE
public static double DROP_CARS_DISTANCE
- If the distance of any car is more than this constant's value from the
player's car, it will get removed from the highway.
addCarProbability
private double addCarProbability
addCoinProbability
private double addCoinProbability
tickLength
private double tickLength
highway
private Highway highway
tickNumber
private long tickNumber
ticksBeforeStart
private long ticksBeforeStart
player
private Driver player
playerLaneNumber
private int playerLaneNumber
timeLeft
private double timeLeft
score
private long score
CarGame
public CarGame(int numLanes,
double tickLength,
java.lang.String playerName,
int ticksBeforeStart)
- Creates a new CarGame object with the specified properties
- Parameters:
numLanes
- Number of lanes in the highwaytickLength
- Length of a tick. Recommended: 0.01playerName
- Name of the playerticksBeforeStart
- Number of ticks before the start of
the game. This allows other parts of the program to do some setup
before the game actually starts and also to display a nice countdown.
getRNG
public java.util.Random getRNG()
- Returns a reference to the random number generator used by this game.
- Returns:
- a reference to the random number generator used by this game.
updateKeystrokes
public void updateKeystrokes(boolean[] keystrokes)
- Updates which keys were pressed before this tick. The keystrokes will
be acknowledged by the game in its next tick. The array indices
correspond to the constants KEY_UP, KEY_RIGHT,
KEY_DOWN and KEY_LEFT in CarGameController.
You should not call this method from anywhere in your code.
- Parameters:
keystrokes
- An array indicating which keys were pressed. An array
element is true if and only if its corresponding key was pressed.
getPlayer
public Driver getPlayer()
- Returns a reference to an object that represents the player.
- Returns:
- a reference to an object that represents the player.
getScore
public long getScore()
- Returns the player's score.
- Returns:
- the player's score.
getTimeLeft
public double getTimeLeft()
- Returns how many seconds are remaining in the game.
- Returns:
- how many seconds are remaining in the game.
getTickNumber
public long getTickNumber()
- Returns the current tick number.
- Returns:
- the current tick number.
setAddCarProbability
public void setAddCarProbability(double addCarProbability)
- Updates the probability with which another car should be added in each
tick using the parameter as the new value.
- Parameters:
addCarProbability
- A positive double value that is less than 1.
setAddCoinProbability
public void setAddCoinProbability(double addObjectProbability)
- Updates the probability with which another object should be added in each
tick using the parameter as the new value.
- Parameters:
addObjectProbability
- A positive double value that is less than 1.
tick
public void tick()
- Runs one tick of the game. You should not modify this method's code.
getHighway
public Highway getHighway()
- Returns a reference to the highway where the game is happening.
- Returns:
- a reference to the highway where the game is happening.
isGameOver
public boolean isGameOver()
- Indicates whether the game is over. The game is over when the player
crashed his car or when time runs out.
- Returns:
- whether the game is over.
reset
public void reset()
- Restarts the game. This should restore the highway to the state it was
in right after the constructor finished.
toString
public java.lang.String toString()
- Returns a string description of the game's state. Here is an example of
what the output could look like:
Tick 962, score: 2172, time left: 53
Lane 0: 5 car(s), 1 coin(s)
CAR 0: ID: 41, distance: 1098.5299999999847, velocity: 33.0, length: 4.0; driven by Bob who prefers 33.0
CAR 1: ID: 35, distance: 1042.9299999999862, velocity: 43.0, length: 3.75; driven by Bob who prefers 43.0
CAR 2: ID: 31, distance: 1008.4099999999748, velocity: 56.0, length: 3.75; driven by Bob who prefers 56.0
CAR 3: ID: 4, distance: 845.9199999999934, velocity: 66.0, length: 3.5; driven by Bob who prefers 66.0
CAR 4: ID: 2, distance: 563.2399999999914, velocity: 52.0, length: 5.75; driven by Bob who prefers 52.0
COIN 0: ID: 3, at distance: 955.850000000004 with score bonus 796(-12 decrease), time bonus 6.300000000000006(-0.1 decrease) and decrease period 5. It has lived 88 out of 400 ticks
Lane 1: 9 car(s), 0 coin(s)
CAR 0: ID: 40, distance: 1277.4299999999923, velocity: 78.0, length: 4.5; driven by Bob who prefers 78.0
CAR 1: ID: 42, distance: 1184.5500000000104, velocity: 70.0, length: 4.0; driven by Bob who prefers 70.0
CAR 2: ID: 36, distance: 1126.8599999999908, velocity: 53.0, length: 4.5; driven by Bob who prefers 53.0
CAR 3: ID: 34, distance: 1119.609999999992, velocity: 52.0, length: 3.25; driven by Bob who prefers 52.0
CAR 4: ID: 39, distance: 993.3899999999787, velocity: 24.0, length: 4.5; driven by Bob who prefers 24.0
CAR 5: ID: 1, distance: 926.25, velocity: 100.0, length: 5.0; driven by Dalibor who prefers 10.0
CAR 6: ID: 15, distance: 906.5300000000148, velocity: 48.0, length: 4.75; driven by Bob who prefers 48.0
CAR 7: ID: 11, distance: 787.7300000000116, velocity: 36.0, length: 4.75; driven by Bob who prefers 36.0
CAR 8: ID: 3, distance: 768.1599999999852, velocity: 68.0, length: 3.0; driven by Bob who prefers 68.0
Lane 2: 9 car(s), 0 coin(s)
CAR 0: ID: 49, distance: 1351.3499999999992, velocity: 30.0, length: 4.25; driven by Bob who prefers 30.0
CAR 1: ID: 46, distance: 1246.3000000000086, velocity: 43.0, length: 4.5; driven by Bob who prefers 43.0
CAR 2: ID: 32, distance: 1102.5899999999992, velocity: 66.0, length: 5.25; driven by Bob who prefers 66.0
CAR 3: ID: 12, distance: 1082.0099999999923, velocity: 76.0, length: 3.75; driven by Bob who prefers 76.0
CAR 4: ID: 24, distance: 1017.5600000000327, velocity: 69.0, length: 5.5; driven by Bob who prefers 69.0
CAR 5: ID: 8, distance: 999.5899999999751, velocity: 78.0, length: 5.0; driven by Bob who prefers 78.0
CAR 6: ID: 7, distance: 720.7999999999781, velocity: 40.0, length: 5.0; driven by Bob who prefers 40.0
CAR 7: ID: 13, distance: 683.0499999999809, velocity: 40.0, length: 5.5; driven by Bob who prefers 40.0
CAR 8: ID: 5, distance: 618.3800000000087, velocity: 49.0, length: 4.25; driven by Bob who prefers 49.0
Lane 3: 10 car(s), 0 coin(s)
CAR 0: ID: 47, distance: 1275.75, velocity: 75.0, length: 3.25; driven by Bob who prefers 75.0
CAR 1: ID: 28, distance: 1205.6399999999908, velocity: 77.0, length: 3.5; driven by Bob who prefers 77.0
CAR 2: ID: 26, distance: 1139.090000000015, velocity: 72.0, length: 5.0; driven by Bob who prefers 72.0
CAR 3: ID: 9, distance: 1050.9299999999841, velocity: 65.0, length: 3.5; driven by Bob who prefers 65.0
CAR 4: ID: 25, distance: 1037.4000000000206, velocity: 71.0, length: 4.75; driven by Bob who prefers 71.0
CAR 5: ID: 23, distance: 977.1699999999805, velocity: 66.0, length: 4.0; driven by Bob who prefers 66.0
CAR 6: ID: 21, distance: 954.5500000000054, velocity: 59.0, length: 4.75; driven by Bob who prefers 59.0
CAR 7: ID: 14, distance: 930.2699999999837, velocity: 78.0, length: 3.75; driven by Bob who prefers 78.0
CAR 8: ID: 20, distance: 890.2200000000081, velocity: 47.0, length: 4.25; driven by Bob who prefers 47.0
CAR 9: ID: 17, distance: 800.0099999999715, velocity: 56.0, length: 5.0; driven by Bob who prefers 56.0
Lane 4: 17 car(s), 0 coin(s)
CAR 0: ID: 50, distance: 1344.25, velocity: 25.0, length: 4.0; driven by Bob who prefers 25.0
CAR 1: ID: 48, distance: 1270.1099999999974, velocity: 36.0, length: 4.5; driven by Bob who prefers 36.0
CAR 2: ID: 44, distance: 1221.8199999999824, velocity: 37.0, length: 5.75; driven by Bob who prefers 37.0
CAR 3: ID: 45, distance: 1188.4500000000062, velocity: 20.0, length: 4.0; driven by Bob who prefers 20.0
CAR 4: ID: 43, distance: 1178.0500000000147, velocity: 41.0, length: 3.75; driven by Bob who prefers 41.0
CAR 5: ID: 37, distance: 1090.7500000000048, velocity: 42.0, length: 3.25; driven by Bob who prefers 42.0
CAR 6: ID: 38, distance: 1000.0899999999892, velocity: 29.0, length: 4.5; driven by Bob who prefers 29.0
CAR 7: ID: 30, distance: 980.469999999988, velocity: 22.0, length: 3.5; driven by Bob who prefers 22.0
CAR 8: ID: 22, distance: 974.7300000000337, velocity: 44.0, length: 4.5; driven by Bob who prefers 44.0
CAR 9: ID: 27, distance: 941.9799999999896, velocity: 27.0, length: 5.5; driven by Bob who prefers 27.0
CAR 10: ID: 18, distance: 929.839999999977, velocity: 67.0, length: 3.0; driven by Bob who prefers 67.0
CAR 11: ID: 33, distance: 894.4700000000081, velocity: 22.0, length: 4.5; driven by Bob who prefers 22.0
CAR 12: ID: 29, distance: 847.9400000000202, velocity: 33.0, length: 4.0; driven by Bob who prefers 33.0
CAR 13: ID: 19, distance: 824.6199999999726, velocity: 31.0, length: 4.0; driven by Bob who prefers 31.0
CAR 14: ID: 16, distance: 682.4900000000073, velocity: 24.0, length: 3.75; driven by Bob who prefers 24.0
CAR 15: ID: 10, distance: 611.5900000000147, velocity: 22.0, length: 3.0; driven by Bob who prefers 22.0
CAR 16: ID: 6, distance: 562.6199999999928, velocity: 51.0, length: 5.75; driven by Bob who prefers 51.0
- Overrides:
toString
in class java.lang.Object
init
private void init()
- Sets up the initial state of the game (empties the highway,
resets the number of ticks to ticksBeforeStart, puts a new player
in there, and so on)
notifyDrivers
private void notifyDrivers()
- For each car on the highway, tell its driver about nearby cars by calling
the makeDecision() method. Once the makeDecision()
method returns, put the car in the correct lane if the driver chose to
switch lanes. The makeDecision() method should not get
called twice for any driver, which you can guarantee by properly using
the canAct() and setCanAct() methods.
pickUpCoins
private void pickUpCoins()
- For each coin on the highway, check if it overlaps
with the player's car. If it does, update the player's score and
time left and remove that coin from the highway.
processKeysPressed
private void processKeysPressed()
- This method processes key presses made before this tick. They key
presses are located in the instance variable keysPressed which we
provided in the code skeleton. Make use of that array to change the
velocity of the player's car and the lane that car is in. Refer to
constants defined in CarGameController to find out what
the individual array elements mean. For example,
this.keysPressed[CarGameController.KEY_UP] tells us whether
the up key was pressed.
The following should happen:
- If the up key is pressed, the player's car should move one
lane to the left, even if this causes an accident. Nothing should
happen if the player's car is in the leftmost lane already.
- If the right key is pressed, the player's car velocity should
increase by 1.
- If the down key is pressed, the player's car should move one
lane to the right, even if this causes an accident. Nothing should
happen if the player's car is in the rightmost lane already.
- If the left key is pressed, the player's car velocity should
decrease by 1.
Once this method returns, the keysPressed array should indicate
that no keys were pressed.
moveEverything
private void moveEverything()
- Every car and every coin should now move (tick) for
the amount of time specified by the game's tick length.
removeExpiredCoins
private void removeExpiredCoins()
- Remove all expired coins from the highway so that
the player cannot pick the up and get points/time for them.
removeDistantCars
private void removeDistantCars()
- To speed up the game, all cars whose distance from the player's car is
more than DROP_CARS_DISTANCE should be removed from the highway.
addRandomCar
private void addRandomCar()
- Add one random car to the highway. Place it in a random lane at a random
distance away from the player's car and give it a new driver
who should have a random preferred velocity.
Placing this car on the highway should not cause an
accident, so make sure to check your random location and change it if
necessary.
addRandomCoin
private void addRandomCoin()
- Add one random coin to the highway. Place it in a random
lane and pick some interesting properties for it. Again, we leave
the details up to your creativity.
checkForCrashes
private void checkForCrashes()
- If there are any cars that crashed into each other (that is, they overlap),
this method should remove them from the highway.