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

void encode_it (char word[], char coded[])
{
	char letter;
	int counter;
	cout<<"Enter a word (No letter repetition): ";
	getline(word);
	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;
	int c;
	
	cout<<"Try to guess the origional word (No letter repetition) : ";
	cin>>guess;

	while (counter < 5)
	{
	    c =0;
		for (i=0; i < strlen(word); i++)
		{
		   for(d=0; d < strlen(word); d++)
		   {
		     
		      if (guess[i] == word[d])
			  {
				  c++;
			  }
			     
		   }
		}
		
		if (strlen(guess) !=  strlen(word))
	        {
		    cout<<"INVALID: Remember word is "<<strlen(word)<<" letters, so Try again: ";
		    cin>>guess;
	        }
		if ( c == strlen(word))
		{
			counter = 5;
			cout<<endl;
			cout<<"Congrats..you got all letters right, from the word --> "<<word<<" <--\n\n";
		}
		else 
		{
		
		   cout<<"You have "<<c<<" correct letters from origional word ,try again: ";
		   cin>>guess;
		   
		}
	}
	
}
void get_info_code(int& spaces,  int& width, int& high, char& chara)
{	
	
	cin>>spaces;
	cin>>width;
	cin>>high;
	cin>>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;
	
}
int 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 to end program \n\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  player 1 will";
		cout<<"    encript a word of his choice. \n\n";
		cout<<" - After Player 1 encripts the word, Player 2 will try to guess letters until";
		cout<<" 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)
	{
		/*char  word[10], guess[10], coded[10];
		cout<<"Player 1: \n";
		encode_it (word, coded);
		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: "<<coded<<endl;
		cout<<"Player 2: \n";
		try_to_guess(guess, word);
		*/
		return 1;
	}
	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 3;
		
	
}

int main()
{
	int i=0;
	char word[8], new_word[8], guess[8];
	char letter;
	int width, spaces, counter,r,  high, mid_count, numb_info;
	char chara;
	
	cout<<"In order for this program to run \n Please Enter the 5 digit code now :";
	get_info_code(spaces, width, high, chara);
	while ((spaces != 1) || (width != 50) || (high != 3) || (chara != 'x'))
	{
		cout<<"Please enter the correct code: ";
		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";
	r = get_info ( numb_info);
	if (r == 1)
	{
		char  word[10], guess[10], coded[10];
		cout<<"Player 1: \n";
		encode_it (word, coded);
		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: "<<coded<<endl;
		cout<<"Player 2: \n";
		 try_to_guess(guess, word);
	}
	return 0;
}
