Commits

Kevin Berridge committed c3af1d3

First implementation: doesn't support draws, control loop not under test (should it be?)

  • Participants
  • Parent commits f01e0db

Comments (0)

Files changed (9)

File RockPaperScissors.Specs/GameSpecs.cs

 
 namespace RockPaperScissors.Specs
 {
-  [Subject( "Game" )]
+  [Subject( "Round" )]
   public class when_playing_a_round
   {
     static Player winner1;
     };
   }
 
-  public class with_stubs
+  [Subject( "Single Round" )]
+  public class when_playing_a_single_round_game
   {
-    protected static PromptUserStub _promptUserStub;
-    protected static DisplayOutputStub _displayOutputStub;
+    static bool shouldPlayAgainIfNoGamesPlayed;
+    static bool shouldPlayAgainIfOneGamePlayed;
+    static Player whoWon;
 
-    Establish context = () =>
+    Because of = () =>
     {
-      _promptUserStub = new PromptUserStub();
-      _displayOutputStub = new DisplayOutputStub();
+      List<Player> gameResults = new List<Player>();
+      SingleRoundStrategy srs = new SingleRoundStrategy();
+
+      shouldPlayAgainIfNoGamesPlayed = srs.ShouldPlayAgain( gameResults );
+
+      gameResults.Add( Player.First );
+
+      shouldPlayAgainIfOneGamePlayed = srs.ShouldPlayAgain( gameResults );
+
+      whoWon = srs.GetWhoWon( gameResults );
     };
+
+    It should_play_again_if_no_games_played = () =>
+      shouldPlayAgainIfNoGamesPlayed.ShouldBeTrue();
+
+    It should_not_play_again_if_one_game_played = () =>
+      shouldPlayAgainIfOneGamePlayed.ShouldBeFalse();
+
+    It should_know_who_won = () =>
+      whoWon.ShouldEqual( Player.First );
   }
-}
+}

File RockPaperScissors.Specs/RockPaperScissors.Specs.csproj

     <Content Include="Readme.txt" />
     <Content Include="References\Machine.Specifications.dll" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\RockPaperScissors\RockPaperScissors.csproj">
+      <Project>{7946806E-1880-46FF-870D-D3041151D117}</Project>
+      <Name>RockPaperScissors</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

File RockPaperScissors/IKnowWhoWon.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public interface IKnowWhoWon
+  {
+    Player GetWhoWon( List<Player> gameResults );
+  }
+}

File RockPaperScissors/IShouldPlayAgain.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public interface IShouldPlayAgain
+  {
+    bool ShouldPlayAgain( List<Player> gameResults );
+  }
+}

File RockPaperScissors/Player.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public enum Player
+  {
+    First,
+    Second
+  }
+}

File RockPaperScissors/Program.cs

   {
     static void Main( string[] args )
     {
+      RockPaperScissors rps = new RockPaperScissors();
+      SingleRoundStrategy srs = new SingleRoundStrategy();
+      List<Player> gameResults = new List<Player>();
+      while ( srs.ShouldPlayAgain( gameResults ) )
+      {
+        Console.Write( "[R]ock, [P]aper, or [S]cissors?" );
+        ConsoleKeyInfo player1Move = Console.ReadKey();
+        Console.WriteLine();
+
+        Console.Write( "[R]ock, [P]aper, or [S]cissors?" );
+        ConsoleKeyInfo player2Move = Console.ReadKey();
+        Console.WriteLine();
+
+        Player winner = rps.Play( player1Move.KeyChar.ToString(), player2Move.KeyChar.ToString() );
+        gameResults.Add( winner );
+
+        Console.WriteLine( ( winner == Player.First ? "Player1" : "Player2" ) + " Wins!" );
+      }
+
+      Player bigWinner = srs.GetWhoWon( gameResults );
+      Console.WriteLine();
+      Console.WriteLine( ( bigWinner == Player.First ? "Player1" : "Player2" ) + " Wins it all!" );
+
+      Console.ReadLine();
     }
   }
-}
+}

File RockPaperScissors/RockPaperScissors.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public class RockPaperScissors
+  {
+    public Player Play( string player1Move, string player2Move )
+    {
+      switch ( player1Move )
+      {
+        case "R":
+          if ( player2Move == "S" ) return Player.First;
+          else return Player.Second;
+        case "S":
+          if ( player2Move == "P" ) return Player.First;
+          else return Player.Second;
+        case "P":
+          if ( player2Move == "R" ) return Player.First;
+          else return Player.Second;
+        default:
+          throw new NotSupportedException( "No matches" );
+      }
+    }
+  }
+}

File RockPaperScissors/RockPaperScissors.csproj

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="IKnowWhoWon.cs" />
+    <Compile Include="IShouldPlayAgain.cs" />
+    <Compile Include="Player.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RockPaperScissors.cs" />
+    <Compile Include="SingleRoundStrategy.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

File RockPaperScissors/SingleRoundStrategy.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public class SingleRoundStrategy : IShouldPlayAgain, IKnowWhoWon
+  {
+    public bool ShouldPlayAgain( List<Player> gameResults )
+    {
+      if ( gameResults == null ) return true;
+      else if ( gameResults.Count != 1 ) return true;
+      else return false;
+    }
+
+    public Player GetWhoWon( List<Player> gameResults )
+    {
+      if ( gameResults == null || gameResults.Count == 0 )
+        throw new InvalidOperationException( "Must have gameResults" );
+
+      return gameResults[0];
+    }
+  }
+}