Google Answers Logo
View Question
 
Q: Java program ( No Answer,   2 Comments )
Question  
Subject: Java program
Category: Computers
Asked by: wcryder-ga
List Price: $35.00
Posted: 20 Nov 2002 18:58 PST
Expires: 04 Dec 2002 02:40 PST
Question ID: 111676
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

Request for Question Clarification by rbnn-ga on 01 Dec 2002 22:48 PST
I just returned from vacation; are you still interested in this program?

Clarification of Question by wcryder-ga on 02 Dec 2002 01:57 PST
That would be wonderful.  Thank you.

Request for Question Clarification by rbnn-ga on 02 Dec 2002 08:35 PST
I have begun to analyze this problem.

The SnackTime client interacts with a SnackTime server which holds the
state of the game, including other player locations and their snack
locations. The SnackTime client and server communicate via some TCP/IP
protocol.

Are you seeking an answer that includes our own communication protocol
and server code, or is there some existing SnackTime server and
protocol out there somewhere?

In this type of multiplayer game, it is not feasible to give a perfect
strategy, since that depends on an assessment of the probable
strategies of the other player. Would a simple strategy be acceptable
(or in fact preferable) to a more complex one? (It is possible to
learn strategies using neural networks and other machine learning
techniques, although this takes time).

By when do you require the code?

Clarification of Question by wcryder-ga on 02 Dec 2002 13:03 PST
Yes, I have access to a SnackTime server, a simple startegy of the
game is preferred and if you could have the code by 6 pm PST that
would be nice.  If this is not possible, then sometime Tuesday will
work.  Thanks.

Request for Question Clarification by rbnn-ga on 02 Dec 2002 16:52 PST
It is not possible either 

 1. to code or 
 2. to assess the estimated difficulty of coding the given problem 

without providing a specification for the SnackTime server and a port
implementing it. As stated the specification does not include that
information, and so it is not possible to write a client.

Clarification of Question by wcryder-ga on 02 Dec 2002 18:10 PST
I am sorry but I am not quite sure of what information you are
needing.  Could you please clarify in laymens terms.  Thanks.

Request for Question Clarification by rbnn-ga on 02 Dec 2002 19:19 PST
The problem is requesting a program called a SnackTime client.

The SnackTime client communicates with another program called a
SnackTime server.

There is one SnackTime server, not written by us, which is sitting on
a machine somewhere and communicates with multiple SnackTime clients.

However, nowhere in the problem description is it stated HOW the
SnackTime server and the SnackTime client communicate.

By "how they communicate" I mean such things as how the client finds
the server, and, once it finds the server, how it sends it a request,
and what format the reply is in.

Without this information it is clearly not possible to write a client,
since there is no way for the client to know how to ask the server
something or how to interpret the servers replies.

Clarification of Question by wcryder-ga on 02 Dec 2002 21:51 PST
I amy be wrong, but I do not think that you have to worry about how
you connect to the server and how it replies.  The program is supposed
to be run from machines on the same server as the Snacktime server,
and will be tested this way.  I do have a sample program that shows
how it should look like, but it can only be executed on the same
server as the snacktime server.  I dont know if this helps but I think
you should be able to code the program, but not test it with other
players.

Request for Question Clarification by rbnn-ga on 03 Dec 2002 02:39 PST
Perhaps posting the sample code will help to clarify this issue. If
you have any other relevant information that might also help.

Clarification of Question by wcryder-ga on 03 Dec 2002 11:41 PST
http://sunpal.ucsd.edu/~cs11f/Assignments/P8/README.html

here is all the information

Request for Question Clarification by rbnn-ga on 03 Dec 2002 14:24 PST
I have read the documented you cited. It appears that the SnackTime
server can only be accessed from within a particular domain, and it
will not accept remote connections from outside that domain, among
other problems. Therefore, even if I were to write code, there would
be no  way to test such code; and I cannot write code without being
able to test it. Therefore, although this certainly does appear to be
an interesting problem, I am unable to provide a solution.

Clarification of Question by wcryder-ga on 03 Dec 2002 19:02 PST
Ok, I am just going to make one last request.  could you attempt to at
least code sorter.java, without testing.  i am not asking that you put
too much time into this, just some code that will compile to complete
part of the problem, note that the list price was lowered. it would be
greatly appreciated.

Request for Question Clarification by rbnn-ga on 03 Dec 2002 21:21 PST
Thank you for the question. The default solution is so trivial it's
not worth $35.00; in java 2 there is a function that does that:

try for the class Sorter something like:

public class Sorter{
 public void sort(Object[] o, java.util.Comparator c){
    java.util.Arrays.sort(o,c);
   }
}

This really is the standard way to sort in Java. It's possible but
very unlikely that you aren't running java 2 yet, and I think that
this is the solution they are looking for.

I play at http://www.topcoder.com a lot, and often use the various
Arrays.sort methods myself to solve those problems.

Clarification of Question by wcryder-ga on 04 Dec 2002 02:40 PST
thanks so much for your help
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java program
From: rbnn-ga on 20 Nov 2002 21:07 PST
 
This sounds like an interesting project. Unfortunately, I'm going to
be out of town (nominally on vacation although I would prefer to be
writing code) until December 1, so I will not have the opportunity to
help answer this.
Subject: Re: Java program
From: ivand-ga on 21 Nov 2002 15:08 PST
 
This sounds like a homework assignment for an introductory computer
science class. Anyways, it looks like it is not too much work.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy