1  // Rock, Paper, Scissors              Bill Rubin
   2  using System;
   3  
   4  public class Strategy {
   5      protected Strategy(string name) {name_ = name; }
   6      public virtual Gesture execute() {return Gesture.Rock;}
   7      public override string ToString() {return name_ + " Strategy";}
   8      public void setPlayer(Player player) {player_ = player;}
   9  
  10      public static Strategy create(string name) {
  11          if (name == "Rock")     return new RockStrategy();
  12          if (name == "Paper")    return new PaperStrategy();
  13          if (name == "Scissors") return new ScissorsStrategy();
  14          if (name == "Random")   return new RandomStrategy();
  15          if (name == "Cyclic")   return new CyclicStrategy();
  16          throw new Exception("Unknown strategy \"" + name + "\"");
  17          }
  18  
  19      public static string[] names = { "Rock", "Paper", "Scissors", "Random", "Cyclic" };
  20      
  21      protected Player player_;
  22      private string name_;
  23  }
  24  
  25  class RockStrategy : Strategy {
  26      public RockStrategy() : base("Rock"){}
  27      public override Gesture execute() { return Gesture.Rock; }
  28  }
  29  
  30  class PaperStrategy : Strategy {
  31      public PaperStrategy() : base("Paper"){}
  32      public override Gesture execute() { return Gesture.Paper; }
  33  }
  34  
  35  class ScissorsStrategy : Strategy {
  36      public ScissorsStrategy() : base("Scissors"){}
  37      public override Gesture execute() { return Gesture.Scissors; }
  38  }
  39  
  40  class RandomStrategy : Strategy {
  41      public RandomStrategy() : base("Random"){}
  42      public override Gesture execute() { return (Gesture)random_.Next(3); }
  43  
  44      private Random random_ = new Random(0);
  45  }
  46  
  47  class CyclicStrategy : Strategy {
  48      public CyclicStrategy() : base("Cyclic"){}
  49  
  50      public override Gesture execute() {
  51          int round = player_.getRound();
  52          Gesture lastGesture = round == 0 ? Gesture.Scissors : player_.getMyGesture(round-1);
  53  
  54          switch (lastGesture) {
  55              case Gesture.Rock:     return Gesture.Paper;    
  56              case Gesture.Paper:    return Gesture.Scissors; 
  57              case Gesture.Scissors: return Gesture.Rock;
  58              default:  throw new Exception("Unknown last gesture."); 
  59              }
  60          }
  61  }