![]() |
|
#1
|
||||
|
||||
[Program/Tutorial] RandMouseName/Brief Description:
RandMouse -- A small, morbid program that simulates a mouse running around on a deserted island looking for a bridge.Date of Original Submission: March 22, 2004Submitted by: Aaron CohnLicense: Free to be used/modified... but not to be turned in as homework!Problems/Limitations: Of course, it's only pseudo-random... other than that, my coding is flawless and perfect. Ye who vieweth the almighty RandMouse shall remove thy tongue from the floor and place it in thine mouth. Just kidding :-PWell, let's start this off with a brief intro as to where I got the idea to write this program, what it does, and why I'm posting it. I am an entry level C/C++ programmer and recently finished my first real programming course. The teacher actually had us get some text books that turned out to be surprisingly good, and I'll tell you exactly which book I'm talking about at the end, but it's basically a general programming book that focuses entirely on C/C++, and delves into object-oriented programming and the like. At the end of each chapter is a bunch of exercises, followed by some practice problems, followed by some examination problems. At the end of chapter twelve, which is a chapter on arrays, there's a problem that involves creating a two-dimensional array to represent a playing field. Now, this has been done over a million times in every which way you could think of, but it was pretty new to me, so I just went at it until it was done. The task the program was designed for is (in the words of the book) this: Quote:
So, what's the first thing that this program needs in order to do what we want it to do? Well, I'd say we should start by creating a playing field. The next thing we might need would be something to fill that field. Particularly, we'll want to fill it with land, water, and a couple of bridges. Well, this is looking pretty good so far, but what else do we need? Ah, yes... a mouse. I believe those are all the things that we'll need. Now we have to figure out how to make them! Upon viewing the picture above, you can see that the playing field should be a two-dimensional grid with each square consisting of a type of terrain. Since this chapter is about arrays, and arrays can form grids in different dimensions, then it would probably be in our best interest to use a two-dimensional array as a representation of our playing field. Well, the terrain types can be taken care of using enumeration, but how would we go about creating a mouse? This isn't so hard if you think about what a "mouse" should consist of. For our purposes, a mouse must consist only of a location. It doesn't matter to the user if we do something really complicated or really simple, so we'll just stick with something simple. In this case, all the mouse needs to consist of is a point denoting where it is on the field. Of course, we'll be adding a few things to it, but the only thing necessary for the mouse to exist is a point. So, we'd probably be better off creating a structured data-type representing a point. It's important to think ahead and predict what you'll be doing with your variables, since you'll be working with them a lot. Make sure to make them easy to work with, and name them very clearly. I have found that when naming a data-type, it's best to use a very generalized term... if you were creating a living organism, such as a mouse, you might use the word rodent to describe what type of data the mouse consists of. We'll just call it an Entity. Since the word encompasses all living things, we could really use any type of living thing we wanted as a subject, and it would still be easy to find out what the variable is for. It's important to do this, because when you start working with other people, or other people attempt to modify your code, those people will have to be able to read and understand it, and you'd like to make that as easy as possible. Not to mention, it makes things easier for you to write in the first place. Right, then... well, on with it. __________________
-Aaron |
|
#2
|
||||
|
||||
|
Let's start writing code for the things we need in order to write this program. The first file we should create is a header file that contains declarations of our various data types, so that they don't clutter up the main code. We'll call this file MouseTypes.h.
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
That's the end of the header file! The next file we're going to go over is the implementation file for RandMouse. __________________
-Aaron |
|
#3
|
||||
|
||||
|
The file that we're working on next contains the code for running the program, as well as all the code for the things it needs to do. We're going to go through this file a little bit differently than we went through the header, since it's bigger and more complicated. Keep in mind that we have an objective to complete, and we need to do a few things to get there.
Let's figure out what the main module should look like. MAIN CPP / C++ / C Code:
RESET CPP / C++ / C Code:
MOVE ENTITY CPP / C++ / C Code:
CPP / C++ / C Code:
DISPLAY STATS CPP / C++ / C Code:
DIR TO STR CPP / C++ / C Code:
DISPLAY FIELD CPP / C++ / C Code:
__________________
-Aaron |
|
#4
|
||||
|
||||
|
Okay, now that we've got the pseudo-code done, we can create the code in our implemation file, Mouse.cpp. First thing to do is do all of our #includes, #defines, and function prototypes.
CPP / C++ / C Code:
Well, that's it! Everything from here to the end is implementation. I have some comments on certain things, but it'll mostly be code. You may use the links at the bottom of this post to download a zip file containing both the header file and the implementation file, as well as an alternate file that implements colored text. The only difference between the two is color, and you can compile either one with the same headers, except the color one requires the windows.h header file. CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
-Aaron |
|
#5
|
||||
|
||||
|
Oh, also.. the book I referred to at the beginning of this tutorial is titled:
"Programming and Problem Solving with C++" -- by Nell Dale, Chip Weems, and Mark Headington __________________
-Aaron |
|
#6
|
||||
|
||||
|
Another good book - "Absolute C++" -- by Walter Savitch
__________________
-Aaron |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The