Difference between revisions of "Add a Help command to display a list of possible commands"
(→The Examine method) |
|||
Line 45: | Line 45: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Now at the start of PlayGame declare a new list: | + | Now at the start of PlayGame declare a new list, and run CreateCommandList: |
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
List<string> commands = new List<string>(); | List<string> commands = new List<string>(); | ||
+ | CreateCommandList(commands); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Now as part of the PlayGame switch case statement add the following code: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | case "help": | ||
+ | Console.WriteLine("The in game commands are: "); | ||
+ | foreach(string s in commands) | ||
+ | Console.WriteLine(s); | ||
+ | break; | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 11:33, 20 December 2018
A Help method
You could add an new method into the skeleton program to display the help information:
private static void help()
{
Console.WriteLine("Commands: \n Get \n Use \n Go \n Read \n Examine \n Open \n Close \n Move \n Playdice \n Attack \n Quit");
}
Now you can add a command into the PlayGame switch case statement by putting the code below:
case "help":
help();
break;
Command Only
Alternatively, if you don't want to make a new method you could just add the code into the PlayGame switch case:
case "help":
Console.WriteLine("Commands: \n Get \n Use \n Go \n Read \n Examine \n Open \n Close \n Move \n Playdice \n Attack \n Quit");
break;
More Complex Method
Alternatively, you could instead of just using Console.WriteLine you could create a List or Array of the commands and output them:
private static void CreateCommandList(List<string list)
{
list.Add("Go");
list.Add("Get");
list.Add("Use");
list.Add("Examine");
list.Add("Move");
list.Add("Open");
list.Add("Close");
list.Add("Read");
list.Add("PlayDice");
list.Add("Quit");
}
Now at the start of PlayGame declare a new list, and run CreateCommandList:
List<string> commands = new List<string>();
CreateCommandList(commands);
Now as part of the PlayGame switch case statement add the following code:
case "help":
Console.WriteLine("The in game commands are: ");
foreach(string s in commands)
Console.WriteLine(s);
break;