AS 2019 SaveGame

From TRCCompSci - AQA Computer Science
Revision as of 09:08, 1 April 2019 by Admin (talk | contribs) (Created page with "=Issue= You can load a saved game, but you can't create a saved game. The game as code to load the game, so it would always be good to start the saved game method by looking a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Issue

You can load a saved game, but you can't create a saved game. The game as code to load the game, so it would always be good to start the saved game method by looking at the LoadPieces method:

        private static void LoadPieces(StreamReader fileHandle, int[,] playersPieces)
        {
            for (int index = 0; index < NumberOfPieces + 1; index++)
            {
                playersPieces[index, Row] = Convert.ToInt32(fileHandle.ReadLine());
                playersPieces[index, Column] = Convert.ToInt32(fileHandle.ReadLine());
                playersPieces[index, Dame] = Convert.ToInt32(fileHandle.ReadLine());
            }
        }

Converting

We can copy the LoadPieces method and make a few changes, firsly the name & the StreamReader:

        private static void LoadPieces(StreamReader fileHandle, int[,] playersPieces)
        {

        }

will become:

        private static void SavePieces(StreamWriter fileHandle, int[,] playersPieces)
        {

        }
</syntaxhighlight

The for loop is still required, so we can copy this across:

<syntaxhighlight lang=c#>
        private static void SavePieces(StreamWriter fileHandle, int[,] playersPieces)
        {
            for (int index = 0; index < NumberOfPieces + 1; index++)
            {

            }
        }

Finally we need to change the ReadLine's of the LoadPieces to WriteLines, and swap the code around. So:

        private static void SavePieces(StreamWriter fileHandle, int[,] playersPieces)
        {
            for (int index = 0; index < NumberOfPieces + 1; index++)
            {
                fileHandle.WriteLine(playersPieces[index, Row]);
                fileHandle.WriteLine(playersPieces[index, Column] );
                fileHandle.WriteLine(playersPieces[index, Dame]);
            }
        }