Skip to main content
added missing tied clause in most recent edit
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
public static int WhoWon(Gesture player1, Gesture player2)
{
    return player1 == player2 ? 0 :_actions.Keys.Where(key => key.Item1 == (int)player1 && key.Item2 == (int)player2).FirstOrDefault() != null ? 1 : 2;
}
public static int WhoWon(Gesture player1, Gesture player2)
{
    return _actions.Keys.Where(key => key.Item1 == (int)player1 && key.Item2 == (int)player2).FirstOrDefault() != null ? 1 : 2;
}
public static int WhoWon(Gesture player1, Gesture player2)
{
    return player1 == player2 ? 0 :_actions.Keys.Where(key => key.Item1 == (int)player1 && key.Item2 == (int)player2).FirstOrDefault() != null ? 1 : 2;
}
Added a different way to do # 12
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47

Edit: I recently realized that using math to determine the winner is all fun and games, but really we created a Dictionary of who beats who called _actions, why not use it? So I've re-written the WhoWon Method, which is now slightly longer, but more easily scaled, and not dependent on the Gesture enums being in a certain order to work. This replaces # 12

public static int WhoWon(Gesture player1, Gesture player2)
{
    return _actions.Keys.Where(key => key.Item1 == (int)player1 && key.Item2 == (int)player2).FirstOrDefault() != null ? 1 : 2;
}

Edit: I recently realized that using math to determine the winner is all fun and games, but really we created a Dictionary of who beats who called _actions, why not use it? So I've re-written the WhoWon Method, which is now slightly longer, but more easily scaled, and not dependent on the Gesture enums being in a certain order to work. This replaces # 12

public static int WhoWon(Gesture player1, Gesture player2)
{
    return _actions.Keys.Where(key => key.Item1 == (int)player1 && key.Item2 == (int)player2).FirstOrDefault() != null ? 1 : 2;
}
formatting fix
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47

enum Gesture { Rock = 1, Paper = 2, Scissors = 3, Spock = 4, Lizard = 5 }

enum Gesture
{
    Rock = 1,
    Paper = 2,
    Scissors = 3,
    Spock = 4,
    Lizard = 5
}

class Program
{
    private static Gesture[] _gestures = (Gesture[])Enum.GetValues(typeof(Gesture));

    private static Dictionary<Tuple<int, int>, string> _actions = new Dictionary<Tuple<int, int>, string>()
    {
        {Tuple.Create<int,int>(1,3), "Crushes"},
        {Tuple.Create<int,int>(1,5), "Crushes"},
        {Tuple.Create<int,int>(2,1), "Covers"},
        {Tuple.Create<int,int>(2,4), "Disproves"},
        {Tuple.Create<int,int>(3,2), "Cuts"},
        {Tuple.Create<int,int>(3,5), "Decapitates"},
        {Tuple.Create<int,int>(4,3), "Smashes"},
        {Tuple.Create<int,int>(4,1), "Vaporizes"},
        {Tuple.Create<int,int>(5,2), "Eats"},
        {Tuple.Create<int,int>(5,4), "Poisons"}
    };

    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 */

        int wins = 0;
        int loses = 0;
        int ties = 0;

