Some minor syntax detail: C# has collection initializers so one can write:
List<string> listOfGestures = new List<string> { "rock", "paper", "scissors", "lizard", "spock" }You use magic strings for yourthe gestures all over the place. This can pose a problem if you want to change them. You could do a find and replace but youthis could inadvertently change a part of a method or variable name which happens to include "rock" if you are not careful.
In other solutions to the problem posted before enums were used instead. If you insist on using strings you shouldthen make them named constants and use those instead
const string Rock = "rock";
const string Paper = "paper";
... etc.
- YourThe main problem is that the entire implementation is basically just one big blob of code in the main method. Logic and input/output are totally intertwined. Assume you have to write an automated test case for this - it will drive you insane.
If I were to encounter code like this in production I would very likely kill it and rewrite it - the problem is fairly simple and given it's untestable there aren't any unit tests anyway. If the code were too much to rewrite then I'd start refactoring it. Possible steps:
- Create a
Gameclass which effectively encapsulates the mainwhileloop up until the lineConsole.WriteLine ("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
Dump everything into one method into that class for starters. - The
Gameclass should also keep track of the score. - OverrideToStringto print out the current scores. - Move the "ask user if he wants to play another round" into a method on theGameclassAfter that yourthe
Mainshould look somewhat like this:var game = new Game(); var continuePlaying = true; while (continuePlaying) { game.Play(); Console.WriteLine(game.ToString()); continuePlaying = game.ShouldContinue(); }
- Next step would be to start refactoring the
Gameclass and the methods within.Move the decision logic into it's own internal private method to reduce the bulk of the
Play()method.Define an interface for getting user input and another one for writing output. Pass these interfaces into the
Gameconstructor. Provide a console implementation for both interfaces. Change all the input and output to use the interfaces rather than the console.Now
Gameis decoupled from the specific input and output methods and you can start writing unit tests for thePlay()and theShouldContinue()methods. Once you have done that you can continue refactoring those methods. And because you have just created unit tests you should be able to detect if you create any regressions (introducing bugs) while doing so.Wash, Rinse, Repeat