File: brutalplayer.cpp

package info (click to toggle)
brutalchess 0.5.2+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,404 kB
  • sloc: cpp: 7,561; sh: 2,707; makefile: 73; ansic: 9
file content (362 lines) | stat: -rw-r--r-- 10,830 bytes parent folder | download | duplicates (4)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/***************************************************************************
 * Brutal Chess
 * http://brutalchess.sf.net
 *
 * File : brutalplayer.cpp
 * Authors : Mike Cook, Joe Flint, Neil Pankey
 **************************************************************************/

#include "board.h"
#include "chessplayer.h"
#include "options.h"

#include <climits>
#include <vector>
#include <time.h>

using namespace std;

BrutalPlayer::BrutalPlayer()
{
    m_ply = Options::getInstance()->brutalplayer2ply;
	m_trustworthy = true;
	srand(time(NULL));
}

void BrutalPlayer::think(const ChessGameState & cgs)
{
	BoardMove move;
    Board board = cgs.getBoard();
	search(board, getColor(), m_ply, -INT_MAX, INT_MAX, move);
	m_move = move;
}

int BrutalPlayer::search(Board board, Piece::Color color, int depth, int alpha, int beta, BoardMove& move)
{
	BoardMove testMove;
	Board testBoard = board;
	int moveScore, bestScore = -INT_MAX;
	vector<BoardMove> moves = board.possibleMoves(color);

	bool gotmove = false;

	for(int i=0; i < moves.size(); i++) {
		if(!board.isMoveLegal(moves[i])) {
			continue;
		}

		if(!gotmove) {
			move = moves[i];
			gotmove = true;
		}

		testBoard = board;
		testBoard.update(moves[i]);
	
        if(depth == 0) {
			moveScore = evaluateBoard(testBoard, color);
		} else {
			moveScore = -search(testBoard, Piece::opposite(color), depth-1, -beta, -alpha, testMove);
		}

        if(moveScore > bestScore) {
			bestScore = moveScore;
			move = moves[i];
        }
        if(bestScore > alpha) {
			alpha = bestScore;
		}
        if(alpha >= beta) {
			return beta;
		}
	}
	
	return bestScore;
}

int BrutalPlayer::evaluateBoard(const Board & board, Piece::Color turn)
{
    vector< vector<BoardPosition> > locations(Piece::LAST_TYPE+1);
    
    // Raw material balance
    int balance = 0;
    int endgamecount = 0;
            
    int queenval = 900, rookval = 500, bishopval = 325, knightval = 310, pawnval = 100, kingval = 100000;
    
    for(int i=0; i < 64; i++) {
        BoardPosition bp = BoardPosition(i);
        if(board.isOccupied(bp)) {
            Piece *p = board.getPiece(bp);
            switch(p->type()) {
                case Piece::QUEEN:
                    locations[Piece::QUEEN].push_back(bp);
                    balance += (p->color() == turn) ? queenval : -queenval;
                    endgamecount += queenval;
                    break;
                case Piece::ROOK:
                    locations[Piece::ROOK].push_back(bp);
                    balance += (p->color() == turn) ? rookval : -rookval;
                    endgamecount += rookval;
                    break;
                case Piece::BISHOP:
                    locations[Piece::BISHOP].push_back(bp);
                    balance += (p->color() == turn) ? bishopval : -bishopval;
                    endgamecount += bishopval;
                    break;
                case Piece::KNIGHT:
                    locations[Piece::KNIGHT].push_back(bp);
                    balance += (p->color() == turn) ? knightval : -knightval;
                    endgamecount += knightval;
                    break;
                case Piece::PAWN:
                    locations[Piece::PAWN].push_back(bp);
                    balance += (p->color() == turn) ? pawnval : -pawnval;
                    endgamecount += pawnval;
                    break;
                case Piece::KING:
                    locations[Piece::KING].push_back(bp);
                    if ((p->color() == turn) && !board.isCheckMate(turn)) {
                        balance += kingval;
                    }
                    else if ((p->color() != turn) && !board.isCheckMate(turn)) {
                        balance -= kingval;
                    }
                    break;
            }
        }
    }

    // Determine end game or not.  THIS IS A TWEAKABLE VALUE.
    bool endgame = false;
    if (endgamecount < 3500) {
        endgame = true;
    }

    for(int i=0; i < locations[Piece::QUEEN].size(); i++) {
        balance += queenBonus(locations[Piece::QUEEN][i], board, turn, endgame);
    }
    for(int i=0; i < locations[Piece::ROOK].size(); i++) {
        balance += rookBonus(locations[Piece::ROOK][i], board, turn, endgame);
    }
    for(int i=0; i < locations[Piece::BISHOP].size(); i++) {
        balance += bishopBonus(locations[Piece::BISHOP][i], board, turn, endgame);
    }
    for(int i=0; i < locations[Piece::KNIGHT].size(); i++) {
        balance += knightBonus(locations[Piece::KNIGHT][i], board, turn, endgame);
    }
    for(int i=0; i < locations[Piece::PAWN].size(); i++) {
        balance += pawnBonus(locations[Piece::PAWN][i], board, turn, endgame);
    }
    for(int i=0; i < locations[Piece::KING].size(); i++) {
        balance += kingBonus(locations[Piece::KING][i], board, turn, endgame);
    }

    return balance;
}

