2022 - There is a load game feature but no way of saving a game

From TRCCompSci - AQA Computer Science
Revision as of 16:48, 13 November 2021 by Admin (talk | contribs) (Created page with "=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:...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 you need to do

  • Study what the LoadGame method reads line by line
  • Now get these values in turn & write it to a text file