-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBot.cs
More file actions
183 lines (140 loc) · 6.09 KB
/
MyBot.cs
File metadata and controls
183 lines (140 loc) · 6.09 KB
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
using ChessChallenge.API;
using System;
using System.Linq;
public class MyBot : IChessBot
{
private class MoveScore
{
public Move Move;
public float Score;
public MoveScore(Move move, float score)
{
Move = move;
Score = score;
}
}
public Move Think(Board board, Timer timer)
{
Move[] moves = board.GetLegalMoves();
MoveScore bestMove = new MoveScore(Move.NullMove, float.MinValue);
bool myColor = board.IsWhiteToMove;
foreach (Move move in moves)
{
board.MakeMove(move);
if (board.IsInCheckmate() || move.IsEnPassant)
{
board.UndoMove(move);
return move;
}
if (board.IsDraw())
{
board.UndoMove(move);
continue;
}
float eval = Evaluate(board, 0, 3, myColor);
if (eval > bestMove.Score)
{
bestMove = new MoveScore(move, eval);
}
board.UndoMove(move);
}
return bestMove.Move != Move.NullMove ? bestMove.Move : moves[0];
}
private float Evaluate(Board board, int depth, int maxDepth, bool myColor, float alpha = float.MinValue, float beta = float.MaxValue)
{
if (depth == maxDepth) return EvaluatePosition(board, myColor);
float bestScore = depth % 2 == 1 ? float.MinValue : float.MaxValue;
Move[] moves = board.GetLegalMoves();
foreach (Move move in moves)
{
board.MakeMove(move);
if (board.IsInCheckmate())
{
board.UndoMove(move);
return (depth % 2 == 1) ? float.MaxValue : float.MinValue;
}
if (board.IsDraw())
{
board.UndoMove(move);
continue;
}
float score = Evaluate(board, depth + 1, maxDepth, myColor, alpha, beta);
if (depth % 2 == 1) {
// my turn
bestScore = Math.Max(score, bestScore);
alpha = Math.Max(alpha, score);
} else {
bestScore = Math.Min(score, bestScore);
beta = Math.Min(beta, score);
}
if (beta <= alpha) {
board.UndoMove(move);
break;
}
board.UndoMove(move);
}
return bestScore;
}
private ulong GetAttackedTiles(Board board, bool color, ulong pieces)
{
ulong bitboard = 0;
int squareindex;
while ((squareindex = BitboardHelper.ClearAndGetIndexOfLSB(ref(pieces)))!= 64) {
Square square = new Square(squareindex);
PieceType piecetype = board.GetPiece(square).PieceType;
bitboard |= BitboardHelper.GetPieceAttacks(piecetype, square, board, color);
}
return bitboard;
}
// evaluate from POV of myColor
private float EvaluatePosition(Board board, bool myColor) {
ulong myPieces = myColor ? board.WhitePiecesBitboard : board.BlackPiecesBitboard;
ulong enemyPieces = myColor ? board.BlackPiecesBitboard : board.WhitePiecesBitboard;
ulong myAttackedTiles = GetAttackedTiles(board, myColor, myPieces);
ulong enemyAttackedTiles = GetAttackedTiles(board, !myColor, enemyPieces);
int numberMyAttackedTiles = BitboardHelper.GetNumberOfSetBits(myAttackedTiles);
int numberEnemyAttackedTiles = BitboardHelper.GetNumberOfSetBits(enemyAttackedTiles);
ulong attackedEnemyPieces = myAttackedTiles & enemyPieces;
ulong attackedMyPieces = enemyAttackedTiles & myPieces;
int valueAttackedEnemyPieces = PieceSetValue(board, attackedEnemyPieces);
int valueAttackedMyPieces = PieceSetValue(board, attackedMyPieces);
int valueMyPieces = PieceSetValue(board, myPieces & ~enemyAttackedTiles) + valueAttackedMyPieces;
int valueEnemyPieces = PieceSetValue(board, enemyPieces & ~myAttackedTiles) + valueAttackedEnemyPieces;
int numberUnprotectedPieces = BitboardHelper.GetNumberOfSetBits(myPieces & ~myAttackedTiles);
int numberUnprotectedEnemyPieces = BitboardHelper.GetNumberOfSetBits(enemyPieces & ~enemyAttackedTiles);
int numberMyMoves = board.GetLegalMoves().Count();
int numberEnemyMoves = board.GetLegalMoves(!myColor).Count();
ulong myrank7 = (ulong)127 << (myColor ? 48 : 8);
int pushedPawns = BitboardHelper.GetNumberOfSetBits(myPieces & myrank7);
Square myKingPosition = board.GetKingSquare(myColor);
int homeRank = myColor ? 0 : 7;
int myKingDistance = Math.Abs(myKingPosition.Rank - homeRank);
Square enemyKingPosition = board.GetKingSquare(!myColor);
int enemyHomeRank = myColor ? 7 : 0;
int enemyKingDistance = Math.Abs(enemyKingPosition.Rank - enemyHomeRank);
//i made these numbers up there is 0 actual chess knowledge involved
return 5 * (valueMyPieces - valueEnemyPieces) + valueAttackedEnemyPieces - valueAttackedMyPieces +
numberUnprotectedEnemyPieces - numberUnprotectedPieces + numberMyMoves - numberEnemyMoves +
pushedPawns + 2 * (numberMyAttackedTiles - numberEnemyAttackedTiles) + 5 * (myKingDistance - enemyKingDistance);
}
private int PieceSetValue(Board board, ulong bitboard)
{
int sum = 0;
int squareindex;
while ((squareindex = BitboardHelper.ClearAndGetIndexOfLSB(ref(bitboard)))!= 64) {
Square square = new Square(squareindex);
sum += PieceValue(board.GetPiece(square).PieceType);
}
return sum;
}
private int PieceValue(PieceType type) {
return type switch {
PieceType.Pawn => 1,
PieceType.Knight => 3,
PieceType.Bishop => 3,
PieceType.Rook => 5,
PieceType.Queen => 9,
PieceType.King => 100
};
}
}