-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chess_Board.cs
190 lines (156 loc) · 5.4 KB
/
Chess_Board.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
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
#region Header
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
#endregion
//---------------------------------------------------------------------------------------
namespace ArdeshirV.Applications.KnightsTour
{
#region Delegates
public delegate void FirstChanged(int intNewFirstCell);
#endregion
//-----------------------------------------------------------------------------------
public partial class Chess_Board : UserControl
{
#region Variables
Position[] m_posArr;
private int m_intCounter = 1;
Timer m_timMain = new Timer();
private volatile bool m_blnNotAllow;
private Board m_brdEnvironment = new Board();
#endregion
//-------------------------------------------------------------------------------
#region Constructor
public Chess_Board()
{
InitializeComponent();
m_timMain.Interval = 200;
DataGridViewInitialize();
m_brdEnvironment.SetMode(0);
m_timMain.Tick += new EventHandler(OnTimerTicker);
}
#endregion
//-------------------------------------------------------------------------------
#region Events
public event EventHandler OnFinishAnimate;
public event FirstChanged OnFirstCellSelect;
#endregion
//-------------------------------------------------------------------------------
#region Properties
[
Browsable(true),
Description("Choose how to show chess board.")
]
public BoardMode BoardMode
{
set
{
m_brdEnvironment.SetMode(value);
m_grdEnvironment.Invalidate();
}
get
{
return m_brdEnvironment.GetMode();
}
}
[
Browsable(false)
]
public int RollTheCompareTableValue
{
set
{
m_brdEnvironment.RollTheCompareTable(value);
m_grdEnvironment.Invalidate();
}
}
#endregion
//-------------------------------------------------------------------------------
#region Event Handlers
private void m_grdEnvironment_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.Handled = true;
m_brdEnvironment.DrawCell(e.Graphics, (sender as Control).Font,
e.CellBounds, e.ColumnIndex, e.RowIndex);
}
private void m_grdEnvironment_Scroll(object sender, ScrollEventArgs e)
{
m_grdEnvironment.Invalidate();
}
private void m_grdEnvironment_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (BoardMode == BoardMode.bmWhichCellCanBeFirst && m_brdEnvironment[e.ColumnIndex, e.RowIndex] != 0)
{
int l_intFirstCell = m_brdEnvironment[e.ColumnIndex, e.RowIndex] - 1;
SetFirstCell(l_intFirstCell);
if (OnFirstCellSelect != null)
OnFirstCellSelect(l_intFirstCell);
}
}
private void OnTimerTicker(object sender, EventArgs e)
{
if (m_blnNotAllow || m_intCounter > m_posArr.Length - 1)
{
m_timMain.Stop();
if (OnFinishAnimate != null)
OnFinishAnimate(this, null);
return;
}
m_brdEnvironment[m_posArr[m_intCounter].X, m_posArr[m_intCounter].Y] = m_intCounter + 1;
m_grdEnvironment.Invalidate();
m_intCounter++;
}
#endregion
//-------------------------------------------------------------------------------
#region Access Methods
public void AnimateStop()
{
m_blnNotAllow = true;
}
public void AnimateStart(Position[] posArrPath)
{
m_intCounter = 0;
m_posArr = posArrPath;
m_blnNotAllow = false;
m_timMain.Start();
}
public void SetFirstCell(int value)
{
m_brdEnvironment.SetFirstCell(value);
m_grdEnvironment.Invalidate();
}
public void ClearBoard()
{
m_brdEnvironment.SetMode(BoardMode.bmNormalChessBoard);
m_grdEnvironment.Invalidate();
}
public object[] GetFirstCells()
{
return m_brdEnvironment.GetFirstCells();
}
public Position[] GetPath()
{
return m_brdEnvironment.GetPath();
}
public void RollTheCompareTable(int intRollPosition)
{
m_brdEnvironment.RollTheCompareTable(intRollPosition);
m_grdEnvironment.Invalidate();
}
#endregion
//-------------------------------------------------------------------------------
#region Utility Functions
private void DataGridViewInitialize()
{
m_grdEnvironment.Rows.Clear();
m_grdEnvironment.Columns.Clear();
for (int l_intIndexer = 0; l_intIndexer < 8; l_intIndexer++)
m_grdEnvironment.Columns.Add(l_intIndexer.ToString(), l_intIndexer.ToString());
m_grdEnvironment.Rows.Add(8 - 1);
}
#endregion
}
}