
Introduction
News and Updates
Short Summary
MBCS Files
Detailed Study
I. Introduction
II. Creation
III. Displaying
IV. Simulation
Misc. Sections
Other Files
Contact
Links
|

Detailed Summary - Part III: Displaying
|
fishsim.cpp, main()
|
|
After emerging from the Display's show function, all the necessary case study preparation is now complete. The next line tells you so by printing out "initialized" and then prompting the user for the amount of steps to run the simulation. Afterward, a getline prompt ("press any key to continue") rests between all the steps the program took and the simulation loop.
The simulation runs in a for-next loop that continues for the amount of steps designated earlier. At the beginning of the loop, sim.Step(env) is called, sending the program into the simulation section.
|
|
Simulation::Step(const Environment & env)
|
|
Once inside the step function, an apvector of fish is made called fishList, and the environment's AllFish function is called again. fishList is set equal to env.AllFish().
After that, a for-next loop goes through fishList one element at a time, sending each element (and the environment) to the fish's Move function.
|
|
Fish::Move(Environment & env) const
|
|
The function starts by declaring a RandGen type called randomVals. A neighborhood type called nbrs is also declared, and is set equal to the value returned from EmptyNeighbors(env, myPos).
|
|
Fish::EmptyNeighbors(const Environment & env)
|
|
This function begins by declaring a neighborhood type called nbrs, and calls AddIfEmpty four times. This essentially checks to see if there's any empty cells on each side of the fish (using environment's isEmpty function) in the matrix. If that's the case, then the empty position is added to the list of possible movement positions in nbrs. Upon function completion, nbrs is returned.
|
|
Fish::Move(Environment & env)
|
|
After the neighborhood type is all set up, the function assigns the fish's position value to oldPos and replaces the fish's old position with an open position randomly selected from the neighborhood (by way of the select function).
Afterward, the environment's update function is called, and the old position of the fish (along with the fish that's currently being processed) is sent along with the call.
|
|
Environment::Update(const Position & oldLoc, Fish & fish)
|
|
As soon as this function starts, a fish of type emptyFish is declared using the default constructor. (This is also the type of fish that represents blank spaces in the matrix.) The program then checks (using InRange) to make sure that the position is in the matrix. If not, then the function terminates. If it is within range, then the function continues and checks to make sure that the fish that's being processed actually resides in the slot that it's moving from. If that's the case, then the fish is placed in its new position in the matrix and the emptyFish is placed in the fish's old slot. Then the function ends.
|
|
fishsim.cpp: main()
|
|
After the simulation function has completed and all the loops are finished, the display function is called again to print out the matrix after the updates. The step number is printed out, and a getline command sits immediately after that line in order to serve as a "press any key to continue" sort of command. After that, the loop starts again... then the program ends by returning 0.
There, that wasn't so bad, now was it? Fine kettle of fish if I do say so myself. :)
|
|