#include <iostream>
#include <ctime>
#include <climits>

using namespace std;
void print_doors(int guess, int giveAway, int prob);

int main(void)
{
	short door[3];
	int prob;
	int j, guess, new_guess, giveAway;
	char answer;

	srand( (unsigned)time(NULL) );
	
	for(j=0; j < 1; ++j){
		//prob = (double) rand() / RAND_MAX;
		prob = rand() % 3;
		//cout << "Initial prob = " << prob+1 << endl;
		// set all doors to false
		door[0] = 0;
		door[1] = 0;
		door[2] = 0;
		
		// set one door to true
		door[prob] = 1;
		
		/*if ( (prob >= 0) && (prob <= 1.0/3) ){
			door[0] = 1;
		}else if( (prob > 1.0/3) && (prob < 2.0/3) ){
			door[1] = 1;
		}else if( (prob >= 2.0/3) && (prob <= 1.0) ){
			door[2] = 1;
		}*/
		/*cout << prob << endl;
		for (i=0; i<=2; ++i){
			cout << door[i] << " " << endl;
		}
		cout << "=" << endl;*/
		
		cout << "Enter your guess (1,2,3) >";
		cin >> guess;

		// because our array is 0 based
		guess = guess -1;

		do{
			//cout << "loop..." << endl;
			giveAway = rand() % 3;
			//cout << "giveAway attempt = " << giveAway << endl;
		}while ( (giveAway == prob) || (giveAway == guess) );

		//print_doors(guess, giveAway, prob);

		cout << "You have chosen door #" << guess+1 << endl;
		cout << "I'll open another door for you.  Door #" << giveAway+1 << " is empty" << endl;
		cout << "Would you like to switch to the unopened door?";

		cin >> answer;

		if (answer == 'y'){
			new_guess = 0;
			//cout << "guess is starting at #" << new_guess+1 << endl;
			while ( (new_guess == guess) || (new_guess == giveAway) ){
				new_guess = new_guess+1;
				//cout << "guess is now door #" << new_guess+1 << endl;
			}
		}else{
			new_guess = guess;
		}
		
		cout << "you have chosen door #" << new_guess+1 << endl;
		
		if ( new_guess == prob ){
			cout << "Congratulations!  You guessed correctly!" << endl;
		}else{
			cout << "I'm sorry, the correct door was door #" << prob+1 << endl;
		}
	}

	return 0;
}

void print_doors(int guess, int giveAway, int prob)
{
	// print which doors are which.
	prob = prob + 1;
	guess = guess + 1;
	giveAway = giveAway + 1;

	cout << "prob = " << prob << endl;
	cout << "guess = " << guess  << endl;
	cout << "giveAway = " << giveAway << endl;
	
}
