-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
132 lines (124 loc) · 4.45 KB
/
GameManager.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SuperSudoku
{
/// <summary>
/// This class implements UI state transitinos all in one place; both the
/// welcome screen's buttons and the game screen's File menu hook in here.
/// </summary>
public class GameManager
{
/// <summary>
/// Shows a dialog for saving the grid.
/// </summary>
/// <param name="grid">The grid to be saved</param>
/// <returns>Whether the save succeeded. False if the user canceled.</returns>
public static bool SaveGame(Grid grid)
{
bool result = false;
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Sudoku Game|*.sud";
dialog.Title = "Save Game";
dialog.ShowDialog();
if (dialog.FileName != "")
{
new File().WriteFile(grid, dialog.FileName);
result = true;
}
return result;
}
/// <summary>
/// Loads a game from disk (showing load dialog) and then shows the game form.
/// </summary>
/// <param name="form">This form will be hidden.</param>
public static bool LoadGame(Form form)
{
bool result = false;
File fileOpener = new File();
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Sudoku Game|*.sud";
dialog.Title = "Load Game";
dialog.ShowDialog();
if (dialog.FileName != "")
{
form.Hide();
Grid newGrid = new Grid();
fileOpener.ReadFile(newGrid, dialog.FileName);
GameForm gform = new GameForm(newGrid, true);
gform.ShowDialog();
form.Close();
result = true;
}
return result;
}
/// <summary>
/// Shows the dialog for creating a puzzle. May also show the new game screen.
/// </summary>
/// <param name="form">This form will be hidden.</param>
public static void GeneratePuzzle(Form form)
{
Cursor.Current = Cursors.WaitCursor;
DifficultyForm dform = new DifficultyForm();
Cursor.Current = Cursors.Default;
dform.ShowDialog();
if (dform.HasResult)
{
form.Hide();
GameForm gform = new GameForm(dform.Result, true);
gform.ShowDialog();
form.Close();
}
}
/// <summary>
/// Shows a blank game screen.
/// </summary>
/// <param name="form">This form will be hidden.</param>
public static void EnterNewPuzzle(Form form)
{
form.Hide();
GameForm gform = new GameForm(new Grid(), false);
gform.ShowDialog();
form.Close();
}
/// <summary>
/// When the player transitions from "edit grid" mode to "play this game" mode
/// </summary>
internal static void PlayThisPuzzle(Grid oldGrid, Form form)
{
// Set all the squares as non-editable
Grid grid = oldGrid.Copy();
grid.ForEachSquare((row, col, val) =>
{
if (val != 0)
{
grid.SetEditable(false, row, col);
}
});
// Tries to solve the grid. If the grid has no unique solution, warn first.
Grid copyGrid = grid.Copy(); // yes another copy; the solver will mess with this one.
bool result = (new Solver().Solve(copyGrid));
if (result)
{
SaveGame(grid);
form.Hide();
GameForm gform = new GameForm(grid, true);
gform.ShowDialog();
form.Close();
}
else
{
if (MessageBox.Show(copyGrid.IsFull()? "This puzzle can be solved in more than one way. Play anyway?" : "This puzzle cannot be solved. Play anyway?", "Unsolvable Puzzle", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
SaveGame(grid);
form.Hide();
GameForm gform = new GameForm(grid, true);
gform.ShowDialog();
form.Close();
}
}
}
}
}