/*----
  signature: letter_elsewhere:
             string string int -> bool

  purpose: expects the word of the day,
          the guessed word, and (zero-based)
          desired position within the guessed
          word, and returns whether the leter
          at that position in the guessed word
          is in the word of the day, BUT at
          a DIFFERENT position. (If that letter
          IS in both the guess and the word of
          day in the same position, this returns
          false.)

  tests: 
      letter_elsewhere("bagel", "great", 0) == true
      letter_elsewhere("bagel", "great", 4) == false
      letter_elsewhere("spice", "sugar", 0) == false
      letter_elsewhere("dolly", "bugle", 3) == false

  by: Sharon Tuttle
  last modified: 2022-08-31 - added some internal comments
                 2022-08-30 - in-class version
----*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include "letter_match.h"
using namespace std;

bool letter_elsewhere(string word_of_day,
                      string word_guess,
                      int pos)
{
    // do the letters at this position match?

    if (letter_match(word_of_day, word_guess, pos))
    {
        return false;
    }

    // if reach here, see if guess's letter at this position
    //     matches a letter at any OTHER position in the
    //     word of the day
    
    else
    {
        char match_letter = word_guess.at(pos);
	bool found_yet = false;
	int word_len = word_of_day.length();
	int next_pos = 0;

        while  ((found_yet == false) &&
	        (next_pos < word_len))
        {
	    // only checking if in OTHER than given position
	    //     in the word of the day

	    if (next_pos != pos)
	    {
	        if (word_of_day.at(next_pos) ==
		    match_letter)
		{
		    found_yet = true;
		}
	    }
	    
	    next_pos = next_pos + 1;
	}

        // when exit loop, found_yet should have been set
	//     appropriately

	return found_yet;
    }
}