-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.cpp
More file actions
102 lines (81 loc) · 2.31 KB
/
evaluation.cpp
File metadata and controls
102 lines (81 loc) · 2.31 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
#include "evaluation.hpp"
#include "patterns.hpp"
#include "Pattern.hpp"
#include <string>
#include <iostream>
int evaluate(move m){
int score = 0;
Board position = board_after_move(m);
U64 *bitboards = position.get_bitboards();
int side = m.side;
int won = hasWon(bitboards[side]);
if(won == 1){
score = 10000;
}
else{
score += determineScore(bitboards, side);
score -= determineScore(bitboards, 1 - side);
}
return score;
}
int hasWon(U64 bitboard){
for (int i = 0; i < 4; i++){
if( pattern_match(bitboard, winning_patterns[i]) > 0){
return 1;
}
}
return 0;
}
int determineScore(U64 *bitboards, int side){
U64 playingBB = bitboards[side];
U64 opponentBB = bitboards[1-side];
int score = 0;
// For a straight four : win at the next round
for (int i = 0; i < 4; i++){
score += 50 * pattern_match(playingBB, patterns_length_4_playing[i], opponentBB, patterns_length_4_opponent_v1[i]);
score += 50 * pattern_match(playingBB, patterns_length_4_playing[i], opponentBB, patterns_length_4_opponent_v2[i]);
}
// For a broken four : may win at the next round
for (int i = 0; i < 12; i++){
score += 50 * pattern_match(playingBB, patterns_length_4_broken[i], opponentBB, patterns_length_4_broken_opponent[i]);
}
// For a straight three : win in two plays
for (int i = 0; i < 4; i++){
score += 30 * pattern_match(playingBB, patterns_length_3[i], opponentBB, patterns_length_3_opponent[i]);
}
// For a broken three
for (int i = 0; i < 8; i++){
score += 30 * pattern_match(playingBB, patterns_length_3_broken[i]);
}
// For two aligned
for (int i = 0; i < 4; i++){
score += 5 * pattern_match(playingBB, patterns_length_2[i]);
}
// For single or isolated points
U64 index = 0x1Ull;
for(int i = 0; i < 63; i++){
if((U64)(index & playingBB) == index)
score += value_coef[i];
index <<=1;
}
return score;
}
int is_game_over(Board board){
U64* bitboards = board.get_bitboards();
if(hasWon(bitboards[WHITE])){
board.print_board();
std::cout << "O's won the game, congratulations !" << std::endl;
return 1;
}
if(hasWon(bitboards[BLACK])){
board.print_board();
std::cout << "X's won the game, congratulations !" << std::endl;
return 1;
}
if(~bitboards[3] == 1ULL){
board.print_board();
std::cout << "The game is a draw." << std::endl;
return 1;
}
return 0;
}