Snake Visualization
To aid in the development of your submission, we are providing a visualization
tool to competitors. This tool is provided as is, with no guarantees of any
sort. The tool is packaged as an executable jar, which can be run either by
double clicking it (in windows anyway) or from the command line as "java -jar
Snake.jar".
As in the previous contest, to use this visualization tool, you should create an application
that communicates with the tool via standard in and standard out. Upon first
launching the application, you should read the parameters from standard in
(the parameters that will be provided to your play function in the real
contest). Once you have accomplished that, you should interact with the
visualizer by writing your move sequences (the strings you would call
Snake.moveSnake with) to standard out. After each sequence of moves, you
should read what would be the return from Snake.moveSnake from standard
in.
For example, lets say that you have written a function readInt() and a
function readIntArray(), then, in addition to the code you would normally
submit, you would need to add a main method (the program's entry point), and a moveSnake method (in a
class Snake). The moveSnake method is where the interaction between your
program and the visualizer takes
place, and the one you write is in place of the one that is provided by
TopCoder when you submit your code on the website (thus when you submit your
code, you should remove the entire Snake class, and in some languages you will
have to remove the main method.
//this is your play function, unchanged from your regular submission
play(N, M, K, sx, sy, obstacleX, obstacleY, foodX, foodY){
//...
newFood = moveSnake(sequence)
//...
}
moveSnake(s){ //put this in class Snake
writeString(s)
return readIntArray()
}
main(){
N = readInt()
M = readInt()
K = readInt()
sx = readInt()
sy = readInt()
obstacleX = readIntArray()
obstacleY = readIntArray()
foodX = readIntArray()
foodY = readIntArray()
play(N,M,K,sx,sy,obstacleX,obstacleY,foodX,foodY)
}
The readInt() function simply reads an integer from standard in. More
specifically, it reads a single
integer from standard in, which is on a line by itself. The readIntArray()
function is only slightly more complicated. It simply reads a sequence of
integers, the first of which is the size of the array. All of these integers
will be on one line. For instance, you should read the line
4 3 9 2 1
as the array {3,9,2,1}.
When writing a sequence of moves (the characters 'L', 'R', and 'S'), simply
write the sequence out on a single line, with no additional characters.
It is important that your program not output anything to standard out beyond
what is described above, as doing so will cause the visualization tool to not
work. If you need to debug, you may print your debug output to either
standard error, or to a file.
Here is a sample trace of the communication between the visualization tool and
your program. Comments follow the # mark and are not part of the
communication, nor are the row labels.
VIS: 20 # N=20
VIS: 5 # M=5
VIS: 7 # K=7
VIS: 17 # sx=17
VIS: 3 # sy=3
VIS: 3 6 12 7 # ox={6,12,7}
VIS: 3 7 10 0 # oy={3,10,0}
VIS: 5 3 2 0 19 7 # fx={3,2,0,19,7}
VIS: 5 7 10 0 1 8 # fy={7,10,0,1,8}
ME: SSSRRSS # go straight 3 times, turn right twice, go straight twice
VIS: 0 # No food was eaten. If it had been,
VIS: 0 # these lines would have given the new food location
Using the visualizer
To use the visualizer, you must have Java 1.5 or greater installed. To run
the visualizer, you can execute the command "java -jar Snake.jar". This will
open up a new window containing the visualization, along with a number of
controls. The first thing you will need to do is specify the executable you
have made from your snake code. You may either enter its path, or select it
via the button provided. If your executable requires arguments, enter them
in the provided field. For example, if your executable is a Java class
SnakePlayer.class, you should enter something like "java SnakePlayer" (without
the quotes) in this field (replacing "java" with the full path to the Java
executable if necessary). The exact details of what you enter here will
depend on your language choice. In particular, in Java, you will need to make
sure that the class file is in the same directory as Snake.jar, or else you
will need to specify a classpath along with the executable location and class
name.
Once you have the executable set properly, you can run the simulation. You
may customize the game by entering any values for N, M, K, p, and seed
that you like, and then clicking generate map to make a new map. The field seed is a seed for
the random number generator and will allow you to repeatedly generate the same
map. Clicking the "Generate Map" button will generate the map based on your
parameters. Alternatively, you can select new random values for all
parameters by clicking "New Parameters and Map". If you've changed the seed,
since you last generated a map, the seed you entered will be used to generate
the parameters as well as the map, otherwise a
new seed will be generated. To test the examples from the problem statement,
you may simply enter the seeds given, and click "New Parameters and Map".
Once you've started the simulation, any output you write to standard error,
along with messages from the visualization tool will appear in the text area below the
visualization. You may control the speed with the slider on the top.
Command Line Options
You can specify a number of parameters on the command line to
simplify the automation of testing (though you don't need to use any of them).
For example, "java -jar Snake.jar -N 20" will set N to 20 when running the
tool. Similarly, you can specify -M, -K, -p, and -s (for seed).
-N <N> | Specify N |
-M <M> | Specify M |
-K <K> | Specify K |
-p <p> | Specify p |
-s <seed> | Specify the initial seed |
-t <speed> | Specify the initial speed |
-exec <command> | Specify the command to execute your code |
-novis | Run the test case without the visualizer (requires
-exec, implies -go) |
-go | Start running immediately (requires -exec) |
Final Notes
- Make sure you flush your buffers after writing to standard out, or the
visualization tool might not get your output, and it will appear to
hang.
-
Java users: if you get something java.lang.NoClassDefFoundError SnakePlayer,
it means that you've correctly set the path to Java, but Java can't find
your class. Among other things, you can try setting the command to something
like "PATH1\java -cp PATH2 SnakePlayer", where PATH2 is the location of your
class file.
-
If your snake fails to move, but there are no error messages, the most likely
explanation is that the visualization tool is still waiting for your program
to tell it which way to go. An obvious cause for this is that your program is
in some sort of an infinite loop. Another possible cause is that your program
is waiting for input which it will never get, as the visualization tool is
waiting for your program.
-
Please note that, while we will do our best to make the workings of the
visualizer as clear as possible, it
is impossible for us to help everyone debug their code to make it work with
the visualization tool. If you do think you have discovered a bug, please email
lbackstrom@topcoder.com or post the bug to the forums. Please to NOT post
code to the forums, as that is against the rules.