Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions PrincessBrideTrivia/PrincessBrideTrivia.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public void GetPercentCorrect_ReturnsExpectedPercentage(int numberOfCorrectGuess
}




Comment on lines +73 to +74

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

private static void GenerateQuestionsFile(string filePath, int numberOfQuestions)
{
for (int i = 0; i < numberOfQuestions; i++)
Expand All @@ -85,4 +87,25 @@ private static void GenerateQuestionsFile(string filePath, int numberOfQuestions
File.AppendAllLines(filePath, lines);
}
}

// Unit test for hardmode

Choose a reason for hiding this comment

The 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 DO NOT use comments unless they describe something that is not obvious to someone other than the developer who wrote the code. There are a few other places in the PR that show similar comments - I'll briefly note one other.

[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);
}
}
74 changes: 66 additions & 8 deletions PrincessBrideTrivia/PrincessBrideTrivia/Program.cs
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++;
Expand All @@ -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) + "%";
}

public static bool AskQuestion(Question question)
//UPDATED THIS FUNCTION WITH PARAMETER isHard

Choose a reason for hiding this comment

The 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);
}

Expand All @@ -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)
Expand All @@ -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)
{
return Regex.Replace(input, @"[^\w\s]", "");
}

public static bool DisplayHardResult(string userGuess, Question question)
{
string normalizedGuess = RemovePunctuation(userGuess).Trim();
if (!int.TryParse(question.CorrectAnswerIndex, out int index) || index < 1 || index > question.Answers.Length)
{
Console.WriteLine("Incorrect");

Choose a reason for hiding this comment

The 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]);
}
}
}

Expand Down Expand Up @@ -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;
}
Expand Down