// Queen Bonuses are finished, queens get bonuses for being near an opposing king.
int BrutalPlayer::queenBonus(const BoardPosition & bp, const Board & board, Piece::Color turn, bool endgame) {
	if (!endgame) {
        return 0;
	}

    Piece * p = board.getPiece(bp);
    int bonus = 0;
	BoardMove dist;
    
    if (p->color() == Piece::WHITE) {
        dist = BoardMove(bp, board.getKing(Piece::BLACK), p);
	}
	else {
		dist = BoardMove(bp, board.getKing(Piece::WHITE), p);
	}

	bonus = -2*abs(dist.fileDiff()+dist.rankDiff());

    return (turn == p->color()) ? bonus : -bonus;
}

int BrutalPlayer::rookBonus(const BoardPosition & bp, const Board & board, Piece::Color turn, bool endgame) {
    int bonus = 0;

	// Rooks get a bonus of 0 points if blocked, or up to 20 points if
	// attacking 12 squares or more.
	unsigned long long rookAttacks = Board::rankAttacks[bp.hash()][board.getRankState(bp)] ||
                                     Board::fileAttacks[bp.hash()][board.getFileState(bp)];
    int numAttacked = numAttackedSquares(rookAttacks);
    bonus += (numAttacked < 12) ? 2*numAttacked-4 : 20;

    return bonus;
}


int BrutalPlayer::bishopBonus(const BoardPosition & bp, const Board & board, Piece::Color turn, bool endgame) {
    Piece::Color color = board.getPiece(bp)->color();
    int bonus = m_bishop[bp.hash()];

    return (turn == color) ? bonus: -bonus;
}


int BrutalPlayer::knightBonus(const BoardPosition & bp, const Board & board, Piece::Color turn, bool endgame) {
    Piece::Color color = board.getPiece(bp)->color();
    int bonus = m_knight[bp.hash()];

	bool drivenBonus = false;

	// Knights are given a bonus for being within 2 squares of each enemy piece.
	

    return (turn == color) ? bonus : -bonus;
}

int BrutalPlayer::pawnBonus(const BoardPosition & bp, const Board & board, Piece::Color turn, bool endgame) {
    Piece::Color color = board.getPiece(bp)->color();
    int bonus = (color == Piece::WHITE) ? m_wpawn[bp.hash()] : m_bpawn[bp.hash()];

	if(isIsolatedPawn(bp, board)) {
        char file = bp.filec();
        if(file == 'a' || file == 'h') {
            bonus -= 12;
        } else if (file == 'b' || file == 'g') {
            bonus -= 14;
        } else if (file == 'c' || file == 'f') {
            bonus -= 16;
        } else if (file == 'd' || file == 'e') {
            bonus -= 20;
        }
    }
	/*
    if(isDoubledPawn(bp, board)) {
    }
	*/
    return (turn == color) ? bonus : -bonus;
}

int BrutalPlayer::kingBonus(const BoardPosition & bp, const Board & board, Piece::Color turn, bool endgame) {
    Piece::Color color = board.getPiece(bp)->color();
    int bonus = 0;

    // Count all pieces attacking the king
    unsigned long long attacked = board.isAttacked(bp, color);
    for(int i=0; i < 64; i++) {
        bonus -= 25*((attacked >> i) & 1);
    }
    
    if(endgame) {
        bonus = m_end_king[bp.hash()];
    } else {
        bonus = (color == Piece::WHITE) ? m_wking[bp.hash()] : m_bking[bp.hash()];
    }
    
    return (turn == color) ? bonus : -bonus;
}

int BrutalPlayer::numAttackedSquares(const unsigned long long & pieceAttacks)
{
	int numSquares = 0;
	for (int i = 0; i < 64; i++) {
		if (pieceAttacks && (1LL << i)) {
			numSquares++;
		}
	}
	return numSquares;
}

bool BrutalPlayer::isIsolatedPawn(const BoardPosition & bp, const Board & board)
{
        Piece::Color c = board.getPiece(bp)->color();
	unsigned long long mask = board.m_color[c] & board.m_pieces[Piece::PAWN];

	BoardPosition test = bp.E();
	if(test.isValid() && (mask & maskFile(test))) {
		return false;
	}

	test = bp.W();
	if(test.isValid() && (mask & maskFile(test))) {
		return false;
	}

	return true;
}

bool BrutalPlayer::isDoubledPawn(const BoardPosition & bp, const Board & board)
{
	return false;
}

// Positional Bonuses
int BrutalPlayer::m_bishop[64] = {
    -5,-5,-5,-5,-5,-5,-5,-5,
    -5,10,5,10,10,5,10,-5,
    -5,5,3,12,12,3,5,-5,
    -5,3,12,3,3,12,3,-5,
    -5,3,12,3,3,12,3,-5,
    -5,5,3,12,12,3,5,-5,
    -5,10,5,10,10,5,10,-5,
    -5,-5,-5,-5,-5,-5,-5,-5 };

int BrutalPlayer::m_knight[64] = {
    -10,-5,-5,-5,-5,-5,-5,-10,
    -5,0,0,3,3,0,0,-5,
    -5,0,5,5,5,5,0,-5,
    -5,0,5,10,10,5,0,-5,
    -5,0,5,10,10,5,0,-5,
    -5,0,5,5,5,5,0,-5,
    -5,0,0,3,3,0,0,-5,
    -10,-5,-5,-5,-5,-5,-5,-10 };

int BrutalPlayer::m_wpawn[64] = {
    0,0,0,0,0,0,0,0,
    0,0,0,-5,-5,0,0,0,
    1,2,3,4,4,3,2,1,
    2,4,6,8,8,6,4,2,
    3,6,9,12,12,9,6,3,
    4,8,12,16,16,12,8,4,
    5,10,15,20,20,15,10,5,
    0,0,0,0,0,0,0,0 };

int BrutalPlayer::m_bpawn[64] = {
    0,0,0,0,0,0,0,0,
    5,10,15,20,20,15,10,5,
    4,8,12,16,16,12,8,4,
    3,6,9,12,12,9,6,3,
    2,4,6,8,8,6,4,2,
    1,2,3,4,4,3,2,1,
    0,0,0,-5,-5,0,0,0,
    0,0,0,0,0,0,0,0 };

int BrutalPlayer::m_wking[64] = {
    2,10,4,0,0,7,10,2,
    -3,-3,-5,-5,-5,-5,-3,-3,
    -5,-5,-8,-8,-8,-8,-5,-5,
    -8,-8,-13,-13,-13,-13,-8,-8,
    -13,-13,-21,-21,-21,-21,-13,-13,
    -21,-21,-34,-34,-34,-34,-21,-21,
    -34,-34,-55,-55,-55,-55,-34,-34,
    -55,-55,-89,-89,-89,-89,-55,-55};

int BrutalPlayer::m_bking[64] = {
    -55,-55,-89,-89,-89,-89,-55,-55,
    -34,-34,-55,-55,-55,-55,-34,-34,
    -21,-21,-34,-34,-34,-34,-21,-21,
    -13,-13,-21,-21,-21,-21,-13,-13,
    -8,-8,-13,-13,-13,-13,-8,-8,
    -5,-5,-8,-8,-8,-8,-5,-5,
    -3,-3,-5,-5,-5,-5,-3,-3,
    2,10,4,0,0,7,10,2};

int BrutalPlayer::m_end_king[64] = {
    -5,-3,-1,0,0,-1,-3,-5,
    -3,5,5,5,5,5,5,-3,
    -1,5,10,10,10,10,5,-1,
    0,5,10,15,15,10,5,0,
    0,5,10,15,15,10,5,0,
    -1,5,10,10,10,10,5,-1,
    -3,5,5,5,5,5,5,-3,
    -5,-3,-1,0,0,-1,-3,-5};

// end of file brutalplayer.cpp