Difference between revisions of "If a player enters a number not in the options it is treated as a word"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==Original== The optimal solution for this IMO is to try and cast the input number to an integer and if said cast succeeds switch through all the integer options; in the off...")
 
(Oopsie daisy, I've re-invented the wheel)
Line 73: Line 73:
  
 
This method offers several improvements over the one displayed above. One - If the player gives an input number which isn't an option, the player is made to input again and doesn't lose their turn. Two - The method makes sure the input is a number (mine does this as well, with less effort). Arguably my method doesn't require any new commands to be placed in 3 different places and allows one to basically just add what a new input should do to the case statement, however when on a time limit replicating existing functionality is good rule of thumb. Therefore we should re-design our GetChoice method to do this as well.
 
This method offers several improvements over the one displayed above. One - If the player gives an input number which isn't an option, the player is made to input again and doesn't lose their turn. Two - The method makes sure the input is a number (mine does this as well, with less effort). Arguably my method doesn't require any new commands to be placed in 3 different places and allows one to basically just add what a new input should do to the case statement, however when on a time limit replicating existing functionality is good rule of thumb. Therefore we should re-design our GetChoice method to do this as well.
 
  
 
<syntaxhighlight lang="C#">
 
<syntaxhighlight lang="C#">

Revision as of 21:08, 14 November 2017

Original

The optimal solution for this IMO is to try and cast the input number to an integer and if said cast succeeds switch through all the integer options; in the off case the cast doesn't suceed interpret it as a word.

private static Object GetCastedChoice() {
    String choice = GetChoice();

    try { return int.Parse(choice); } 
    catch { return choice; } 
}

Firstly I've defined a GetCastedChoice Method. This method gets the users valid input using our existing GetChoice method and then attempts to cast it to an integer. If this cast is succesful, the result is returned as an integer. Otherwise the result is returned as a String. Because both these types inherit from Object, we can box both these return values to an Object and just check whether it is an int or string at a later date.

S.N. While some would prefer to use the Integer.TryParse method to check whether the input is an int or not, I personally would like to avoid defining as many variables as possible. For those who would rather not worry about the affect on memory of such an act (especially when the fact will be a few bytes at most on a system spanning countless tera-bytes) please do so; I prefer to design things with the intention they'll become huge and convoluted quite quickly.


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) {
    Object _choice = GetCastedChoice();

    if (_choice is int) {
        switch (_choice.ToString()) {
            case "1":
                // Command One Functionality
                break;
            case "7":
                // Command Seven Functionality
                break;
            case "3":
                // Command Three Functionality
                break;
            case "5":
                // Command Five Functionality
                break;
            case "0":
                // Command Zero Functionality
                break;
            default:
                Console.WriteLine("The command {0} could not be found, your turn has been skipped", _choice);
                break;
        }
    } else { // Can only be string
        // What to do when input is word.
    }
}

Oopsie daisy, I've re-invented the wheel

Upon re-inspection, I've found what's needed to do this already exists within the program. Take a look at

private static string GetNewTileChoice() {
    string NewTileChoice = "";
    string[] TileChoice = { "1", "2", "3", "4", "5" };
    
    while (Array.IndexOf(TileChoice, NewTileChoice) == -1) {
        Console.WriteLine("Do you want to:");
        Console.WriteLine("     replace the tiles you used (1) OR");
        Console.WriteLine("     get three extra tiles (2) OR");
        Console.WriteLine("     replace the tiles you used and get three extra tiles (3) OR");
        Console.WriteLine("     get no new tiles (4)?");
        Console.Write("> ");

        NewTileChoice = Console.ReadLine();
    }

    return NewTileChoice; ;
}

This method offers several improvements over the one displayed above. One - If the player gives an input number which isn't an option, the player is made to input again and doesn't lose their turn. Two - The method makes sure the input is a number (mine does this as well, with less effort). Arguably my method doesn't require any new commands to be placed in 3 different places and allows one to basically just add what a new input should do to the case statement, however when on a time limit replicating existing functionality is good rule of thumb. Therefore we should re-design our GetChoice method to do this as well.

private static string GetChoice() {
    string Choice;
    String[] choices = { "1", "3", "4", "7", "5", "0" };

    Console.WriteLine();

    do {
        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 5 to swap 5 random tiles from hand");
        Console.WriteLine("     press 0 to fill hand and stop the game.");
        Console.Write("> ");

        Choice = Console.ReadLine().ToUpper();
        Console.WriteLine();
    } while (Array.IndexOf(choices, Choice) == -1);

    return Choice;
}

Now unless the player inputs a choice which has been pre specified within the choices array, the program will not move on. I feel I should note that this method doesn't indicate why the user isn't being allowed to move on, but that's easily fixable. The pros of this method is that it requires minimal tweaking and can quickly achieve the desired affect. The cost however is it's manual nature and the need to define any new options in multiple locations whenever designed (no good programmer should need to repeat himself/herself). I should also note, I switched from a while to a do-while method because as it stands, the entire array was being iterated at the beginning for no reason; it was wasteful to the CPU (albeit by a miniscule amount).