Ask user to confirm their word choice

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

This isn't all that complicated a task, but for the sake of reliability I'll also explain how to do it.

The logic needed for this, needs to be within the HaveTurn method within the else block which is executed when the player enters a word as their choice.


else {
    ValidChoice = true;

    if (Choice.Length == 0) {
        // Some stuff was here
    } else {   
        // Some stuff was here
    }

    #region NewStuffGoesHere

    String askInput; // Variable to hold the users input to below looped question

    do {
        Console.WriteLine("Are you sure you want to input this word (Yes/No) :> ");
        askInput = Console.ReadLine().Trim().ToLower(); // Keep asking till yes or no
    } while (askInput != "no" || askInput != "yes");

    if (askInput != "yes") { // If user wishes to change words
        ValidChoice = false; // Don't let loop end.
    }

    #endregion

    if (ValidWord) {
        // Some stuff was here
    }

    if (!ValidWord) {
        // Some stuff was here
    }

    if (NewTileChoice != "4") {
        // Some stuff was here
    }

    // Some stuff is here
}

Now we've got a method to ask the user whether they truly wish to enter their given word, however there is an error in this. Even if the user enters no, the word they've inputted will still be processed and then said user will be given another turn. To fix this, we can wrap everything after the "if (askInput != "yes")" in an else block to make sure it only runs when user wishes to enter the chosen word... like so:

else {
    ValidChoice = true;

    if (Choice.Length == 0) {
        // Some stuff was here
    } else {   
        // Some stuff was here
    }

    String askInput; // Variable to hold the users input to below looped question

    do {
        Console.WriteLine("Are you sure you want to input this word (Yes/No) :> ");
        askInput = Console.ReadLine().Trim().ToLower(); // Keep asking till yes or no
    } while (askInput != "no" || askInput != "yes");

    if (askInput != "yes") { // If user wishes to change words
        ValidChoice = false; // Don't let loop end.
    } else {
        if (ValidWord) {
            // Some stuff was here
        }

        if (!ValidWord) {
            // Some stuff was here
        }

        if (NewTileChoice != "4") {
            // Some stuff was here
        }

        // Some stuff is here
    }
}