Difference between revisions of "2022 - When you start the game it automatically loads a saved game, adapt it to ask which game to load"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Issue= When you currently start the game you are prompted "Enter L to load a game from a file, anything else to play a new game:> ". This can be found in the SetupGame method...")
 
(What you need to do)
Line 33: Line 33:
  
 
=What you need to do=
 
=What you need to do=
* Add the code to ask for a file name
+
* Add the code to ask for a file name & read it in from the keyboard
* test if the file name exists
+
* Test if the file name exists, do this using File.Exists("test.txt") this will return true or false
* if it does, load that file
+
* Create an if statement to use the File.Exists
* if it doesn't, load "game1.txt"
+
* if true, load that file,
 +
* if false, load "game1.txt"

Revision as of 12:15, 18 November 2021

Issue

When you currently start the game you are prompted "Enter L to load a game from a file, anything else to play a new game:> ". This can be found in the SetupGame method of the BreakThrough class. This is shown below:

private void SetupGame()
        {
            string Choice;
            Console.Write("Enter L to load a game from a file, anything else to play a new game:> ");
            Choice = Console.ReadLine().ToUpper();
            if (Choice == "L")
            {
                if (!LoadGame("game1.txt"))
                {
                    GameOver = true;
                }
            }
            else
            {
                CreateStandardDeck();
                Deck.Shuffle();
                for (int Count = 1; Count <= 5; Count++)
                {
                    MoveCard(Deck, Hand, Deck.GetCardNumberAt(0));
                }
                AddDifficultyCardsToDeck();
                Deck.Shuffle();
                CurrentLock = GetRandomLock();
            }
        }

When you choose to load a game, the program automatically loads "game1.txt".

What you need to do

  • Add the code to ask for a file name & read it in from the keyboard
  • Test if the file name exists, do this using File.Exists("test.txt") this will return true or false
  • Create an if statement to use the File.Exists
  • if true, load that file,
  • if false, load "game1.txt"