Be the first to post for 'Writing & Using Getter Methods'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

1 Like

I had the strangest experience. I kept thinking i must have snoozed somewhere and suddenly the class is being used in the main .cpp file.

I got all the way through here and finally saw that I had skipped lecture 26.

My consolation prize is that I was so wrapped up in this game that I completed my version after Lecture 27 and am now looking for a better refactoring. My PlayGame procedure has grown out of hand with all of the input-output and the error cases. I must handle my abstractions better.

As my Happy Holiday offering and greetings for New Year 2017, my latest FBullCowGame.cpp is at the end, below.

For penance, I am very interested to see if there is a decent heuristic for playing the game and getting good results. That would be an interesting side project along with finding an upper bound for the least guesses that always work for an isogram of length n. There is such information for isograms of length 4 with a 10-letter alphabet (actually, ‘0’ to ‘9’). I will look for more.

So, back to Lecture 25 for me, and then ever-onward.

/* FBullCowGame.cpp 0.0.4            UTF-8                        2016-12-23 */
/* ------1---------2---------3---------4---------5---------6---------7------ */

#include "FBullCowGame.hpp"

FBullCowGame::FBullCowGame(std::string SecretWord)
{
    MySecret = SecretWord;
    MyCurrentGuess = SecretWord;

    if (!IsGoodIsogram())
        MySecret = std::string("");

    MyCurrentGuess = std::string("");
    MyWellFormedTries = 0;
    MySuggestedMaxTries = 5;
}

unsigned FBullCowGame::WordSize()
{
    return MySecret.length();
}

void FBullCowGame::SetGuess(std::string Guess)
{
    MyCurrentGuess = Guess;
    if (IsGoodIsogram()) MyWellFormedTries++;
}

std::string FBullCowGame::CurrentGuess()
{
    return MyCurrentGuess;
}

bool FBullCowGame::IsOnlyLetters()
{
    if (MyCurrentGuess.length() == 0) return false;

    for (unsigned i = 0; i < MyCurrentGuess.length(); i++)
    {
        if (!isalpha(MyCurrentGuess[i])) return false;
    }

    return true; 
  
}

bool FBullCowGame::IsCorrectLength()
{
    if (!IsOnlyLetters()) return false;

    return MyCurrentGuess.length() == MySecret.length();
}

bool FBullCowGame::IsGoodIsogram()
{
    if (!IsCorrectLength()) return false;

    for (unsigned i = 1; i < MyCurrentGuess.length(); i++)
        /* Require CurrentGuess[i] to differ from all preceding 
           CurrentGuess[j], j < i
           */
        for (unsigned j = 0; j < i; j++)
            if (MyCurrentGuess[i] == MyCurrentGuess[j]) return false;

    return true;
}

bool FBullCowGame::IsSecretGuessed()
{
    if (!IsGoodIsogram()) return false;

    return Bulls() == MySecret.length();
}

unsigned FBullCowGame::Bulls()
{
    if (!IsGoodIsogram()) return 0;

    unsigned MyBulls = 0;

    for (unsigned i = 0; i < MySecret.length(); i++)
        if (MySecret[i] == MyCurrentGuess[i]) MyBulls++;

    return MyBulls;
}

unsigned FBullCowGame::Cows()
{
    if (!IsGoodIsogram()) return 0;

    unsigned MyCows = 0;

    for (unsigned i = 0; i < MySecret.length(); i++)
        for (unsigned j = 0; j < MyCurrentGuess.length(); j++)
            if (MySecret[i] == MyCurrentGuess[j]) MyCows++;

    return MyCows - Bulls();
}

unsigned FBullCowGame::WellFormedTries()
{
    return MyWellFormedTries;
}

unsigned FBullCowGame::SuggestedMaxTries()
{
    return MySuggestedMaxTries;
}


/* ------1---------2---------3---------4---------5---------6---------7------ */

/* 0.0.4 2016-12-23-17:44 Adjust method names, add CurrentGuess(), and stub
         out verification of SecretWord until IsGoodIsogram() is working.
   0.0.3 2016-12-23-08:06 Use Ladder of guards to get to IsGoodIsogram().
         All preconditions checked and all methods guarded regardless of
         the order in which they are checked by an user of the interface.
   0.0.2 2016-12-22-22:30 Clean up constructer, fix string.length() usage, 
         and add consistency checking.
   0.0.1 2016-12-22-18:24 Stub/Coordinate all FBullCowGame.hpp methods. 
         */


/*        *** end of FBullCowGame.cpp ***                  */

I’m glad you’re enjoying the course.

Privacy & Terms