Difference between revisions of "AS 2019 SaveGame"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Converting)
(Issue)
Line 12: Line 12:
 
             }
 
             }
 
         }
 
         }
 +
</syntaxhighlight>
 +
 +
The code to run LoadPieces is in the SetupBoard Method, look for the following try & catch block:
 +
 +
<syntaxhighlight lang=c#>
 +
            try
 +
            {
 +
                StreamReader filehandle = new StreamReader(fileName);
 +
                fileFound = true;
 +
                LoadPieces(filehandle, a);
 +
                LoadPieces(filehandle, b);
 +
                filehandle.Close();
 +
                CreateNewBoard(board);
 +
                AddPlayerA(board, a);
 +
                AddPlayerB(board, b);
 +
            }
 +
            catch (Exception)
 +
            {
 +
                DisplayErrorCode(4);
 +
            }
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 10:17, 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());
            }
        }

The code to run LoadPieces is in the SetupBoard Method, look for the following try & catch block:

            try
            {
                StreamReader filehandle = new StreamReader(fileName);
                fileFound = true;
                LoadPieces(filehandle, a);
                LoadPieces(filehandle, b);
                filehandle.Close();
                CreateNewBoard(board);
                AddPlayerA(board, a);
                AddPlayerB(board, b);
            }
            catch (Exception)
            {
                DisplayErrorCode(4);
            }

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