Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CPI221 – Assignment – World Building
With a Factory!
Topics:
• Review Pillars of OOP
• Create a simple command line application
• Create and use inheritance & polymorphism
• Explore “IS A” and “HAS A”
• Build a factory class
Description:
We’re going to use Slick2D and a 2D array to generate a simple world out of
simple terrain types.
This time there will be no polymorphism because the factory will offload that
functionality.
There will be very low user interaction.
Use the following Guidelines:
• Give identifiers semantic meaning and make them easy to read (examples
numStudents, grossPay, etc).
• Keep identifiers to a reasonably short length.
• User upper case for constants. Use title case (first letter is upper case)
for classes. Use lower case with uppercase word separators for all other
identifiers (variables, methods, objects).
• Use tabs or spaces to indent code within blocks (code surrounded by braces).
This includes classes, methods, and code associated with ifs, switches and
loops. Be consistent with the number of spaces or tabs that you use to
indent.
• Use white space to make your program more readable.
Important Note:
All submitted assignments must begin with the descriptive comment block. To avoid
losing trivial points, make sure this comment header is included in every
assignment you submit, and that it is updated accordingly from assignment to
assignment.
Specifications:
The software will generate a world using objects from an inheritance/polymorphism
hierarchy. You wills store these objects in a 2D array.
User Interface:
When the program starts you will ask the user to input a width and height for the
world. This should be between 20 and 50. This will determine how big the world
will be and how many objects will be created.
Object Orientation:
The software will consist of several classes:
• Terrain
• Terrain Factory
• Main class
Each terrain type will have rules on how it should be shown and what kind of
things can be its neighbors.
Terrain Class
• Properties
o char type
G – grass
H – hills
M – mountains
D – desert
R – River
F – forest
B – Beach
L – Lake/Ocean
o int width
o int height
o int x
o int y
o Color color
• Methods
o drawSelf(Graphics):void
Factory
Your factory should build new terrain objects by taking in type of the object
asking for a new neighbor. Based on the type you’ll use the probability rules of
that type to create and return a new object. Feel free to make your Factory with
static methods or as an instantiated object with a public interface.
Grass
• Should be light green in color
• Should generate a neighbor object
with these probabilities:
40% G – grass
15% H – hills
0% M – mountains
5% D – desert
15% R – River
15% F – forest
10% B – Beach
0% L – Lake/Ocean
Hill
• Should be middle green in color
• Should generate a neighbor object
with these probabilities:
20% G – grass
40% H – hills
15% M – mountains
0% D – desert
10% R – River
10% F – forest
0% B – Beach
5% L – Lake/Ocean
Forest
• Should be dark green in color
• Should generate a neighbor object
with these probabilities:
15% G – grass
15% H – hills
10% M – mountains
0% D – desert
10% R – River
45% F – forest
0% B – Beach
5% L – Lake/Ocean
River
• Should be light blue in color
• Should generate a neighbor object
with these probabilities:
5% G – grass
5% H – hills
5% M – mountains
0% D – desert
50% R – River
5% F – forest
10% B – Beach
20% L – Lake/Ocean
Desert
• Should be yellow-brown in color
• Should generate a neighbor object
with these probabilities:
30% G – grass
0% H – hills
5% M – mountains
55% D – desert
0% R – River
0% F – forest
10% B – Beach
0% L – Lake/Ocean
Mountain
• Should be grey in color
• Should generate a neighbor object
with these probabilities:
0% G – grass
25% H – hills
50% M – mountains
5% D – desert
5% R – River
10% F – forest
0% B – Beach
5% L – Lake/Ocean
Beach
• Should be sandy in color
• Should generate a neighbor object
with these probabilities:
10% G – grass
0% H – hills
0% M – mountains
5% D – desert
5% R – River
0% F – forest
40% B – Beach
40% L – Lake/Ocean
Lake/Ocean
• Should be dark blue in color
• Should generate a neighbor object
with these probabilities:
0% G – grass
0% H – hills
0% M – mountains
0% D – desert
10% R – River
0% F – forest
30% B – Beach
60% L – Lake/Ocean
Algorithm breakdown:
Setup:
1. Get size of array from user
2. Create Terrain 2D array
3. Divide screen width/height by user inputted size
4. Create first piece of terrain in upper left corner
a. Pick randomly
b. Make sure you pass in the pixel width height to the constructor
First row:
1. Generate the second object by passing the type of the upper left corner
object to the factory method.
2. Store the new Terrain in the second index
3. Continue down the row by passing the type of the object “to the left” to to
the factory to generate the new object
Subsequent rows:
1. The first item in the row will use the previous row’s first item type
generate the object
2. Each item in the rest of the row will ask the object “up from” the current
index and “to the left” for their types
3. Flip a coin to choose between the two and pass that type to the factory to
generate the new object. Store the result in your array.
4. Continue
Extra Credit
+5 – find or make images to represent each terrain type. PNG files. Each image
should be the same size and that size should be a power of 2. Modify the Terrain
class to include an Image objects. Modify your factory as well to provide the
Image objects to the constructor. Modify your draw code to draw these PNG tiles
instead of rectangles.
You’ll want to research how to create & use image objects and how to draw them
using Graphics.
Suggestions
Build helper methods in your factory class. You can make private static methods
to call on to help build the objects. I wrote one for each terrain type.
Don’t forget to pass your pixel width/height and x/y location into your factory
so you can completely build the object. Use your factory to set the color of the
object. (why make the terrain object responsible for it?)
The array filling algorithm is almost identical. The difference is that you’ll
have the factory to build the objects.
The rendering method should be very simple: loop through each object and call
drawSelf. You should be constructing the Terrain objects to hold everything
internally (x,y position, pixel width and height, color)
Once again, the ASCII version is “slightly easier” because you don’t have to
worry about things like calculating an x/y position or pixel width or height or
making it work in Slick2D. Get the logic down by doing the ASCII version first.
Test your code with non-squares!!
Output
After the terrain is generated … draw it to the screen using slick2D
If you can’t make slick2D work … use ASCII
Here’s a sample of one I generated 20x20:
GGGGGGDLLBLRBLLLLLBB
GFFBGHFHLLLRGFGHBBBL
RHHGHRRGDLBLGGFRBLGL
RGFRHLLRMLBBGGFRBLLL
LGBRRLLLRRLRFGHHRRBB
LLLMLLLLLBLBHHHGBLLR
BBBLLRRRRLLLMRLBGLLB
LBGFFFRRFLRLBRRBBBBL
LBFFMHHMRRGFFHMMMLLL
LBFFFHHLHLGFGMFRHGLB
BBGFGBMHLLDGFFFRRRLL
GLBLLGFHHLBGRGRRRLLL
BLLLLLLRBLLLRHRRLBLB
LLLBBLLLRBRLBHHLLBLL
LLBBLBBBRRBLLHGGFGRR
LRDLLRGBLLGGLHFFFBRR
BRHGGGHHBBFMHHHFHHMR
LLBGGGGGBBGRBGFFRLLR
BLGBGHHGFBBMLGHGFFLR
LBBBGBFHFMMHLHHFLBLB
What to turn in:
Create a zip file to submit:
• __hw3.java
• Terrain.java
• TerrainFactory.java
Name your zip file __hw3.zip
Submit to the course shell.
Grading of Programming Assignment
The grader will grade your program following these steps:
(1) Compile the code. If it does not compile you will receive a U on the
Specifications in the Rubric
(2) The grader will read your program and give points based on the points
allocated to each component, the readability of your code (organization of the
code and comments), logic, inclusion of the required functions, and correctness
of the implementations of each function.