Allow players to add new words to the game

From TRCCompSci - AQA Computer Science
Revision as of 14:57, 16 November 2017 by M0hk4l3 (talk | contribs) (Created page with "There are 2 ways to do this: * Make the player add words to the aqawords text file (which admittedly isn't very programatic). * Create a method which is run after the 'aqawo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

There are 2 ways to do this:

  • Make the player add words to the aqawords text file (which admittedly isn't very programatic).
  • Create a method which is run after the 'aqawords.txt' file has been loaded to add words to the dictionary.

Seeing as the former requires no effort on my part whatsoever, I will work on the assumption the second is what is being asked. I've only included the former solution in case you are asked as to whether it is possible within the exam.

Without any further dawdling, let's get to the solution.

static void Main(string[] args) {
    // Some stuff was here

    while (Choice != "9") {
        DisplayMenu();
        Console.Write("Enter your choice: ");
        Choice = Console.ReadLine();

        if (Choice == "1") {
            // Some stuff is here
        } else if (Choice == "2") {
            // Some stuff is here
        } else if (Choice == "3") { 
            AddSomeNewWords(ref AllowedWords); // <- New stuff goes here
        }
    }
}

private static void DisplayMenu() {
    // Some Stuff Was Here
			
    Console.WriteLine("1. Play game with random start hand");
    Console.WriteLine("2. Play game with training start hand");
    Console.WriteLine("3. Add some words to dictionary of words");
    Console.WriteLine("9. Quit");

    // Some stuff is here
}

/// Method to do everything for the desired functionality in question
private static void AddSomeNewWords(ref List<String> AllowedWords) { }

Firstly, I've added a new option to the displayed menu with the ID number of 3. I've also taken liberty to add a new conditional handler (else/if statement) on the main method before the actual game has started. Now some may argue this task should be taken care of during game but there are two reasons why this isn't a good idea.

  • Firstly, the functionality specied here doesn't directly affect the game or the games logic & hence makes no sense to exist within the game.
  • Secondly, It's really easy to cheat. I mean, all a person has to do in that case is to add their entire hand as a word and get full points.

The contents of our newly defined else/if statement calls a newly defined method called add some words. Here is where will be doing some actual work.

S.N. I've removed any redundantly repetitive portions of the above method with commenting explaining some stuff is there but not relevent to the task at hand (Please don't delete this stuff or replace it with comments in the exam, you'll fail).

private static void AddSomeNewWords(ref List<String> AllowedWords) {
    Console.WriteLine($"Welcome to the word addition editor, to stop adding at words at any time please type /Exit");
    String newWordInput = String.Empty; // Stores new words player will be inputting to memory, also used in loop

    do {
        Console.Write("Please Enter A New Word :> ");
        newWordInput = Console.ReadLine().Trim().ToUpper();

        if (!AllowedWords.Contains(newWordInput))
            AllowedWords.Add(newWordInput);
    } while (newWordInput != "/EXIT");

    Console.Write("\nEditor has included, press enter to return to game :> ");
    Console.ReadLine(); // Wait for player to press enter before returning
}

I don't feel there's anything so complex here that it needs to be explained, hence I'll finish the task now.