Annie Lowry Annie Lowry - 2 months ago 5
C# Question

C# Increment adding more than intended

I'm coding a rock paper scissors game and some reason the counter is incrementing weird. Could anyone explain to me why when I win it increments by 1, when I lose it increments by 2, and when I tie, it increments by 3? I just want it to add by 1. I appreciate all answers.

class RPSLS
{


public enum GameMode
{

Unknown,
Single_Player,
Two_Player,

}
public GameMode GameType {get; set;}
public enum Hand { Rock = 1, Paper, Scissors, Lizard, Spock, Report, Exit };
public enum Outcome { Win, Lose, Tie };

public Hand ComputerHand { get; set; }
public Hand PlayerHand { get; set; }
public char UserSelection { get; set; }
int win = 0;
int lose = 0;
int tie = 0;
public Hand getUserHand()
{
while (!validateSelection())
{
Console.Clear();
Console.WriteLine("Invalid Input");
UserSelection = Convert.ToChar(Console.ReadLine());

}

switch (Char.ToUpper(UserSelection))
{
case 'R':
PlayerHand = Hand.Rock;
break;
case 'P':
PlayerHand = Hand.Paper;
break;
case 'S':
PlayerHand = Hand.Scissors;
break;
case 'L':
PlayerHand = Hand.Lizard;
break;
case 'K':
PlayerHand = Hand.Spock;
break;
case 'Q':
PlayerHand = Hand.Exit;
break;
case 'T':
PlayerHand = Hand.Report;
break;
default:
throw new Exception("Unexpected Error");
}
return PlayerHand;
}

public Outcome DetermineWinner()
{


if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Paper)
{
win++;
return Outcome.Win;

}
else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Lizard)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Rock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Spock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Paper)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Spock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Scissors)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Rock)
{
win++;
return Outcome.Win;
}

else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Rock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Spock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Paper)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Spock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Scissors)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Lizard)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Rock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Scissors)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Lizard)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Paper)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Exit)
{
Environment.Exit(0);
}
else if (PlayerHand == Hand.Report)
{
Report();
}
tie++;
return Outcome.Tie;
}

private void Report()
{
Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
}

private void PlayGame()
{
bool gameOver = false;
var rand = new Random();
char response;

while (!gameOver)
{
Screen();
UserSelection = Convert.ToChar(Console.ReadLine());
getUserHand();
ComputerHand = (Hand)rand.Next(1, 6);
Console.Clear();
Console.WriteLine("Computer's Hand: {0}", ComputerHand);
Console.WriteLine("Player's Hand: {0}", PlayerHand);

if (DetermineWinner() == Outcome.Win)
Console.WriteLine("Win");
else if (DetermineWinner() == Outcome.Lose)
Console.WriteLine("Lose");
else if (DetermineWinner() == Outcome.Tie)
Console.WriteLine("Tie");

Console.WriteLine("Would you Like to play another game? (y/n)");
response = Convert.ToChar(Console.ReadLine());

while(validateResponse(response) == false)
{
Console.WriteLine("Invalid Input, Please re-enter your selection: ");
response = Convert.ToChar(Console.ReadLine());
}

if (response == 'N' || response == 'n')
gameOver = true;

Console.Clear();
}
}

public bool validateResponse (char response)
{
if (Char.ToUpper(response) != 'Y' && Char.ToUpper(response) != 'N')
return false;

return true;
}
private bool validateSelection()
{
char value = Char.ToUpper(UserSelection);
if (value != 'R' && value != 'P' && value != 'S' && value != 'L' && value != 'K' && value != 'T' && value != 'Q')
return false;
return true;
}
private void Screen()
{
Console.WriteLine("(R)ock | (P)aper | (S)cissors | (L)izard | Spoc(k) | (Q)uit | Repor(t)");

}
public GameMode getGameType()
{
bool flag = true;
string buffer;
GameMode GameType = GameMode.Unknown;

GameType = GameMode.Unknown;
Console.WriteLine("Available options are: (S)ingle Player | (T)wo Player");
Console.Write("What would you like to play?: ");

while (flag)
{
buffer = Console.ReadLine();
try
{
switch (buffer.ToUpper())
{
case "S":
case "SINGLE PLAYER":
GameType = GameMode.Single_Player;
break;
case "T":
case "TWO PLAYER":
GameType = GameMode.Two_Player;
break;

default:
Console.WriteLine("Action not understood.");
break;
}
if (GameType != GameMode.Unknown)
{
PerformWork(GameType);
}
}
catch (ArgumentException)
{
Console.WriteLine("'{0}' is not understood.", buffer);
}

}
return GameType;
}

private void PerformWork(GameMode cmd)
{
if (cmd == GameMode.Single_Player)
{
Console.Clear();
SinglePlayer(cmd);


}
else if (cmd == GameMode.Two_Player)
{
Console.WriteLine("Two Player Mode");
}
else
{
Console.WriteLine("Action not understood.");
}
}
private void SinglePlayer(GameMode cmd)
{
Console.WriteLine("Hello. You are now playing Single Player Mode");
Console.Write("Please Enter Your Name: ");
String nameInput = Console.ReadLine();
Console.Write("Please Enter A Name For Computer: ");
string computerNameInput = Console.ReadLine();
Console.Clear();
Console.WriteLine(nameInput + " vs " + computerNameInput);

PlayGame();


}
}
}

Answer

The side effects from this block of code will increment your class level variables.

 if (DetermineWinner() == Outcome.Win)
    Console.WriteLine("Win");
 else if (DetermineWinner() == Outcome.Lose)
    Console.WriteLine("Lose");
 else if (DetermineWinner() == Outcome.Tie)
    Console.WriteLine("Tie");

Each time DetermineWinner() is called the fields are touched.