/*----
  signature: main: void -> int
  purpose: to allow the user to guess a word
      of the day, and keep letting them guess
      until they get it or want to quit

      NOW tries to read the word of the day from a file
      in the current directory named
      word-of-the-day.txt,

      and tries to log all guesses in runs of this
      program to a file guesses_made.txt
      in the current working directory

  compile using (typed all on one line)
      g++ guess_word.cpp guess_match.cpp letter_match.cpp
          letter_elsewhere.cpp -o guess_word
  run using:
      ./guess_word

  by: Sharon Tuttle
  last modified: 2022-09-08
----*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>  // NEEDED for file input/output!
#include "guess_match.h"   
using namespace std;

int main()
{
    cout << boolalpha;

    /*---
    some pseudocode for my approach:

    get a first guess
    while the guess is wrong or not empty
        show the guess_match result
        get a next guess

    give a closing message
    ---*/

    // read the word of the day from a specific file
    
    const string WORD_FILE = "word-of-the-day.txt";
    
    ifstream read_stream;
    read_stream.open(WORD_FILE);
    
    string word_of_day;

    read_stream >> word_of_day;
    read_stream.close();

    // append today's guesses to a guess-history file

    ofstream write_guess_stream;
    const string GUESS_HISTORY = "guesses_made.txt";
    
    write_guess_stream.open(GUESS_HISTORY, ios::app);
    write_guess_stream << "\nNEXT GAME: \n===========\n";
    
    // get a first guess

    string guess;
    cout << "Enter a guess, or type enter to quit: ";

    getline(cin, guess);
    write_guess_stream << guess << endl;

    // while the guess is wrong or not empty
    //     show the guess_match result
    //     get a next guess

    while ((guess != word_of_day)
           && (guess != ""))
    {
        cout << "No, but here is a hint: "
             << guess_match(word_of_day, guess)
             << endl;

        cout << "\nEnter another guess, or type "
             << " enter to quit: " << endl;
        getline(cin, guess);
        write_guess_stream << guess << endl;
    }

    // give a closing message
    
    if (guess == word_of_day)
    {
        cout << "Congratulations! You got it!"
             << endl;
        write_guess_stream << "*** Congratulations! You got it! ***"
                           << endl;
    }
    else
    {
        cout << "OK, bye!" << endl;
        write_guess_stream << "*** Did not guess the word this time ***"
                           << endl;
    }

    write_guess_stream.close();
    
    return EXIT_SUCCESS;
}