Difference between revisions of "2022 - When you start the game it automatically loads a saved game, adapt it to ask which game to load"
(→What you need to do) |
(→Issue) |
||
Line 30: | Line 30: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | When you choose to load a game, the program automatically loads "game1.txt". | + | When you choose to load a game, the program automatically loads "game1.txt". The if statement below is what loads the actual file: |
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | if (!LoadGame("game1.txt")) | ||
+ | { | ||
+ | GameOver = true; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
=What you need to do= | =What you need to do= |
Latest revision as of 11:16, 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". The if statement below is what loads the actual file:
if (!LoadGame("game1.txt"))
{
GameOver = true;
}
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"