Difference between revisions of "Add a command to Save to a new gme file"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "The key to completing this task is to use the same process as the LoadGame method but obviously instead of reading we need to write. The order of each item of data must be exa...")
 
(The method)
Line 2: Line 2:
  
 
=The method=
 
=The method=
Start by copying the entire LoadGame method, and rename it SaveGame.
+
Start by copying the entire LoadGame method, and rename it SaveGame. You need to keep all of the parameters:
 +
 
 +
<syntaxhighlight lang=c#>
 +
        private static bool SaveGame(string filename, List<Character> characters, List<Item> items, List<Place> places)
 +
        {
 +
            int noOfCharacters = characters.Count;
 +
            int noOfPlaces = places.Count;
 +
            int NoOfItems = items.Count;
 +
</syntaxhighlight>
 +
 
 +
We need to convert:
 +
<syntaxhighlight lang=c#>
 +
            int noOfCharacters, noOfPlaces, NoOfItems;
 +
</syntaxhighlight>
 +
 
 +
This original line of the LoadGame method is to store the number of characters, places, and items stored in the gme file. To save we need to give each of these a value by counting the number of elements in each list:
 +
 
 +
<syntaxhighlight lang=c#>
 +
            int noOfCharacters = characters.Count;
 +
            int noOfPlaces = places.Count;
 +
            int NoOfItems = items.Count;
 +
</syntaxhighlight>
 +
 
 +
At the moment this method uses BinaryReader, and we need to change this to BinaryWriter, so change:
 +
 
 +
<syntaxhighlight lang=c#>
 +
using (BinaryReader Reader = new BinaryReader(File.Open(filename, FileMode.Open)))
 +
</syntaxhighlight>
 +
 
 +
to this:
 +
 
 +
<syntaxhighlight lang=c#>
 +
using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create)))
 +
</syntaxhighlight>

Revision as of 12:01, 13 December 2018

The key to completing this task is to use the same process as the LoadGame method but obviously instead of reading we need to write. The order of each item of data must be exactly the same or the data would be unreadable.

The method

Start by copying the entire LoadGame method, and rename it SaveGame. You need to keep all of the parameters:

        private static bool SaveGame(string filename, List<Character> characters, List<Item> items, List<Place> places)
        {
            int noOfCharacters = characters.Count;
            int noOfPlaces = places.Count;
            int NoOfItems = items.Count;

We need to convert:

            int noOfCharacters, noOfPlaces, NoOfItems;

This original line of the LoadGame method is to store the number of characters, places, and items stored in the gme file. To save we need to give each of these a value by counting the number of elements in each list:

            int noOfCharacters = characters.Count;
            int noOfPlaces = places.Count;
            int NoOfItems = items.Count;

At the moment this method uses BinaryReader, and we need to change this to BinaryWriter, so change:

using (BinaryReader Reader = new BinaryReader(File.Open(filename, FileMode.Open)))

to this:

using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create)))