From c675cd3af801855747e894201981cba7dbacb3a0 Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Sat, 10 Apr 2021 23:27:22 +0100 Subject: [PATCH 1/5] Initial version of Craps game. --- 29 Craps/csharp/Craps/Craps.sln | 31 +++++++++++++++++++ 29 Craps/csharp/Craps/Craps/Craps.csproj | 8 +++++ 29 Craps/csharp/Craps/Craps/Dice.cs | 24 ++++++++++++++ 29 Craps/csharp/Craps/Craps/Program.cs | 12 +++++++ .../Craps/CrapsTester/CrapsTester.csproj | 20 ++++++++++++ .../csharp/Craps/CrapsTester/CrapsTests.cs | 31 +++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 29 Craps/csharp/Craps/Craps.sln create mode 100644 29 Craps/csharp/Craps/Craps/Craps.csproj create mode 100644 29 Craps/csharp/Craps/Craps/Dice.cs create mode 100644 29 Craps/csharp/Craps/Craps/Program.cs create mode 100644 29 Craps/csharp/Craps/CrapsTester/CrapsTester.csproj create mode 100644 29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs diff --git a/29 Craps/csharp/Craps/Craps.sln b/29 Craps/csharp/Craps/Craps.sln new file mode 100644 index 00000000..f6ab76ec --- /dev/null +++ b/29 Craps/csharp/Craps/Craps.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31112.23 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Craps", "Craps\Craps.csproj", "{783A49D7-DADE-477A-9973-D9457258573B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrapsTester", "CrapsTester\CrapsTester.csproj", "{44DFE8DB-715F-428E-992D-A97C34D47B98}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {783A49D7-DADE-477A-9973-D9457258573B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {783A49D7-DADE-477A-9973-D9457258573B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {783A49D7-DADE-477A-9973-D9457258573B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {783A49D7-DADE-477A-9973-D9457258573B}.Release|Any CPU.Build.0 = Release|Any CPU + {44DFE8DB-715F-428E-992D-A97C34D47B98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44DFE8DB-715F-428E-992D-A97C34D47B98}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44DFE8DB-715F-428E-992D-A97C34D47B98}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44DFE8DB-715F-428E-992D-A97C34D47B98}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {871679FF-B86C-468B-960E-DFC625CDB5D0} + EndGlobalSection +EndGlobal diff --git a/29 Craps/csharp/Craps/Craps/Craps.csproj b/29 Craps/csharp/Craps/Craps/Craps.csproj new file mode 100644 index 00000000..c73e0d16 --- /dev/null +++ b/29 Craps/csharp/Craps/Craps/Craps.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/29 Craps/csharp/Craps/Craps/Dice.cs b/29 Craps/csharp/Craps/Craps/Dice.cs new file mode 100644 index 00000000..3485f0f6 --- /dev/null +++ b/29 Craps/csharp/Craps/Craps/Dice.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Craps +{ + public class Dice + { + private Random rand = new Random(); + public readonly int sides; + + public Dice() + { + sides = 6; + } + + public Dice(int sides) + { + this.sides = sides; + } + + public int Roll() => rand.Next(1, sides); + } +} diff --git a/29 Craps/csharp/Craps/Craps/Program.cs b/29 Craps/csharp/Craps/Craps/Program.cs new file mode 100644 index 00000000..17688427 --- /dev/null +++ b/29 Craps/csharp/Craps/Craps/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace Craps +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/29 Craps/csharp/Craps/CrapsTester/CrapsTester.csproj b/29 Craps/csharp/Craps/CrapsTester/CrapsTester.csproj new file mode 100644 index 00000000..748e7da4 --- /dev/null +++ b/29 Craps/csharp/Craps/CrapsTester/CrapsTester.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + diff --git a/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs b/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs new file mode 100644 index 00000000..9765ba61 --- /dev/null +++ b/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs @@ -0,0 +1,31 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Craps; + +namespace CrapsTester +{ + [TestClass] + public class DiceTests + { + [TestMethod] + public void SixSidedDiceReturnsValidRolls() + { + var dice = new Dice(); + for (int i = 0; i < 100000; i++) + { + var roll = dice.Roll(); + Assert.IsTrue(roll >= 1 && roll <= dice.sides); + } + } + + [TestMethod] + public void TwentySidedDiceReturnsValidRolls() + { + var dice = new Dice(20); + for (int i = 0; i < 100000; i++) + { + var roll = dice.Roll(); + Assert.IsTrue(roll >= 1 && roll <= dice.sides); + } + } + } +} From e1f2c92b191ad70d99234f5dbccb8d2d24c35fa5 Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Sun, 11 Apr 2021 20:57:43 +0100 Subject: [PATCH 2/5] Added test that dice rolls are random. --- 29 Craps/csharp/Craps/Craps/Dice.cs | 5 +- 29 Craps/csharp/Craps/Craps/Program.cs | 3 +- .../csharp/Craps/CrapsTester/CrapsTests.cs | 65 +++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/29 Craps/csharp/Craps/Craps/Dice.cs b/29 Craps/csharp/Craps/Craps/Dice.cs index 3485f0f6..43186026 100644 --- a/29 Craps/csharp/Craps/Craps/Dice.cs +++ b/29 Craps/csharp/Craps/Craps/Dice.cs @@ -1,6 +1,5 @@ using System; -using System.Collections.Generic; -using System.Text; + namespace Craps { @@ -19,6 +18,6 @@ namespace Craps this.sides = sides; } - public int Roll() => rand.Next(1, sides); + public int Roll() => rand.Next(1, sides + 1); } } diff --git a/29 Craps/csharp/Craps/Craps/Program.cs b/29 Craps/csharp/Craps/Craps/Program.cs index 17688427..d8109a16 100644 --- a/29 Craps/csharp/Craps/Craps/Program.cs +++ b/29 Craps/csharp/Craps/Craps/Program.cs @@ -1,12 +1,13 @@ using System; + + namespace Craps { class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); } } } diff --git a/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs b/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs index 9765ba61..2534f21f 100644 --- a/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs +++ b/29 Craps/csharp/Craps/CrapsTester/CrapsTests.cs @@ -1,6 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Craps; + + namespace CrapsTester { [TestClass] @@ -27,5 +29,68 @@ namespace CrapsTester Assert.IsTrue(roll >= 1 && roll <= dice.sides); } } + + [TestMethod] + public void DiceRollsAreRandom() + { + // Roll 600,000 dice and count how many rolls there are for each side. + + var dice = new Dice(); + + int numOnes = 0; + int numTwos = 0; + int numThrees = 0; + int numFours = 0; + int numFives = 0; + int numSixes = 0; + int numErrors = 0; + + for (int i = 0; i < 600000; i++) + { + switch (dice.Roll()) + { + case 1: + numOnes++; + break; + + case 2: + numTwos++; + break; + + case 3: + numThrees++; + break; + + case 4: + numFours++; + break; + + case 5: + numFives++; + break; + + case 6: + numSixes++; + break; + + default: + numErrors++; + break; + } + } + + // We'll assume that a variation of 10% in rolls for the different numbers is random enough. + // Perfectly random rolling would produce 100000 rolls per side, +/- 5% of this gives the + // range 90000..110000. + const int minRolls = 95000; + const int maxRolls = 105000; + Assert.IsTrue(numOnes >= minRolls && numOnes <= maxRolls); + Assert.IsTrue(numTwos >= minRolls && numTwos <= maxRolls); + Assert.IsTrue(numThrees >= minRolls && numThrees <= maxRolls); + Assert.IsTrue(numFours >= minRolls && numFours <= maxRolls); + Assert.IsTrue(numFives >= minRolls && numFives <= maxRolls); + Assert.IsTrue(numSixes >= minRolls && numSixes <= maxRolls); + Assert.AreEqual(numErrors, 0); + } } } From 815dc4ae722a15c21a6ccd2a980aa5a39c96190d Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Sun, 11 Apr 2021 23:54:46 +0100 Subject: [PATCH 3/5] First cut at implementing the game. Added .gitignore file. --- 29 Craps/csharp/Craps/.gitignore | 5 ++ 29 Craps/csharp/Craps/Craps/Program.cs | 70 ++++++++++++++++++++ 29 Craps/csharp/Craps/Craps/UserInterface.cs | 42 ++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 29 Craps/csharp/Craps/.gitignore create mode 100644 29 Craps/csharp/Craps/Craps/UserInterface.cs diff --git a/29 Craps/csharp/Craps/.gitignore b/29 Craps/csharp/Craps/.gitignore new file mode 100644 index 00000000..c1cb0dce --- /dev/null +++ b/29 Craps/csharp/Craps/.gitignore @@ -0,0 +1,5 @@ +.vs +TestResults +bin +obj + diff --git a/29 Craps/csharp/Craps/Craps/Program.cs b/29 Craps/csharp/Craps/Craps/Program.cs index d8109a16..916f331a 100644 --- a/29 Craps/csharp/Craps/Craps/Program.cs +++ b/29 Craps/csharp/Craps/Craps/Program.cs @@ -6,8 +6,78 @@ 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(); + + int winnings = 0; + + ui.Intro(); + + do + { + var bet = ui.PlaceBet(); + var diceRoll = dice1.Roll() + dice2.Roll(); + bool isWinner = false; + + if (Win(diceRoll)) + { + winnings += bet; + isWinner = true; + } + else if (Lose(diceRoll)) + { + winnings -= bet; + isWinner = false; + } + else + { + var point = diceRoll; + ui.Point(point); + + 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; + } + + ui.RollAgain(); + } + } + + ui.ShowResult(isWinner, 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 new file mode 100644 index 00000000..5b1bb280 --- /dev/null +++ b/29 Craps/csharp/Craps/Craps/UserInterface.cs @@ -0,0 +1,42 @@ +using System; + +namespace Craps +{ + public class UserInterface + { + public UserInterface() + { + } + + public void Intro() + { + } + + public int PlaceBet() + { + return 0; + } + + public bool PlayAgain(int bet) + { + throw new NotImplementedException(); + } + + internal void ShowResult(bool isWinner, int winnings) + { + throw new NotImplementedException(); + } + + internal void RollAgain() + { + throw new NotImplementedException(); + } + + internal void Point(int point) + { + throw new NotImplementedException(); + } + } +} + + From 25f8f3bb3f285e381a59394717f69ef6d3f15e0d Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Mon, 12 Apr 2021 23:17:37 +0100 Subject: [PATCH 4/5] Added CrapsGame class. Implemented playing the game but it doesn't work properly yet. --- 29 Craps/csharp/Craps/Craps/CrapsGame.cs | 72 ++++++++++++++ 29 Craps/csharp/Craps/Craps/Program.cs | 78 +++++---------- 29 Craps/csharp/Craps/Craps/UserInterface.cs | 98 ++++++++++++++++--- .../csharp/Craps/CrapsTester/CrapsTests.cs | 2 +- 4 files changed, 179 insertions(+), 71 deletions(-) create mode 100644 29 Craps/csharp/Craps/Craps/CrapsGame.cs 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; From 5c447bac872ced98fc94ae6780445957af1ca7fe Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Sun, 18 Apr 2021 22:28:22 +0100 Subject: [PATCH 5/5] Added missing bit of UI. Fixed final bugs. --- 29 Craps/csharp/Craps/Craps/CrapsGame.cs | 9 ++-- 29 Craps/csharp/Craps/Craps/Program.cs | 2 + 29 Craps/csharp/Craps/Craps/UserInterface.cs | 51 ++++++++++++++++---- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/29 Craps/csharp/Craps/Craps/CrapsGame.cs b/29 Craps/csharp/Craps/Craps/CrapsGame.cs index c0da368c..71a3931f 100644 --- a/29 Craps/csharp/Craps/Craps/CrapsGame.cs +++ b/29 Craps/csharp/Craps/Craps/CrapsGame.cs @@ -16,6 +16,8 @@ class CrapsGame { private readonly UserInterface ui; + private Dice dice1 = new Dice(); + private Dice dice2 = new Dice(); public CrapsGame(ref UserInterface ui) { @@ -24,9 +26,6 @@ public Result Play(out int diceRoll) { - - var dice1 = new Dice(); - var dice2 = new Dice(); diceRoll = dice1.Roll() + dice2.Roll(); if (Win(diceRoll)) @@ -47,14 +46,16 @@ var newRoll = dice1.Roll() + dice2.Roll(); if (newRoll == point) { + diceRoll = newRoll; return Result.pointWin; } else if (newRoll == 7) { + diceRoll = newRoll; return Result.pointLoss; } - ui.NoPoint(diceRoll); + ui.NoPoint(newRoll); } } } diff --git a/29 Craps/csharp/Craps/Craps/Program.cs b/29 Craps/csharp/Craps/Craps/Program.cs index 31caffd8..d0aa5961 100644 --- a/29 Craps/csharp/Craps/Craps/Program.cs +++ b/29 Craps/csharp/Craps/Craps/Program.cs @@ -44,6 +44,8 @@ namespace Craps ui.ShowResult(result, diceRoll, bet); } while (ui.PlayAgain(winnings)); + + ui.GoodBye(winnings); } } } diff --git a/29 Craps/csharp/Craps/Craps/UserInterface.cs b/29 Craps/csharp/Craps/Craps/UserInterface.cs index 72bdb17b..149a3ecc 100644 --- a/29 Craps/csharp/Craps/Craps/UserInterface.cs +++ b/29 Craps/csharp/Craps/Craps/UserInterface.cs @@ -17,31 +17,62 @@ namespace Craps // 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. + // This is not necessary in C#; the random number generator uses the current 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"); + // So that the game exactly matches the original game we ask the question but then ignore + // the answer. + Console.Write("PICK A NUMBER AND INPUT TO ROLL DICE "); GetInt(); } public int PlaceBet() { - Console.WriteLine("INPUT THE AMOUNT OF YOUR WAGER."); + Console.Write("INPUT THE AMOUNT OF YOUR WAGER. "); int n = GetInt(); Console.WriteLine("I WILL NOW THROW THE DICE"); return n; } - public bool PlayAgain(int bet) + public bool PlayAgain(int winnings) { // 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"); + Console.Write("IF YOU WANT TO PLAY AGAIN PRINT 5 IF NOT PRINT 2 "); - return GetInt() == 5; + bool playAgain = (GetInt() == 5); + + if (winnings < 0) + { + Console.WriteLine($"YOU ARE NOW UNDER ${-winnings}"); + } + else if (winnings > 0) + { + Console.WriteLine($"YOU ARE NOW OVER ${winnings}"); + } + else + { + Console.WriteLine($"YOU ARE NOW EVEN AT ${winnings}"); + } + + return playAgain; + } + + public void GoodBye(int winnings) + { + if (winnings < 0) + { + Console.WriteLine("TOO BAD, YOU ARE IN THE HOLE. COME AGAIN."); + } + else if (winnings > 0) + { + Console.WriteLine("CONGRATULATIONS---YOU CAME OUT A WINNER. COME AGAIN!"); + } + else + { + Console.WriteLine("CONGRATULATIONS---YOU CAME OUT EVEN, NOT BAD FOR AN AMATEUR"); + } } public void NoPoint(int diceRoll) @@ -102,7 +133,7 @@ namespace Craps } else { - Console.WriteLine("ENTER AN INTEGER"); + Console.Write("ENTER AN INTEGER "); } } }