Commits

Kevin Berridge  committed 2dec106

Added draws

  • Participants
  • Parent commits 25f32d5

Comments (0)

Files changed (4)

File RockPaperScissors.Specs/GameSpecs.cs

     static Player winner4;
     static Player winner5;
     static Player winner6;
+    static Player winner7;
+    static Player winner8;
+    static Player winner9;
 
     Because of = () =>
     {
 
       winner5 = rps.Play( "P", "R" );
       winner6 = rps.Play( "R", "P" );
+
+      winner7 = rps.Play( "R", "R" );
+      winner8 = rps.Play( "S", "S" );
+      winner9 = rps.Play( "P", "P" );
     };
 
     It should_pick_rock_over_scissors = () =>
       winner5.ShouldEqual( Player.First );
       winner6.ShouldEqual( Player.Second );
     };
+
+    It should_draw_on_rock_rock = () => winner7.ShouldEqual( Player.Draw );
+
+    It should_draw_on_scissors_scissors = () => winner8.ShouldEqual( Player.Draw );
+    
+    It should_draw_on_paper_paper = () => winner9.ShouldEqual( Player.Draw );
   }
 
   [Subject( "Single Round" )]
   public class when_playing_a_single_round_game
   {
     static bool shouldPlayAgainIfNoGamesPlayed;
-    static bool shouldPlayAgainIfOneGamePlayed;
+    static bool shouldPlayAgainIfOneGameWon;
+    static bool shouldPlayAgainIfDraws;
     static Player whoWon;
 
     Because of = () =>
 
       gameResults.Add( Player.First );
 
-      shouldPlayAgainIfOneGamePlayed = srs.ShouldPlayAgain( gameResults );
+      shouldPlayAgainIfOneGameWon = srs.ShouldPlayAgain( gameResults );
+
+      gameResults = new List<Player>();
+      gameResults.Add( Player.Draw );
+      gameResults.Add( Player.Draw );
+
+      shouldPlayAgainIfDraws = srs.ShouldPlayAgain( gameResults );
+
+      gameResults.Add( Player.First );
 
       whoWon = srs.GetWhoWon( gameResults );
     };
 
-    It should_play_again_if_no_games_played = () =>
-      shouldPlayAgainIfNoGamesPlayed.ShouldBeTrue();
+    It should_play_again_if_no_games_played = () => shouldPlayAgainIfNoGamesPlayed.ShouldBeTrue();
 
-    It should_not_play_again_if_one_game_played = () =>
-      shouldPlayAgainIfOneGamePlayed.ShouldBeFalse();
+    It should_not_play_again_if_one_game_won = () => shouldPlayAgainIfOneGameWon.ShouldBeFalse();
+
+    It should_play_again_if_games_drawn_but_none_won = () => shouldPlayAgainIfDraws.ShouldBeTrue();
 
     It should_know_who_won = () =>
       whoWon.ShouldEqual( Player.First );

File RockPaperScissors/Player.cs

   public enum Player
   {
     First,
-    Second
+    Second,
+    Draw
   }
-}
+}

File RockPaperScissors/RockPaperScissors.cs

       {
         case "R":
           if ( player2Move == "S" ) return Player.First;
+          else if ( player2Move == "R" ) return Player.Draw;
           else return Player.Second;
         case "S":
           if ( player2Move == "P" ) return Player.First;
+          else if ( player2Move == "S" ) return Player.Draw;
           else return Player.Second;
         case "P":
           if ( player2Move == "R" ) return Player.First;
+          else if ( player2Move == "P" ) return Player.Draw;
           else return Player.Second;
         default:
           throw new NotSupportedException( "No matches" );

File RockPaperScissors/SingleRoundStrategy.cs

   {
     public bool ShouldPlayAgain( List<Player> gameResults )
     {
-      if ( gameResults == null ) return true;
-      else if ( gameResults.Count != 1 ) return true;
-      else return false;
+      return FindFirstWinner( gameResults ) == Player.Draw;
     }
 
     public Player GetWhoWon( List<Player> gameResults )
     {
-      if ( gameResults == null || gameResults.Count == 0 )
-        throw new InvalidOperationException( "Must have gameResults" );
+      return FindFirstWinner( gameResults );
+    }
 
-      return gameResults[0];
+    Player FindFirstWinner( List<Player> gameResults )
+    {
+      if ( gameResults == null || gameResults.Count == 0 ) return Player.Draw;
+
+      foreach ( Player p in gameResults ) // find first winner
+        if ( p != Player.Draw ) return p;
+
+      return Player.Draw;
     }
   }
 }