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() {...") |
|||
(One intermediate revision by the same user not shown) | |||
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#> | ||
case "help": | case "help": | ||
help(); | help(); | ||
+ | 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, and run CreateCommandList: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | 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; | 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;