/* * ----------------------------------------------------- * CS2S03/SE2S03, October 2011 * Assignment 3, Question 2 * File: PE0413.cpp * ----------------------------------------------------- * This program illustrates the chain reaction in a * nuclear fission. The setting is a cubical box, the * bottom of which is a square grid of mousetraps. Each * of the mousetraps is loaded with balls. At the beginning * of the simulation, an additional ball is released from * the top of the box and falls on a random mousetrap. * That mousetrap springs and shoots its balls into the * air. Each of the balls bounces around the sides of * the box for random number of time units and eventually * lands on the bottom, where it likely to set off a * mousetrap. The simulation runs until there are no balls * in the air. * ----------------------------------------------------- */ #include "genlib.h" #include "random.h" #include "vector.h" #include /* Simulation parameters */ const int GRID_SIZE = 25; const int LOAD = 2; // number of balls loaded on each trap const int MIN_CYCLES = 1; // min bouncing cycles const int MAX_CYCLES = 4; // max bouncing cycles /* Private function prototypes */ void RunSimulation(); void ReportResults(int elapsedTime, int nSprungTraps, int maxBallsInAir); /* Main program */ int main() { Randomize(); RunSimulation(); return 0; } /* * Function: RunSimulation * Usage: RunSimulation(); * ----------------------------------------------------- * This function runs the actual simulation. */ void RunSimulation() { int elapsedTimeUnits = 0; int nBallsInAir = 1; int maxBallsInAir = 1; int nSprungMouseTraps = 0; /* initialize bottom grid */ bool trap[GRID_SIZE][GRID_SIZE]; for (int x = 0; x < GRID_SIZE; x++) for (int y = 0; y < GRID_SIZE; y++) trap[x][y] = true; /* bouncingBalls[k] is the number of balls with k cycles * remaining in air */ Vector bouncingBalls; bouncingBalls.add(1); // the first ball // initially no other balls in the air for (int i = 1; i <= MAX_CYCLES; i++) { bouncingBalls.add(0); } while (nBallsInAir > 0) { int nLanding = bouncingBalls.getAt(0); // number of landing balls for (int i = 0; i < nLanding; i++) { nBallsInAir--; int row = RandomInteger(0, GRID_SIZE - 1); // trap hit int col = RandomInteger(0, GRID_SIZE - 1); if (trap[row][col]) { nSprungMouseTraps++; for (int j = 0; j < LOAD; j++) { // shoot balls bouncingBalls[RandomInteger(MIN_CYCLES, MAX_CYCLES)]++; nBallsInAir++; } trap[row][col] = false; } } if (nBallsInAir > maxBallsInAir) maxBallsInAir = nBallsInAir; bouncingBalls.removeAt(0); bouncingBalls.add(0); elapsedTimeUnits++; } ReportResults(elapsedTimeUnits, nSprungMouseTraps, maxBallsInAir); } /* * Function: ReportResults * Usage: ReportResults(); * ---------------------------------------------------- * This function reports the results of the simulation. */ void ReportResults(int elapsedTime, int nSprungTraps, int maxBallsInAir) { float percentage = float (nSprungTraps) / (GRID_SIZE * GRID_SIZE) * 100; cout << "Elapsed Time Units: " << elapsedTime << endl; cout << "Sprung Mouse Traps Percentage: " << percentage << "%" << endl; cout << "Max Number of Balls in Air: " << maxBallsInAir << endl; }