Esempio n. 1
0
/**************************************************************************************************
 ** Function:       void runGame(GameOfLife game, int scol, int waitMS, int cycles)
 ** Description:    Runs the seed to cycles iterations, with waitMS wait time.
 *************************************************************************************************/
void runGame(GameOfLife game, int scol, int col, int waitMS, int cycles){
	int currentCycle = 1;
	
	std::string msg = "hit any key to return to main";
	mvprintw(2,(col - strlen(msg.c_str()))/2,"%s",msg.c_str());	//print header menu
	
	for (int loop=0;loop<cycles;loop++){	//run the # of cycles
		char ch;
		mvprintw(0,2,"%d of %d",currentCycle,cycles);
		currentCycle++;
		nodelay(stdscr,TRUE);
		if((ch=getch())==ERR){}
		else {
			nodelay(stdscr,FALSE);
			return;
		}
	
		std::vector<std::vector<char> > v = game.run();
		
		move(3,scol);		//top line of *
		for(int i = 0 ; i < game.getRows() + 2 ; i++){
			addch('*');
		}
		
		for (int i = 0 ; i < v.size() ; i++){	//print vector
			move(4+i,scol);
			addch('*');
			for (int j = 0 ; j < v[i].size() ; j++){
				addch(v[i][j]);
			}
			addstr("*\n");
		}
		
		move(4 + game.getColumns(),scol);		//bottom line of *
		for(int i = 0 ; i < game.getRows() + 2 ; i++){
			addch('*');
		}
		
		refresh();
		usleep(waitMS);
	}
	nodelay(stdscr,FALSE);
}
Esempio n. 2
0
/*******************************
 * Function: main
 * --------------
 * reads in arguments and runs generations of those boards according to said
 * arguments.
 */
int main(int argc, char* argv[]){
  GameOfLife *g;
  bool usingGameboard = false;
  bool printGameboard = false;
  string gameboard = "";
  int iterations = 100000;
  int threads = 2;
  int boardsize = 64;

  // Declare the supported options.
  po::options_description desc("Allowed options");
  desc.add_options()
      ("help,h", "produce help message")
      ("iterations,i", po::value<int>(), "set number of iterations (default: 100,002) ")
      ("threads,t", po::value<int>(), "set number of threads (default: 2)")
      ("boardsize,b", po::value<int>(), "set board dimensions to N, results in a NxN array (default: 64x64)")
      ("gameboard,g", po::value<string>(), "set the gameboard file (default: random board)")
      ("printboard,p", "print the game board? (default: false)")
  ;

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);

  if (vm.count("help")) {
      cout << desc << "\n";
      return 1;
  }

  if (vm.count("iterations")) {
    iterations = vm["iterations"].as<int>();
  }
  if (vm.count("threads")) {
    threads = vm["threads"].as<int>();
  }
  if (vm.count("boardsize")) {
    boardsize = vm["boardsize"].as<int>();
  }
  if (vm.count("gameboard")) {
    usingGameboard = true;
    gameboard = vm["gameboard"].as<string>();
  }
  if (vm.count("printboard")) {
    printGameboard = true;
  }

  if (usingGameboard) {
    g = new GameOfLife(gameboard);
  } else {
    g = new GameOfLife(boardsize);
  }

  if (printGameboard)
      g->printBoard();

  //while (true){
  struct timeval start_t, stop_t;
  double total_time;

  gettimeofday(&start_t, 0);  // start timer for measured portion of code

  cout << "Board Size is " << g->getRows() << " x " << g->getCols() << endl;
  cout << "Running " << iterations << " iterations with " << threads <<
    " thread" << (threads != 1 ? "s" : "") << "..." << endl;

  for (int i=0; i<iterations; i++){
    g->runGeneration();
  }

  gettimeofday(&stop_t, 0);  // stop timer for measured portion of code

  total_time = (stop_t.tv_sec + stop_t.tv_usec*0.000001)
    - (start_t.tv_sec + start_t.tv_usec*0.000001);

  // don't want to include printf in timed code
  if (printGameboard)
      g->printBoard();

  printf("total time is: %g\n", total_time);

  delete g;
  return 0;
}