//  YOUSEF RABAH       CS35 Final Project       Dec. 18th           Drawer 1602

// The code is : 1 50 3 x 

#include "CS35lib.h"
#include <cstdlib>

void encode_it (char word[], char coded[])
{
	char letter;
	int counter;
	
	for (counter=0; counter <strlen(word) ;counter++ )
	{
		cout<<"Change letter "<<word[counter]<<" to: ";
		cin>>letter;
		coded[counter]=letter;
	}
	cout<<"This is the new word encoded "<<coded<<endl;
}	
void try_to_guess(char guess[], char word[])
{
	int counter = 0;
	int i,d=0, y=0;
	int c;
	cout<<"Try to guess the origional word (No letter repetition) : ";
	cin>>guess;
	if (strlen(guess) !=  strlen(word))
	{
	    cout<<"INVALID: Remember word is "<<strlen(word)<<" letters, so Try again: ";
	    cin>>guess;
    }

	while (counter < 5)
	{
		c =0;
		y++;
		for (i=0; i < strlen(word); i++)
		{
		   for(d=0; d < strlen(word); d++)
		   {
		      if (guess[i] == word[d])
			  {
				  c++;
			  }
		   }
		}
		
		if ( c == strlen(word))
		{

			counter =5 ;
			cout<<endl;
			cout<<"Congrats..you got all letter(s) right, from the word --> "<<word<<" <--  \n";
			cout<<"It took you "<< y<<" times to guess it ! \n\n";
			cout<<endl;
			
		}
		else 
		{
		   cout<<"You have "<<c<<" correct letter(s) from origional word ,try again: ";
		   cin>>guess;
		   if (strlen(guess) !=  strlen(word))
		   {
			    cout<<"INVALID: Remember word is "<<strlen(word)<<" letters, so Try again: ";
			    cin>>guess;
	       }
		   
		}
	}
	
}

void get_info_code(int& spaces,  int& width, int& high, char& chara)
{	
	cout<<"In order for this program to run \n Please Enter the 5 digit code now :";
	cin>>spaces;
	cin>>width;
	cin>>high;
	cin>>chara;
	while ((spaces != 1) || (width != 50) || (high != 3) || (chara != 'x'))
	{
		cout<<"Please enter the correct code: ";
		get_info_code(spaces, width, high, chara);
	}
}
void line_of (int length, char chara)
{
	int counter;
	for (counter= 1; counter <= length ;counter++ )
	{
		cout<<chara;
	}
	return;
}

void top (int width, int spaces, char chara )
{
	line_of (spaces, ' ' );
	line_of(width, chara);
	cout<<endl;
}


void middle (int width, int spaces, char chara)
{
	line_of ( spaces, ' ' );
	cout<<chara<<"    Welcome to Crypto & Guess Game";
	line_of (width - 36, ' ' );
	cout<<chara;
	cout<<endl;
	
}
char get_info (int& numb_info)
{
	
	char for_more_info;
	cout<<" => Press 1 to get more info about game and programer. \n";
	cout<<" => Press 2 to start the game. \n";
	cout<<" => Press 3  or any other letter to end program. \n";
	cin>>numb_info;
	if (numb_info == 1)
	{
		int zero;
		cout<<" >>>> Hello and welcome to Crypto then Guess game. <<<<\n\n\n";
		cout<<" - This game is played by two players.\n\n";
		cout<<" - Player one will be asked to enter a word, then step by step ";
		cout<<" player 1 will   encript a word of his choice. \n\n";
		cout<<" - After Player 1 encripts the word, Player 2 will try to guess ";
		cout<<" letters until he/she gets the correct word.\n\n";
		cout<<endl<<endl;
		cout<<"Programer:  YOUSEF B. RABAH \n";
		cout<<"Web-address: http://www.earlham.edu/~rabahyo \n";
		cout<<endl<<endl;
		cout<<" Press 0 to return to program OR any other number or letter to quit program. ";
		cin>>zero;
		if (zero == 0)
		{
			cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
			get_info (numb_info);
		}
	}
	if (numb_info == 2)
	{
		return 'l';
	}
	if (numb_info == 3)
	{
		char answer;
		cout<<"Are you sure you want to quit? ";
		answer = get_Y_or_N();
		if (answer == 'Y')
		exit(3);
		else 
		{
			cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
			get_info (numb_info);
		}
	
	}

	
		return 'r';
	
}
void main_mn (char y_n )
{
	int numb_info;
	cout<<"Go Back to Main Menu? (y/n) ";
	cin>>y_n;
	if ((y_n == 'y') || (y_n == 'Y'))
	{
		cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
		get_info(numb_info);
	}
	else 
	exit(3);
}


int main()
{	
	char word[8], new_word[8], guess[8];
	char letter, chara, da, y_n;
	int i=0, width, spaces, counter,r,  high, mid_count, numb_info;

	get_info_code(spaces, width, high, chara);
	cout<<endl;
	top( width, spaces, chara);
	for (mid_count=0 ; mid_count < high - 2 ;mid_count++ )
	{
		middle ( width, spaces, chara);
	}
	top (width, spaces, chara );
	cout<<endl;
	cout<<" What do you want to do today : \n\n";
	da = get_info ( numb_info);
	if (da == 'l')
	{

		cout<<"Player 1: \n";
		cout<<"Enter a 5 letter word (No letter repetition): ";
		cin >> word;
		 encode_it (word, new_word);
		cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
		cout<<"This is the new encoded word: "<<new_word<<endl;
		cout<<"Player 2: \n";
		try_to_guess (guess, word);	
		main_mn (y_n);
		
	}

	return 0;


}


