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();
}
}

New Topic/Question
Reply




MultiQuote




|