        var newGame = true;
        do
        {
            GameSetup();
            Gesture playerGesture = GetPlayerGesture();
            Gesture computerGesture = GetComputerGesture();

            Console.WriteLine("Computer: " + computerGesture);
            Console.WriteLine("Your Gesture: " + playerGesture);

            switch (WhoWon(playerGesture, computerGesture))
            {
                case 0: ties++; Console.WriteLine("You have tied with the the computer."); break;
                case 1: wins++; Console.WriteLine("You win, " + GetReason(playerGesture, computerGesture)); break;
                case 2: loses++; Console.WriteLine("You lose, " + GetReason(computerGesture, playerGesture)); break;
            }

            Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", wins, loses, ties);

            Console.Write("Would you like to play again? ");
            if (!GetYN())
            {
                Console.WriteLine("Would you like to reset your Score?");
                if (newGame = GetYN())
                {
                    wins = loses = ties = 0;
                }
                if (!newGame)
                    break;

                Console.Write("Would you like to play again? ");
                newGame = GetYN();
            }
            Console.WriteLine();
        } while (newGame);
        Console.WriteLine("Goodbye\nPress any key to close...");
        Console.ReadKey(true);
    }

    public static int WhoWon(Gesture player1, Gesture player2)
    {
        return (player1 == player2) ? 0 : (((int)player1 % 2 == 0) == (((int)player1 + (int)player2) % 2 == 0)) ? 1 : 2;
    }

    public static int GetIntInRange(int min = int.MinValue, int max = int.MaxValue, string prompt = "Please enter an Integer: ")
    {
        int g;
        do
        {
            Console.Write(prompt);
            if (int.TryParse(Console.ReadLine(), out g))
            {
                if (g >= min && g <= max)
                    return g;
                Console.WriteLine("You entered {0}, but the input must be in the range of ({1} - {2}. Please try again...", g, min, max);
            }
            else
                Console.WriteLine("That is not a real number. Please try again...");
        } while (true);
    }

    public static bool GetYN(string prompt = "(y/n): ")
    {
        do
        {
            Console.Write(prompt);
            switch (Console.ReadKey(true).Key)
            {
                case ConsoleKey.Y: Console.Write("Y\n"); return true;
                case ConsoleKey.N: Console.Write("N\n"); return false;
            }
        } while (true);
    }

    public static void GameSetup()
    {
        foreach (Gesture gesture in _gestures)
        {
            Console.WriteLine((int)gesture + ": " + Enum.GetName(typeof(Gesture), gesture));
        }
    }

    public static Gesture GetComputerGesture()
    {
        return (Gesture)_gestures.GetValue(new Random().Next(_gestures.Length));
    }

    public static Gesture GetPlayerGesture()
    {
        return (Gesture)GetIntInRange((int)_gestures.First(), (int)_gestures.Last(), "Please choose your Gesture: ");
    }

    public static string GetReason(Gesture winner, Gesture loser)
    {
        Tuple<int, int> k = _actions.Keys.Where(key => key.Item1 == (int)winner && key.Item2 == (int)loser).FirstOrDefault();

        foreach (var key in _actions.Keys)
        {
            int w = (int)winner;
            int l = (int)loser;
            if (key.Item1 == w && key.Item2 == l)
                k = key;
        }

        return winner + " " + _actions[k] + " " + loser;
    }
}

enum Gesture { Rock = 1, Paper = 2, Scissors = 3, Spock = 4, Lizard = 5 }

class Program
{
    private static Gesture[] _gestures = (Gesture[])Enum.GetValues(typeof(Gesture));

    private static Dictionary<Tuple<int, int>, string> _actions = new Dictionary<Tuple<int, int>, string>()
    {
        {Tuple.Create<int,int>(1,3), "Crushes"},
        {Tuple.Create<int,int>(1,5), "Crushes"},
        {Tuple.Create<int,int>(2,1), "Covers"},
        {Tuple.Create<int,int>(2,4), "Disproves"},
        {Tuple.Create<int,int>(3,2), "Cuts"},
        {Tuple.Create<int,int>(3,5), "Decapitates"},
        {Tuple.Create<int,int>(4,3), "Smashes"},
        {Tuple.Create<int,int>(4,1), "Vaporizes"},
        {Tuple.Create<int,int>(5,2), "Eats"},
        {Tuple.Create<int,int>(5,4), "Poisons"}
    };

    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 */

        int wins = 0;
        int loses = 0;
        int ties = 0;

