Random number guessing game I'm working on a random number guessing game.  I'm done with the code and it's working, but there is a part that I want to make better.  I declared an instance of a Guess class I created, and now how to make this part more efficient.

int counter = 0;
do
{             
    myGuess.UserGuess = GetUserGuess(); //read user guess
    if (myGuess.Compair() == "match")
    {
        Console.WriteLine("\n\t Correct!You WIN !");
    }


    else if (myGuess.Compair() == "high")
    {
        if (counter &lt; 3)
            Console.WriteLine("\n\tTry a lower number,");
        else
            Console.WriteLine("\n\tSorry you LOSE !, The right number is " + myGuess.RndNum);

        counter++;
    }

    else if (myGuess.Compair() == "low")
    {
        if (counter &lt; 3)
            Console.WriteLine("\n\tTry a higher number,");
        else
            Console.WriteLine("\n\tSorry you LOSE !, The right number is " + myGuess.RndNum);
        counter++;
    }


} while (myGuess.Compair() != "match" &amp;&amp; counter &lt; 4);


For example, I used the same message the same condition twice, which I think is not the best way.  Any way to loop better than that?
I think a way to make this code generally more efficient and predictable is to have your classes have a more defined role.  For example, it seems like you're defining an instance of Guess as myGuess, then externally loading in a random number (RndNum), and the current guess (UserGuess = GetUserGuess()).

You should clearly define the goals for classes, such as:


Manage the Game lifecycle.
Handle user input, ensuring the user enters only numbers and is properly prompted, etc.


In this way, your code might look more like this psuedo-code:

class GuessingGame
{
    static void Main(string[] args)
    {
        game = new GuessingGame();
        game.run();
    }

    int numGuesses;
    int correctNumber;
    UserInputHandler userInputHandler;

    GuessingGame(numGuesses = 4)
    {
        Random random = new Random();
        this.correctNumber = random.Next(10);
        this.numGuesses = numGuesses;
        this.userInputHandler = new UserInputHandler();
    }

    void run()
    {
        for (int thisGuess = 0; thisGuess &lt; numGuesses; ++thisGuess)
        {
            Console.WriteLine(string.Format("Guess #{0}!\n", thisGuess + 1));
            guess = this.userInputHandler.getGuess();
            if (guess &lt; this.correctNumber)
            {
                Console.WriteLine("WRONG! Try a higher number!");
            }
            else if (guess &gt; this.correctNumber)
            {
                Console.WriteLine("WRONG! Try a lower number!");
            }
            else
            {
                win();
                return();
            }
        }
        lose();
    }

    void win()
    {
        Console.WriteLine("You WIN!  The number was indeed " + this.correctNumber);
    }

    void lose()
    {
        Console.WriteLine("You LOSE!  The number was " + this.correctNumber);
    }
}

// UserInputHandler Source
class UserInputHandler
{
    public int getGuess()
    {
        // Handle user input, and ensure the guesses are actually numbers.
    }
}


The idea is that each class is responsible for encapsulating one piece of functionality around another class.  The Main program entry point doesn't care about how the game works, the GuessingGame class doesn't care about interacting with the user at all, while the UserInputHandler class doesn't care about the correct answer, etc.

I've also wrapped the win/loss messages into a single method, so that you're not repeating the victory message.

Some benefits include:


Having a clear understanding of the role of each class.
Being able to assert that with unit tests easily.
Code reuse.  You could plug the UserInputClass into a different game that requires simple user input, for example.
Collaboration.  Properly scoped and documented code means other developers could jump into your project with less friction.


Now this example is a bit much considering that a guessing game is fairly basic, but it's good practice for tackling larger projects.