1  // Rock, Paper, Scissors              Bill Rubin
   2  using System;
   3  using System.Collections.Generic;
   4  using System.Threading;
   5  
   6  public class Player {
   7      public Player(Barrier barrier, int playerNumber, Strategy strategy) {
   8          barrier_ = barrier;
   9          playerNumber_ = playerNumber;
  10          strategy_ = strategy;
  11          strategy_.setPlayer(this);
  12          }
  13  
  14      public void playMatch() {
  15          while (barrier_.CurrentPhaseNumber < numRounds) {
  16              /*Console.Write("Player " + playerNumber_.ToString() 
  17                  + " is about to throw a gesture.  Barrier phase = " 
  18                  + barrier_.CurrentPhaseNumber.ToString() + "\n");*/
  19              throwGesture();
  20              barrier_.SignalAndWait();
  21              /*Console.Write("Player " + playerNumber_.ToString()  
  22                   + " is about to increment score.\n");*/
  23              incrementScore();
  24              }
  25          }
  26  
  27      private void throwGesture() { thrown_[barrier_.CurrentPhaseNumber] = strategy_.execute(); }
  28  
  29      private void incrementScore() { 
  30          foreach (Player player in otherPlayers_) 
  31              incrementScore(player.thrown_[barrier_.CurrentPhaseNumber - 1]); 
  32              }
  33  
  34      private void incrementScore(Gesture gesture) {
  35          switch (thrown_[barrier_.CurrentPhaseNumber - 1]) {
  36              case Gesture.Rock:     if (gesture != Gesture.Scissors) return; break;
  37              case Gesture.Paper:    if (gesture != Gesture.Rock    ) return; break;
  38              case Gesture.Scissors: if (gesture != Gesture.Paper   ) return; break;
  39              }
  40          totalScore_++;
  41          }
  42      
  43      public void setPlayers(List<Player> players) 
  44          { otherPlayers_ = players.FindAll( player => player!=this );  /* Exclude this. */ }
  45  
  46      public void displayScore() { 
  47          Console.Write(playerNumber_.ToString() + ". " + strategy_.ToString() + " \tScore: " 
  48              + totalScore_.ToString() + "\n"); 
  49              }
  50  
  51      public int getRound() {return (int)barrier_.CurrentPhaseNumber;}
  52  
  53      public Gesture getMyGesture(int round) {return thrown_[round];}
  54  
  55      private Barrier barrier_;
  56      private int playerNumber_;
  57      private Strategy strategy_;
  58      private int totalScore_ = 0;
  59      private List<Player> otherPlayers_;
  60      private static int numRounds  = 1000;
  61      private Gesture[] thrown_ = new Gesture[numRounds]; 
  62  }
  63