        var newGame = true;
        do
        {
            GameSetup();
            Gesture playerGesture = GetPlayerGesture();
            Gesture computerGesture = GetComputerGesture();

            Console.WriteLine("Computer: " + computerGesture);
            Console.WriteLine("Your Gesture: " + playerGesture);

            switch (WhoWon(playerGesture, computerGesture))
            {
                case 0: ties++; Console.WriteLine("You have tied with the the computer."); break;
                case 1: wins++; Console.WriteLine("You win, " + GetReason(playerGesture, computerGesture)); break;
                case 2: loses++; Console.WriteLine("You lose, " + GetReason(computerGesture, playerGesture)); break;
            }

            Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", wins, loses, ties);

            Console.Write("Would you like to play again? ");
            if (!GetYN())
            {
                Console.WriteLine("Would you like to reset your Score?");
                if (newGame = GetYN())
                {
                    wins = loses = ties = 0;
                }
                if (!newGame)
                    break;

                Console.Write("Would you like to play again? ");
                newGame = GetYN();
            }
            Console.WriteLine();
        } while (newGame);
        Console.WriteLine("Goodbye\nPress any key to close...");
        Console.ReadKey(true);
    }

    public static int WhoWon(Gesture player1, Gesture player2)
    {
        return (player1 == player2) ? 0 : (((int)player1 % 2 == 0) == (((int)player1 + (int)player2) % 2 == 0)) ? 1 : 2;
    }

    public static int GetIntInRange(int min = int.MinValue, int max = int.MaxValue, string prompt = "Please enter an Integer: ")
    {
        int g;
        do
        {
            Console.Write(prompt);
            if (int.TryParse(Console.ReadLine(), out g))
            {
                if (g >= min && g <= max)
                    return g;
                Console.WriteLine("You entered {0}, but the input must be in the range of ({1} - {2}. Please try again...", g, min, max);
            }
            else
                Console.WriteLine("That is not a real number. Please try again...");
        } while (true);
    }

    public static bool GetYN(string prompt = "(y/n): ")
    {
        do
        {
            Console.Write(prompt);
            switch (Console.ReadKey(true).Key)
            {
                case ConsoleKey.Y: Console.Write("Y\n"); return true;
                case ConsoleKey.N: Console.Write("N\n"); return false;
            }
        } while (true);
    }

    public static void GameSetup()
    {
        foreach (Gesture gesture in _gestures)
        {
            Console.WriteLine((int)gesture + ": " + Enum.GetName(typeof(Gesture), gesture));
        }
    }

    public static Gesture GetComputerGesture()
    {
        return (Gesture)_gestures.GetValue(new Random().Next(_gestures.Length));
    }

    public static Gesture GetPlayerGesture()
    {
        return (Gesture)GetIntInRange((int)_gestures.First(), (int)_gestures.Last(), "Please choose your Gesture: ");
    }

    public static string GetReason(Gesture winner, Gesture loser)
    {
        Tuple<int, int> k = _actions.Keys.Where(key => key.Item1 == (int)winner && key.Item2 == (int)loser).FirstOrDefault();

        foreach (var key in _actions.Keys)
        {
            int w = (int)winner;
            int l = (int)loser;
            if (key.Item1 == w && key.Item2 == l)
                k = key;
        }

        return winner + " " + _actions[k] + " " + loser;
    }
}
enum Gesture
{
    Rock = 1,
    Paper = 2,
    Scissors = 3,
    Spock = 4,
    Lizard = 5
}

class Program
{
    private static Gesture[] _gestures = (Gesture[])Enum.GetValues(typeof(Gesture));

    private static Dictionary<Tuple<int, int>, string> _actions = new Dictionary<Tuple<int, int>, string>()
    {
        {Tuple.Create<int,int>(1,3), "Crushes"},
        {Tuple.Create<int,int>(1,5), "Crushes"},
        {Tuple.Create<int,int>(2,1), "Covers"},
        {Tuple.Create<int,int>(2,4), "Disproves"},
        {Tuple.Create<int,int>(3,2), "Cuts"},
        {Tuple.Create<int,int>(3,5), "Decapitates"},
        {Tuple.Create<int,int>(4,3), "Smashes"},
        {Tuple.Create<int,int>(4,1), "Vaporizes"},
        {Tuple.Create<int,int>(5,2), "Eats"},
        {Tuple.Create<int,int>(5,4), "Poisons"}
    };

    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 */

