Commits

Kevin Berridge  committed f27bfc8

Scubs input

  • Participants
  • Parent commits 2dec106

Comments (0)

Files changed (6)

File RockPaperScissors.Specs/GameSpecs.cs

     Because of = () =>
     {
       RockPaperScissors rps = new RockPaperScissors();
-      winner1 = rps.Play( "R", "S" );
-      winner2 = rps.Play( "S", "R" );
+      winner1 = rps.Play( Move.Rock, Move.Scissors );
+      winner2 = rps.Play( Move.Scissors, Move.Rock );
 
-      winner3 = rps.Play( "S", "P" );
-      winner4 = rps.Play( "P", "S" );
+      winner3 = rps.Play( Move.Scissors, Move.Paper );
+      winner4 = rps.Play( Move.Paper, Move.Scissors );
 
-      winner5 = rps.Play( "P", "R" );
-      winner6 = rps.Play( "R", "P" );
+      winner5 = rps.Play( Move.Paper, Move.Rock );
+      winner6 = rps.Play( Move.Rock, Move.Paper );
 
-      winner7 = rps.Play( "R", "R" );
-      winner8 = rps.Play( "S", "S" );
-      winner9 = rps.Play( "P", "P" );
+      winner7 = rps.Play( Move.Rock, Move.Rock );
+      winner8 = rps.Play( Move.Scissors, Move.Scissors );
+      winner9 = rps.Play( Move.Paper, Move.Paper );
     };
 
     It should_pick_rock_over_scissors = () =>
     It should_know_who_won = () =>
       whoWon.ShouldEqual( Player.First );
   }
+
+  [Subject( "Input" )]
+  public class when_converting_char_to_move
+  {
+    static Move R;
+    static Move Rlower;
+    static Move P;
+    static Move Plower;
+    static Move S;
+    static Move Slower;
+    static Exception caught;
+
+    Because of = () =>
+    {
+      R = InputConverter.Convert( 'R' );
+      Rlower = InputConverter.Convert( 'r' );
+
+      P = InputConverter.Convert( 'P' );
+      Plower = InputConverter.Convert( 'p' );
+
+      S = InputConverter.Convert( 'S' );
+      Slower = InputConverter.Convert( 's' );
+
+      caught = Catch.Exception( () => InputConverter.Convert( 'A' ) );
+    };
+
+    It should_convert_lower_or_upper_R_to_Rock = () =>
+    {
+      R.ShouldEqual( Move.Rock );
+      Rlower.ShouldEqual( Move.Rock );
+    };
+
+    It should_convert_lower_or_upper_P_to_Paper = () =>
+    {
+      P.ShouldEqual( Move.Paper );
+      Plower.ShouldEqual( Move.Paper );
+    };
+
+    It should_convert_lower_or_upper_S_to_Scissors = () =>
+    {
+      S.ShouldEqual( Move.Scissors );
+      Slower.ShouldEqual( Move.Scissors );
+    };
+
+    It should_throw_exception_otherwise = () => caught.ShouldBeOfType( typeof( ArgumentOutOfRangeException ) );
+  }
 }

File RockPaperScissors/InputConverter.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public class InputConverter
+  {
+    public static Move Convert( char input )
+    {
+      char scrubbedInput = Char.ToUpper( input );
+      switch ( scrubbedInput )
+      {
+        case 'R': return Move.Rock;
+        case 'P': return Move.Paper;
+        case 'S': return Move.Scissors;
+        default: throw new ArgumentOutOfRangeException( scrubbedInput + " not recognized" );
+      }
+    }
+  }
+}

File RockPaperScissors/Move.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace RockPaperScissors
+{
+  public enum Move
+  {
+    Rock,
+    Paper,
+    Scissors
+  }
+}

File RockPaperScissors/Program.cs

       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();
+        Move player1Move = PromptForMove();
+        Move player2Move = PromptForMove();
 
-        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() );
+        Player winner = rps.Play( player1Move, player2Move );
         gameResults.Add( winner );
 
-        Console.WriteLine( ( winner == Player.First ? "Player1" : "Player2" ) + " Wins!" );
+        if ( winner == Player.Draw )
+          Console.WriteLine( "It's a draw" );
+        else
+          Console.WriteLine( ( winner == Player.First ? "Player1" : "Player2" ) + " Wins!" );
       }
 
       Player bigWinner = srs.GetWhoWon( gameResults );
 
       Console.ReadLine();
     }
+
+    static Move PromptForMove()
+    {
+      Move m = Move.Rock;
+      bool hasMove = false;
+      while ( !hasMove )
+      {
+        Console.Write( "[R]ock, [P]aper, or [S]cissors? " );
+        ConsoleKeyInfo player1Key = Console.ReadKey();
+        Console.WriteLine();
+
+        try
+        {
+          m = InputConverter.Convert( player1Key.KeyChar );
+          hasMove = true;
+        }
+        catch ( ArgumentOutOfRangeException )
+        {
+          Console.WriteLine( "Invalid selection" + Environment.NewLine );
+        }
+      }
+
+      return m;
+    }
   }
 }

File RockPaperScissors/RockPaperScissors.cs

 {
   public class RockPaperScissors
   {
-    public Player Play( string player1Move, string player2Move )
+    public Player Play( Move player1Move, Move player2Move )
     {
       switch ( player1Move )
       {
-        case "R":
-          if ( player2Move == "S" ) return Player.First;
-          else if ( player2Move == "R" ) return Player.Draw;
+        case Move.Rock:
+          if ( player2Move == Move.Scissors ) return Player.First;
+          else if ( player2Move == Move.Rock ) return Player.Draw;
           else return Player.Second;
-        case "S":
-          if ( player2Move == "P" ) return Player.First;
-          else if ( player2Move == "S" ) return Player.Draw;
+        case Move.Scissors:
+          if ( player2Move == Move.Paper ) return Player.First;
+          else if ( player2Move == Move.Scissors ) return Player.Draw;
           else return Player.Second;
-        case "P":
-          if ( player2Move == "R" ) return Player.First;
-          else if ( player2Move == "P" ) return Player.Draw;
+        case Move.Paper:
+          if ( player2Move == Move.Rock ) return Player.First;
+          else if ( player2Move == Move.Paper ) return Player.Draw;
           else return Player.Second;
         default:
           throw new NotSupportedException( "No matches" );

File RockPaperScissors/RockPaperScissors.csproj

   </ItemGroup>
   <ItemGroup>
     <Compile Include="IKnowWhoWon.cs" />
+    <Compile Include="InputConverter.cs" />
     <Compile Include="IShouldPlayAgain.cs" />
+    <Compile Include="Move.cs" />
     <Compile Include="Player.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />