Difference between revisions of "If a player presses enter without typing a word it is an invalid move"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Method 01)
(Method 01)
 
Line 5: Line 5:
 
     // Some stuff was here
 
     // Some stuff was here
 
     string Choice = "";
 
     string Choice = "";
 +
    bool validChoice = false;
  
 
     #region ChoiceChecker
 
     #region ChoiceChecker
Line 17: Line 18:
 
     #endregion
 
     #endregion
  
     // Notice while loop using 'ValidChoice' boolean gone
+
     while (!validChoice) {
    if (Choice == "1") {
+
        if (Choice == "1") {
        // Choice 1 functionality
+
            // Choice 1 functionality
    } else if (Choice == "4") {
+
        } else if (Choice == "4") {
        // Choice 4 functionality
+
            // Choice 4 functionality
    } else if (Choice == "7") {
+
        } else if (Choice == "7") {
        // Choice 7 functionality
+
            // Choice 7 functionality
    } else if (Choice == "0") {
+
        } else if (Choice == "0") {
        // Choice 0 functionality  
+
            // Choice 0 functionality  
    } else {
+
        } else {
// If input is word functionality
+
            validChoice = true; // break loop
 +
    // If input is word functionality
 +
        }
 
     }
 
     }
 
}
 
}

Latest revision as of 17:24, 15 November 2017

Method 01

private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles) {
    // Some stuff was here
    string Choice = "";
    bool validChoice = false;

    #region ChoiceChecker
    while (true) { // Infinite loop
        Choice = GetChoice();
        
        if (String.IsNullOrWhiteSpace(Choice)) { // If nothing or whitespace "   " input
            Console.WriteLine("You inputted a word with no letters, this is not a good option!");
            Console.WriteLine("Try again please!");
        }  else { break;} // On valid input break loop and continue method
    }
    #endregion

    while (!validChoice) {
        if (Choice == "1") {
            // Choice 1 functionality
        } else if (Choice == "4") {
            // Choice 4 functionality
        } else if (Choice == "7") {
            // Choice 7 functionality
        } else if (Choice == "0") {
            // Choice 0 functionality 
        } else {
            validChoice = true; // break loop
	    // If input is word functionality
        }
    }
}

The above method in question consists of an infinite loop which continually requests a choice and upon the entering of the first valid choice, it stops execution and carries on with the method. The actual-new content appears at the beginning of the infinite loop. The valid choice boolean which is defined in the original method has been removed because I find it un-necessary and I have also removed the while loop sorrounding the choce conditions. Notice in this case because if choice isn't a number it's seen as the users word input, you don't have to re-check the input isn't of length 0 or not again.

Method 02

While the above method is perfectly fine, in all honesty it feels somewhat forceful. I feel a much quicker approach in this case would be to use recursion alongside the GetChoice method. As of now, the GetChoice method looks like this.

private static string GetChoice() {
    string Choice;
    Console.WriteLine();
    Console.WriteLine("Either:");
    Console.WriteLine("     enter the word you would like to play OR");
    Console.WriteLine("     press 1 to display the letter values OR");
    Console.WriteLine("     press 4 to view the tile queue OR");
    Console.WriteLine("     press 7 to view your tiles again OR");
    Console.WriteLine("     press 0 to fill hand and stop the game.");
    Console.Write("> ");
    Choice = Console.ReadLine();
    Console.WriteLine();
    Choice = Choice.ToUpper();
    return Choice;
}

With a little recursion we can have the same affect as above in 4 lines with no real effort.

private static string GetChoice() {
    string Choice;

    Console.WriteLine();
    Console.WriteLine("Either:");
    Console.WriteLine("     enter the word you would like to play OR");
    Console.WriteLine("     press 1 to display the letter values OR");
    Console.WriteLine("     press 4 to view the tile queue OR");
    Console.WriteLine("     press 7 to view your tiles again OR");
    Console.WriteLine("     press 0 to fill hand and stop the game.");
    Console.Write("> ");

    Choice = Console.ReadLine().ToUpper();
    Console.WriteLine();

    if (String.IsNullOrWhiteSpace(Choice)) { // Invalid choice
        Console.WriteLine("You inputted a word with no letters, this is not a good option!\nTry Again\n");
        return GetChoice(); // Rerun method again from the top and then return its value when valid
    } else return Choice; // Valid so return
}

This recursive method functions similair to a synthetic while loop. In fact, you recieve a StackOverflowException precisely when this continually nesting loop runs far too many times (interesting point for any who're interested); For those who worry this may mean this method is unreliable or "Exception-prone", please note that for a StackOverflowException to be thrown the user must enter an invalid choice over 1,000,000 times and for a computer this may be instant but for a user it's highly unlikely. To finish off the program let's return to the above method and modify the one line we need to (also removing the while loop) to complete our task.

private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles) {
    // ... Var initalisation region here    

    string Choice = GetChoice(); // That's it

    // .. If statement stuff here
}

That's it, just change the choice variable definition to call the variable and you'll get a valid choice of desired length > 0 (including whitespace as 0). Pick and choose whatever solution u prefer.