-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pawn.cs
163 lines (141 loc) · 6.42 KB
/
Pawn.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiniChess
{
class Pawn : ChessPiece,ICloneable
{
public Pawn(Point location, bool color) : base(location, color)
{
if (color == false)
{ Image = Properties.Resources.BlackPawn; }
if (color == true)
{ Image = Properties.Resources.WhitePawn; }
}
public new object Clone()
{
Pawn clone = (Pawn)base.Clone();
return clone;
}
protected override List<Point> CalculateMoves(List<ChessPiece> chessPieces)
{
possibleMoves.Clear();
//Console.WriteLine(hasMoved + " " + Color);
//黑子
if (Color == false)
{
//初始可以走2步或者1步
if (hasMoved == false)
{
if (Location.X - 1 >= 0 && this.findChessPiece(new Point(Location.X - 1, Location.Y), chessPieces) == null)
{
possibleMoves.Add(new Point(Location.X - 1, Location.Y));
if (Location.X - 2 >= 0 && this.findChessPiece(new Point(Location.X - 2, Location.Y), chessPieces) == null)
{
possibleMoves.Add(new Point(Location.X - 2, Location.Y));
}
}
}
//移动过,可以走1步
if (hasMoved == true)
{
if (Location.X - 1 >= 0 && this.findChessPiece(new Point(Location.X - 1, Location.Y), chessPieces) == null)
{
possibleMoves.Add(new Point(Location.X - 1, Location.Y));
}
}
//寻找有无可吃的子
ChessPiece possibleTarget = this.findChessPiece(new Point(Location.X - 1, Location.Y + 1), chessPieces);
if (Location.X - 1 >= 0 && Location.Y + 1 < 8 && possibleTarget != null && possibleTarget.getColor() != Color)
{
possibleMoves.Add(new Point(Location.X - 1, Location.Y + 1));
}
possibleTarget = this.findChessPiece(new Point(Location.X - 1, Location.Y - 1), chessPieces);
if (Location.X - 1 >= 0 && Location.Y - 1 >= 0 && possibleTarget != null && possibleTarget.getColor() != Color)
{
possibleMoves.Add(new Point(Location.X - 1, Location.Y - 1));
}
}
//白子,同理
else
{
if (hasMoved == false)
{
if (Location.X + 1 < 8 && this.findChessPiece(new Point(Location.X + 1, Location.Y), chessPieces) == null)
{
possibleMoves.Add(new Point(Location.X + 1, Location.Y));
if (Location.X + 2 < 8 && this.findChessPiece(new Point(Location.X + 2, Location.Y), chessPieces) == null)
{
possibleMoves.Add(new Point(Location.X + 2, Location.Y));
}
}
}
if (hasMoved == true)
{
if (Location.X + 1 < 8 && this.findChessPiece(new Point(Location.X + 1, Location.Y), chessPieces) == null)
{
possibleMoves.Add(new Point(Location.X + 1, Location.Y));
}
}
ChessPiece possibleTarget = this.findChessPiece(new Point(Location.X + 1, Location.Y + 1), chessPieces);
if (Location.X + 1 < 8 && Location.Y + 1 < 8 && possibleTarget != null && possibleTarget.getColor() != Color)
{
possibleMoves.Add(new Point(Location.X + 1, Location.Y + 1));
}
possibleTarget = this.findChessPiece(new Point(Location.X + 1, Location.Y - 1), chessPieces);
if (Location.X + 1 < 8 && Location.Y - 1 >= 0 && possibleTarget != null && possibleTarget.getColor() != Color)
{
possibleMoves.Add(new Point(Location.X + 1, Location.Y - 1));
}
}
return possibleMoves;
}
protected override List<Point> addExtraMoves(List<ChessPiece> chessPieces)
{
//吃过路兵
ChessPiece tempChessPiece = null;
if(Color && Location.X == 4)
{
if(Location.Y+1<8)
{
tempChessPiece = findChessPiece(new Point(4, Location.Y + 1), chessPieces);
if(tempChessPiece != null && tempChessPiece.getColor() != Color && tempChessPiece is Pawn && tempChessPiece.getLocationLast().X == 6)
{
possibleMoves.Add(new Point(5, Location.Y + 1));
}
}
if (Location.Y - 1 >= 0)
{
tempChessPiece = findChessPiece(new Point(4, Location.Y - 1), chessPieces);
if (tempChessPiece != null && tempChessPiece.getColor() != Color && tempChessPiece is Pawn && tempChessPiece.getLocationLast().X == 6)
{
possibleMoves.Add(new Point(5, Location.Y - 1));
}
}
}
else if (!Color && Location.X == 3)
{
if (Location.Y + 1 < 8)
{
tempChessPiece = findChessPiece(new Point(3, Location.Y + 1), chessPieces);
if (tempChessPiece != null && tempChessPiece.getColor() != Color && tempChessPiece is Pawn && tempChessPiece.getLocationLast().X == 1)
{
possibleMoves.Add(new Point(2, Location.Y + 1));
}
}
if (Location.Y - 1 >= 0)
{
tempChessPiece = findChessPiece(new Point(3, Location.Y - 1), chessPieces);
if (tempChessPiece != null && tempChessPiece.getColor() != Color && tempChessPiece is Pawn && tempChessPiece.getLocationLast().X == 1)
{
possibleMoves.Add(new Point(2, Location.Y - 1));
}
}
}
return possibleMoves;
}
}
}