#include <iostream.h>
#include <string.h>
#include "Word.h"

Word::Word(char secretWord[])
{
	//
	// pre-condition: NULL character '\0' must exist at end of array.
	//
	// post-condition: Private member variable arrays are initialised.
	//				   Word is formatted into '** ***' secret word
	//                 format.

	setWord(secretWord);

}

void Word::setWord(char secretWord[])
{
	//
	// pre-condition: NULL character '\0' must exist at end of array.
	//
	// post-condition: Private member variable arrays are initialised.
	//				   Word is formatted into '** ***' secret word
	//                 format.

	stringCopy(secretWord,phrase);	// copy secretWord array into private phrase array.

	// initialise formatedWord array.

	formatWord();	// initialise formatedWord array.
					// format word into '** ***' secret word format.

}

void Word::formatWord()
{
	//
	// pre-condition: Private member array 'phrase' contains 
	//				  character array with NULL character at end.
	//		
	// post-condition: Phrase is stored in an private member array 
	//                 'formatedWord' where each letter is represented 
	//                 by a '*' and spaces as ' '.
	//

	int count;

	for (count = 0; phrase[count] !='\0'; count++)
	{
		if (phrase[count] !=' ')
			formatedWord[count] = '*';
		else
			formatedWord[count] = ' ';
	}

	formatedWord[count] = '\0';
}

void Word::getFormatedWord(char wordDisplay[MaxWordLength]) const
{
	//
	// post-condition: Copies private formatedWord array into 
	//				   wordDisplay array.
	//				   The formatedWord contains letters which
	//                 have been guessed correctly.
	//

	int count;

	for (count = 0; formatedWord[count] != '\0'; count++)
		wordDisplay[count] = formatedWord[count];

	wordDisplay[count] = '\0';
}


int Word::checkLetterInWord(char letter)
{
	//
	// post-condition: return value = 1 if letter is in word.
	//			       return value = 0 if letter is not in word.
	//				   '*' in formatedWord array will be changed to 'letter'
	//				   if the letter is in the phrase.
	//

	int count, foundLetter = 0;

	// Checks each letter in turn to see if it is the same as
	// parameter.

	for (count = 0; formatedWord[count] != '\0'; count++)
	{
		if (phrase[count] == letter)
		{
			formatedWord[count] = letter;
			foundLetter = 1;
		}
	}
	
	return foundLetter;
}

int Word::hasWordBeenGuessed() const
{
	//
	// post-condition: Return value = 1 if all letters in word have
	//                 been guessed.
	//				   Return value = 0 if there are letters still
	//				   to be guessed.
	//

	int count, gameWon = 1;

	// checks for '*' in formatedWord. If there is one that letter
	// has still to be guessed.

	for (count = 0; formatedWord[count] != '\0'; count++)
	{
		if (formatedWord[count] == '*')
			gameWon = 0;
	}
	return gameWon;
}

void Word::stringCopy(char source[], char destination[])
{
	//
	// pre-conditions: Source is a NULL terminated character string.
	//				   Destination is same length or longer than
	//                 source.
	//
	// post-conditions: Destination will contain a copy of source.
	//

	int count;
	for (count = 0; source[count] !='\0'; count++)
	{
	destination[count] = source[count];
	}
	destination[count] = '\0';
}

int Word::operator==(const Word & rhs) const
{

	enum BOOL {False, True};
	int result, BOOL = False;
	result = strcmp(phrase, rhs.phrase);	// if strings are the same strcmp will return 0.

	if (!result)
		BOOL = True;

	return BOOL;

}


void Word::getPhrase(char wordDisplay[MaxWordLength]) const
{

	// Copies phrase array into wordDisplay[MaxWordLength] parameter.

	int count;

	for (count = 0; phrase[count] != '\0'; count++)
		wordDisplay[count] = phrase[count];

	wordDisplay[count] = '\0';
}
