コード例 #1
0
int runSim(int a1, int a2, int a3){
	BinaryHeap<Soldier> test;
	test.buildSoldiers(a2, a3);
	return test.events();
	test.makeEmpty();

}
コード例 #2
0
ファイル: sim300.cpp プロジェクト: jseuribe/335_project3
int main(int argc, char *argv[] )
{	

	srand (time(NULL));

	clock_t start;

	double seconds_since_start;

        BinaryHeap<soldier> heap;
	
	soldier nextUp;

	vector<int> alive_Spartans, alive_Persians;

	

	int total_Spartans, total_Persians, increase_Amount, time_Elapsed = 0, spartan_Count = 0, persian_Count = 0;
	int numberOfSpartanVictories = 0, numberOfPersianVictories = 0;

	double sum_Spartans = 0, sum_Persians = 0, sum_Time = 0, spartan_Squared= 0, persian_Squared = 0, time_Squared = 0; 
	double spartan_Final_Average = 0, persian_Final_Average = 0, time_Final_Average = 0, omega_Spartan = 0, omega_Time = 0;
	long double omega_Persian = 0;

	vector<double> soldier_count_Spartans, soldier_count_Persians, time_Count;

	total_Spartans = atoi(argv[2]);
	total_Persians = atoi(argv[3]);
	
	spartan_Count = total_Spartans;
	persian_Count = total_Persians;

	int numberOfTimes;
	numberOfTimes = atoi(argv[1]);

	cout << "You have set the simulations to run for: " << numberOfTimes << " times." << endl;
	cout << "You have selected the number of spartans to be: " << spartan_Count << endl;
	cout << "You have selected the number of persians to be: " << persian_Count << endl;

	if(total_Spartans == 0 || total_Persians == 0){
		cout << "what are you doing" << endl;
		exit(1);
	}

	string outputFile;
	cout << "Enter the name of the file to output results to: " << endl;
	cin >> outputFile;

	ofstream file(outputFile);
	
	
	//cout << "How many times would you like to simulate the battle?" << endl;
	

	//char begin;

	cout << "You have set the simulations to run for: " << numberOfTimes << " times." << endl;
	cout << "You have selected the number of spartans to be: " << spartan_Count << endl;
	cout << "You have selected the number of persians to be: " << persian_Count << endl;
	//cout << "Enter any character to proceed..." << endl;
	//cin >> numberOfTimes;
	
	for(int i = 0; i < numberOfTimes; i++)
	{
		start = clock();
		generate_Soldiers(heap, total_Spartans, total_Persians, alive_Spartans, alive_Persians);

		//invigorate(heap, alive_Spartans, total_Spartans);


		//cout << "The ID's of all our Spartans: " << endl;
		//printOutIDs(alive_Spartans, total_Spartans);
		//cout << "The ID's of all our Persians: " << endl;
		//printOutIDs(alive_Persians, total_Persians);
	
		cout << "Our heap reports these soldiers: " << endl;
		//heap.printAllSoldiers();
		cout << "------------------------Let the battle begin!-------------------------" << endl;
		while( spartan_Count > 0 && persian_Count > 0)
		{
			//cout << "Spartans left: " << spartan_Count << endl;
			//cout << "Persians left: " << persian_Count << endl;

			nextUp = heap.findMin();//Peek at the first soldier. This is the soldier whose turn is next

			time_Elapsed = nextUp.return_actionTime();
			//cout << "Next up: " << "soldier number: " << nextUp.return_iD() << endl;
			//cout << "This soldier's turn is at: " << nextUp.return_actionTime() << endl;
			//if(nextUp.return_Faction() == true) //cout << "A Spartan! " << endl;
			//else //cout << "A Persian!" << endl;
			if(nextUp.return_Faction()  == true) processTurn(heap, alive_Persians, persian_Count);
			else processTurn(heap, alive_Spartans, spartan_Count);

			//The soldier's key is edited; his next turn is determined by adding a random value to his turn time atm.
			if(nextUp.return_Faction() == true) heap.increase_by_iD(nextUp.return_iD(), rand() % 7 + 1);
			else if(nextUp.return_Faction() == false) heap.increase_by_iD(nextUp.return_iD(), rand() % 51 + 10);

			//cout << "After the event, our heap looks like: " << endl;
			//cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
			//heap.printAllSoldiers();
			//cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
 
		}
	
		

		//cout << "our heap reports these soldiers: " << endl;
		//heap.printAllSoldiers();


		cout << "Spartans remaining: " << spartan_Count << endl;

		sum_Spartans = sum_Spartans + spartan_Count;
		soldier_count_Spartans.push_back(spartan_Count);

		cout << "Persians remaining: " << persian_Count << endl;

		sum_Persians = sum_Persians + persian_Count;
		soldier_count_Persians.push_back(persian_Count);

		cout << "The ID's of all our Spartans: " << endl;

		file << "Output for simulation: " << i << endl;
		file << "-----------------------" << endl;
		file << "Remaining soldiers: " << endl;
		file << "Spartans: " << spartan_Count << " Persians: " << persian_Count << endl;

		//printOutIDs(alive_Spartans, total_Spartans);
		cout << "The ID's of all our Persians: " << endl;
		//printOutIDs(alive_Persians, total_Persians);

		cout << "*****************************************************" << endl;
		cout << "******************THE WINNER IS..********************" << endl;
		if(spartan_Count == 0){
			 cout << "The Persians are victorious!" << endl;
			 numberOfPersianVictories++;

			 file << "The Persians have won simulation: " << i << endl;

		}
		else{
			 cout << "The Spartans are victorious!" << endl;
			 numberOfSpartanVictories++;
			
			 file << "The Spartans have won simulation: " << i << endl;
		}

		heap.makeEmpty();	
		spartan_Count = total_Spartans;
		persian_Count = total_Persians;
		alive_Spartans.clear();
		alive_Persians.clear();


		seconds_since_start = (double)(clock() - start)/CLOCKS_PER_SEC;
		cout << "This battle took: " << seconds_since_start << " seconds."  << endl;
		file << "Battle " << i << " took a total of: " << seconds_since_start << " seconds." << endl;
		time_Count.push_back(seconds_since_start);
		sum_Time += seconds_since_start;
		
		file << "------------------" << endl;
		
	}
	
	file << "~~~~~~~~~~~~~~~~~~~~~SIMULATIONS HAVE ENDED~~~~~~~~~~~~~~~~~~" << endl;

	cout << "The Spartans have won: " << numberOfSpartanVictories << " time(s)" << endl;
	cout << "The Persians have won: " << numberOfPersianVictories << " time(s)" << endl;
	
	spartan_Final_Average = arithMean(sum_Spartans, numberOfTimes);
	persian_Final_Average = arithMean(sum_Persians, numberOfTimes);
	time_Final_Average = arithMean(sum_Time, numberOfTimes);

	cout << "Average number of living Spartans: " << spartan_Final_Average << endl;
	cout << "Average number of living Persians: " << persian_Final_Average << endl;
	cout << "Average battle length: " << time_Final_Average << endl;

	omega_Spartan = standard_deviation(soldier_count_Spartans, numberOfTimes, spartan_Final_Average);
	omega_Persian = standard_deviation(soldier_count_Persians, numberOfTimes, persian_Final_Average);
	omega_Time = standard_deviation(time_Count, numberOfTimes, time_Final_Average);

	cout << "Std. dev of living Spartans: " << omega_Spartan << endl;
	cout << "Std. dev of living Persians: " << omega_Persian << endl;
	cout << "Std. dev of battle length: " << omega_Time << endl;

	file << "TOTAL BATTLES WON BY EACH TEAM: " << endl;
	file << "Spartan victories: " << numberOfSpartanVictories << endl;
	file << "Persian victories: " << numberOfPersianVictories << endl;
	file << "STATISTICS:" << endl;
	file << "Average number of living Spartans: " << spartan_Final_Average << endl;
	file << "Average number of living Persians: " << persian_Final_Average << endl;
	file << "Average battle length: " << time_Final_Average << endl;
	file << "Std. Dev of living Spartans: " << omega_Spartan << endl;
	file << "Std. Dev of living Persians: " << omega_Persian << endl;
	file << "Std. Dev of battle length: " << omega_Time << endl;

	//cout << "I will show you where each soldier is in the heap: " << endl;
	
	//heap.determine_hash_positions(10);


	//heap.remove(5, removed);

	//cout << removed.return_iD() << endl;
	//cout << removed.return_actionTime() << endl;
	//Test min remove snippet

	/*soldier min;
	while(!heap.isEmpty()){
	cout << "1 if not empty, 0 if empty: " << !heap.isEmpty() << endl;
	min = heap.findMin();
	cout << "This soldier's ID is: " << new_Mook.return_iD() << endl;
	if(new_Mook.return_Faction() == true) cout << "Tis a Spartan!" << endl;
	else cout << "tis a persian" << endl;
	heap.deleteMin(min);

	}*/

	cout << "Done!" << endl;
	return 0;
}