-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramEngine.cs
74 lines (66 loc) · 2.57 KB
/
ProgramEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace Treblecross
{
// public delegate GameOperator StartNewGameDelegate();
// public delegate void LoadGameDelegate();
public delegate GameOperator MenuCallback();
public enum MenuAction
{
StartNewGame = 0,
LoadGame = 1,
AvailableCommends = 2,
Quit = 3,
}
public class ProgramEngine
{
public static string GlobalCommends = "save: Save game, the command will save the game of current state to a file which can be loaded from the menu.\r\n" +
"hint: Return a hint about the game. It also reminds user of what commands are available.\r\n" +
"quit: Quit the game without saving.";
public Delegate DisplayMenu()
{
int action = -1;
bool valid = false;
do
{
Log.Info("Menu", "Choose an action? (0: Start a new game, 1: Load game, 2: Check global commands, 3: Quit!)");
valid = int.TryParse(Console.ReadLine(), out action);
if (!Enum.IsDefined(typeof(MenuAction), action) || !valid)
{
Log.Info("Menu", "Wrong action, please re-enter!");
valid = false;
}
} while (!valid);
switch ((MenuAction)action)
{
case MenuAction.StartNewGame:
return new MenuCallback(startNewGame);
case MenuAction.LoadGame:
return new MenuCallback(loadGame);
case MenuAction.AvailableCommends:
Log.Info("Menu","In the game, there are some global commands that are available to use:");
Console.WriteLine(GlobalCommends);
return DisplayMenu();
default:
Log.Info("Menu", "Bye!");
return null;
}
}
public GameOperator startNewGame()
{
// (not urgent) TODO: prompt "choose game"
return new TreblecrossOperator();
}
public GameOperator loadGame()
{
GameOperator gameOp;
using (GameFile file = new GameFile()){
var reuslt = file.Load();
GameMode gameMode = (GameMode) reuslt.Item1;
Player[] players = reuslt.Item2;
Board board = reuslt.Item3;
// (not urgent) TODO: Load game type
gameOp = new TreblecrossOperator(gameMode, players, board);
}
return gameOp;
}
}
}