Contents

Kata 7

Setup instructions

  • Download the starting folder for this Kata.
  • Open a Command Prompt window and navigate to the starting folder using cd.
  • Open the starting folder for this Kata in VSCode.

Overview

In this Kata we’ll use loops and arrays to make our quiz game able to cope with more than just two questions.

Kata instructions

Program.cs currently has two methods to ask questions, AskQuestionOne, and AskQuestionTwo. These methods are very similar, and have a lot of repeated code. Because of this, we’re going to merge them into one. We’re going to use loops and arrays so that the program can support many questions, only using a single method to ask them to the user.

Questions and answers

First, in DoQuizQuestions, create an array to store the questions and answers:

string[] questions = new string[2];
string[] answers = new string[2];

Assign the question and answer variables:

questions[0] = "In what year did the Titanic sink?";
answers[0] = "1912";
questions[1] = "In what year did the Titanic II sink?";
answers[1] = "1913";

Ask me a question

Now let’s write a method that can ask the user a question, and tell us if their answer was correct. (Note - this is basically the answer to the ‘Try on your own…’ section of Kata 5…)

Create a new method, AskQuestion. Write:

static bool AskQuestion(string question, string correctAnswer) {
    Console.WriteLine(question);
    string answer = Console.ReadLine();

    if (answer == correctAnswer) {
        Console.WriteLine("Correct!");
        return true;
    }
    else {
        Console.WriteLine("Incorrect!");
        return false;
    }  
}

Back in DoQuizQuestions, we are going to use a for loop to ask the questions. The for loop is going to iterate through the questions and answers arrays, passing the values to AskQuestion.

In DoQuizQuestions, replace the variable declarations of questionOneCorrect and questionTwoCorrect with the following:

for (int i = 0; i < 2; i++) {
    AskQuestion(questions[i], answers[i]);
}

questions and answers are of size 2. This means we want the for loop to iterate two times. To do this,we set the loop condition to be i < 2. This means the loop will exit when i < 2 becomes false.

Winners and losers

Now that we’ve made these changes, we no longer have a way to tell if the user has won or lost.

To fix this, declare a variable, hasUserWon outside of the for loop. Initialize it as true.

bool hasUserWon = true;

for(int i = 0; i < 2; i++) {
    AskQuestion(questions[i], answers[i]);
}

Inside the loop, declare another variable, questionResult, which stores the return value of AskQuestion:

bool hasUserWon = true;

for(int i = 0; i < 2; i++) {
    bool questionResult = AskQuestion(questions[i], answers[i]);
}

If result is false, then hasUserWon needs to become false. Write:

bool hasUserLost = true;

for(int i = 0; i < 2; i++) {
    bool questionResult = AskQuestion(questions[i], answers[i]);

    if (questionResult == false) {
        hasUserWon = false;
    }
}

Now the player will lose the game if they get a single question wrong. However they can still complete all the questions even if they’ve already lost.

Amend the if statement that checks if the user has won to game to use hasUserWon. Also, delete any reference to AskQuestionOne and AskQuestionTwo.

DoQuizQuestions should now look like this:

static void DoQuizQuestions() {
    string[] questions = new string[2];
    string[] answers = new string[2];

    questions[0] = "In what year did the Titanic sink?";
    answers[0] = "1912";
    questions[1] = "In what year did the Titanic II sink?";
    answers[1] = "1913";

    bool hasUserWon = true;

    for(int i = 0; i < 2; i++) {
        bool questionResult = AskQuestion(questions[i], answers[i]);

        if (questionResult == false) {
            hasUserWon = false;
        }
    }

    if (hasUserWon == true) {
        Console.WriteLine("You won!");
    }
    else {
        Console.WriteLine("You lost!");
    }
}

Finished!

You’ve now seen how to create your own arrays, and access them using loops. Complete the ‘Try on your own…’ section for more practice using loops and arrays.

Try on your own…

Q1

Add a third question to the program

  • Remember to alter the arrays to be of the correct size
  • Remember to amend the for loop to cope with three questions instead of just two

Q2

Try and change the for loop to use a do...while or while loop instead.

  • You’ll need to create a new variable to keep track of which array index to use for answers and questions
  • You’ll also need to ensure you ask the correct amount of questions - don’t try and access an array index that doesn’t exist!
Copyright Mikiel Agutu 2019