Please write the code implementing the program that is described in
detail below. Please include javadoc comments. When writing the code
please try to keep at a level at which I can understand. I do not
consider myself an advanced programmer and want to be able to
interpret the code given to me instead of just having a program that
compiles. I have included links to code from an Item class hierarchy
(Item.java, Player.java, and Snack.java) for which the files should
work with. These files CANNOT be modified.
Description of the SnackTime game
SnackTime is a networked multiplayer game. A player in the game
connects to the SnackTime server; any number of other players can be
connected at the same time. When a player connects, they receive from
the server a description of the current state of the game. Based on
this information, the player decides on a move and communicates it to
the server. The server responds by giving the player a description of
the state of the game after that move, and the process repeats.
Players can disconnect themselves from the game at any time, but a
player will always be automatically disconnected after they have moved
1000 times.
The game's playing surface is two-dimensional. At any moment, the
surface is populated by Items, each of which has a location and an
energy level. Items are of two kinds: Snacks and Players. Besides the
properties inherited from the Item class, Players each also have a
name, and a counter that keeps track of the number of moves the Player
has made since joining the game. At any time, then, the state of the
game can be represented by an array of Item objects.
The basic goal for a player in the game is to accumulate as much
energy as possible within the 1000 move limit. When a Player moves to
a location occupied by a Snack, it is considered to have eaten it, and
that Player acquires all the Snack's energy (and the Snack disappears
while a similar one appears elsewhere, so the total number of
available Snacks in the game is constant). When a Player moves to a
location occupied by one or more other Players, all Players at that
location are assigned the average of their energies. So a high-energy
Player may want to avoid other Players while continuing to grab
Snacks, but a low-energy Player might try to track down high-energy
Players as well as Snacks. Snacks never move (except when they are
eaten and disappear), so they are easier to catch; high-energy Players
can move, but not as fast as low-energy Players.
A player will run the SnackTime client program as follows:
java -jar SnackTime.jar
This opens a window, consisting of the game playing surface (intially
blank) and a list of connected Players and their properties (initially
empty). To connect to the SnackTime server, use the "Server
connection..." pulldown menu. When the client connects, the current
world state is obtained from the server, and a control loop is started
within the client program, with control flow that looks something like
this:
Item[] currentState = getCurrentState();
while(connected) {
displayPlayingSurface(currentState);
Player[] pList = getPlayerList(currentState);
Sorter.sort(pList,currentComparator);
displayPlayerList(pList);
Point newLoc = Action.move(currentState);
currentState = sendMoveToServer(newLoc);
}
Each time through the loop, user code (i.e., the Action.move() and
Sorter.sort() methods) is called to determine the next move of the
player, and to sort the player list. The next move is sent to the game
server, which replies with an Item array describing the state of the
game after that move. The server disconnects the client automatically
after 1000 moves, or upon a user-initiated disconnect.
By using the "Get high scores" option from the "Server connection..."
menu, the top 100 scores so far (i.e., player energy at disconnect)
can be viewed.
Implement the move generator for your player, and the sort method to
order the displayed Player list.
CLASS INTERFACE SPECIFICATIONS: Action.java
The Action class must provide one public static method:
public static Point move(Item[] worldState)
This method will take as argument an Item array representing the
current state of a SnackTime game. The elements of the array are
guaranteed to be instances of two subclasses of Item: Snack and
Player. All Players in the array have larger indexes than any Snack
(i.e., Snacks come first in the array), and the last element of the
array is a Player who is being asked to move. The method must return,
as a java.awt.Point object, the new desired location of that player.
Every Player has a maximum distance in pixels that they can move from
their current location. This maximum move distance is a function of
the Player's energy, and decreases with higher energy (so, energy
weighs you down; hey, Einstein showed energy is mass!). The maximum
move distance is given by this formula, where "^" represents
exponentiation:
maxd = 10.0 / 2^(energy/1000.0)
So, the maximum move distance starts at 10 (when energy is 0),
decreases to 5 when energy is 1000, etc.
The requirements of the move() method are:
1. The new position shall not be more than the maximum move distance
from the player's current position. This is measured in Euclidean
distance, so, for example, a change of 6 pixels in X and 6 pixels in Y
is a move of sqrt(72) pixels.
2. The new position shall not be outside the playing surface, which
has a width of 600 and a height of 400 pixels.
3. If there are no other Players, and only one Snack, the new position
shall be closer to the Snack. If there are no other Players and one
Snack, and the one Snack is within the maximum move distance of the
player's current position, the new position shall be the same as the
Snack's position (i.e., the Player must eat the Snack).
Consistent with those requirements, you are free to make the move()
method implement as sophisticated an energy-seeking strategy as you
want. See the Item, Snack, and Player class source code files for
information about methods that will be useful for finding out
information about the Snacks and Players in the worldState array. Note
that nothing the move() method does to the elements of the worldState
array will have any effect on the state of the game; only the server
updates the state of the game, based on move requests it receives.
CLASS INTERFACE SPECIFICATIONS: Sorter.java
The Sorter class must provide one public static method:
public static void sort(Object a[], java.util.Comparator c) {
This method takes two arguments: an Object array to sort, and a
Comparator to use in sorting. When the method returns, the array
should be in ascending order, according to the usual interpretation of
the values returned by the Comparator's compare() method. In this
application, the array will be an array of pointers to Player objects,
but the method should be designed to work generically. It may also be
advantageous if it is efficient, since your player will be able to
make more moves if your client program is not spending most of its
time sorting. This may make a noticeable difference when there are
many Players.
The user can use the "Sort player list by..." pulldown menu to select
how they want the Players list to be sorted. This determines which
Comparator is used to do the sort.
Here are the links:
http://www24.brinkster.com/westcoastryderz/Item.java
http://www24.brinkster.com/westcoastryderz/Player.java
http://www24.brinkster.com/westcoastryderz/Snack.java |