-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.vb
289 lines (230 loc) · 10 KB
/
MainWindow.vb
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
Public Class MainWindow
Private m_settings As Settings
Private Sub Me_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_settings = Settings.Load()
If Not m_settings.Location.Equals(Point.Empty) AndAlso Not m_settings.Size.Equals(Drawing.Size.Empty) Then
Location = m_settings.Location
'HACK: shorten the size by 20, somehow we are growing in height.
'Dim size As New Size(m_settings.Size.Width, m_settings.Size.Height - 20)
Size = m_settings.Size
Else
'TODO: should probably validate that the coordinates are visible on the screen.
Dim x = (Screen.PrimaryScreen.WorkingArea.Width - Size.Width) \ 2
Dim y = (Screen.PrimaryScreen.WorkingArea.Height - Size.Height) \ 2
Location = New Point(x, y)
End If
If Not m_settings.StatusBar Then
StatusBarToolStripMenuItem.Checked = False
StatusBar1.Visible = False
Board.Height += StatusBar1.Height
End If
If m_settings.Hints Then
HintsToolStripMenuItem.Checked = True
Board.ShowHints = True
End If
Board.Rows = m_settings.BoardRows
Board.Columns = m_settings.BoardColumns
Board.Depth = m_settings.BoardDepth
UndoToolStripMenuItem.Enabled = False
HistoryToolStripMenuItem.Enabled = False
StatusBar1.Items(1).Text = Board.Count.ToString & " pieces left."
End Sub
Private Sub Board1_PieceMoved(sender As Object, e As GameBoard.MoveEventArgs) Handles Board.PieceMoved
UndoToolStripMenuItem.Enabled = (Board.History.Count > 0)
HistoryToolStripMenuItem.Enabled = UndoToolStripMenuItem.Enabled
StatusBar1.Items(1).Text = Board.Count.ToString & " pieces left."
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Close()
End Sub
Private Sub NewGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewGameToolStripMenuItem.Click
UndoToolStripMenuItem.Enabled = False
HistoryToolStripMenuItem.Enabled = False
Board.Reset()
End Sub
Private Sub OptionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OptionsToolStripMenuItem.Click
Dim dialog As New OptionsDialog(Board.Rows, Board.Columns, Board.Depth)
If dialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Board.Rows = dialog.Rows
Board.Columns = dialog.Columns
Board.Depth = dialog.Depth
StatusBar1.Items(1).Text = Board.Count.ToString & " pieces left."
End If
dialog.Close()
End Sub
Private Sub HintsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HintsToolStripMenuItem.Click
HintsToolStripMenuItem.Checked = Not HintsToolStripMenuItem.Checked
Board.ShowHints = HintsToolStripMenuItem.Checked
End Sub
Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
Board.Undo()
UndoToolStripMenuItem.Enabled = (Board.History.Count > 0)
HistoryToolStripMenuItem.Enabled = UndoToolStripMenuItem.Enabled
StatusBar1.Items(1).Text = Board.Count.ToString & " pieces left."
End Sub
Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
Using dialog As New AboutDialog
dialog.ShowDialog(Me)
End Using
End Sub
Private Sub StatusBarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StatusBarToolStripMenuItem.Click
StatusBarToolStripMenuItem.Checked = Not StatusBarToolStripMenuItem.Checked
If StatusBarToolStripMenuItem.Checked Then
StatusBar1.Visible = True
Board.Height -= StatusBar1.Height
Else
StatusBar1.Visible = False
Board.Height += StatusBar1.Height
End If
End Sub
Private Sub Me_Closing(sender As Object, e As ComponentModel.CancelEventArgs)
Board.CancelReplay()
m_settings.Location = Location
m_settings.Size = Size
m_settings.Hints = HintsToolStripMenuItem.Checked
m_settings.StatusBar = StatusBarToolStripMenuItem.Checked
m_settings.BoardRows = Board.Rows
m_settings.BoardColumns = Board.Columns
m_settings.BoardDepth = Board.Depth
Settings.Persist(m_settings)
End Sub
Private Sub HowToPlayToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HowToPlayToolStripMenuItem.Click
Using dialog As New HelpDialog
dialog.ShowDialog(Me)
End Using
End Sub
Private Sub HistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HistoryToolStripMenuItem.Click
Using dialog As New HistoryDialog
dialog.Rows = Board.Rows
dialog.Columns = Board.Columns
dialog.Depth = Board.Depth
Dim move = 1
For Each history In Board.History
Dim item = dialog.HistoryListView.Items.Add(move.ToString)
item.SubItems.Add((history.Source.Row + 1).ToString)
item.SubItems.Add((history.Source.Column + 1).ToString)
item.SubItems.Add("-->")
item.SubItems.Add((history.Destination.Row + 1).ToString)
item.SubItems.Add((history.Destination.Column + 1).ToString)
move += 1
Next
dialog.ShowDialog(Me)
End Using
End Sub
Private Sub ReplayToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReplayToolStripMenuItem.Click
If Board.History IsNot Nothing AndAlso Board.History.Count > 0 Then
Dim history As New List(Of GameHistory)
For Each item As GameHistory In Board.History
history.Add(New GameHistory(item.Source, item.Destination))
Next
Board.Replay(history)
End If
End Sub
Private Sub Me_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Escape Then
Board.CancelReplay()
End If
End Sub
Private Sub Board1_ReplayBegin(sender As Object, e As EventArgs) Handles Board.ReplayBegin
StatusBar1.Items(2).Text = "Replay"
End Sub
Private Sub Board1_ReplayFinish(sender As Object, e As EventArgs) Handles Board.ReplayFinish
StatusBar1.Items(2).Text = ""
End Sub
Private Sub SaveHistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveHistoryToolStripMenuItem.Click
Using dialog As New SaveFileDialog With {.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*",
.FilterIndex = 1,
.RestoreDirectory = True,
.OverwritePrompt = True,
.AddExtension = True}
If dialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Using stream As New IO.StreamWriter(dialog.OpenFile())
If stream IsNot Nothing Then
stream.WriteLine($"""Checkers Solitaire"",""{Application.ProductVersion}"",""{Board.Rows}"",""{Board.Columns}"",""{Board.Depth}""")
stream.WriteLine("""Move #"",""Start Row"",""Start Column"",""Destination Row"",""Destination Column""")
Dim move = 1
For Each item As GameHistory In Board.History
Dim line = $"{move},{item.Source.Row + 1},{item.Source.Column + 1},{item.Destination.Row + 1},{item.Destination.Column + 1}"
stream.WriteLine(line)
move += 1
Next
End If
End Using
End If
End Using
End Sub
Private Sub LoadHistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadHistoryToolStripMenuItem.Click
Dim history As List(Of GameHistory) = Nothing
Dim rows = 8
Dim columns = 8
Dim depth = 2
Using dialog As New OpenFileDialog With {.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*",
.FilterIndex = 1,
.RestoreDirectory = True,
.CheckFileExists = True}
If dialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Using stream As New IO.StreamReader(dialog.OpenFile())
Dim header = """Move #"",""Start Row"",""Start Column"",""Destination Row"",""Destination Column"""
If stream IsNot Nothing Then
Dim valid = False
Dim line = stream.ReadLine
If line.IndexOf("Checkers Solitaire") > -1 Then
Dim line2 = stream.ReadLine
If line2 = header Then
valid = True
Dim elements = Split(line.Replace(Chr(34), ""), ",")
rows = CInt(elements(2))
columns = CInt(elements(3))
depth = CInt(elements(4))
End If
ElseIf line = header Then
' valid file for an 8x8x2 game.
valid = True
rows = 8
columns = 8
depth = 2
End If
If valid Then
' Assume the file is good.
history = New List(Of GameHistory)
Do
line = stream.ReadLine
If line IsNot Nothing Then
Dim values = Split(line, ","c)
If values.Length = 5 Then
Dim source = New GameBoard.RowColumn(CInt(values(1)) - 1, CInt(values(2)) - 1)
Dim destination = New GameBoard.RowColumn(CInt(values(3)) - 1, CInt(values(4)) - 1)
history.Add(New GameHistory(source, destination))
End If
Else
Exit Do
End If
Loop
End If
End If
End Using
End If
End Using
If history IsNot Nothing AndAlso
history.Count > 0 Then
Board.Rows = rows
Board.Columns = columns
Board.Depth = depth
Dim delay = 1
Using replay As New ReplayDialog
If replay.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
If IsNumeric(replay.DelayTextBox.Text) Then
delay = CInt(replay.DelayTextBox.Text)
If delay < 1 Then
delay = 1
End If
End If
Board.ReplayDelay = delay
Else
delay = -1
End If
End Using
Board.Replay(history, Not delay = -1)
End If
End Sub
End Class