        int wins = 0;
        int loses = 0;
        int ties = 0;

        var newGame = true;
        do
        {
            GameSetup();
            Gesture playerGesture = GetPlayerGesture();
            Gesture computerGesture = GetComputerGesture();

            Console.WriteLine("Computer: " + computerGesture);
            Console.WriteLine("Your Gesture: " + playerGesture);

            switch (WhoWon(playerGesture, computerGesture))
            {
                case 0: ties++; Console.WriteLine("You have tied with the the computer."); break;
                case 1: wins++; Console.WriteLine("You win, " + GetReason(playerGesture, computerGesture)); break;
                case 2: loses++; Console.WriteLine("You lose, " + GetReason(computerGesture, playerGesture)); break;
            }

            Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", wins, loses, ties);

            Console.Write("Would you like to play again? ");
            if (!GetYN())
            {
                Console.WriteLine("Would you like to reset your Score?");
                if (newGame = GetYN())
                {
                    wins = loses = ties = 0;
                }
                if (!newGame)
                    break;

                Console.Write("Would you like to play again? ");
                newGame = GetYN();
            }
            Console.WriteLine();
        } while (newGame);
        Console.WriteLine("Goodbye\nPress any key to close...");
        Console.ReadKey(true);
    }

    public static int WhoWon(Gesture player1, Gesture player2)
    {
        return (player1 == player2) ? 0 : (((int)player1 % 2 == 0) == (((int)player1 + (int)player2) % 2 == 0)) ? 1 : 2;
    }

    public static int GetIntInRange(int min = int.MinValue, int max = int.MaxValue, string prompt = "Please enter an Integer: ")
    {
        int g;
        do
        {
            Console.Write(prompt);
            if (int.TryParse(Console.ReadLine(), out g))
            {
                if (g >= min && g <= max)
                    return g;
                Console.WriteLine("You entered {0}, but the input must be in the range of ({1} - {2}. Please try again...", g, min, max);
            }
            else
                Console.WriteLine("That is not a real number. Please try again...");
        } while (true);
    }

    public static bool GetYN(string prompt = "(y/n): ")
    {
        do
        {
            Console.Write(prompt);
            switch (Console.ReadKey(true).Key)
            {
                case ConsoleKey.Y: Console.Write("Y\n"); return true;
                case ConsoleKey.N: Console.Write("N\n"); return false;
            }
        } while (true);
    }

    public static void GameSetup()
    {
        foreach (Gesture gesture in _gestures)
        {
            Console.WriteLine((int)gesture + ": " + Enum.GetName(typeof(Gesture), gesture));
        }
    }

    public static Gesture GetComputerGesture()
    {
        return (Gesture)_gestures.GetValue(new Random().Next(_gestures.Length));
    }

    public static Gesture GetPlayerGesture()
    {
        return (Gesture)GetIntInRange((int)_gestures.First(), (int)_gestures.Last(), "Please choose your Gesture: ");
    }

    public static string GetReason(Gesture winner, Gesture loser)
    {
        Tuple<int, int> k = _actions.Keys.Where(key => key.Item1 == (int)winner && key.Item2 == (int)loser).FirstOrDefault();

        foreach (var key in _actions.Keys)
        {
            int w = (int)winner;
            int l = (int)loser;
            if (key.Item1 == w && key.Item2 == l)
                k = key;
        }

        return winner + " " + _actions[k] + " " + loser;
    }
}
Added full dump
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
fixed some bugs
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
added 19 characters in body
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
added more
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
added 1310 characters in body
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
added more points
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
added more points
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 20
  • 47
Loading