1

I'm working on a random number guessing game as a c# console program. It's done with the code and working. However, there is a part that I want to make better: I declared an instance of a Guess class I created, 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 < 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 < 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" && counter < 4);

Thanks in advance.

davidsbro
  • 2,761
  • 4
  • 23
  • 33
sunflower
  • 501
  • 1
  • 4
  • 14
  • Your question isn't very clear. What do you mean by more efficient? Questions on SO should generally be quite specific, this seems far to general. – Ade Miller Mar 01 '14 at 23:48
  • for example I used the same message the same condition twice wich I think this is not the best way .Any way to loop better than that ? – sunflower Mar 01 '14 at 23:51
  • 1
    @sunflower This would be better for [Code Review](http://codereview.stackexchange.com) –  Mar 01 '14 at 23:52
  • How to do Code Review ? Sorry I'm not familiar with the website yet. – sunflower Mar 01 '14 at 23:55
  • @sunflower, just so you know, Compare is spelled like "Compare", not "Compair". :) – davidsbro Mar 02 '14 at 00:15
  • Would you be shocked if you know tha my teacher is the one who wrote that way ? LOL ! :D – sunflower Mar 02 '14 at 00:43

1 Answers1

1

What does "Compair()" function look like? It seems like that could return an integer rather than a string for a simpler function. An example of that looks like:

// just an example implementation
public int Compair() {
   if (UserGuess < actualValue) return -1;
   if (UserGuess > actualValue) return 1;
   return 0;
}

And then your routine becomes:

int counter = 0;
bool success = false;

do
{
    myGuess.UserGuess = GetUserGuess();
    int compair= myGuess.Compair()
    switch (compair) {
      case 0:
        Console.WriteLine("\n\t Correct!You WIN !");
        success = true;
        break;
      case 1:
      case -1:
        if (counter < 3) Console.WriteLine("\n\tTry a {0} number,", compair == -1 ? "lower" : "higher");
        break;
    }

    counter++;
    if (counter >= 3 && !success)
      Console.WriteLine("\n\tSorry you LOSE !, The right number is " + myGuess.RndNum);
  } while (!success && counter < 4);

That should do it! This should be faster because it isn't using string comparisons, it might be a bit easier to read and it should have fixed a few logical issues.

Note - I made a few assumptions about the use of properties so this example might not compile out of the get but it should get you most of the way there. Best of luck!

FabianCook
  • 20,269
  • 16
  • 67
  • 115
drew_w
  • 10,320
  • 4
  • 28
  • 49
  • Thank you ,I posted the question at CodeReview any way. – sunflower Mar 02 '14 at 00:05
  • So it's better practise not compair strings ? Do you know where can I read about best programming practise ?A good useful source I mean ? – sunflower Mar 02 '14 at 00:07
  • @sunflower The best way to learn is by asking questions, trying and of course - searching for answers. [This question](http://stackoverflow.com/questions/1642659/is-it-more-efficient-to-compare-ints-and-ints-or-strings-and-strings) for example, has information on integer versus string comparisons. Keep working and you'll keep learning! – drew_w Mar 02 '14 at 01:12