View Single Post
Old 08-27-2005, 11:20 PM   #17 (permalink)
smckittr
Scott
 
Join Date: Aug 2005
Posts: 29
smckittr is on a distinguished road
Heres the game loop.
Code:
void first_part(std::string * scrambledWord, std::string * word)
{
  std::cout << "Welcome to Word-Scramble." << std::endl;
  std::cout << "Your Word is: " << *scrambledWord << std::endl;

  middle_part(word);
}

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

void middle_part(std::string * word)
{
  bool tryAgain = true;
  std::string guess;

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

    tryAgain = process(guess, *word);
  }
  end_part();
}

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

bool process(std::string guess, std::string word)
{
  bool tryAgain = true;
  //Give up?
  if (guess == "give")
  {
    //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;
}

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

void end_part()
{
  bool playAgain;

  std::cout << "Thanks, for playing." << std::endl;
//Let the functions finish
}
first_part() is called by main() after the word has been scrambled.
I think this works better. How would I start the program over at the user request? (that would be found in the end_part() function).

Missed your second reply so i've been woring on the scramble function too.
Code:
std::string scramble_word(std::string sWord)
{
  int wordLength=0;
  std::string scrambledWord;
  int letter;
  int count=0;
  int lettersLeft;
  std::string sLetter;

  wordLength = sWord.length();
  lettersLeft = wordLength;

  std::cout << "Scrambling Word";

  while (count <= (wordLength-1))
  {

    //choose a letter from sWord
    letter = (choose_rand(lettersLeft) - 1);
    std::cout << ".";

   //Asign it to scrambledWord[count]
    sLetter = sWord[letter];
    scrambledWord.append(sLetter);

   //Remove chosen letter from sWord
    int i = (letter);
    while (i<(lettersLeft-1))
    {
      sWord[i] = sWord[i+1];
      i++;
    }

    lettersLeft--;
    count++;
  }
  return scrambledWord;
}
it works resonably well but would work better if i had a random number generator that could come up with a different number several times per second. any suggestions there?

I'll take a crack at your puzzle code.
thanks
Scott
smckittr is offline   Reply With Quote