From 1ddacf0d6ffc8f3f6726e659b9200ee2297f5a03 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 13:57:29 +0000 Subject: [PATCH 01/27] rename int HowManyPlayers to NumberOfPlayers --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index a93758d9..849571a8 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -40,7 +40,7 @@ public string CreateRockQuestion(int index) public bool IsPlayable() { - return (HowManyPlayers() >= 2); + return (NumberOfPlayers() >= 2); } public bool Add(string playerName) @@ -48,16 +48,16 @@ public bool Add(string playerName) players.Add(playerName); - places[HowManyPlayers()] = 0; - purses[HowManyPlayers()] = 0; - inPenaltyBox[HowManyPlayers()] = false; + places[NumberOfPlayers()] = 0; + purses[NumberOfPlayers()] = 0; + inPenaltyBox[NumberOfPlayers()] = false; Console.WriteLine(playerName + " was added"); Console.WriteLine("They are player number " + players.Count); return true; } - public int HowManyPlayers() + public int NumberOfPlayers() { return players.Count; } From c331167b778958c75eedf7aab68887a0a2788763 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 14:00:42 +0000 Subject: [PATCH 02/27] rename from aGame to round --- .../csharp/TriviaGame/TriviaGame/GameRunner.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs b/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs index e4c7b900..55e591f3 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs @@ -14,26 +14,26 @@ public class GameRunner public static void Main(String[] args) { - Game aGame = new Game(); + Game round = new Game(); - aGame.Add("Chet"); - aGame.Add("Pat"); - aGame.Add("Sue"); + round.Add("Chet"); + round.Add("Pat"); + round.Add("Sue"); Random rand = new Random(Int32.Parse(args[0])); do { - aGame.Roll(rand.Next(5) + 1); + round.Roll(rand.Next(5) + 1); if (rand.Next(9) == 7) { - notAWinner = aGame.WrongAnswer(); + notAWinner = round.WrongAnswer(); } else { - notAWinner = aGame.WasCorrectlyAnswered(); + notAWinner = round.WasCorrectlyAnswered(); } From 80f31cd1f6b42d309dd9ae587104350ab4bf06eb Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 14:19:17 +0000 Subject: [PATCH 03/27] rename index to questionNumber --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 849571a8..de23ab28 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -24,18 +24,18 @@ public class Game public Game() { - for (int i = 0; i < 50; i++) + for (int questionNumber = 0; questionNumber < 50; questionNumber++) { - popQuestions.AddLast("Pop Question " + i); - scienceQuestions.AddLast(("Science Question " + i)); - sportsQuestions.AddLast(("Sports Question " + i)); - rockQuestions.AddLast(CreateRockQuestion(i)); + popQuestions.AddLast("Pop Question " + questionNumber); + scienceQuestions.AddLast(("Science Question " + questionNumber)); + sportsQuestions.AddLast(("Sports Question " + questionNumber)); + rockQuestions.AddLast(CreateRockQuestion(questionNumber)); } } - public string CreateRockQuestion(int index) + public string CreateRockQuestion(int questionNumber) { - return "Rock Question " + index; + return "Rock Question " + questionNumber; } public bool IsPlayable() From 33d888dad2ab1f47482afdc7c090f0e4b2c3995f Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 14:22:14 +0000 Subject: [PATCH 04/27] remove duplicate code - lines related to currentPlayer to CurrentPlayerCount method - lines related to penalty box to MoveInPlace method depending on roll --- .../csharp/TriviaGame/TriviaGame/Game.cs | 50 ++++++++----------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index de23ab28..32276fdf 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -74,14 +74,7 @@ public void Roll(int roll) isGettingOutOfPenaltyBox = true; Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); - places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; - - Console.WriteLine(players[currentPlayer] - + "'s new location is " - + places[currentPlayer]); - Console.WriteLine("The category is " + CurrentCategory()); - AskQuestion(); + MoveInPlace(roll); } else { @@ -92,19 +85,23 @@ public void Roll(int roll) } else { - - places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; - - Console.WriteLine(players[currentPlayer] - + "'s new location is " - + places[currentPlayer]); - Console.WriteLine("The category is " + CurrentCategory()); - AskQuestion(); + MoveInPlace(roll); } } + private void MoveInPlace(int roll) + { + places[currentPlayer] = places[currentPlayer] + roll; + if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; + + Console.WriteLine(players[currentPlayer] + + "'s new location is " + + places[currentPlayer]); + Console.WriteLine("The category is " + CurrentCategory()); + AskQuestion(); + } + private void AskQuestion() { if (CurrentCategory() == "Pop") @@ -158,20 +155,15 @@ public bool WasCorrectlyAnswered() + " Gold Coins."); bool winner = DidPlayerWin(); - currentPlayer++; - if (currentPlayer == players.Count) currentPlayer = 0; + CurrentPlayerCount(); return winner; } else { - currentPlayer++; - if (currentPlayer == players.Count) currentPlayer = 0; + CurrentPlayerCount(); return true; } - - - } else { @@ -184,8 +176,7 @@ public bool WasCorrectlyAnswered() + " Gold Coins."); bool winner = DidPlayerWin(); - currentPlayer++; - if (currentPlayer == players.Count) currentPlayer = 0; + CurrentPlayerCount(); return winner; } @@ -196,13 +187,16 @@ public bool WrongAnswer() Console.WriteLine("Question was incorrectly answered"); Console.WriteLine(players[currentPlayer] + " was sent to the penalty box"); inPenaltyBox[currentPlayer] = true; + CurrentPlayerCount(); + return true; + } + private void CurrentPlayerCount() + { currentPlayer++; if (currentPlayer == players.Count) currentPlayer = 0; - return true; } - private bool DidPlayerWin() { return !(purses[currentPlayer] == 6); From d98576c0a7202634d9ac4c1135fb9a321ab23c21 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 14:26:36 +0000 Subject: [PATCH 05/27] rename method from movePlace to moveAndAskQuestion --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 32276fdf..1800140d 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -74,7 +74,7 @@ public void Roll(int roll) isGettingOutOfPenaltyBox = true; Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); - MoveInPlace(roll); + MoveToNewLocationAndAskQuestion(roll); } else { @@ -85,12 +85,12 @@ public void Roll(int roll) } else { - MoveInPlace(roll); + MoveToNewLocationAndAskQuestion(roll); } } - private void MoveInPlace(int roll) + private void MoveToNewLocationAndAskQuestion(int roll) { places[currentPlayer] = places[currentPlayer] + roll; if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; From d498bd1a6481e5c5736564e28f4eb713f3dccedc Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 14:32:43 +0000 Subject: [PATCH 06/27] remove duplicate in askquestion method adding askAndRemoveQuestion method --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 1800140d..73ba3e64 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -106,26 +106,27 @@ private void AskQuestion() { if (CurrentCategory() == "Pop") { - Console.WriteLine(popQuestions.First()); - popQuestions.RemoveFirst(); + AskAndRemoveQuestion(popQuestions); } if (CurrentCategory() == "Science") { - Console.WriteLine(scienceQuestions.First()); - scienceQuestions.RemoveFirst(); + AskAndRemoveQuestion(scienceQuestions); } if (CurrentCategory() == "Sports") { - Console.WriteLine(sportsQuestions.First()); - sportsQuestions.RemoveFirst(); + AskAndRemoveQuestion(sportsQuestions); } if (CurrentCategory() == "Rock") { - Console.WriteLine(rockQuestions.First()); - rockQuestions.RemoveFirst(); + AskAndRemoveQuestion(rockQuestions); } } + private void AskAndRemoveQuestion(LinkedList questions) + { + Console.WriteLine(questions.First()); + questions.RemoveFirst(); + } private string CurrentCategory() { From 9ba0833a0bdfe9e5221c3eb209148939c48bc174 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 14:49:11 +0000 Subject: [PATCH 07/27] extract new bool method for winner --- .../csharp/TriviaGame/TriviaGame/Game.cs | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 73ba3e64..1bc16804 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -149,16 +149,7 @@ public bool WasCorrectlyAnswered() if (isGettingOutOfPenaltyBox) { Console.WriteLine("Answer was correct!!!!"); - purses[currentPlayer]++; - Console.WriteLine(players[currentPlayer] - + " now has " - + purses[currentPlayer] - + " Gold Coins."); - - bool winner = DidPlayerWin(); - CurrentPlayerCount(); - - return winner; + return isWinner(); } else { @@ -170,17 +161,22 @@ public bool WasCorrectlyAnswered() { Console.WriteLine("Answer was corrent!!!!"); - purses[currentPlayer]++; - Console.WriteLine(players[currentPlayer] - + " now has " - + purses[currentPlayer] - + " Gold Coins."); + return isWinner(); + } + } - bool winner = DidPlayerWin(); - CurrentPlayerCount(); + private bool isWinner() + { + purses[currentPlayer]++; + Console.WriteLine(players[currentPlayer] + + " now has " + + purses[currentPlayer] + + " Gold Coins."); - return winner; - } + bool winner = DidPlayerWin(); + CurrentPlayerCount(); + + return winner; } public bool WrongAnswer() From dcf51f9676f6859f26c8baf3ce9ac7aecea7b00b Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 15:03:36 +0000 Subject: [PATCH 08/27] extract number 6 to consts for total coins for winning and maximum number of players --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 1bc16804..b6f80cbd 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -9,10 +9,13 @@ public class Game { List players = new List(); - int[] places = new int[6]; - int[] purses = new int[6]; + private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; + private const int WINNING_TOTAL = 6; - bool[] inPenaltyBox = new bool[6]; + int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; + int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; + + bool[] inPenaltyBox = new bool[MAXIMUM_NUMBER_OF_PLAYERS]; LinkedList popQuestions = new LinkedList(); LinkedList scienceQuestions = new LinkedList(); @@ -159,7 +162,6 @@ public bool WasCorrectlyAnswered() } else { - Console.WriteLine("Answer was corrent!!!!"); return isWinner(); } @@ -196,7 +198,7 @@ private void CurrentPlayerCount() private bool DidPlayerWin() { - return !(purses[currentPlayer] == 6); + return !(purses[currentPlayer] == WINNING_TOTAL); } } From 5eb2ac8fd16bcdc50a11faaafbab29e7f5726c26 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 15:05:27 +0000 Subject: [PATCH 09/27] extract to const the minimum number of palyers --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index b6f80cbd..413053ae 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -11,6 +11,7 @@ public class Game private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; private const int WINNING_TOTAL = 6; + private const int MINIMUM_NUMBER_OF_PLAYERS = 2; int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; @@ -43,7 +44,7 @@ public string CreateRockQuestion(int questionNumber) public bool IsPlayable() { - return (NumberOfPlayers() >= 2); + return (NumberOfPlayers() >= MINIMUM_NUMBER_OF_PLAYERS); } public bool Add(string playerName) From 28a6abcc1fcde70938fc03f59b31ccf12405e40f Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 15:06:59 +0000 Subject: [PATCH 10/27] extract total number of players to const --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 413053ae..72c0b812 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -12,7 +12,7 @@ public class Game private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; private const int WINNING_TOTAL = 6; private const int MINIMUM_NUMBER_OF_PLAYERS = 2; - + private const int TOTAL_NUMBER_OF_QUESTIONS = 50; int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; @@ -28,7 +28,7 @@ public class Game public Game() { - for (int questionNumber = 0; questionNumber < 50; questionNumber++) + for (int questionNumber = 0; questionNumber < TOTAL_NUMBER_OF_QUESTIONS; questionNumber++) { popQuestions.AddLast("Pop Question " + questionNumber); scienceQuestions.AddLast(("Science Question " + questionNumber)); From 265037c57ed74f0c8ac2dbf08b4ee9f58fffa2b1 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 15:12:01 +0000 Subject: [PATCH 11/27] extract const for total places in board and bool for roll if it is odd --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 72c0b812..51f657ff 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -13,6 +13,7 @@ public class Game private const int WINNING_TOTAL = 6; private const int MINIMUM_NUMBER_OF_PLAYERS = 2; private const int TOTAL_NUMBER_OF_QUESTIONS = 50; + private const int TOTAL_PLACES_OF_BOARD = 12; int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; @@ -73,7 +74,8 @@ public void Roll(int roll) if (inPenaltyBox[currentPlayer]) { - if (roll % 2 != 0) + bool isRollOdd = roll % 2 != 0; + if (isRollOdd) { isGettingOutOfPenaltyBox = true; @@ -97,7 +99,7 @@ public void Roll(int roll) private void MoveToNewLocationAndAskQuestion(int roll) { places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; + if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - TOTAL_PLACES_OF_BOARD; Console.WriteLine(players[currentPlayer] + "'s new location is " From 6835eee5beed2beca84758771b70a0476b497830 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Mon, 8 Jan 2024 15:14:09 +0000 Subject: [PATCH 12/27] refactor 11 to total places of board -1 --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 51f657ff..9b6875e8 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -14,6 +14,7 @@ public class Game private const int MINIMUM_NUMBER_OF_PLAYERS = 2; private const int TOTAL_NUMBER_OF_QUESTIONS = 50; private const int TOTAL_PLACES_OF_BOARD = 12; + int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; @@ -99,7 +100,7 @@ public void Roll(int roll) private void MoveToNewLocationAndAskQuestion(int roll) { places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - TOTAL_PLACES_OF_BOARD; + if (places[currentPlayer] > TOTAL_PLACES_OF_BOARD - 1) places[currentPlayer] = places[currentPlayer] - TOTAL_PLACES_OF_BOARD; Console.WriteLine(players[currentPlayer] + "'s new location is " From 5a5bf3eb5fbe0956904ddddb35e677a2508d226a Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 10:25:44 +0000 Subject: [PATCH 13/27] add modifier to consts (private and readonly) --- .../csharp/TriviaGame/TriviaGame/Game.cs | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 9b6875e8..c9d539f1 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -15,18 +15,16 @@ public class Game private const int TOTAL_NUMBER_OF_QUESTIONS = 50; private const int TOTAL_PLACES_OF_BOARD = 12; - int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; - int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; - - bool[] inPenaltyBox = new bool[MAXIMUM_NUMBER_OF_PLAYERS]; + readonly int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; + readonly int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; + readonly bool[] inPenaltyBox = new bool[MAXIMUM_NUMBER_OF_PLAYERS]; + private int currentPlayer; + private bool isGettingOutOfPenaltyBox; - LinkedList popQuestions = new LinkedList(); - LinkedList scienceQuestions = new LinkedList(); - LinkedList sportsQuestions = new LinkedList(); - LinkedList rockQuestions = new LinkedList(); - - int currentPlayer = 0; - bool isGettingOutOfPenaltyBox; + readonly LinkedList popQuestions = new LinkedList(); + readonly LinkedList scienceQuestions = new LinkedList(); + readonly LinkedList sportsQuestions = new LinkedList(); + readonly LinkedList rockQuestions = new LinkedList(); public Game() { @@ -51,8 +49,6 @@ public bool IsPlayable() public bool Add(string playerName) { - - players.Add(playerName); places[NumberOfPlayers()] = 0; purses[NumberOfPlayers()] = 0; From b9872af3c0768e6206d48545eadad311117edaa8 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 10:26:48 +0000 Subject: [PATCH 14/27] add readonly to the list players --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index c9d539f1..85c9eb8a 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -7,7 +7,7 @@ namespace TriviaGame { public class Game { - List players = new List(); + readonly List players = new List(); private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; private const int WINNING_TOTAL = 6; From a034133a5a54f45db6bcd9839d66c2799c2e68e5 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 10:31:16 +0000 Subject: [PATCH 15/27] rename methods related to answers --- .../csharp/TriviaGame/TriviaGame/Game.cs | 26 +++++++++---------- .../TriviaGame/TriviaGame/GameRunner.cs | 4 +-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 85c9eb8a..0cb9413f 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -145,14 +145,14 @@ private string CurrentCategory() return "Rock"; } - public bool WasCorrectlyAnswered() + public bool IsAnswerCorrect() { if (inPenaltyBox[currentPlayer]) { if (isGettingOutOfPenaltyBox) { Console.WriteLine("Answer was correct!!!!"); - return isWinner(); + return IsWinner(); } else { @@ -163,11 +163,20 @@ public bool WasCorrectlyAnswered() else { Console.WriteLine("Answer was corrent!!!!"); - return isWinner(); + return IsWinner(); } } - private bool isWinner() + public bool IsAnswerWrong() + { + Console.WriteLine("Question was incorrectly answered"); + Console.WriteLine(players[currentPlayer] + " was sent to the penalty box"); + inPenaltyBox[currentPlayer] = true; + CurrentPlayerCount(); + return true; + } + + private bool IsWinner() { purses[currentPlayer]++; Console.WriteLine(players[currentPlayer] @@ -181,15 +190,6 @@ private bool isWinner() return winner; } - public bool WrongAnswer() - { - Console.WriteLine("Question was incorrectly answered"); - Console.WriteLine(players[currentPlayer] + " was sent to the penalty box"); - inPenaltyBox[currentPlayer] = true; - CurrentPlayerCount(); - return true; - } - private void CurrentPlayerCount() { currentPlayer++; diff --git a/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs b/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs index 55e591f3..907a6704 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs @@ -29,11 +29,11 @@ public static void Main(String[] args) if (rand.Next(9) == 7) { - notAWinner = round.WrongAnswer(); + notAWinner = round.IsAnswerWrong(); } else { - notAWinner = round.WasCorrectlyAnswered(); + notAWinner = round.IsAnswerCorrect(); } From 496a205c2ac37ff04c2f74ee5313b24821c9d570 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 10:32:59 +0000 Subject: [PATCH 16/27] move all private methods to the bottom --- .../csharp/TriviaGame/TriviaGame/Game.cs | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 0cb9413f..4c70d3f6 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -93,6 +93,37 @@ public void Roll(int roll) } + public bool IsAnswerCorrect() + { + if (inPenaltyBox[currentPlayer]) + { + if (isGettingOutOfPenaltyBox) + { + Console.WriteLine("Answer was correct!!!!"); + return IsWinner(); + } + else + { + CurrentPlayerCount(); + return true; + } + } + else + { + Console.WriteLine("Answer was corrent!!!!"); + return IsWinner(); + } + } + + public bool IsAnswerWrong() + { + Console.WriteLine("Question was incorrectly answered"); + Console.WriteLine(players[currentPlayer] + " was sent to the penalty box"); + inPenaltyBox[currentPlayer] = true; + CurrentPlayerCount(); + return true; + } + private void MoveToNewLocationAndAskQuestion(int roll) { places[currentPlayer] = places[currentPlayer] + roll; @@ -145,37 +176,6 @@ private string CurrentCategory() return "Rock"; } - public bool IsAnswerCorrect() - { - if (inPenaltyBox[currentPlayer]) - { - if (isGettingOutOfPenaltyBox) - { - Console.WriteLine("Answer was correct!!!!"); - return IsWinner(); - } - else - { - CurrentPlayerCount(); - return true; - } - } - else - { - Console.WriteLine("Answer was corrent!!!!"); - return IsWinner(); - } - } - - public bool IsAnswerWrong() - { - Console.WriteLine("Question was incorrectly answered"); - Console.WriteLine(players[currentPlayer] + " was sent to the penalty box"); - inPenaltyBox[currentPlayer] = true; - CurrentPlayerCount(); - return true; - } - private bool IsWinner() { purses[currentPlayer]++; From 2653c383b138ca5a5243b276e3dc6422fcf98076 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 10:53:10 +0000 Subject: [PATCH 17/27] extract the method ask question from movelocation because of single responsibility principal --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 4c70d3f6..8143e16e 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -77,7 +77,8 @@ public void Roll(int roll) isGettingOutOfPenaltyBox = true; Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); - MoveToNewLocationAndAskQuestion(roll); + MoveToNewLocation(roll); + AskQuestion(); } else { @@ -88,7 +89,8 @@ public void Roll(int roll) } else { - MoveToNewLocationAndAskQuestion(roll); + MoveToNewLocation(roll); + AskQuestion(); } } @@ -124,7 +126,7 @@ public bool IsAnswerWrong() return true; } - private void MoveToNewLocationAndAskQuestion(int roll) + private void MoveToNewLocation(int roll) { places[currentPlayer] = places[currentPlayer] + roll; if (places[currentPlayer] > TOTAL_PLACES_OF_BOARD - 1) places[currentPlayer] = places[currentPlayer] - TOTAL_PLACES_OF_BOARD; @@ -133,7 +135,6 @@ private void MoveToNewLocationAndAskQuestion(int roll) + "'s new location is " + places[currentPlayer]); Console.WriteLine("The category is " + CurrentCategory()); - AskQuestion(); } private void AskQuestion() From 0b93b2a5fa62f474edd0058acfed05b817660606 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 11:23:15 +0000 Subject: [PATCH 18/27] extract contitional logic from roll method --- .../csharp/TriviaGame/TriviaGame/Game.cs | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 8143e16e..f431233c 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -69,25 +69,16 @@ public void Roll(int roll) Console.WriteLine(players[currentPlayer] + " is the current player"); Console.WriteLine("They have rolled a " + roll); - if (inPenaltyBox[currentPlayer]) - { - bool isRollOdd = roll % 2 != 0; - if (isRollOdd) - { - isGettingOutOfPenaltyBox = true; + bool isRollOdd = roll % 2 != 0; + bool IsInPenaltyBox = inPenaltyBox[currentPlayer]; - Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); - MoveToNewLocation(roll); - AskQuestion(); - } - else - { - Console.WriteLine(players[currentPlayer] + " is not getting out of the penalty box"); - isGettingOutOfPenaltyBox = false; - } + if (IsInPenaltyBox) + { + DecideIfPlayerGettingOutOfPenantyBox(isRollOdd); } - else + bool isInBoxButGettingOut = IsInPenaltyBox && isGettingOutOfPenaltyBox; + if (!IsInPenaltyBox || isInBoxButGettingOut) { MoveToNewLocation(roll); AskQuestion(); @@ -95,6 +86,21 @@ public void Roll(int roll) } + private void DecideIfPlayerGettingOutOfPenantyBox(bool isRollOdd) + { + if (isRollOdd) + { + isGettingOutOfPenaltyBox = true; + + Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); + } + else + { + Console.WriteLine(players[currentPlayer] + " is not getting out of the penalty box"); + isGettingOutOfPenaltyBox = false; + } + } + public bool IsAnswerCorrect() { if (inPenaltyBox[currentPlayer]) From 3bf60428056ab2638e8f37ba3d6701cb5351768e Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 11:30:39 +0000 Subject: [PATCH 19/27] extract nested contitional logic from the method IsAnswerCorrect --- .../csharp/TriviaGame/TriviaGame/Game.cs | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index f431233c..2bfbf149 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -86,40 +86,31 @@ public void Roll(int roll) } - private void DecideIfPlayerGettingOutOfPenantyBox(bool isRollOdd) + + public bool IsAnswerCorrect() { - if (isRollOdd) + if (inPenaltyBox[currentPlayer]) { - isGettingOutOfPenaltyBox = true; - - Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); + return DecideIfPlayerWon(); } else { - Console.WriteLine(players[currentPlayer] + " is not getting out of the penalty box"); - isGettingOutOfPenaltyBox = false; + Console.WriteLine("Answer was corrent!!!!"); + return IsWinner(); } } - public bool IsAnswerCorrect() + private bool DecideIfPlayerWon() { - if (inPenaltyBox[currentPlayer]) + if (isGettingOutOfPenaltyBox) { - if (isGettingOutOfPenaltyBox) - { - Console.WriteLine("Answer was correct!!!!"); - return IsWinner(); - } - else - { - CurrentPlayerCount(); - return true; - } + Console.WriteLine("Answer was correct!!!!"); + return IsWinner(); } else { - Console.WriteLine("Answer was corrent!!!!"); - return IsWinner(); + CurrentPlayerCount(); + return true; } } @@ -132,6 +123,21 @@ public bool IsAnswerWrong() return true; } + private void DecideIfPlayerGettingOutOfPenantyBox(bool isRollOdd) + { + if (isRollOdd) + { + isGettingOutOfPenaltyBox = true; + + Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); + } + else + { + Console.WriteLine(players[currentPlayer] + " is not getting out of the penalty box"); + isGettingOutOfPenaltyBox = false; + } + } + private void MoveToNewLocation(int roll) { places[currentPlayer] = places[currentPlayer] + roll; From 4eb05f48f766b5b7511e3f0016a8af736e72324a Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 11:44:16 +0000 Subject: [PATCH 20/27] move private method lower to the private methods --- .../csharp/TriviaGame/TriviaGame/Game.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 2bfbf149..c333441e 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -100,20 +100,7 @@ public bool IsAnswerCorrect() } } - private bool DecideIfPlayerWon() - { - if (isGettingOutOfPenaltyBox) - { - Console.WriteLine("Answer was correct!!!!"); - return IsWinner(); - } - else - { - CurrentPlayerCount(); - return true; - } - } - + public bool IsAnswerWrong() { Console.WriteLine("Question was incorrectly answered"); @@ -148,6 +135,19 @@ private void MoveToNewLocation(int roll) + places[currentPlayer]); Console.WriteLine("The category is " + CurrentCategory()); } + private bool DecideIfPlayerWon() + { + if (isGettingOutOfPenaltyBox) + { + Console.WriteLine("Answer was correct!!!!"); + return IsWinner(); + } + else + { + CurrentPlayerCount(); + return true; + } + } private void AskQuestion() { From 8b466064d5c3682609fb432fd8245922a8b7e691 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 11:44:33 +0000 Subject: [PATCH 21/27] add categories class --- .../csharp/TriviaGame/TriviaGame/Category.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 exercises/csharp/TriviaGame/TriviaGame/Category.cs diff --git a/exercises/csharp/TriviaGame/TriviaGame/Category.cs b/exercises/csharp/TriviaGame/TriviaGame/Category.cs new file mode 100644 index 00000000..a431d8b2 --- /dev/null +++ b/exercises/csharp/TriviaGame/TriviaGame/Category.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TriviaGame +{ + public class Category + { + public string Name { get; set; } + + public Category(string name) + { + Name = name; + } + } +} From 633e37cdec5bbea30eca118817e67c39bc6e9f39 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 12:16:10 +0000 Subject: [PATCH 22/27] add deck class to the solution and extract properties --- .../csharp/TriviaGame/TriviaGame/Deck.cs | 23 +++++++++++++++ .../csharp/TriviaGame/TriviaGame/Game.cs | 29 ++++++++----------- 2 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 exercises/csharp/TriviaGame/TriviaGame/Deck.cs diff --git a/exercises/csharp/TriviaGame/TriviaGame/Deck.cs b/exercises/csharp/TriviaGame/TriviaGame/Deck.cs new file mode 100644 index 00000000..38846eed --- /dev/null +++ b/exercises/csharp/TriviaGame/TriviaGame/Deck.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TriviaGame +{ + public class Deck + { + public Category category { get; set; } + public LinkedList Questions { get; set; } = new LinkedList(); + + public Deck(string categoryName) + { + category = new Category(categoryName); + } + + public void AddQuestion(int questionNumber) + { + Questions.AddLast($"{category.Name} Question " + questionNumber); + + } + } +} diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index c333441e..b31f72d0 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -21,27 +21,22 @@ public class Game private int currentPlayer; private bool isGettingOutOfPenaltyBox; - readonly LinkedList popQuestions = new LinkedList(); - readonly LinkedList scienceQuestions = new LinkedList(); - readonly LinkedList sportsQuestions = new LinkedList(); - readonly LinkedList rockQuestions = new LinkedList(); + readonly Deck popQuestions = new Deck("Pop"); + readonly Deck scienceQuestions = new Deck("Science"); + readonly Deck sportsQuestions = new Deck("Sports"); + readonly Deck rockQuestions = new Deck("Rock"); public Game() { for (int questionNumber = 0; questionNumber < TOTAL_NUMBER_OF_QUESTIONS; questionNumber++) { - popQuestions.AddLast("Pop Question " + questionNumber); - scienceQuestions.AddLast(("Science Question " + questionNumber)); - sportsQuestions.AddLast(("Sports Question " + questionNumber)); - rockQuestions.AddLast(CreateRockQuestion(questionNumber)); + popQuestions.AddQuestion(questionNumber); + scienceQuestions.AddQuestion(questionNumber); + sportsQuestions.AddQuestion(questionNumber); + rockQuestions.AddQuestion(questionNumber); } } - public string CreateRockQuestion(int questionNumber) - { - return "Rock Question " + questionNumber; - } - public bool IsPlayable() { return (NumberOfPlayers() >= MINIMUM_NUMBER_OF_PLAYERS); @@ -153,19 +148,19 @@ private void AskQuestion() { if (CurrentCategory() == "Pop") { - AskAndRemoveQuestion(popQuestions); + AskAndRemoveQuestion(popQuestions.Questions); } if (CurrentCategory() == "Science") { - AskAndRemoveQuestion(scienceQuestions); + AskAndRemoveQuestion(scienceQuestions.Questions); } if (CurrentCategory() == "Sports") { - AskAndRemoveQuestion(sportsQuestions); + AskAndRemoveQuestion(sportsQuestions.Questions); } if (CurrentCategory() == "Rock") { - AskAndRemoveQuestion(rockQuestions); + AskAndRemoveQuestion(rockQuestions.Questions); } } From 154ae43ca5fcef53a842c80fd4428cbfe3d0844b Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 14:11:18 +0000 Subject: [PATCH 23/27] add new player class and use it in the game class --- .../csharp/TriviaGame/TriviaGame/Game.cs | 18 +++++++------ .../TriviaGame/TriviaGame/GameRunner.cs | 2 -- .../csharp/TriviaGame/TriviaGame/Player.cs | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 exercises/csharp/TriviaGame/TriviaGame/Player.cs diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index b31f72d0..ea258398 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -7,7 +7,7 @@ namespace TriviaGame { public class Game { - readonly List players = new List(); + readonly List players = new List(); private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; private const int WINNING_TOTAL = 6; @@ -26,6 +26,8 @@ public class Game readonly Deck sportsQuestions = new Deck("Sports"); readonly Deck rockQuestions = new Deck("Rock"); + public Player Player { get; set; } + public Game() { for (int questionNumber = 0; questionNumber < TOTAL_NUMBER_OF_QUESTIONS; questionNumber++) @@ -44,7 +46,7 @@ public bool IsPlayable() public bool Add(string playerName) { - players.Add(playerName); + players.Add(new Player(playerName)); places[NumberOfPlayers()] = 0; purses[NumberOfPlayers()] = 0; inPenaltyBox[NumberOfPlayers()] = false; @@ -61,7 +63,7 @@ public int NumberOfPlayers() public void Roll(int roll) { - Console.WriteLine(players[currentPlayer] + " is the current player"); + Console.WriteLine(players[currentPlayer].Name + " is the current player"); Console.WriteLine("They have rolled a " + roll); bool isRollOdd = roll % 2 != 0; @@ -99,7 +101,7 @@ public bool IsAnswerCorrect() public bool IsAnswerWrong() { Console.WriteLine("Question was incorrectly answered"); - Console.WriteLine(players[currentPlayer] + " was sent to the penalty box"); + Console.WriteLine(players[currentPlayer].Name + " was sent to the penalty box"); inPenaltyBox[currentPlayer] = true; CurrentPlayerCount(); return true; @@ -111,11 +113,11 @@ private void DecideIfPlayerGettingOutOfPenantyBox(bool isRollOdd) { isGettingOutOfPenaltyBox = true; - Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box"); + Console.WriteLine(players[currentPlayer].Name + " is getting out of the penalty box"); } else { - Console.WriteLine(players[currentPlayer] + " is not getting out of the penalty box"); + Console.WriteLine(players[currentPlayer].Name + " is not getting out of the penalty box"); isGettingOutOfPenaltyBox = false; } } @@ -125,7 +127,7 @@ private void MoveToNewLocation(int roll) places[currentPlayer] = places[currentPlayer] + roll; if (places[currentPlayer] > TOTAL_PLACES_OF_BOARD - 1) places[currentPlayer] = places[currentPlayer] - TOTAL_PLACES_OF_BOARD; - Console.WriteLine(players[currentPlayer] + Console.WriteLine(players[currentPlayer].Name + "'s new location is " + places[currentPlayer]); Console.WriteLine("The category is " + CurrentCategory()); @@ -187,7 +189,7 @@ private string CurrentCategory() private bool IsWinner() { purses[currentPlayer]++; - Console.WriteLine(players[currentPlayer] + Console.WriteLine(players[currentPlayer].Name + " now has " + purses[currentPlayer] + " Gold Coins."); diff --git a/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs b/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs index 907a6704..291306d5 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/GameRunner.cs @@ -36,8 +36,6 @@ public static void Main(String[] args) notAWinner = round.IsAnswerCorrect(); } - - } while (notAWinner); } diff --git a/exercises/csharp/TriviaGame/TriviaGame/Player.cs b/exercises/csharp/TriviaGame/TriviaGame/Player.cs new file mode 100644 index 00000000..a40bcb38 --- /dev/null +++ b/exercises/csharp/TriviaGame/TriviaGame/Player.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TriviaGame +{ + public class Player + { + public string Name { get; set; } + public int Place { get; set; } + public int Purse { get; set; } + public bool InPenaltyBox { get; set; } + + public Player(string name) + { + Name = name; + Place = 0; + Purse = 0; + InPenaltyBox = false; + } + + /*public bool IsWinner() + { + return !(Purse == 6); + }*/ + } +} From 606c33511bd0e7e3e8bc0b022bd0217dfe5c9725 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 15:00:17 +0000 Subject: [PATCH 24/27] remove places, purses and inPenaltyBox variables from add method --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index ea258398..7af1d3ee 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -9,6 +9,7 @@ public class Game { readonly List players = new List(); + private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; private const int WINNING_TOTAL = 6; private const int MINIMUM_NUMBER_OF_PLAYERS = 2; @@ -26,8 +27,6 @@ public class Game readonly Deck sportsQuestions = new Deck("Sports"); readonly Deck rockQuestions = new Deck("Rock"); - public Player Player { get; set; } - public Game() { for (int questionNumber = 0; questionNumber < TOTAL_NUMBER_OF_QUESTIONS; questionNumber++) @@ -47,9 +46,6 @@ public bool IsPlayable() public bool Add(string playerName) { players.Add(new Player(playerName)); - places[NumberOfPlayers()] = 0; - purses[NumberOfPlayers()] = 0; - inPenaltyBox[NumberOfPlayers()] = false; Console.WriteLine(playerName + " was added"); Console.WriteLine("They are player number " + players.Count); @@ -74,6 +70,7 @@ public void Roll(int roll) DecideIfPlayerGettingOutOfPenantyBox(isRollOdd); } + bool isInBoxButGettingOut = IsInPenaltyBox && isGettingOutOfPenaltyBox; if (!IsInPenaltyBox || isInBoxButGettingOut) { From b68d822abdeeb2794e6151a2b6e522c3e004ce0d Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 15:09:24 +0000 Subject: [PATCH 25/27] update inPenaltyBox using the new setter from player class --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 7af1d3ee..3f2e3a76 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -63,7 +63,7 @@ public void Roll(int roll) Console.WriteLine("They have rolled a " + roll); bool isRollOdd = roll % 2 != 0; - bool IsInPenaltyBox = inPenaltyBox[currentPlayer]; + bool IsInPenaltyBox = players[currentPlayer].InPenaltyBox; if (IsInPenaltyBox) { @@ -83,7 +83,7 @@ public void Roll(int roll) public bool IsAnswerCorrect() { - if (inPenaltyBox[currentPlayer]) + if (players[currentPlayer].InPenaltyBox) { return DecideIfPlayerWon(); } @@ -99,7 +99,7 @@ public bool IsAnswerWrong() { Console.WriteLine("Question was incorrectly answered"); Console.WriteLine(players[currentPlayer].Name + " was sent to the penalty box"); - inPenaltyBox[currentPlayer] = true; + players[currentPlayer].InPenaltyBox = true; CurrentPlayerCount(); return true; } From b0328f95987ed65bfcd2226b33da3ebf277c05c2 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 15:12:53 +0000 Subject: [PATCH 26/27] update places using the pnew setter from Player class --- .../csharp/TriviaGame/TriviaGame/Game.cs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index 3f2e3a76..f9efe1cb 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -16,9 +16,7 @@ public class Game private const int TOTAL_NUMBER_OF_QUESTIONS = 50; private const int TOTAL_PLACES_OF_BOARD = 12; - readonly int[] places = new int[MAXIMUM_NUMBER_OF_PLAYERS]; readonly int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; - readonly bool[] inPenaltyBox = new bool[MAXIMUM_NUMBER_OF_PLAYERS]; private int currentPlayer; private bool isGettingOutOfPenaltyBox; @@ -121,12 +119,12 @@ private void DecideIfPlayerGettingOutOfPenantyBox(bool isRollOdd) private void MoveToNewLocation(int roll) { - places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > TOTAL_PLACES_OF_BOARD - 1) places[currentPlayer] = places[currentPlayer] - TOTAL_PLACES_OF_BOARD; + players[currentPlayer].Place = players[currentPlayer].Place + roll; + if (players[currentPlayer].Place > TOTAL_PLACES_OF_BOARD - 1) players[currentPlayer].Place = players[currentPlayer].Place - TOTAL_PLACES_OF_BOARD; Console.WriteLine(players[currentPlayer].Name + "'s new location is " - + places[currentPlayer]); + + players[currentPlayer].Place); Console.WriteLine("The category is " + CurrentCategory()); } private bool DecideIfPlayerWon() @@ -171,15 +169,15 @@ private void AskAndRemoveQuestion(LinkedList questions) private string CurrentCategory() { - if (places[currentPlayer] == 0) return "Pop"; - if (places[currentPlayer] == 4) return "Pop"; - if (places[currentPlayer] == 8) return "Pop"; - if (places[currentPlayer] == 1) return "Science"; - if (places[currentPlayer] == 5) return "Science"; - if (places[currentPlayer] == 9) return "Science"; - if (places[currentPlayer] == 2) return "Sports"; - if (places[currentPlayer] == 6) return "Sports"; - if (places[currentPlayer] == 10) return "Sports"; + if (players[currentPlayer].Place == 0) return "Pop"; + if (players[currentPlayer].Place == 4) return "Pop"; + if (players[currentPlayer].Place == 8) return "Pop"; + if (players[currentPlayer].Place == 1) return "Science"; + if (players[currentPlayer].Place == 5) return "Science"; + if (players[currentPlayer].Place == 9) return "Science"; + if (players[currentPlayer].Place == 2) return "Sports"; + if (players[currentPlayer].Place == 6) return "Sports"; + if (players[currentPlayer].Place == 10) return "Sports"; return "Rock"; } From c07a57bb706ae20f2727ef7e4cea36da70ef1f40 Mon Sep 17 00:00:00 2001 From: Sofia Bezertzi Date: Tue, 9 Jan 2024 15:25:24 +0000 Subject: [PATCH 27/27] update purse using the purse setter from the player class --- exercises/csharp/TriviaGame/TriviaGame/Game.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/exercises/csharp/TriviaGame/TriviaGame/Game.cs b/exercises/csharp/TriviaGame/TriviaGame/Game.cs index f9efe1cb..b16bae1f 100644 --- a/exercises/csharp/TriviaGame/TriviaGame/Game.cs +++ b/exercises/csharp/TriviaGame/TriviaGame/Game.cs @@ -9,14 +9,10 @@ public class Game { readonly List players = new List(); - - private const int MAXIMUM_NUMBER_OF_PLAYERS = 6; private const int WINNING_TOTAL = 6; private const int MINIMUM_NUMBER_OF_PLAYERS = 2; private const int TOTAL_NUMBER_OF_QUESTIONS = 50; private const int TOTAL_PLACES_OF_BOARD = 12; - - readonly int[] purses = new int[MAXIMUM_NUMBER_OF_PLAYERS]; private int currentPlayer; private bool isGettingOutOfPenaltyBox; @@ -183,10 +179,10 @@ private string CurrentCategory() private bool IsWinner() { - purses[currentPlayer]++; + players[currentPlayer].Purse++; Console.WriteLine(players[currentPlayer].Name + " now has " - + purses[currentPlayer] + + players[currentPlayer].Purse + " Gold Coins."); bool winner = DidPlayerWin(); @@ -203,7 +199,7 @@ private void CurrentPlayerCount() private bool DidPlayerWin() { - return !(purses[currentPlayer] == WINNING_TOTAL); + return !(players[currentPlayer].Purse == WINNING_TOTAL); } }