Difference between revisions of "AS 2019 SaveGame"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Issue)
(New Save Method)
Line 78: Line 78:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=New Save Method=
 
=New Save Method=
 +
Create a new method called SaveGame and pass it the following parameters, these parameters are from SetUpBoard:
 +
 +
<syntaxhighlight lang=c#>
 +
        private static void SaveGame(int[,] a, int[,] b, ref bool fileFound)
 +
        {
 +
            try
 +
            {
 +
                Console.WriteLine("Please enter a filename for the game save: ");
 +
                string fileName = Console.ReadLine();
 +
                StreamWriter filehandle = new StreamWriter(fileName);
 +
                fileFound = true;
 +
                SavePieces(filehandle, a);
 +
                SavePieces(filehandle, b);
 +
                filehandle.Close();
 +
            }
 +
            catch (Exception)
 +
            {
 +
                DisplayErrorCode(4);
 +
            }
 +
        }
 +
</syntaxhighlight>
 +
 +
Now copy the try & catch block from the SetUpBoard method, and paste it inside the SaveGame Method:
 +
 +
<syntaxhighlight lang=c#>
 +
        private static void SaveGame(int[,] a, int[,] b, ref bool fileFound)
 +
        {
 +
            try
 +
            {
 +
                StreamWriter filehandle = new StreamWriter(fileName);
 +
                fileFound = true;
 +
                SavePieces(filehandle, a);
 +
                SavePieces(filehandle, b);
 +
                filehandle.Close();
 +
            }
 +
            catch (Exception)
 +
            {
 +
                DisplayErrorCode(4);
 +
            }
 +
        }
 +
</syntaxhighlight>
 +
 +
We can change the StreamReader to a StreamWriter, and also LoadPieces to SavePieces. The lines after the fileHandle.Close() are no longer required. Now we just need to get a filename for the save:
 +
 +
<syntaxhighlight lang=c#>
 +
        private static void SaveGame(int[,] a, int[,] b, ref bool fileFound)
 +
        {
 +
            try
 +
            {
 +
                Console.WriteLine("Please enter a filename for the game save: ");
 +
                string fileName = Console.ReadLine();
 +
                StreamWriter filehandle = new StreamWriter(fileName);
 +
                fileFound = true;
 +
                SavePieces(filehandle, a);
 +
                SavePieces(filehandle, b);
 +
                filehandle.Close();
 +
            }
 +
            catch (Exception)
 +
            {
 +
                DisplayErrorCode(4);
 +
            }
 +
        }
 +
</syntaxhighlight>
 +
 +
=Run SaveGame=

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

New Save Method

Create a new method called SaveGame and pass it the following parameters, these parameters are from SetUpBoard:

        private static void SaveGame(int[,] a, int[,] b, ref bool fileFound)
        {
            try
            {
                Console.WriteLine("Please enter a filename for the game save: ");
                string fileName = Console.ReadLine();
                StreamWriter filehandle = new StreamWriter(fileName);
                fileFound = true;
                SavePieces(filehandle, a);
                SavePieces(filehandle, b);
                filehandle.Close();
            }
            catch (Exception)
            {
                DisplayErrorCode(4);
            }
        }

Now copy the try & catch block from the SetUpBoard method, and paste it inside the SaveGame Method:

        private static void SaveGame(int[,] a, int[,] b, ref bool fileFound)
        {
            try
            {
                StreamWriter filehandle = new StreamWriter(fileName);
                fileFound = true;
                SavePieces(filehandle, a);
                SavePieces(filehandle, b);
                filehandle.Close();
            }
            catch (Exception)
            {
                DisplayErrorCode(4);
            }
        }

We can change the StreamReader to a StreamWriter, and also LoadPieces to SavePieces. The lines after the fileHandle.Close() are no longer required. Now we just need to get a filename for the save:

        private static void SaveGame(int[,] a, int[,] b, ref bool fileFound)
        {
            try
            {
                Console.WriteLine("Please enter a filename for the game save: ");
                string fileName = Console.ReadLine();
                StreamWriter filehandle = new StreamWriter(fileName);
                fileFound = true;
                SavePieces(filehandle, a);
                SavePieces(filehandle, b);
                filehandle.Close();
            }
            catch (Exception)
            {
                DisplayErrorCode(4);
            }
        }

Run SaveGame