Difference between revisions of "Create a method to dump the data to the screen"
(→The method) |
(→Using the Method) |
||
Line 39: | Line 39: | ||
==Using the Method== | ==Using the Method== | ||
+ | ===Dump Data during Game=== | ||
+ | You could add a command to the switch case statement in playgame: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | case "quit": | ||
+ | Say("You decide to give up, try again another time."); | ||
+ | stopGame = true; | ||
+ | break; | ||
+ | default: | ||
+ | Console.WriteLine("Sorry, you don't know how to " + Command + "."); | ||
+ | break; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | find the code above and add a new case: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | case "quit": | ||
+ | Say("You decide to give up, try again another time."); | ||
+ | stopGame = true; | ||
+ | break; | ||
+ | case "dump": | ||
+ | DumpData(characters, items, places); | ||
+ | break; | ||
+ | default: | ||
+ | Console.WriteLine("Sorry, you don't know how to " + Command + "."); | ||
+ | break; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===Dump Data at Start=== | ||
Add this code as a method into your program. Now we need to run it so in main method so change: | Add this code as a method into your program. Now we need to run it so in main method so change: | ||
Latest revision as of 13:39, 11 December 2018
The method
The following method will cycle through each Character, Place and Item and display all of the data for each:
private static void DumpData(List<Character> characters, List<Item> items, List<Place> places)
{
foreach(Character C in characters)
{
Console.Write(C.ID +" ");
Console.Write(C.Name + " ");
Console.Write(C.Description + " ");
Console.WriteLine(C.CurrentLocation);
}
foreach(Place P in places)
{
Console.Write(P.id + " ");
Console.Write(P.Description + " ");
Console.Write(P.North + " ");
Console.Write(P.East + " ");
Console.Write(P.South + " ");
Console.Write(P.West + " ");
Console.Write(P.Up + " ");
Console.WriteLine(P.Down);
}
foreach(Item I in items)
{
Console.Write(I.ID + " ");
Console.Write(I.Description + " ");
Console.Write(I.Status + " ");
Console.Write(I.Location+ " ");
Console.Write(I.Name + " ");
Console.Write(I.Commands + " ");
Console.WriteLine(I.Results);
}
}
Using the Method
Dump Data during Game
You could add a command to the switch case statement in playgame:
case "quit":
Say("You decide to give up, try again another time.");
stopGame = true;
break;
default:
Console.WriteLine("Sorry, you don't know how to " + Command + ".");
break;
find the code above and add a new case:
case "quit":
Say("You decide to give up, try again another time.");
stopGame = true;
break;
case "dump":
DumpData(characters, items, places);
break;
default:
Console.WriteLine("Sorry, you don't know how to " + Command + ".");
break;
Dump Data at Start
Add this code as a method into your program. Now we need to run it so in main method so change:
if (LoadGame(filename, characters, items, places))
{
PlayGame(characters, items, places);
}
to add the extra code before PlayGame is called:
if (LoadGame(filename, characters, items, places))
{
Console.WriteLine("Do you want to dump the data??");
string choice = Console.ReadLine();
if (choice == "yes")
DumpData(characters, items, places);
PlayGame(characters, items, places);
}