Skip to content

Instantly share code, notes, and snippets.

@karthikkondagalla
Created August 27, 2017 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikkondagalla/a09ef1761ed24357f81515ba886ce0dd to your computer and use it in GitHub Desktop.
Save karthikkondagalla/a09ef1761ed24357f81515ba886ce0dd to your computer and use it in GitHub Desktop.
Ball Game Simulation in C++
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include "ball.h"
vector<vector <bool> > traps(GRID_SIZE, vector<bool>(GRID_SIZE));
vector<unsigned> ballCycles;
int main()
{
unsigned B_air = 1;//Balls in air
cout<<"Simulation started"<<endl;
init_sim(traps, ballCycles);//funtion call for init_sum function
print_grid(traps, CLOCK, B_air);//function call for print_grid function
while(B_air > 0)//This loop repeats itself untill there are no more balls in air
{
CLOCK++;
B_air = release_balls(traps, ballCycles);//funtion call for release_balls function
print_grid(traps,CLOCK,B_air);//function call for print_grid function
if(B_air > max_balls) max_balls = B_air;
}
print_stat(traps, max_balls, CLOCK);//function call for print_stat function
return(0);
}
/****************************************************************
FUNCTION: init_sim()
ARGUMENTS: (vector <vector <bool> >& traps, vector<unsigned>& ballCycles)i.e a 2D vector and a 1D vector.
RETURNS: Nothing
NOTES: This routine initializes each mousetrap in the traps grid to logical value true, initializes the RNG and also inserts the cycle 1 for the first ball in the vector ballCycles.
****************************************************************/
void init_sim(vector <vector <bool> >& traps, vector<unsigned>& ballCycles)
{
unsigned i=0,j=0;
srand(time(0));
for(i = 0;i<traps.size();i++)//setting every value as true initialy
{
for(j = 0;j<traps[i].size();j++)
{
traps[i][j] = true;
}
}
ballCycles.push_back(MIN_CYCLES);
}
/****************************************************************
FUNCTION: release_balls()
ARGUMENTS: (vector<vector<bool> >& traps, vector<unsigned>& ballCycles)i.e a 2D vector and a 1D vector.
RETURNS: Unsigned Integer.
NOTES: This function handles the mousetraps and then generate cycles for the newly sprung balls if any and in the end it returns the total no. of balls in air at that logical simulation
****************************************************************/
unsigned release_balls(vector<vector<bool> >& traps, vector<unsigned>& ballCycles)
{
unsigned i = 0;
int pos_x = 0;
int pos_y = 0;
int ballsInAir = 0;
for(i=0;i<ballCycles.size();i++)
{
if(ballCycles[i] != 0)
{
ballCycles[i]--;
if(ballCycles[i] == 0)
{
pos_x = RNG(0, GRID_SIZE-1);
pos_y = RNG(0, GRID_SIZE-1);
if(traps[pos_x][pos_y] == true)
{
ballsInAir += 2;
traps[pos_x][pos_y] = false;//making it to false if it was initialy true
}
}
}
}
for(int j=0;j<ballsInAir;j++)
{
ballCycles.push_back(RNG(MIN_CYCLES, MAX_CYCLES));
}
for(i=0;i<ballCycles.size();i++)
{
if(ballCycles[i] > 0) ballsInAir++;
}
return ballsInAir;//returning balls in air at that logical simulation
}
/****************************************************************
FUNCTION: RNG
ARGUMENTS: (int low, int high)i.e the integer range
RETURNS: Integer
NOTES: This function generates a random integer no. between the given range
****************************************************************/
int RNG(int low, int high)
{
int r=(rand()%(high-low+1))+low;
return r;
}
/****************************************************************
FUNCTION: print_grid
ARGUMENTS: (const vector<vector<bool>> &traps, const unsigned& simClock, const unsigned& B_air)
RETURNS: Nothing
NOTES: This function prints the current values of the simulation clock,total number of balls and the grid.
****************************************************************/
void print_grid(const vector<vector<bool>> &traps, const unsigned& simClock, const unsigned& B_air)
{
if(((simClock%PRN_CYCLES) == 0) || (B_air == 0))
{
cout<<"Simulation clock : "<<simClock;
cout<<" Balls in air : "<<B_air<<endl;
for(unsigned i = 0;i<traps.size();i++)
{
for(unsigned j = 0;j<traps[i].size();j++)
{
if(traps[i][j] == true) cout<<"X ";
else cout<<". ";
}
cout<<endl;
}
cout<<endl;
}
}
/****************************************************************
FUNCTION: print_stat
ARGUMENTS: (const vector<vector<bool> > &traps, const unsigned& maxNoBallsInAir, const unsigned& simClock)
RETURNS: Nothing
NOTES: This function prints the total simulation time clock, maximum number of balls in the air and computes the total number of mousetraps have sprung in the grid traps in the simulation and print out the percentage of sprung traps.
****************************************************************/
void print_stat(const vector<vector<bool> > &traps, const unsigned& maxNoBallsInAir, const unsigned& simClock)
{
int sprungTraps = 0;
cout<<"Total simulation clock: "<<simClock<<endl;
cout<<"Max no of balls in air: "<<maxNoBallsInAir<<endl;
for(unsigned i = 0;i<traps.size();i++)
{
for(unsigned j = 0;j<traps[i].size();j++)
{
if(traps[i][j] == false)
sprungTraps++;
}
}
cout<<"Total no. of sprung traps: "<<sprungTraps<<endl;
cout<<"Percentage of traps Sprung: "<<((sprungTraps/625.0)*100)<<endl;
cout<<" Max number of balls in air : "<<maxNoBallsInAir<<endl;
}
#ifndef H_BALL
#define H_BALL
#include "/home/cs689/common/689.h"
#endif
#define GRID_SIZE 25
#define MIN_CYCLES 1
#define MAX_CYCLES 4
int PRN_CYCLES = 10;
int CLOCK = 0;
unsigned int max_balls = 0;
void print_grid(const vector<vector<bool> > &traps, const unsigned& clock, const unsigned& noBallsInAir);
void print_stat(const vector<vector<bool> > &traps, const unsigned& noBallsInAir, const unsigned& clock);
void init_sim(vector <vector <bool> >& traps, vector<unsigned>& ballCycles);
unsigned release_balls(vector<vector<bool> >& traps, vector<unsigned>& ballCycles);
int RNG(int low, int high);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment