protected
override
void
OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base
.OnMouseLeftButtonDown(e);
int
col
=
(
int
)e.GetPosition(
this
).X
/
ChessSize;
int
row
=
(
int
)e.GetPosition(
this
).Y
/
ChessSize;
if
(m_Steps[col, row].Color
!=
ChessColor.Empty)
{
return
;
}
if
(m_NotInPos.X
==
col
&&
m_NotInPos.Y
==
row)
{
return
;
}
else
{
m_NotInPos.X
=
-
1
;
m_NotInPos.Y
=
-
1
;
}
DrawChess(col, row);
if
(
!
EatOther(col, row))
{
if
(EatSelf(col, row))
{
//
自杀一子为禁入
if
(m_DeadPos.Count
==
1
)
{
BackOne();
}
}
}
}
private
void
DrawChess(
int
col,
int
row)
{
int
left
=
col
*
ChessSize;
int
top
=
row
*
ChessSize;
Brush brush;
if
(m_IsBlack
=
!
m_IsBlack)
{
brush
=
Brushes.Black;
m_Steps[col, row].Color
=
ChessColor.Black;
}
else
{
brush
=
Brushes.White;
m_Steps[col, row].Color
=
ChessColor.White;
}
if
(m_Steps[col, row].Chess
==
null
)
{
Chess chess
=
new
Chess(brush, ChessSize);
Canvas.SetLeft(chess, left);
Canvas.SetTop(chess, top);
m_Canvas.Children.Add(chess);
m_Steps[col, row].Chess
=
chess;
}
else
if
(m_Steps[col, row].Chess.Content
==
null
)
{
Ellipse e
=
new
Ellipse();
e.Fill
=
brush;
e.Width
=
e.Height
=
ChessSize;
m_Steps[col, row].Chess.Content
=
e;
}
else
{
throw
new
Exception(
string
.Format(
"
Cannot Draw Chess at: col = {0}, row = {1}.
"
, col, row));
}
m_Steps[col, row].Count
=
++
m_Count;
m_Steps[col, row].Column
=
col;
m_Steps[col, row].Row
=
row;
}
private
bool
EatOther(
int
col,
int
row)
{
//
上、下、左、右各吃一通。
bool
ok1
=
false
;
if
(col
!=
0
&&
m_Steps[col, row].Color
!=
m_Steps[col
-
1
, row].Color)
{
ok1
=
EatSelf(col
-
1
, row);
}
bool
ok2
=
false
;
if
(col
!=
18
&&
m_Steps[col, row].Color
!=
m_Steps[col
+
1
, row].Color)
{
ok2
=
EatSelf(col
+
1
, row);
}
bool
ok3
=
false
;
if
(row
!=
0
&&
m_Steps[col, row].Color
!=
m_Steps[col, row
-
1
].Color)
{
ok3
=
EatSelf(col, row
-
1
);
}
bool
ok4
=
false
;
if
(row
!=
18
&&
m_Steps[col, row].Color
!=
m_Steps[col, row
+
1
].Color)
{
ok4
=
EatSelf(col, row
+
1
);
}
return
ok1
||
ok2
||
ok3
||
ok4;
}
private
bool
EatSelf(
int
col,
int
row)
{
ClearForEat();
if
(CanEat(col, row, m_Steps[col, row].Color))
{
DeadStep deadStep;
deadStep.Count
=
m_Count;
deadStep.Color
=
m_Steps[col, row].Color;
deadStep.DeadInfo
=
new
Dictionary
<
int
, Pos
>
();
if
(m_DeadPos.Count
==
1
)
{
Pos pos;
pos.X
=
m_DeadPos[
0
].X;
pos.Y
=
m_DeadPos[
0
].Y;
m_NotInPos.X
=
pos.X;
m_NotInPos.Y
=
pos.Y;
foreach
(var item
in
m_Steps)
{
//
变色而已,m_Count 为上一步
if
(item.Count
==
m_Count)
{
ClearForEat();
if
(CanEat(pos.X, pos.Y, item.Color))
{
//
因有一子变色,故减一。实际 Count 至少为 3
if
(m_DeadPos.Count
-
1
>
0
)
{
m_NotInPos.X
=
-
1
;
m_NotInPos.Y
=
-
1
;
}
}
}
}
deadStep.DeadInfo.Add(m_Steps[pos.X, pos.Y].Count,
new
Pos(pos.X, pos.Y));
m_Steps[pos.X, pos.Y].Color
=
ChessColor.Empty;
m_Steps[pos.X, pos.Y].Chess.Content
=
null
;
}
else
{
foreach
(var item
in
m_DeadPos)
{
deadStep.DeadInfo.Add(m_Steps[item.X, item.Y].Count,
new
Pos(item.X, item.Y));
m_Steps[item.X, item.Y].Color
=
ChessColor.Empty;
m_Steps[item.X, item.Y].Chess.Content
=
null
;
}
}
m_DeadSteps.Add(deadStep);
return
true
;
}
return
false
;
}
void
ClearForEat()
{
foreach
(var item
in
m_DeadPos)
{
m_Steps[item.X, item.Y].IsDead
=
false
;
}
m_DeadPos.Clear();
}
bool
CanEat(
int
col,
int
row, ChessColor currentColor)
{
if
(m_Steps[col, row].Color
==
ChessColor.Empty)
{
return
false
;
}
m_DeadPos.Add(
new
Pos(col, row));
m_Steps[col, row].IsDead
=
true
;
//
边界问题,头疼问题。笨人笨法,分别处理之
return
CanEatLeft(col, row, currentColor)
&&
CanEatRight(col, row, currentColor)
&&
CanEatUp(col, row, currentColor)
&&
CanEatDown(col, row, currentColor);
}
bool
CanEatLeft(
int
col,
int
row, ChessColor currentColor)
{
if
(col
!=
0
)
{
if
(m_Steps[col
-
1
, row].Color
==
ChessColor.Empty)
{
return
false
;
}
else
if
(m_Steps[col
-
1
, row].Color
==
currentColor
&&
m_Steps[col
-
1
, row].IsDead
==
false
)
{
m_DeadPos.Add(
new
Pos(col
-
1
, row));
m_Steps[col
-
1
, row].IsDead
=
true
;
if
(
!
CanEatUp(col
-
1
, row, currentColor))
{
return
false
;
}
if
(
!
CanEatDown(col
-
1
, row, currentColor))
{
return
false
;
}
if
(
!
CanEatLeft(col
-
1
, row, currentColor))
{
return
false
;
}
}
}
return
true
;
}
bool
CanEatRight(
int
col,
int
row, ChessColor currentColor)
{
if
(col
!=
18
)
{
if
(m_Steps[col
+
1
, row].Color
==
ChessColor.Empty)
{
return
false
;
}
else
if
(m_Steps[col
+
1
, row].Color
==
currentColor
&&
m_Steps[col
+
1
, row].IsDead
==
false
)
{
m_DeadPos.Add(
new
Pos(col
+
1
, row));
m_Steps[col
+
1
, row].IsDead
=
true
;
if
(
!
CanEatUp(col
+
1
, row, currentColor))
{
return
false
;
}
if
(
!
CanEatDown(col
+
1
, row, currentColor))
{
return
false
;
}
if
(
!
CanEatRight(col
+
1
, row, currentColor))
{
return
false
;
}
}
}
return
true
;
}
bool
CanEatUp(
int
col,
int
row, ChessColor currentColor)
{
if
(row
!=
0
)
{
if
(m_Steps[col, row
-
1
].Color
==
ChessColor.Empty)
{
return
false
;
}
else
if
(m_Steps[col, row
-
1
].Color
==
currentColor
&&
m_Steps[col, row
-
1
].IsDead
==
false
)
{
m_DeadPos.Add(
new
Pos(col, row
-
1
));
m_Steps[col, row
-
1
].IsDead
=
true
;
if
(
!
CanEatLeft(col, row
-
1
, currentColor))
{
return
false
;
}
if
(
!
CanEatRight(col, row
-
1
, currentColor))
{
return
false
;
}
if
(
!
CanEatUp(col, row
-
1
, currentColor))
{
return
false
;
}
}
}
return
true
;
}
bool
CanEatDown(
int
col,
int
row, ChessColor currentColor)
{
if
(row
!=
18
)
{
if
(m_Steps[col, row
+
1
].Color
==
ChessColor.Empty)
{
return
false
;
}
else
if
(m_Steps[col, row
+
1
].Color
==
currentColor
&&
m_Steps[col, row
+
1
].IsDead
==
false
)
{
m_DeadPos.Add(
new
Pos(col, row
+
1
));
m_Steps[col, row
+
1
].IsDead
=
true
;
if
(
!
CanEatLeft(col, row
+
1
, currentColor))
{
return
false
;
}
if
(
!
CanEatRight(col, row
+
1
, currentColor))
{
return
false
;
}
if
(
!
CanEatDown(col, row
+
1
, currentColor))
{
return
false
;
}
}
}
return
true
;
}