Difference between revisions of "Add a Help command to display a list of possible commands"
Georgebarber (talk | contribs) (Created page with "=The Examine method= There is already a method to examine the inventory or an item so find this code: <syntaxhighlight lang=c#> private static void help() {...") |
(→The Examine method) |
||
Line 1: | Line 1: | ||
− | = | + | =A Help method= |
− | + | You could add an new method into the skeleton program to display the help information: | |
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 6: | Line 6: | ||
{ | { | ||
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"); | 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"); | ||
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | + | Now you can add a command into the PlayGame switch case statement by putting the code below: | |
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 16: | Line 15: | ||
help(); | help(); | ||
break; | break; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | =Command Only= | ||
+ | Alternatively, if you don't want to make a new method you could just add the code into the PlayGame switch case: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | 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; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | =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: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | 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"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Now at the start of PlayGame declare a new list: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | List<string> commands = new List<string>(); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 09:58, 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:
List<string> commands = new List<string>();