#include <iostream.h>
#include <ctype.h>
#include "ComputerOpponent.h"

ComputerOpponent::ComputerOpponent():
		theGameState(notStarted),
		numWordsUsed(0)
{
}

int ComputerOpponent::startNewGame(char theFileName[])
{
	int NewGameStartedStatus = 0;	// return value: ok == 1, Error == 0.

	// Create word list.

	NewGameStartedStatus = theFileInterface.loadWordFile(theFileName, theWordList);

	if(NewGameStartedStatus == 1)
	{
		// initialise/reset score card/lists  etc.
		
		theScoreCard.resetNumGuessesLeft();
		theScoreCard.resetScore();
		theUsedGuessList.resetGuessList();
		theRandWordGenerator.resetGenerator();	// resets used word list.

		numWordsUsed = 1;

		// Choose secret word

		theSecretWord = theRandWordGenerator.chooseSecretWord(theWordList);

		theGameState = inProgress;		// Set gameState = inProgress
	 }

	return NewGameStartedStatus;
}


void ComputerOpponent::getSecretWord(char secretWord[])
{
	theSecretWord.getFormatedWord(secretWord);
}


void ComputerOpponent::getCategory(char category[])
{
	Word tempWord;

	tempWord = theWordList.getWord(0);
	tempWord.getPhrase(category);
}

void ComputerOpponent::getScore(int &gamesPlayed, int &gamesWon)
{
	gamesWon = theScoreCard.getNumGamesWon();
	gamesPlayed = theScoreCard.getNumGamesPlayed();
}


int ComputerOpponent::getNumGuessesLeft()
{
	return theScoreCard.getNumGuessesLeft();
}


int ComputerOpponent::checkGuess(char guess)
{
	int resultOfCheck = 0;	// Equals zero if letter is incorrect.
							// Equals 1 if letter is correct.
							// Equals 2 if letter is has been used before.

	int validGuess = 0;		// Equals zero if invalid, 1 if valid.

	// validate guess

	validGuess = validateLetter(guess);


	if ((theGameState == inProgress) && (validGuess))	// If the game is not in progress the guess will not be processed
	{
		// check guessList to see if letter has been used before

		if(theUsedGuessList.LetterBeenUsedStatus(guess) == 0)
		{
			// check if letter is in word.

			resultOfCheck = theSecretWord.checkLetterInWord(guess);
			if (resultOfCheck == 0)
			{
				theScoreCard.decrementNumGuessesLeft();
			}
		}
	}

	// update the current game state.

	updateGameState();

	return resultOfCheck;
}


int ComputerOpponent::getGameState()
{
	return theGameState;
}


void ComputerOpponent::updateGameState()
{
	int gameWonStatus;	// Equals 1 if game has been won.
						// Equals 0 if game has not been won.

	// check and see if there the player has any guesses left.

	if  ((theScoreCard.getNumGuessesLeft()) >= 1)
	{

		// check to see if word has been guessed and player has won,
		// update score card accordingly.

		gameWonStatus = theSecretWord.hasWordBeenGuessed();

		if (gameWonStatus == 1)
		{
			theScoreCard.incrementNumGamesWon();
			theScoreCard.incrementNumGamesPlayed();
			theGameState = won;
		}
	}
	// if there are no guesses left and the player hasn't won yet then they must have lost.
	// update score card accordingly.

	else
	{
		theGameState = lost;
		theScoreCard.incrementNumGamesPlayed();
	}
}


int ComputerOpponent::chooseNewWord()
{

	int success = 0;	// Equals 1 if a new word has been chosen succesfully
						// Equals 0 otherwise.


	// If all the words in the list have been used up return success = 0

	if ((theWordList.getNumOfElementsInList()) == (numWordsUsed))
	{
		success = 0;
	}
	else
	{
	theSecretWord = theRandWordGenerator.chooseSecretWord(theWordList);	// choose word.
	theGameState = inProgress;		// Set gameState = inProgress.
	theScoreCard.resetNumGuessesLeft(); // reset number of guesses left.
	theUsedGuessList.resetGuessList();

	numWordsUsed++;
	success = 1;
	}

	return success;
}


void ComputerOpponent::getUncoveredWord(char uncoveredWord[])
{
	// The phrase revealed is copied into uncoverdWord parameter.

	theSecretWord.getPhrase(uncoveredWord);
}


int ComputerOpponent::validateLetter(char &guess)
{

	// Function checks that guess is valid i.e. a letter.
	// If valid it is converted to
	// upper case if it is in lower case.
	// If guess is not a letter the value returned is 0 (FALSE)

	enum BOOL {False, True};

	int validLetter = False;

	if (isalpha(guess))	// true if guess is a letter.
	{
		if (islower(guess))	// true if guess is lower case.
		{
			guess = guess - 32;	// convert to upper case.
		}

		validLetter = True;
	}

	return validLetter;
}

void ComputerOpponent::getUsedGuessList(char list[MaxGuessListLength])
{
	theUsedGuessList.getGuessList(list);
}

void ComputerOpponent::resetComputerOpponent()
{
	theGameState = notStarted;	// reset theGameState
	theScoreCard.resetNumGuessesLeft();	// reset numGuessesLeft
	theUsedGuessList.resetGuessList();	// reset usedGuessList
}
