CS 226: Data Structures, Program #6
Turtle Text
Assigned: Monday, November 22
Font characters due: Wednesday,
December 1 (so we can build and distribute the complete font to all
students)
Due: Monday,
December 6, Tuesday, December 7, 11:59 pm (GRADED)
(extended by one day)
(Reminder: Please do not
look
at each other's source code except for any designated project partner.
We
will be actively looking for programs that are "too similar", and I do
not wish to find any.)
Overview
In this program, you will build on the previous
ungraded
assignment to develop a font. Each character of the font is a macro
call,
with certain expectations as to where the character should be drawn
from
the current turtle position as well as expectations as to where the
turtle
should end up after drawing the character. Using this font facility, it
will
be possible to print strings of characters at any orientation. In
addition,
we will allow commands to be loaded from a file to facilitate loading
of
macros such as font characters, etc.
New Turtle Commands
We will add a few more commands to our command set as follows:
load "filename"
save "filename"
font <fontname>
print "string"
scale <scalefactor>
load
The load command takes a filename as an argument. The filename should
actually
be surrounded by quotes. This will allow you to use file names
including
spaces. When preparing to read the filename, you may have to set your
parser
to accept the double quote character as a delimeter and not to use
spaces
as a delimeter. The commands read from the file should be exectued as
if
you had just typed them in. Thus, the load command itself will not
appear
in the undo queue, and calling an undo after a load will just undo the
last
valid command from the loaded file. As a consequence, you will not be
reloading
the file with every future undo command (as you might if you just
enqueued
the load command itself). Note that this is different from the expected
behavior
of the call command from program 5. For the call command, the call
itself
should be stored on the queue, so you can undo an entire macro call
with
a single undo. Because of this, you should not currently allow an
undo/redo
command to appear in a macro definition, because its behavior would be
undefined
(but undo/redo should be allowed in a file, though perhaps not terribly
useful).
save
The save command will write out the entire command sequence from the
undo
queue into a text file. Besides being a generally useful operation,
this
should be helpful when you want to save out your font macros to a file.
It
should then be possible to perform a load on this file during a future
program
execution to reload all your macros, etc. (Of course, you may often
want
to edit this file to remove unnecessary commands, etc.)
font
The font command is followed by a font name. As with macro names, the
font
name should begin with a letter, followed by a sequence of letters,
numbers,
and underscores. This font name will be used to convert the print
command
(see description below) into a sequence of macro calls. Thus, the
current
font name should be stored as part of your turtle's state (so it can be
pushed,
popped, etc.). The default value for the font is just a null string.
print
The macro names for your individual characters will take the form:
<fontname>-<charactercode>
That is, the font name (without the angle brackets), followed by a
hyphen,
followed by an integer representing the Unicode character code (which
is
a just superset of ASCII -- your keyboard will typically only allow you
to
type in ASCII characters in any case). Given the string (which is
delimited
by double quotes, as in the load and save commands), the print command
should
call a sequence of macro calls to print the individual characters, one
after
the other. The print command itself should be placed on the undo queue,
so
a print command followed by an undo should undo all of the printed
characters.
scale
The scale command should set the current scale factor of the turtle
state.
The scale factor is a positive floating point value, with a default
setting
of 1.0. The scale factor should be used to multiply the distance the
turtle
moves in each move command. Thus, setting the scale factor to 2.0 will
make
the turtle draw things twice as big in subsequent move commands. Notice
that
this will allow you to draw your fonts and other macros at different
scales
as well.
Using Files
Similar to reading from System.in, you can set up a BufferedReader for
a
file as follows:
java.io.FileInputStream file = new java.io.FileInputStream(filename);
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(file));
StreamTokenizer tok = new StreamTokenizer(in);
And similar to printing to System.out and System.err, you can set up a
PrintWriter
for a file as follows:
java.io.FileOutputStream file = new java.io.FileOutputStream(filename);
java.io.PrintWriter out = new java.io.PrintWriter(file);
This print writer supports the print( ) and println( ) methods you are
familiar
with from System.out and System.err.
Font Character Formatting
The turtle print command you develop should place characters end-to-end
in
a "left-to-right" fashion. However, the actual orientation of what is
"left-to-right"
depends on the current orientation of the turtle. When you develop your
font
characters, assume that the turtle is pointing to the right and is
located
at the baseline of the font. In other words, whatever position and
orientation
the turtle is currently in when you issue a macro call to draw a
character,
the current orientation may be thought of as "right" and the current
position
will be used as the baseline. When you are done drawing a character,
you
should leave the turtle position at the same baseline and pointing in
the
same direction ("right") as it was when the macro was called, but
leaving
some number of spaces between characters. Thus the turtle ends up in
the
correct position to begin drawing another character.
The name of the font we will design together as a class is CS226 (so
the
macro name for "A" should be CS226-65). For this font, please design
all
your characters to be 12 pixels high and 10 pixels wide. Lower-case
characters
that dip below the baseline (for example, "p", "g", "y", etc.) should
go
down to 3 pixels below the baseline. You should leave an additional two
pixel
space between the characters. This is a monospaced font (as opposed to
a
variable width font). Each person will be assigned characters to design
and
we will collect them all into a font. If you want to model your font
after
an existing font, try Courier. Please feel free to trade characters
with
your classmates in advance of the deadline so you can better test and
use
your program. (This trading will not involve exchanging any code - just
font
macro definition files).
Program Execution
Your TurtleCommander program should execute in the same way as the
TurtleCommander
in Program #3. You should be able to type into it interactively or
redirect
standard input from a file or another program as described in that
assignment.
Of course, now you can also load files during an interactive session
using
the new load command.
As in previous assignments, you should handle error conditions
gracefully,
without causing the program to crash or exit unless absolutely
necessary.
Restrictions
You may use the java.io and java.lang class
libraries.
You may use the StreamTokeninzer or the StringTokenizer to parse your
input,
as in previous assignments. In general, you may not use
classes
from the java.util library unless you receive permission to do so for
some
particular class. One exception is the java.util.Random class, which
may be
useful for testing some of your programs and does not jeopardize the
goals
of the class.
Submission - Read
this Carefully
This is a graded program. Your submission should
include
a set of .java files, a single macro file called font.txt containing
all
your assigned font characters (for both members of a team) and a
README.txt
file with any information you want to tell the grader about running
your
program. You should also turn in a couple of command files you used to
test
your program. Your main program class must be called TurtleCommander,
and
thus it should be compilable using "javac *.java" and runnable with the
command
"java TurtleCommander". You should submit your program as a
single
.zip (zip archive) file. This file should have no directory structure
in
it -- just the actual files, so extracting them should result in all
your
original files placed in the current directory.
To submit, you should follow these steps:
- Create the .zip file for submission
- Make a new, empty directory
- Copy the submission file to the new directory
- Extract all the files from your submission file into the new
directory
- Verify that you can compile and run the program on your test
cases
in the new directory.
- Go to the submissions page
- Submit the .zip file, making sure that the file appears in the
directory
listing displayed in the browser after submission (the file size listed
could
be slightly different on the recipient SunOS machine from that of a
Windows
OS machine, but they should be very similar)
- Also in the submissions page, View the submission to be certain
that
the file is there (you should see basically the same directory listing
you
saw at the end of step 7).
If anything goes wrong with steps 7 or 8, try repeating them
(resubmitting
your file overwrites any previous submission and time stamp). If you
cannot
successfully submit your assignment, immediately send e-mail to the
professor
andthe head TA explaining the problem (with all details so we
can hopefully
fix it) and including the submission file as an e-mail (MIME)
attachment.
It is your responsibility to verify that the assignment is received.
If
we do not have an assignment or record of its submission, it will
receive
zero points.
Grading
This program is ungraded, but the following one
will
be graded based on:
Compiling and running
Proper execution on a number of our test cases (with valid and invalid
inputs)
Overall implementation correctness
Comments, style, and readability (informative, but not extraneous
comments
are appreciated)
Font character readability and correctness
If your program does not compile, you can expect to receive very few
points,
if any. This should encourage you to take care in your submission
process.
Parting Words
Have fun with fonts! The sooner you design your
font
characters, the sooner you can trade with your friends and collect the
whole
set!
November 22, 2004