Difference between revisions of "AS 2019 SaveGame"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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...")
 
(Converting)
Line 30: Line 30:
  
 
         }
 
         }
</syntaxhighlight
+
</syntaxhighlight>
  
 
The for loop is still required, so we can copy this across:
 
The for loop is still required, so we can copy this across:

Revision as of 10:15, 1 April 2019

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)
        {

        }

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

        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]);
            }
        }