3 Replies - 314 Views - Last Post: 03 February 2011 - 08:10 AM Rate Topic: -----

#1 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Outputs wrong value

Posted 02 February 2011 - 08:53 PM

Hey guys,
This is a console game I'm programming and it basically goes like this: User types in a number of marbles to start with (like 100). Two players take turns taking away 1 to 4 marbles and the player left with 1 marble loses. I can't get the following code to work:

 //**********USER'S TURN****************//
        static int UserRound(int _marblesInitial)
        {
            Console.WriteLine("Type the number of marbles you wish to take away: ");
            int _numberOfMarbles = int.Parse(Console.ReadLine());
            int _marblesLeft = _numberOfMarbles;
                if (_numberOfMarbles <= 4 && _numberOfMarbles > 0 && !(_numberOfMarbles >= _marblesInitial))
                {
                    _marblesLeft = _marblesInitial - _numberOfMarbles;
                    return _marblesLeft;
                }
                else if (_numberOfMarbles > 4 || _numberOfMarbles < 0)
                {
                    Console.WriteLine("Incorrect number of marbles entered. \nPlease input a number 1 through 4: ");
                    UserRoundTest(_marblesInitial);
                }
                else if (_numberOfMarbles >= _marblesInitial && !(_marblesInitial == 1))
                {
                    Console.WriteLine("Your number of marbles is too high!  Processes failed... ");
                    UserRoundTest(_marblesInitial);
                }
                else if (_marblesInitial == 1)
                {
                    Lose();
                }
            return _marblesLeft;
        }



The problem is here:
else if (_numberOfMarbles >= _marblesInitial && !(_marblesInitial == 1))
                {
                    Console.WriteLine("Your number of marbles is too high!");
                    UserRound(_marblesInitial);
                }



I pass UserRound _marblesInitial, which SHOULD still be the original amount passed to UserRound before, but when I type a number that would bring up that error statement (I.E. 5), the error comes up as it should and prints "Your number of marbles is too high!" and I try to enter in the next number that's correct (I.E. 4) and it says "There are 5 marbles left"
I don't know why it's printing the number I put in on purpose to debug instead of what it should print. Heres the function that UserRound is called in:

 static void PlayTwoPlayer()
        {
             Console.WriteLine("Please input the number of marbles you would like to start with: ");
            int _marblesAmount = int.Parse(Console.ReadLine());
            if (_marblesAmount > 0)
            {
                Console.WriteLine("Begin!");
                while (_marblesAmount > 0)
                {
                    Console.WriteLine("Player 1's turn...");
                    int _marblesLeft = UserRound(_marblesAmount);
                    Console.WriteLine("There are {0} marbles left after your turn.\n", _marblesLeft);
                    if (_marblesLeft == 0)
                    {
                        Player1Lose();
                        break;
                    }
                    Console.WriteLine("Player 2's turn...");
                    _marblesAmount = UserRound(_marblesLeft);
                    Console.WriteLine("There are {0} marbles left after your turn.\n", _marblesAmount);
                    if (_marblesAmount == 0)
                    {
                        Player2Lose();
                        break;
                    }
                }
                Console.ReadLine();
            }

            else
            {
                Console.WriteLine("Error!  Negative amount of marbles!  Try again!");
                PlayTwoPlayer();
            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Outputs wrong value

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Outputs wrong value

Posted 02 February 2011 - 11:24 PM

Ok, you have a problem with regard to unwinding your recursion. Let's say you type 5, it goes into your problem if statement where it says you are too high. You then pass _marblesinitial to UserRound again. Where it prompts you again for a number and this time you enter 4 (a legit value). Great, it goes into the first if statement and returns _marblesleft.

When you do this, where does it go next? It goes back to that first call where it failed and continues on. Since you never use the value that was returned and do something with it, it gets thrown away (you are not modifying the variable passed itself, it takes a copy remember?). It then drops down to the end of the if statement where it hits the statement return _marblesLeft;. But what is the value of _marblesleft? It is the original value you called with, the 5.

Get what I am saying? So how does this get fixed? Well, use the return value to then modify the _marblesleft variable.

else if (_numberOfMarbles >= _marblesInitial && !(_marblesInitial == 1))
{
    Console.WriteLine("Your number of marbles is too high!  Processes failed... ");
    _marblesleft = UserRound(_marblesInitial); //<-- Notice we are modifying _marblesleft in this call with the return value of the next call
}



As you can see from the code we are using the return value of the UserRound call to modify THIS call's version of _marblesleft. Remember, no matter how many times you recursively call your function, it will return back to the previous call until it reaches the very first call again and then continues on.

Get it? :)

This post has been edited by Martyr2: 02 February 2011 - 11:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Outputs wrong value

Posted 02 February 2011 - 11:26 PM

Hi,

In your UserRound() Method Try This code :


 else if (_numberOfMarbles > 4 || _numberOfMarbles < 0)
            {
                Console.WriteLine("Incorrect number of marbles entered. \nPlease input a number 1 through 4: ");
               _marblesLeft = UserRound(_marblesInitial);
               return _marblesLeft;
                //UserRoundTest(_marblesInitial);
            }


Was This Post Helpful? 0
  • +
  • -

#4 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Outputs wrong value

Posted 03 February 2011 - 08:10 AM

OHHHHH I get it now! Wow I completely forgot it doesn't edit the actual variable, just a copy *smacks forehead* thanks Martyr and Vishu! Thank you! On to build the computer AI! haha
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1