diff --git a/29 Craps/csharp/Craps/Craps/CrapsGame.cs b/29 Craps/csharp/Craps/Craps/CrapsGame.cs new file mode 100644 index 00000000..c0da368c --- /dev/null +++ b/29 Craps/csharp/Craps/Craps/CrapsGame.cs @@ -0,0 +1,72 @@ +namespace Craps +{ + public enum Result + { + // It's not used in this program but it's often a good idea to include a "none" + // value in an enum so that you can set an instance of the enum to "invalid" or + // initialise it to "none of the valid values". + noResult, + naturalWin, + snakeEyesLoss, + naturalLoss, + pointLoss, + pointWin, + }; + + class CrapsGame + { + private readonly UserInterface ui; + + public CrapsGame(ref UserInterface ui) + { + this.ui = ui; + } + + public Result Play(out int diceRoll) + { + + var dice1 = new Dice(); + var dice2 = new Dice(); + diceRoll = dice1.Roll() + dice2.Roll(); + + if (Win(diceRoll)) + { + return Result.naturalWin; + } + else if (Lose(diceRoll)) + { + return (diceRoll == 2) ? Result.snakeEyesLoss : Result.naturalLoss; + } + else + { + var point = diceRoll; + ui.Point(point); + + while (true) + { + var newRoll = dice1.Roll() + dice2.Roll(); + if (newRoll == point) + { + return Result.pointWin; + } + else if (newRoll == 7) + { + return Result.pointLoss; + } + + ui.NoPoint(diceRoll); + } + } + } + + private bool Lose(int diceRoll) + { + return diceRoll == 2 || diceRoll == 3 || diceRoll == 12; + } + + private bool Win(int diceRoll) + { + return diceRoll == 7 || diceRoll == 11; + } + } +} diff --git a/29 Craps/csharp/Craps/Craps/Program.cs b/29 Craps/csharp/Craps/Craps/Program.cs index 916f331a..31caffd8 100644 --- a/29 Craps/csharp/Craps/Craps/Program.cs +++ b/29 Craps/csharp/Craps/Craps/Program.cs @@ -1,4 +1,4 @@ -using System; +using System.Diagnostics; @@ -6,21 +6,10 @@ namespace Craps { class Program { - enum Result - { - naturalWin, - snakeEyesLoss, - loss, - pointLoss, - pointWin, - }; - static void Main(string[] args) { - var dice1 = new Dice(); - var dice2 = new Dice(); var ui = new UserInterface(); - + var game = new CrapsGame(ref ui); int winnings = 0; ui.Intro(); @@ -28,56 +17,33 @@ namespace Craps do { var bet = ui.PlaceBet(); - var diceRoll = dice1.Roll() + dice2.Roll(); - bool isWinner = false; + var result = game.Play(out int diceRoll); - if (Win(diceRoll)) + switch (result) { - winnings += bet; - isWinner = true; - } - else if (Lose(diceRoll)) - { - winnings -= bet; - isWinner = false; - } - else - { - var point = diceRoll; - ui.Point(point); + case Result.naturalWin: + winnings += bet; + break; - while (true) - { - var newRoll = dice1.Roll() + dice2.Roll(); - if (newRoll == point) - { - winnings += bet; - isWinner = true; - break; - } - else if (newRoll == 7) - { - winnings -= bet; - isWinner = false; - break; - } + case Result.naturalLoss: + case Result.snakeEyesLoss: + case Result.pointLoss: + winnings -= bet; + break; - ui.RollAgain(); - } + case Result.pointWin: + winnings += (2 * bet); + break; + + // Include a default so that we will be warned if the values of the enum + // ever change and we forget to add code to handle the new value. + default: + Debug.Assert(false); // We should never get here. + break; } - ui.ShowResult(isWinner, bet); + ui.ShowResult(result, diceRoll, bet); } while (ui.PlayAgain(winnings)); } - - private static bool Lose(int diceRoll) - { - throw new NotImplementedException(); - } - - private static bool Win(int diceRoll) - { - throw new NotImplementedException(); - } } } diff --git a/29 Craps/csharp/Craps/Craps/UserInterface.cs b/29 Craps/csharp/Craps/Craps/UserInterface.cs index 5b1bb280..72bdb17b 100644 --- a/29 Craps/csharp/Craps/Craps/UserInterface.cs +++ b/29 Craps/csharp/Craps/Craps/UserInterface.cs @@ -1,40 +1,110 @@ using System; +using System.Diagnostics; + + namespace Craps { - public class UserInterface + public class UserInterface { - public UserInterface() - { - } - public void Intro() { + Console.WriteLine(" CRAPS"); + Console.WriteLine(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n"); + Console.WriteLine("2,3,12 ARE LOSERS; 4,5,6,8,9,10 ARE POINTS; 7,11 ARE NATURAL WINNERS."); + + // In the original game a random number would be generated and then thrown away for as many + // times as the number the user entered. This is presumably something to do with ensuring + // that different random numbers will be generated each time the program is run. + // + // This is not necessary in C#; the random number generator uses the time as a seed so the + // results will always be different every time it is run. + // + // So that the game exactly matches the original game we ask the question but then ignore the + // answer. + Console.WriteLine("PICK A NUMBER AND INPUT TO ROLL DICE"); + GetInt(); } - public int PlaceBet() + public int PlaceBet() { - return 0; + Console.WriteLine("INPUT THE AMOUNT OF YOUR WAGER."); + int n = GetInt(); + Console.WriteLine("I WILL NOW THROW THE DICE"); + + return n; } public bool PlayAgain(int bet) { - throw new NotImplementedException(); + // Goodness knows why we have to enter 5 to play + // again but that's what the original game asked. + Console.WriteLine("IF YOU WANT TO PLAY AGAIN PRINT 5 IF NOT PRINT 2"); + + return GetInt() == 5; } - internal void ShowResult(bool isWinner, int winnings) + public void NoPoint(int diceRoll) { - throw new NotImplementedException(); + Console.WriteLine($"{diceRoll} - NO POINT. I WILL ROLL AGAIN "); } - internal void RollAgain() + public void Point(int point) { - throw new NotImplementedException(); + Console.WriteLine($"{point} IS THE POINT. I WILL ROLL AGAIN"); } - internal void Point(int point) + public void ShowResult(Result result, int diceRoll, int bet) { - throw new NotImplementedException(); + switch (result) + { + case Result.naturalWin: + Console.WriteLine($"{diceRoll} - NATURAL....A WINNER!!!!"); + Console.WriteLine($"{diceRoll} PAYS EVEN MONEY, YOU WIN {bet} DOLLARS"); + break; + + case Result.naturalLoss: + Console.WriteLine($"{diceRoll} - CRAPS...YOU LOSE."); + Console.WriteLine($"YOU LOSE {bet} DOLLARS."); + break; + + case Result.snakeEyesLoss: + Console.WriteLine($"{diceRoll} - SNAKE EYES....YOU LOSE."); + Console.WriteLine($"YOU LOSE {bet} DOLLARS."); + break; + + case Result.pointLoss: + Console.WriteLine($"{diceRoll} - CRAPS. YOU LOSE."); + Console.WriteLine($"YOU LOSE ${bet}"); + break; + + case Result.pointWin: + Console.WriteLine($"{diceRoll} - A WINNER.........CONGRATS!!!!!!!!"); + Console.WriteLine($"AT 2 TO 1 ODDS PAYS YOU...LET ME SEE... {2 * bet} DOLLARS"); + break; + + // Include a default so that we will be warned if the values of the enum + // ever change and we forget to add code to handle the new value. + default: + Debug.Assert(false); // We should never get here. + break; + } + } + + private int GetInt() + { + while (true) + { + string input = Console.ReadLine(); + if (int.TryParse(input, out int n)) + { + return n; + } + else + { + Console.WriteLine("ENTER AN INTEGER"); + } + } } } } diff --git a/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs b/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs index 2534f21f..576c2b82 100644 --- a/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs +++ b/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs @@ -1,5 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; using Craps; +using Microsoft.VisualStudio.TestTools.UnitTesting;