-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pawn.cs
163 lines (137 loc) · 5.37 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.Text;
namespace ChessExample
{
public class Pawn : Piece
{
public bool WasMoved
{
get
{
return Color == PieceColor.BLACK ? (Position.Row > 1) : (Position.Row < 6);
}
}
public Pawn(ChessLogic logic, PieceColor color) : base(logic, color)
{
}
public Pawn(ChessLogic logic, Cell position, PieceColor color) : base(logic, position, color)
{
}
internal override void GenerateMoveList(List<Cell> moveList, bool checkCheck)
{
Cell src = Position; // agora sei onde estou
// verifica se tem movimento (sem captura)
int delta = Color == PieceColor.WHITE ? -1 : 1;
Cell dst = new Cell(src.Row + delta, src.Col);
if (logic.IsEmpty(dst)) // verifica se pode andar uma casa
{
CheckCheckAndAdd(moveList, src, dst, checkCheck);
dst = new Cell(src.Row + 2 * delta, src.Col);
if (!WasMoved && logic.IsEmpty(dst)) // verifica se pode andar duas casas
CheckCheckAndAdd(moveList, src, dst, checkCheck);
}
// verifica possíveis capturas
dst = new Cell(src.Row + delta, src.Col + 1);
if (logic.IsValidPosition(dst))
{
Piece other = logic.GetPiece(dst); // obtém a peça que está na posição de destino
if (other != null && IsEnemy(other)) // verifica se é uma peça inimiga
CheckCheckAndAdd(moveList, src, dst, checkCheck);
}
dst = new Cell(src.Row + delta, src.Col - 1);
if (logic.IsValidPosition(dst))
{
Piece other = logic.GetPiece(dst); // obtém a peça que está na posição de destino
if (other != null && IsEnemy(other)) // verifica se é uma peça inimiga
CheckCheckAndAdd(moveList, src, dst, checkCheck);
}
// verifica se é possível realizar o en passant
if (logic.pawnWalkedTwoRows)
switch (Color)
{
case PieceColor.WHITE:
if (src.Row == 3)
{
dst = new Cell(src.Row - 1, src.Col - 1);
if (logic.pawnCol == src.Col - 1)
CheckCheckAndAdd(moveList, src, dst, checkCheck);
dst = new Cell(src.Row - 1, src.Col + 1);
if (logic.pawnCol == src.Col + 1)
CheckCheckAndAdd(moveList, src, dst, checkCheck);
}
break;
case PieceColor.BLACK:
if (src.Row == 4)
{
dst = new Cell(src.Row + 1, src.Col - 1);
if (logic.pawnCol == src.Col - 1)
CheckCheckAndAdd(moveList, src, dst, checkCheck);
dst = new Cell(src.Row + 1, src.Col + 1);
if (logic.pawnCol == src.Col + 1)
CheckCheckAndAdd(moveList, src, dst, checkCheck);
}
break;
}
}
internal override void UnsafeMove(Cell position)
{
Cell src = Position;
bool enPassant = false;
if (Math.Abs(position.Col - src.Col) == 1) // é uma captura
{
Piece piece = logic.GetPiece(position);
if (piece == null)
enPassant = true;
}
base.UnsafeMove(position);
if (enPassant)
{
Cell captured = new Cell(src.Row, position.Col);
Piece other = logic.board[captured.Row, captured.Col];
if (other != null)
other.RemoveFromBoard();
logic.board[captured.Row, captured.Col] = null;
}
logic.pawnWalkedTwoRows = Math.Abs(position.Row - src.Row) == 2;
logic.pawnCol = position.Col;
if (Color == PieceColor.WHITE)
{
if (position.Row == 0)
logic.pawnToPromote = this;
}
else
{
if (position.Row == 7)
logic.pawnToPromote = this;
}
}
private void PromoteTo(Piece piece)
{
piece.SetPosition(Position);
logic.pawnToPromote = null;
logic.SwapColor();
logic.GenerateMoveList();
}
public void PromoteToRook()
{
if (logic.PawnToPromote == this)
PromoteTo(new Rook(logic, Color));
}
public void PromoteToKnight()
{
if (logic.PawnToPromote == this)
PromoteTo(new Knight(logic, Color));
}
public void PromoteToBishop()
{
if (logic.PawnToPromote == this)
PromoteTo(new Bishop(logic, Color));
}
public void PromoteToQueen()
{
if (logic.PawnToPromote == this)
PromoteTo(new Queen(logic, Color));
}
}
}