Difference between revisions of "Create a method to list the words available within a given hand"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Syntax prettifying)
Line 1: Line 1:
 +
==Listing Words Method
 +
 
The option "See available words with this hand." is added to the player's options in GetChoice().
 
The option "See available words with this hand." is added to the player's options in GetChoice().
 
  
 
<syntaxhighlight lang=Csharp>
 
<syntaxhighlight lang=Csharp>
private static string GetChoice()
+
private static string GetChoice() {
        {
+
    string Choice;
            string Choice;
+
    Console.WriteLine();
            Console.WriteLine();
+
    Console.WriteLine("Either:");
            Console.WriteLine("Either:");
+
    Console.WriteLine("    enter the word you would like to play OR");
            Console.WriteLine("    enter the word you would like to play OR");
+
    Console.WriteLine("    press 1 to display the letter values OR");
            Console.WriteLine("    press 1 to display the letter values OR");
+
    Console.WriteLine("    press 4 to view the tile queue OR");
            Console.WriteLine("    press 4 to view the tile queue OR");
+
    Console.WriteLine("    press 7 to view your tiles again OR");
            Console.WriteLine("    press 7 to view your tiles again OR");
+
    Console.WriteLine("    press 0 to fill hand and stop the game.");
            Console.WriteLine("    press 0 to fill hand and stop the game.");
+
    Console.WriteLine("    press 3 to see available words with this hand.");
            Console.WriteLine("    press 3 to see available words with this hand.");
+
    Console.Write("> ");
            Console.Write("> ");
+
    Choice = Console.ReadLine();
            Choice = Console.ReadLine();
+
    Console.WriteLine();
            Console.WriteLine();
+
    Choice = Choice.ToUpper();
            Choice = Choice.ToUpper();
+
    return Choice;
            return Choice;
+
    }
        }
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
The following code is added to the choices in Have Turn:
 
The following code is added to the choices in Have Turn:
  
 +
<syntaxhighlight lang=Csharp>
 +
// ... Some stuff was here
 +
 +
else if (Choice == "3") {
 +
    for (int i = 0; i < AllowedWords.Count; i++) {
 +
        bool wordintiles = CheckWordIsInTiles(AllowedWords[i], PlayerTiles);
  
<syntaxhighlight lang=Csharp>
+
        if (wordintiles == true) {
else if (Choice == "3")
+
            Console.WriteLine(AllowedWords[i]);
                {
+
        }
                    for (int i = 0; i < AllowedWords.Count; i++)
+
    }
                    {
+
}
                        bool wordintiles = CheckWordIsInTiles(AllowedWords[i], PlayerTiles);
 
                        if (wordintiles == true)
 
                        {
 
                            Console.WriteLine(AllowedWords[i]);
 
                        }
 
  
                    }
+
// ... Some stuff is here
                }
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 43: Line 43:
  
 
One problem is that you can make quite a lot of words from any given hand (even if it doesn't seem like it), so this outputs so many words that by the time it's done you can no longer see the top of the list.
 
One problem is that you can make quite a lot of words from any given hand (even if it doesn't seem like it), so this outputs so many words that by the time it's done you can no longer see the top of the list.
 +
 +
==Print in Alphabetical Order==
  
 
Another improvement you could make is for it to output the words by length or score order instead of alphabetical.
 
Another improvement you could make is for it to output the words by length or score order instead of alphabetical.
 +
 +
 +
==Print by Word Length==
 +
 +
== Print by Word Score==

Revision as of 18:31, 14 November 2017

==Listing Words Method

The option "See available words with this hand." is added to the player's options in GetChoice().

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.WriteLine("     press 3 to see available words with this hand.");
    Console.Write("> ");
    Choice = Console.ReadLine();
    Console.WriteLine();
    Choice = Choice.ToUpper();
    return Choice;
    }

The following code is added to the choices in Have Turn:

// ... Some stuff was here

else if (Choice == "3") {
    for (int i = 0; i < AllowedWords.Count; i++) {
        bool wordintiles = CheckWordIsInTiles(AllowedWords[i], PlayerTiles);

        if (wordintiles == true) {
            Console.WriteLine(AllowedWords[i]);
        }
    }
}

// ... Some stuff is here

This goes through the list of allowed words in alphabetical order and checks if the word can be made from the player's current tiles. If it can be, the word is outputted.

One problem is that you can make quite a lot of words from any given hand (even if it doesn't seem like it), so this outputs so many words that by the time it's done you can no longer see the top of the list.

Print in Alphabetical Order

Another improvement you could make is for it to output the words by length or score order instead of alphabetical.


Print by Word Length

Print by Word Score