View Single Post
Old 08-29-2005, 04:33 PM   #21 (permalink)
smckittr
Scott
 
Join Date: Aug 2005
Posts: 29
smckittr is on a distinguished road
Valmont,
I got the rest of the game loop in. Heres the whole program
Code:
#include <fstream>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorith>

unsigned choose_word(unsigned);
unsigned calculate_lines(std::ifstream&);
std::string fetch_word(std::ifstream&, unsigned);
bool process(std::string, std::string);

int main()
{
  using std::endl;
  using std::cout;
  using std::cin;

  std::string sWord;
  std::string sGuess = "quit";
  unsigned nLines(0);

  std::ifstream dictionary("dictionary.txt");

  if (!dictionary)
  {
    std::cout << "ERROR: There was error opening the file dictionary.txt." << std::endl;
    std::cout<< "Please read readme.txt for possible solutions." << std::endl;
    std::cout<< "Terminating program...";
    std::exit(1);
  }

  while(1)
  {
    cout << "1 = Play Game" << endl;
    cout << "2 = Quit App" << endl;
    char appRun;
    cin >> appRun;
    if(appRun == '2')
    {
      break;
    }
    if(appRun == '1')
    {
      //Let's first determine how many words there are.
      nLines = calculate_lines(dictionary);
      //We know how many words there are, so let's choose a random word line.
      unsigned RandomLine;
      RandomLine = choose_word(nLines);
      //We know which line to pick. So let's retreive the word on that line.
      sWord = fetch_word(dictionary, RandomLine);

      //Make a copy and shuffle
      std::string word = sWord;
      std::random_shuffle(sWord.begin(), sWord.end());

      //Start Game
      std::cout << "Welcome to Word-Scramble." << std::endl;
      std::cout << "Your Word is: " << sWord << std::endl;

      bool tryAgain = true;
      std::string guess;

      //Start Loop
      while (tryAgain)
      {
        std::cout << "What is the word? (type \"3\" to get the answer): ";
        std::cin >> guess;
        std::cout << std::endl;

        tryAgain = process(guess, word);
      }

      std::cout << "Thanks, for playing." << std::endl;

    }
    else
    {
      cout << endl << "ERROR: you didn't enter \"1\" or \"2\" " << endl;
      cout << "Please try again." << endl << endl;
    }
  }

  return 0;
}

//---------------------------------------------------

//So how many lines (=words) has this file?
unsigned calculate_lines(std::ifstream& dict)
{
  unsigned lns(0);
  std::string sBuffer;
  while( std::getline(dict, sBuffer) )
  {
    ++lns;
  }

  //Did something went wrong during the read of this stream?
  if(!dict.eof())
  {
    //Yep, something went wront. Ring the alarms!
    std::cout<<"ERROR: something go wrong during file reading.";
    dict.close();
    std::cout<<"exiting app...";
  }

  //Do not forget to reset the state of the dictionary file object since it is
  //in a error state due to EOF.
  dict.clear();
  //And let the file pointer point to the beginning as it was before all this.
  dict.seekg(0, std::ios::beg);
  return lns;
}

//---------------------------------------------------

unsigned choose_word(unsigned maxnum)
{
  unsigned randnum;
  std::srand(std::time(0));
  randnum= std::rand() % maxnum + 1;
  return randnum;
}

//----------------------------------------------------

std::string fetch_word(std::ifstream& dict, unsigned line)
{
  std::string buffer;
  while( std::getline(dict, buffer) && line != 1)
  {
    --line;
  }

  //Do not forget to reset the state of the dictionary file object since it is
  //in a error state due to EOF.
  dict.clear();
  //And let the file pointer point to the beginning as it was before all this.
  dict.seekg(0, std::ios::beg);

  return buffer;
}

//-----------------------------------------------------------------------------

bool process(std::string guess, std::string word)
{
  bool tryAgain = true;
  //Give up?
  if (guess == "3")
  {
    //Yup, Give up
    std::cout << "The Word is: " << word << std::endl;
    tryAgain = false;
  }
  else
  {
    //Is it right?
    if (guess == word)
    {
      //Yup, It's right
      std::cout << "Correct! You Win!" << std::endl;
      tryAgain = false;
    }
    else
    {
      //Nope not right
      std::cout << "No, that\'s not the word. Try Again" << std::endl;
    }
  }
  return tryAgain;
}
#include <algorithm> didn't work so i looked in my include files and the file was named algorth so I changed it.

Also the random_shuffle() function doesn't compile for me. I get this error:
"_algo.c": E2268 Call to undefined function 'rand' in function _STL::int __random_number<int>(int) at line 432

Thanks
Scott
smckittr is offline   Reply With Quote