2022 - There is a load game feature but no way of saving a game
Contents
Issue
The program contains the code required to load a game, but doesn't contain the code to save a game.
The LoadGame method of the BreakThrough class can be seen below:
private bool LoadGame(string fileName)
{
string LineFromFile;
string LineFromFile2;
try
{
using (StreamReader MyStream = new StreamReader(fileName))
{
LineFromFile = MyStream.ReadLine();
Score = Convert.ToInt32(LineFromFile);
LineFromFile = MyStream.ReadLine();
LineFromFile2 = MyStream.ReadLine();
SetupLock(LineFromFile, LineFromFile2);
LineFromFile = MyStream.ReadLine();
SetupCardCollectionFromGameFile(LineFromFile, Hand);
LineFromFile = MyStream.ReadLine();
SetupCardCollectionFromGameFile(LineFromFile, Sequence);
LineFromFile = MyStream.ReadLine();
SetupCardCollectionFromGameFile(LineFromFile, Discard);
LineFromFile = MyStream.ReadLine();
SetupCardCollectionFromGameFile(LineFromFile, Deck);
}
return true;
}
catch
{
Console.WriteLine("File not loaded");
return false;
}
}
The easiest way to figure out how to save a file is to use the LoadGame sequence, every MyStream.ReadLine() will read one line of the text file. You therefore need to reverse the process.
What does a save game need to write
Each of the following needs to be on its own line:
- the score
- the current lock as a string for the conditions
- the status of the current lock conditions
- the Hand card collection as a string include card numbers
- the Sequence card collection as a string include card numbers
- the Discard card collection as a string include card numbers
- the Deck card collection as a string include card numbers
What you need to do
Lock class
- create a method to output the lock conditions as a string
- Look at the GetLockDetails method of the lock class
- look at the foreach loop, it loops through the challenges
- the code 'ConvertConditionToString(C.GetCondition())' gets the condition of the challenge
- Challenges need to separated with a ';'.
- create a method to output the status of the conditions as a string
- essentially as above, but we need to write 'Y' if the challenge is met or 'N' if it isn't
CardCollection class
- create a method to output the entire collection
- You can use a foreach loop, ie 'foreach(Card c in Cards)'
- you can then write the 'GetDescription()' of the card
- followed by a space and then the card number 'GetCardNumber()'
- each card is separated by a ','.
- make sure you check if the card collection is not empty
SaveGame method
Create a save game method which sets up a StreamWriter, you can copy the StreamReader code and change read for write, now instead of using myStream.ReadLine use myStream.WriteLine().
The SaveGame method should:
- write the score
- use your method to write the current lock conditions
- use your method to write the status of each condition
- use your method to write each card collection in order