free web hosting | business web hosting | dot com domain | reseller hosting | free domain hosting no ads | joomla templates | free mysql hosting


Introduction

News and Updates

Short Summary

MBCS Files

Detailed Study

I. Introduction

II. Creation

III. Displaying

IV. Simulation

Misc. Sections

Other Files

Contact

Links


Case Study: Short Summary

Printable Copy: Case Study: Short Summary
Welcome to the quick walkthrough of the case study. Essentially, this is a brief run through the MBCS (part II) program using a set of sample data. For our purposes, fish.dat contains the values 4 4, 0 0, 1 1, 2 2, and 3 3 (in the standard and coherent form that the program cam process).

The first line of code in the driver program declares an ifstream type called input, with the source file being "fish.dat". On the next line, input is passed to the Environment constructor.

The Environment constructor starts out by declaring an apmatrix of fish called "myWorld" (with no rows or columns) and two integer types called myFishCreated and myFishCount, with each set equal to zero. After that, the program creates some temporary integer values (numRows, numCols, row, col), and proceeds to check if fish.dat contains any data that can be coherently read for the program. If that's determined to be the case (essentially, if inserting data from the file into the temporary values was successful), then the matrix is resized based on the values in the first slot. Since the first line in fish.dat is "4 4", the program has just resized myWorld to be a 4x4 matrix (numRows contained a 4, and so did numCols).

Next, the program enters a while loop to deposit fish into the matrix. The program then takes the second line, "0 0", sets row and col equal to these values respectively, then calls the AddFish function.

However, prior to calling the AddFish function, the program first calls the Position constructor. The position constructor makes a struct type that contains a row value and a column value. Contained within the new position type created (which is called "pos" in the AddFish function) are values for a row and a column.

Now, the AddFish function checks the initial space right from the start to see if there's a fish already there. Since there isn't, the program continues. Before the Fish constructor is called, myFishCreated is incremented. It is then passed into the Fish constructor along with the position structure.

In the Fish constructor, myId is set equal to myFishCreated, the fish's position (pos) is stored in the myPos slot, and amIDefined, a boolean type, is set equal to true. Afterward, myFishCount is incremented back in the AddFish function. Once that's done, the program returns to the while loop in the Environment constructor. The AddFish process is then repeated as long as there's comprehensible data in fish.dat. When the constructor finishes, the matrix is filled with blank fish, and defined fish reside at (0,0), (1,1), (2,2), and (3,3).

Once Environment env has successfully been created, the program returns to the driver program. Once there, the program creates a Display type called display, an apstring called s, a Simulation type called sim, and two integer types called step and numSteps. After that, display's show function is invoked, with the Environment env being passed into it.

The show function starts by creating a constant integer type called WIDTH and setting it equal to 1. It then creates a series of integer types. Rows is set equal to env.NumRows(), which is the amount of rows in env's matrix. Cols is set equal to env.NumCols(), which is the number of columns in env's matrix. FishIndex is set equal to zero, and NumFish, r, and c are all created with no specific value. Finally, a Position type called pos is created. After that, an apvector of Fish called fishList is created, and fishList is set equal to the value returned by Environment env's AllFish function.

In AllFish, an apvector of Fish called fishList is also created, and its length is equal to myFishCount. Integer types called r, c, and k are also created, and an integer type called count is set equal to 0. An apstring called s is also made, and it's filled with an empty space (" "). Afterward, the program uses a nested loop to go row by row through the matrix in order to find defined fish. Once it detects a defined fish (through usage of the Environment's IsUndefined function), then the fish that is at that location is copied into fishList, and a count is incremented.

After every one of the fish in the matrix is placed into fishList, another loop starts up for the debug print. However, this really isn't relevant to the main functioning of the program, so we'll just skip it. After the debug printouts, AllFish returns fishList.

After fishList is returned to the Show function, numFish is set equal to the length of the array. After that, an if statement checks to see if the array has no elements. Since this array does have elements, pos is set equal to the value of myPos of the fish in the first array slot. Then, a series of nested loops starts up to print out the matrix row by row. If the row and column values are concurrent with the fish's position, then the ShowMe function is called (which prints out a letter for the fish on the screen) and fishIndex is incremented. Pos is then reassigned to the position of the fish occupying the next slot in fishList. When the row and column values don't match up with a value from pos, then a blank space is outputted instead. At the end of the function, a blank line is couted, and the program returns to the main.

The main then prints out a line that says the program is initialized. Another line is printed out asking how many steps the program should run, and the following two lines receive the input from the user. The program then enters the last stretch of code by starting a for loop to the number of steps the user inputted.

The first thing within the for loop is a line that calls Simulation sim's Step function and passes Environment env. Step makes a vector of fish called fishList, gathers up all of the fish by calling env's AllFish function again. Then a for loop is started that moves each fish through the use of the Fish class' Move function.

In the Move class, a RandGen type called randomVals is created. A Neighborhood called nbrs is set equal to the value returned from the EmptyNeighbors function, with env and myPos being passed into it.

Essentially, the function creates another Neighborhood called nbrs, checks to see if the positions north, south, east, and west of the fish are all open, and then returns a neighborhood that contains a list of possible positions for where the fish can move.

Once nbrs is returned, then an if statement checks to see if nbrs contains any data. If it does, then a position type called oldPos is set equal to the current position of the fish, and the fish's new position is randomly set equal to one of the positions contained within nbrs (using the RandGen function). Afterward, the Environment update function is called, and the current Fish is passed into it along with oldPos.

Update makes an empty fish using the default fish constructor. It then checks to see if the fish that got moved is actually the fish that's being handled by the program. Once it's sure of that, a position type called NewLoc is created and set equal the current fish's position. The fish is then placed in its respective positon in the matrix, and an empty fish made by the default constructor is stuffed into the slot pointed to by oldPos.

After that, Move finishes up and goes back to the driver program, where the matrix is once again shown after all of the moves, and a "press any key to continue" line allows the user to view each move. After that, the loop continues for the designated number of turns, and when all is said and done, the program returns zero and returns almost all of the memory it happily gobbled up during its processes and goes home.