-
Notifications
You must be signed in to change notification settings - Fork 22
Assignment 1 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Assignment1
Are you sure you want to change the base?
Assignment 1 #16
Changes from 9 commits
843918c
847c6b2
8177200
9fb5971
4f40a96
c893bda
e2f930c
cd00498
5ddb020
41f0ef3
77b0965
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,8 @@ public void GetPercentCorrect_ReturnsExpectedPercentage(int numberOfCorrectGuess | |
} | ||
|
||
|
||
|
||
|
||
private static void GenerateQuestionsFile(string filePath, int numberOfQuestions) | ||
{ | ||
for (int i = 0; i < numberOfQuestions; i++) | ||
|
@@ -85,4 +87,25 @@ private static void GenerateQuestionsFile(string filePath, int numberOfQuestions | |
File.AppendAllLines(filePath, lines); | ||
} | ||
} | ||
|
||
// Unit test for hardmode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments describing things that can be determined by looking at the code clutter the codebase. I'll reference the guidelines in a sec, but I'd lean towards writing self-documenting code. Under the books guidelines, it says |
||
[TestMethod] | ||
[DataRow("Answer 2", "2", true)] | ||
[DataRow("answer 2", "2", true)] | ||
[DataRow("Answer 1", "2", false)] | ||
public void DisplayHardResult_ReturnsExpectedResult(string userGuess, string correctAnswerIndex, bool expected) | ||
{ | ||
// Arrange | ||
var question = new Question | ||
{ | ||
Answers = new[] { "Answer 1", "Answer 2", "Answer 3" }, | ||
CorrectAnswerIndex = correctAnswerIndex | ||
}; | ||
|
||
// Act | ||
bool result = Program.DisplayHardResult(userGuess, question); | ||
|
||
// Assert | ||
Assert.AreEqual(expected, result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
namespace PrincessBrideTrivia; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace PrincessBrideTrivia; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
string choice; | ||
do | ||
{ | ||
Console.Write("Princess Bride Trivia!!! Do you want to try hard mode?(y/n): "); | ||
choice = Console.ReadLine()?.Trim().ToLower(); | ||
if (choice != "y" && choice != "n") | ||
{ | ||
Console.WriteLine("Please enter 'y' for yes or 'n' for no."); | ||
} | ||
} while (choice != "y" && choice != "n"); | ||
|
||
bool isHardMode = choice == "y"; | ||
if (isHardMode) | ||
{ | ||
Console.WriteLine("HARD MODE ACTIVATED!!!!!!!!!!!!"); | ||
} | ||
|
||
string filePath = GetFilePath(); | ||
Question[] questions = LoadQuestions(filePath); | ||
|
||
int numberCorrect = 0; | ||
for (int i = 0; i < questions.Length; i++) | ||
{ | ||
bool result = AskQuestion(questions[i]); | ||
|
||
bool result = AskQuestion(questions[i], isHardMode); | ||
|
||
if (result) | ||
{ | ||
numberCorrect++; | ||
|
@@ -21,14 +42,19 @@ public static void Main(string[] args) | |
|
||
public static string GetPercentCorrect(int numberCorrectAnswers, int numberOfQuestions) | ||
{ | ||
return (numberCorrectAnswers / numberOfQuestions * 100) + "%"; | ||
return (100 * numberCorrectAnswers / numberOfQuestions) + "%"; | ||
c-stanton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static bool AskQuestion(Question question) | ||
//UPDATED THIS FUNCTION WITH PARAMETER isHard | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one isn't necessary either for example |
||
public static bool AskQuestion(Question question, bool isHard) | ||
{ | ||
DisplayQuestion(question); | ||
DisplayQuestion(question, isHard); | ||
|
||
string userGuess = GetGuessFromUser(); | ||
if (isHard) | ||
{ | ||
return DisplayHardResult(userGuess, question); | ||
} | ||
return DisplayResult(userGuess, question); | ||
} | ||
|
||
|
@@ -37,6 +63,7 @@ public static string GetGuessFromUser() | |
return Console.ReadLine(); | ||
} | ||
|
||
//WE CANT CHANGE THIS METHOD BECAUSE IT IS RUNNING TESTS | ||
public static bool DisplayResult(string userGuess, Question question) | ||
{ | ||
if (userGuess == question.CorrectAnswerIndex) | ||
|
@@ -49,12 +76,42 @@ public static bool DisplayResult(string userGuess, Question question) | |
return false; | ||
} | ||
|
||
public static void DisplayQuestion(Question question) | ||
// Helper method that strips punctuation from user input | ||
public static string RemovePunctuation(string input) | ||
{ | ||
BillMillerCoding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Regex.Replace(input, @"[^\w\s]", ""); | ||
} | ||
|
||
public static bool DisplayHardResult(string userGuess, Question question) | ||
{ | ||
BillMillerCoding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string normalizedGuess = RemovePunctuation(userGuess).Trim(); | ||
if (!int.TryParse(question.CorrectAnswerIndex, out int index) || index < 1 || index > question.Answers.Length) | ||
{ | ||
Console.WriteLine("Incorrect"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nitpick, but here you simply write "Incorrect" when the user enters something that CorrectAnswerIndex can't parse or is out of range. This reads like they just got the answer wrong, when it's actually a parsing issue. It might be clearer to print a more descriptive message, "Error - user answered out of range" and prompt for new input, or throwing an exception on it. |
||
return false; | ||
} | ||
string normalizedAnswer = RemovePunctuation(question.Answers[index - 1]).Trim(); | ||
|
||
if (String.Equals(normalizedGuess, normalizedAnswer, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Console.WriteLine("Correct"); | ||
return true; | ||
} | ||
|
||
Console.WriteLine("Incorrect"); | ||
return false; | ||
} | ||
|
||
//UPDATED THIS FUNCTION WITH PARAMETER isHard | ||
public static void DisplayQuestion(Question question, bool isHard) | ||
{ | ||
Console.WriteLine("Question: " + question.Text); | ||
for (int i = 0; i < question.Answers.Length; i++) | ||
if (!isHard) | ||
{ | ||
Console.WriteLine((i + 1) + ": " + question.Answers[i]); | ||
for (int i = 0; i < question.Answers.Length; i++) | ||
{ | ||
Console.WriteLine((i + 1) + ": " + question.Answers[i]); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -86,6 +143,7 @@ public static Question[] LoadQuestions(string filePath) | |
question.Answers[1] = answer2; | ||
question.Answers[2] = answer3; | ||
question.CorrectAnswerIndex = correctAnswerIndex; | ||
questions[i] = question; | ||
} | ||
return questions; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the extra whitespace adds anything here