
/**************************************
**  Dice Rollr Program
**    An incredibly simple dice software
***************************************
** License: Mewcetti amended MIT
**  http://mewcetti.com/?p=8
***************************************
** Feel free to use this program for 
**  whatever, as long as it fits within
**  the scope of the license.  Also if
**  you use this program for commercial
**  purposes, I'd like to know about it
**   plz
**************************************/

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <time.h>

using namespace std;

int main()
{
	bool rerun = true;

	do
	{
		// Create a few variables for our purposes
		unsigned int diceSides;
		vector<int> randDataTab;
		unsigned int randData = 0;
		char crerun;

		// Instructions for the user and accept their input

		cout << "Please input dicetype(numeric only): ";
		cin >> diceSides;

		// Clear out the input stream
		cin.sync();

		// If the input fails then exit
		if( cin.fail() )
		{
			cout << endl << "Error Encountered: Data entered not accepted as valid...exiting" << endl;
			exit(1);
		}

		//Just for this program, nothing greater than 1000
		else if(diceSides > 1000)
		{
			cout << endl << "Error Encountered: Data entered is greater than 1000...exiting" << endl;
			exit(1);
		}


		// initialize the random number generator according to what time it is
		srand(static_cast<unsigned int>(time(0)));

		// We're putting a pseudorandom value into an element of a vector using a for loop
		// this will just add a little bit of unpredictability to our program
		for(int index = 0; index < diceSides; index++)
		{
			randDataTab.push_back( (rand() * rand()) % diceSides );
			
		}


		// randData is going to decide which element of the vector to use
		//  So we're populating it with a random value
		randData = ( rand() * rand() ) % diceSides;

		// And output the random number
		cout << endl << "Number: " << randDataTab[randData] << endl;

		// Ask the user if they want to rerun, take their input, and rerun if they input yes
		cout << endl << "Rerun?(y|n): ";
		
		cin >> crerun;
 
		// Clear out the input stream
		cin.sync();

		if( cin.fail() )
		{
			cout << endl << "Error, data entered was not accepted as valid...exiting" << endl;
			exit(1);
		}

		if(tolower(crerun) == 'y')
		{
			rerun = true;
		}

		else
		{
			rerun = false;
		}


	} while(rerun == true);

	return 0;
}