Difference between revisions of "AS 2019 SaveGame"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Run SaveGame)
(Run SaveGame)
Line 167: Line 167:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
We need to add the following to the parameters:
 +
 +
<syntaxhighlight lang=c#>
 +
        private static int SelectMove(MoveRecord[] listOfMoves, int[,] a, int[,] b)
 +
 +
</syntaxhighlight>
 
The Console.Write() line asks for the piece to move, we can edit this message:
 
The Console.Write() line asks for the piece to move, we can edit this message:
  
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>
         private static int SelectMove(MoveRecord[] listOfMoves)
+
         private static int SelectMove(MoveRecord[] listOfMoves,  int[,] a, int[,] b)
 
         {
 
         {
 
             bool validPiece = false, validMove, found, endOfList;
 
             bool validPiece = false, validMove, found, endOfList;
Line 185: Line 191:
 
                 if (piece == "")
 
                 if (piece == "")
 
                 {
 
                 {
 +
                    endOfList = true;
 +
                }
 +
 +
</syntaxhighlight>
 +
 +
Now to run SaveGame if 'S' is entered:
 +
 +
<syntaxhighlight lang=c#>
 +
        private static int SelectMove(MoveRecord[] listOfMoves,  int[,] a, int[,] b)
 +
        {
 +
            bool validPiece = false, validMove, found, endOfList;
 +
            string piece = "", rowString, columnString;
 +
            int index = 0, chosenPieceIndex;
 +
            int newRow, newColumn;
 +
            while (!validPiece)
 +
            {
 +
                found = false;
 +
                endOfList = false;
 +
                Console.Write("Which piece do you want to move? - S to save "); //change here
 +
                piece = Console.ReadLine();
 +
                index = 0;
 +
                if (piece == "")
 +
                {
 +
                    endOfList = true;
 +
                }
 +
                else if (piece == "S")
 +
                {
 +
                    SaveGame()
 
                     endOfList = true;
 
                     endOfList = true;
 
                 }
 
                 }
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 09:45, 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

We will need to run the SaveGame method from within the Select Move method. Find the following from the Select Move method:

        private static int SelectMove(MoveRecord[] listOfMoves)
        {
            bool validPiece = false, validMove, found, endOfList;
            string piece = "", rowString, columnString;
            int index = 0, chosenPieceIndex;
            int newRow, newColumn;
            while (!validPiece)
            {
                found = false;
                endOfList = false;
                Console.Write("Which piece do you want to move? ");
                piece = Console.ReadLine();
                index = 0;
                if (piece == "")
                {
                    endOfList = true;
                }

We need to add the following to the parameters:

        private static int SelectMove(MoveRecord[] listOfMoves, int[,] a, int[,] b)

The Console.Write() line asks for the piece to move, we can edit this message:

        private static int SelectMove(MoveRecord[] listOfMoves,  int[,] a, int[,] b)
        {
            bool validPiece = false, validMove, found, endOfList;
            string piece = "", rowString, columnString;
            int index = 0, chosenPieceIndex;
            int newRow, newColumn;
            while (!validPiece)
            {
                found = false;
                endOfList = false;
                Console.Write("Which piece do you want to move? - S to save "); //change here
                piece = Console.ReadLine();
                index = 0;
                if (piece == "")
                {
                    endOfList = true;
                }

Now to run SaveGame if 'S' is entered:

        private static int SelectMove(MoveRecord[] listOfMoves,  int[,] a, int[,] b)
        {
            bool validPiece = false, validMove, found, endOfList;
            string piece = "", rowString, columnString;
            int index = 0, chosenPieceIndex;
            int newRow, newColumn;
            while (!validPiece)
            {
                found = false;
                endOfList = false;
                Console.Write("Which piece do you want to move? - S to save "); //change here
                piece = Console.ReadLine();
                index = 0;
                if (piece == "")
                {
                    endOfList = true;
                }
                else if (piece == "S")
                {
                    SaveGame()
                    endOfList = true;
                }