Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Created June 2, 2019 10:59
Show Gist options
  • Save DhavalDalal/116ddd483da92bcfdaaf79b400c1a2cb to your computer and use it in GitHub Desktop.
Save DhavalDalal/116ddd483da92bcfdaaf79b400c1a2cb to your computer and use it in GitHub Desktop.
Paper Scissor Rock (C#)

Paper, Scissor, Rock (C#)

Problem Statement

Two players choose one of rock, paper and scissors after counting to three and making one of these gestures:

  • A clenched fist representing a rock.

  • A flat hand representing a piece of paper.

  • Index and middle finger extended representing a pair of scissors

Rules

If they choose the same gesture, neither wins; if not, the result is decided this way:

  • Rock defeats scissors, because a rock will blunt a pair of scissors

  • Paper defeats rock, because a paper can wrap up a rock

  • Scissors defeat paper, because scissors cut paper

using System;
public interface Beatable {
string Beats(Beatable other);
}
class Paper : Beatable {
public string Beats(Beatable other) {
if (other.GetType() == typeof(Rock))
return "paper wins";
if (other.GetType() == typeof(Scissor))
return "paper loses";
return "draw";
}
}
class Scissor : Beatable {
public string Beats(Beatable other) {
if (other.GetType() == typeof(Rock))
return "scissor loses";
if (other.GetType() == typeof(Paper))
return "scissor wins";
return "draw";
}
}
class Rock : Beatable {
public string Beats(Beatable other) {
if (other.GetType() == typeof(Paper))
return "rock loses";
if (other.GetType() == typeof(Scissor))
return "rock wins";
return "draw";
}
}
public class PaperScissorRock {
public static void Main(string[] args) {
var paper = new Paper();
var rock = new Rock();
var scissor = new Scissor();
Console.WriteLine("paper.Beats(paper) = " + paper.Beats(paper));
Console.WriteLine("paper.Beats(scissor) = " + paper.Beats(scissor));
Console.WriteLine("paper.Beats(rock) = " + paper.Beats(rock));
Console.WriteLine("scissor.Beats(paper) = " + scissor.Beats(paper));
Console.WriteLine("scissor.Beats(scissor) = " + scissor.Beats(scissor));
Console.WriteLine("scissor.Beats(rock) = " + scissor.Beats(rock));
Console.WriteLine("rock.Beats(paper) = " + rock.Beats(paper));
Console.WriteLine("rock.Beats(scissor) = " + rock.Beats(scissor));
Console.WriteLine("rock.Beats(rock) = " + rock.Beats(rock));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment