//********************************************************* // // Purpose: simulate a mouse wandering around on an island // looking for a bridge. If the mouse doesn't drown and // finds no bridge within 100 turns, it will starve. Run // the simulation 1000 times and report the number of // times that the mouse starves, the number of times the // mouse drowns, and the number of times the mouse // escapes across a bridge. // // Input File Format // // First line: number of rows and cols (both <= MAX) // // Subsequent lines: the array representing the island, // surrounding water, and bridges: // 0 ==> water // 1 ==> land // 2 ==> bridge // 3 ==> initial location of mouse // // D Searls // ****** Replace this line with your name ******/ // Asbury College // Nov 2007 //********************************************************* #include #include #include #include using namespace std; // Constant declarations const int MAX = 20; // maximum number of rows and columns const int NUM_TRIALS = 1000; // number of times simulation is to be run const int MAX_MOVES = 100; // number of moves by mouse until starvation const int WATER = 0; // Indicates a water square on map const int LAND = 1; // Indicates a land square on map const int BRIDGE = 2; // Indicates a bridge square on map /********** Feel free to add more constants **********/ // Declaration of island type struct islandType { int map[MAX][MAX]; // the array representing the island int numRows, numCols; // dimensions of the island int startRow, startCol; // Starting location of mouse }; //--------------------------------------------------------- // openFileOK // // Return: true if user chose to open a file and false if // user chose the quit option. // // In/Out Parameter: file //--------------------------------------------------------- bool openFileOK(ifstream& file) { bool success = false; string filename; cout << "Enter the file name ('Q' to quit): "; cin >> filename; cout << endl; while ((filename != "Q") && (filename != "q") && !success) { file.open(filename.c_str()); if (!file.fail()) success = true; else { file.clear(); cout << "Could not open file " << filename << "!\n\n"; cout << "Enter the file name ('Q' to quit): "; cin >> filename; cout << endl; } } return success; } //--------------------------------------------------------- // readIsland // // Purpose: read the island data from the file. // // Pre-condition: the file is open and there are no errors // in the data. // // In/Out Parameter: file // // Out Parameter: island //--------------------------------------------------------- void readIsland(ifstream& file, islandType& island) { /********** Implement this function **********/ } //--------------------------------------------------------- // fateOfMouse // // Purpose: simulate wandering mouse until mouse drowns, // escapes, or starves. // // Returns: the location (water, land, or bridge) of the // mouse at the end of the simulation. The location of // the mouse determines its fate. The mouse drowns if it // is in water, escapes if it is on a bridge, and starves // if it is on land. // // In Parameter: island //--------------------------------------------------------- int fateOfMouse(const islandType& island) { /********** Implement this function **********/ } //--------------------------------------------------------- // displayMap // // Purpose: display the map of the island. // // In Parameter: island //--------------------------------------------------------- void displayMap(const islandType& island) { /********** Implement this function **********/ } //--------------------------------------------------------- // displaySummaryResults // // Purpose: Display the number of times that the mouse // starved, the number of times that it drowned, and the // number of times that it escaped. // // In Parameters: ??? //--------------------------------------------------------- void displaySummaryResults(???) { /********** Implement this function **********/ } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { islandType island; // the island ifstream infile; // the input file /********** Feel free to add more variables **********/ while (openFileOK(infile)) { readIsland(infile, island); infile.close(); infile.clear(); // Write the code that simulates the experiment // 1,000 times counting the number of times the // mouse starves, the number of times the mouse // drowns, and the number of times the mouse // escapes. displayMap(island); displaySummaryResults(???); } return 0; }