AS 2019 Re-enter Filename if incorrect

From TRCCompSci - AQA Computer Science
Revision as of 08:21, 2 April 2019 by Admin (talk | contribs) (Created page with "=Issue= At the moment, if the user enters a filename that doesn't exist the game produces an error code and then exits. It would be better to add the additional code for the u...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Issue

At the moment, if the user enters a filename that doesn't exist the game produces an error code and then exits. It would be better to add the additional code for the user to be asked again.

SetupBoard

This is the current method for SetupBoard:

        private static void SetUpBoard(string[,] board, int[,] a, int[,] b, ref bool fileFound)
        {
            string fileName = "game1.txt";
            Console.Write("Do you want to load a saved game? (Y/N): ");
            string answer = Console.ReadLine();
            if (answer == "Y" || answer == "y")
            {
                Console.Write("Enter the filename: ");
                fileName = Console.ReadLine();
            }
            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);
            }

        }