This is a follow up to the following questions:
Ensuring user input is an integer in a range
I haven't made my way to DecideWinner()
yet, but that is next on my To-Do List. I would like the majority of the focus to go into what I have done with the rest of the code, but would still enjoy some input on what to do with my nasty if then else
statements.
I did not change to using an Enumeration for my Gestures
. I read the Microsoft page for Enumeration and it didn't seem like it would be a good fit, maybe I didn't understand fully though either.
I also used while
loops instead of do while
loops because I like them better. not really a good reason I guess other than that I prefer while
loops.
I am not afraid to say that I am still learning, so here is the code:
/// <summary>
/// Thanks to
/// https://codereview.stackexchange.com/users/38054/benvlodgi
/// https://codereview.stackexchange.com/users/4318/eric-lippert
/// https://codereview.stackexchange.com/users/23788/mats-mug
/// https://codereview.stackexchange.com/users/30346/chriswue
/// </summary>
namespace RPSLSGame
{
class MainClass
{
public static void Main (string[] args)
{
/* Here are your rules:
"Scissors cuts paper,
paper covers rock,
rock crushes lizard,
lizard poisons Spock,
Spock smashes scissors,
scissors decapitate lizard,
lizard eats paper,
paper disproves Spock,
Spock vaporizes rock.
And as it always has, rock crushes scissors."
-- Dr. Sheldon Cooper */
List<string> Gestures = new List<string>{"rock","paper","scissors", "lizard","spock"};
int win = 0;
int lose = 0;
int tie = 0;
var newGame = true;
while (newGame)
{
var playerGesture = "";
Console.WriteLine("Please choose your Gesture ");
PrintMenu(Gestures);
playerGesture = Gestures[PromptForNumber("Please choose your gesture",1,Gestures.Count)-1];
var computerPlay = GetRandomOption(Gestures);
Console.WriteLine ("Computer: " + computerPlay);
Console.WriteLine ("your Gesture: " + playerGesture);
if (playerGesture == computerPlay)
{
tie++;
Console.WriteLine ("you have tied with the computer");
Console.WriteLine ("Computer: " + computerPlay);
Console.WriteLine ("your Gesture: " + playerGesture);
}
else
{
if (playerGesture == "rock")
{
if (computerPlay == "lizard" || computerPlay == "scissors")
{
Console.WriteLine ("You Win, " + playerGesture + " Crushes " + computerPlay);
win++;
}
else if (computerPlay == "paper")
{
Console.WriteLine ("You Lose, Paper Covers Rock");
lose++;
}
else if (computerPlay == "spock")
{
Console.WriteLine ("You Lose, Spock Vaporizes Rock");
lose++;
}
}
else if (playerGesture == "paper")
{
if (computerPlay == "spock")
{
Console.WriteLine ("You Win, Paper Disproves Spock");
win++;
}
else if (computerPlay == "rock")
{
Console.WriteLine ("You Win, Paper Covers Rock");
win++;
}
else if (computerPlay == "lizard")
{
Console.WriteLine ("You Lose, Lizard Eats Paper");
lose++;
}
else if (computerPlay == "scissors")
{
Console.WriteLine ("You Lose, Scissors Cut Paper");
lose++;
}
}
else if (playerGesture == "scissors")
{
if (computerPlay == "paper")
{
Console.WriteLine ("You Win, Scissors Cut Paper");
win++;
}
else if (computerPlay == "lizard")
{
Console.WriteLine ("You Win, Scissors Decapitate Lizard");
win++;
}
else if (computerPlay == "rock")
{
Console.WriteLine ("You Lose, Rock Crushes Scissors");
lose++;
}
else if (computerPlay == "spock")
{
Console.WriteLine ("You Lose, Spock Smashes Scissors");
lose++;
}
}
else if ( playerGesture == "lizard")
{
if (computerPlay == "paper")
{
Console.WriteLine ("You Win, Lizard Eats Paper");
win++;
}
else if (computerPlay == "spock")
{
Console.WriteLine ("You Win, Lizard Poisons Spock");
win++;
}
else if (computerPlay == "scissors")
{
Console.WriteLine ("You Lose, Scissors Decapitates Lizard");
lose++;
}
else if (computerPlay == "rock")
{
Console.WriteLine ("You Lose, Rock Crushes Lizard");
lose++;
}
}
else if (playerGesture == "spock")
{
if (computerPlay == "rock")
{
Console.WriteLine("You Win, Spock Vaporizes Rock");
win++;
}
else if (computerPlay == "scissors")
{
Console.WriteLine ("You Win, Spock Smashes Scissors");
win++;
}
else if (computerPlay == "paper")
{
Console.WriteLine ("You Lose, Paper Disproves Spock");
lose++;
}
else if (computerPlay == "lizard")
{
Console.WriteLine("You Lose, Lizard Poisons Spock");
lose++;
}
}
}
Console.WriteLine ("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
if(!Choice ("Would you like to play again? Y/N"))
{
if (Choice("Would you like to reset your score?"))
{
win = 0;
lose=0;
tie=0;
}
if (!Choice ("Would you like to play another game?"))
{
newGame=false;
}
}
}
Console.WriteLine("Goodbye");
Console.ReadLine ();
}
public static void DecideWinner ()
{
//TODO: Create Method for Deciding the Winner.
}
static int PromptForNumber (string prompt, int lower, int upper)
{
int? pick = null;
while (pick == null) {
Console.WriteLine(prompt);
pick = Console.ReadLine().BoundedParse (lower, upper);
}
return pick.Value;
}
public static void PrintMenu (List<string> List)
{
for (int i=0; i<List.Count; i++) {
Console.WriteLine ((i+1) + ": " + List[i]);
}
}
public static string GetRandomOption (List<string> options)
{
Random rand = new Random();
return options[rand.Next(0,options.Count)];
}
public static Boolean Choice (string prompt)
{
while(true)
{
Console.WriteLine (prompt);
switch (Console.ReadKey (true).Key)
{
case ConsoleKey.Y:
{
Console.Write ("Y\n");
return true;
}
case ConsoleKey.N:
{
Console.Write ("N\n");
return false;
}
}
}
}
}
static class Extensions
{
/// <summary>
/// Parses the Input for an Integer between the lower and upper bounds
/// </summary>
/// <returns>
/// null or integer
/// </returns>
/// <param name='str'>
/// String to be parsed for an Integer
/// </param>
/// <param name='lower'>
/// Lower Bound of the Acceptable range of input integers
/// </param>
/// <param name='upper'>
/// Upper bound of the acceptable range of the input integers
/// </param>
public static int? BoundedParse (this string str, int lower, int upper)
{
if (str == null) {
return null;
}
int result;
bool success;
success = int.TryParse (str, out result);
if (!success) {
return null;
}
if (result < lower) {
return null;
}
if (result > upper) {
return null;
}
return result;
}
}
}
DecideWinner
method? I just haven't gotten to that method yet. I like the tuple idea, but there was something that someone said on someone else's RPSLS post that really stuck with me that I want to try. \$\endgroup\$DecideWinner
method? \$\endgroup\$DecideWinner
is massive overkill, but from a learning perspective it makes for a fun approach. \$\endgroup\$