Due: Wednesday, April 19, 2017, at noon, 100 points
For this assignment, you will submit multiple files containing a program written in C++. You know how to use the submit program by now; I needn't tell you.
The following is a list of topics we wish to address with this assignment:
classes
more classes
some classy classes
yet another class
everything else you learned up to this point
Resources: www.cplusplus.com Check out the different libraries, especially the iostream library.
Foreground: Programming assignments #9 and #10 are tied together. In this part (#9), you are going to build some of the classes used in the final program. After building them, you will use a main function that will test the functionality of the classes that you have created. This kind of main function is called a "driver". Again, it's only purpose is to test your classes. When you build hw #10, you replace this main with a "real" main that does something useful to solve the given problem. The details of the problem are going to be described in hw #10. For now, you just build some of the classes (described below) and use a driver to test them.
Background: As you all know, Milhouse has had great success in life as a young entrepreneur selling his fashion idea - high-water pants. So Milhouse is going into business altering peoples' pants to be high-waters. He will ride around town on his bicycle picking up pants to alter and delivering already altered pants. In hw 9 and hw 10, you will create a 'world' that simulates for him how life will be. Milhouse is going to move about in a 2-D array representing his town. In that town, you will place actors: a tailor most likely named Milhouse; bullies that try to ruin his day; pants that have come alive and try to catch Milhouse; and maybe more. But, of course, there are problems these actors will encounter in their ramblings: bumping into walls, getting mugged, etc. In hw #10, you will code a simulation of a day in Milhouse's life as a "tailor". It will be complex, but that's for hw 10.
In this, the current assignment (hw 9), you are going to build some pieces of hw 10. You will build two classes and test them. First, you build a town class that is a 2-D array of characters. The first and last columns and the top and bottom rows are used to represent a structure, such as a wall or dome-like-thing, that people can't pass through. Put the letter 'W' (for Wall) in those positions. Next, you will build a tailor class to represent a person, like Milhouse, who fixes pants...mostly. This type of object will "move" about a town. To do that, the tailor must have a location variable(s). Of course, there's much more. Let's proceed....
Specifications: You are going to create classes in this part of the project for town and tailor. Here are descriptions of them:
town class:
member variables:
a 2-dim array of chars declared for MAX size of 25 x 25
a short integer describing the actual size of the town ( <= MAX). Your particular town object may not be as big as MAX x MAX, thus using only part of the array.
member functions:
a constructor that allows you to pass in a grid size and defaults to MAX. This constructor will call private functions that will clear() the grid (the s-D array), then a build() function that will place walls as described in the background above.
a print function or the overloaded insertion operator.
appropriate accessor/mutator (set/get) functions.
tailor class:
member variables:
a member for the tailor's name - ntca or string.
a member to represent how much money he/she has.
appropriate variable(s) for his/her location on the grid (town).
a char for how he/she is to be represented (in the town array).
state variable to indicate whether he/she is alive.
member functions:
a constructor that takes arguments for a name, funds (defaulted to $0), aliveness (defaulted to alive), and a representing character.
a place_me() function that has a reference parameter of type town. It will determine the dimensions of the town, and from that will place the tailor in the town's grid and change the tailor's (private) coordinates. Of course, tailor always prefers to be in the middle of things (places the object as close to the middle of the town as possible).
a rand_walk function() that has a reference to a town parameter. It will pick a random direction (up/down/left/right - no diagonal movement in Springfield!) in which to take a step, and it will change its own coordinates and the location it occupies in the town passed to it. This function should prevent movement into any new location in the town in which there is a character other than 'blank'. So, if it randomly chooses to move left and there is a char in that spot in the town grid, such as a 'T' for tree, then it has to move in some other direction.
a print() function or overloaded insertion operator. This should output on one line the object's name, aliveness, and location in town. (e.g. "Milhouse is alive at (4, 5)." )
Notes: Here are some things to think about when making these classes.
For the random move function above, the object should not be allowed to move to a location in the grid (the 2-D array of the town) already occupied by any other 'thing', like a wall or a tree ('T'). If something is in the way, then move to a different location.
In your code you need to think about the limitations on what size town array (grid) you can create. Does it make sense to allow sizes of towns that are negative, greater than 25 (the max), or even 3x3? (no, you'd have walls and one spot for one actor to do nothing in!)
What things should you use constants to represent?
Notice that your town will not "contain" (have as a member variable) a tailor, nor will a tailor contain a town. But there will be affects one has upon the other. As stated, you will pass a town object to the tailor's move function. Why? Because the town will have a char in one of it's cells to represent where that tailor is. So, you must think about accessibility when these objects interact. If the tailor moves, then it has to have a way to let the town know it has moved and to where.
It is almost always the case that when I code a class, I will code the overloaded insertion operator (or a print function) so that I can output the state of the object of that type so that I can see the object. If you can't output it, you don't really know what you have. So, my advice is to do that first.
Now let's talk about the driver, your main. A driver would need to declare instances of each of the above classes and test their functions. You will do just that. Declare a town and output it to the screen. Make sure it is what you expected. Declare a tailor object and output it. Is it what you expected? Then you'd want to pass the town to the tailor's rand_walk() function and output again to see if the tailor is in a different location and if the town displays him/her as such.
Please understand that the final hw 10 program will be more interesting and fun, but you can only do so much at a time. This much will do for now. There will be objects of types that will make the tailor's (c'mon, we all know it's Milhouse) life interesting and exciting. Ha ha, it'll be a hoot.
When you submit: This will be a little different in that there will be no interaction with a "user". Your main will simply do its thing. But that thing is this: test your classes by declaring them like mentioned above. Declare a town of size 10 x 10. But after you declare your town, put a 'T'1 in locations (1, 2), (1, 5), (2, 5), (3, 2), (4, 1) and (4, 3). Declare your tailor (certainly naming him Milhouse) and put him at (2, 3). Now put a call to the rand_walk() function in a loop of 12 steps to see what he does. To make it interesting, you could keep track of how many times he bumps into either a wall ('W') or a tree ('T') and output that at the end of the test. Output the town in the loop after every step.
As usual, don't hesitate to ask questions of your instructor.
1T stands for tree, tiller, tiger, transylvanian canine hybrid, TNT, ternary operator, or possibly even Tauritz....none of which a tailor should be stepping on!