Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:23:57+00:00 2026-05-16T18:23:57+00:00

Is there a formula without if/else/switch/? etc that could replace the solution[,] matrix and

  • 0

Is there a formula without if/else/switch/? etc that could replace the solution[,] matrix and what are the implications or differences on performance/efficiency for that?

     class Program
{
    private static Random r = new Random();
    // names of the "moves"
    private static string[] rps = { "PAPER", "ROCK", "SCISSORS"};
    // result feedback string to be formatted
    private static string[] feedback = { "{1} Beats {0}, You Loose!","{0} Equals {1}, Draw!",  "{0} Beats {1}, You Win!" };
    // solution matrix ( 0 = loose ; 1 = draw ; 2 = win  // array1: for paper; array2: for rock; array3: for scissors; )
    private static int[,] solution = {{1, 2, 0},{0, 1, 2},{2, 0, 1}};

    /// <summary>
    /// Rock Paper scissors solution w/o calculation or if/case/else/
    /// </summary>
    /// <param name="args">dummy.</param>
    static void Main(string[] args)
    {
            // simulate the players move
            int player = r.Next(3);

            // simulate the computers move
            int computer = r.Next(3);

            // retrieve the result from the matrix
            int result = solution[player, computer];

            //write the result of the match
            Console.WriteLine(String.Format("you : {0} vs {1} : computer", rps[player], rps[computer]));
            Console.WriteLine(String.Format(feedback[result], rps[player], rps[computer]));

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:23:57+00:00Added an answer on May 16, 2026 at 6:23 pm

    Yes, there is a quite simple formula:

    player computer  solution  (c+4-p)%3
      0       0         1          1
      0       1         2          2
      0       2         0          0
      1       0         0          0
      1       1         1          1
      1       2         2          2
      2       0         2          2
      2       1         0          0
      2       2         1          1
    

    So you can use:

    int result = (computer + 4 - player) % 3;
    

    Accessing the array and calculating the value would take rougly the same time. However, the difference in performance in this application is negligible. Just writing the result to the console takes a lot longer than either using the array or calculating the value. Of course, by calculating the value you don’t need the array, but as it’s so small that doesn’t make much of a difference.

    Consider also the readability of the solution. The formula has no logical connection to what you use it for, it’s just a means to get to a specific result, so you would need a large comment that explains what it accomplishes…

    Edit:

    If you want focus on readability, you could put the logic in a separate class:

    public class Play {
    
      public enum Value { Paper = 0, Rock = 1, Scissors = 2 }
    
      private Value _value;
    
      public Play(Random rnd) {
        _value = (Value)rnd.Next(3);
      }
    
      public bool SameAs(Play other) {
        return _value == other._value;
      }
    
      public bool Beats(Play other) {
        return
          (_value == Value.Paper && other._value == Value.Rock) ||
          (_value == Value.Rock && other._value == Value.Scissors) ||
          (_value == Value.Scissors && other._value == Value.Paper);
      }
    
      public override string ToString() {
        switch (_value) {
          case Value.Paper: return "PAPER";
          case Value.Rock: return "ROCK";
          default: return "SCISSORS";
        }
      }
    
    }
    

    Now the logic gets clearer:

    Random r = new Random();
    
    Play player = new Play(r);
    Play computer = new Play(r);
    
    Console.WriteLine("you : {0} vs {1} : computer", player, computer);
    
    string feedback;
    if (player.SameAs(computer)) {
      feedback = "{0} Equals {1}, Draw!";
    } else if (player.Beats(computer)) {
      feedback = "{0} Beats {1}, You Win!";
    } else {
      feedback = "{1} Beats {0}, You Loose!";
    }
    
    Console.WriteLine(feedback, player, computer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Without using VBA (pure formula solution), is there a way to get the last
is there a way in Excel to have a formula that does something like
I could've sworn there was a way to do this (without DLLs), but going
Is there a way (without creating a formula datevalue(datetime) field) to aggregate a SOQL
Is there a formula that returns a value from the first line matching two
Sorry, I am not good at math But is there a mathematical formula that
Is there any formula to direct finding D.O.B from put Year,month and day's in
I know there is formula for going RGB -> Luminance, but I need given
Is there a tool or a formula for calculating man-hours required for a certain
Is there is a library for invoking math symbols/formula(not sure what to call), which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.