-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.cpp
89 lines (80 loc) · 2.89 KB
/
ai.cpp
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
#include "ai.h"
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
pair<int, int> minimax(long long bitboard[], int height[], int alpha, int beta, int depth, bool isMax)
{
vector<int> moves = listMoves(height);
if(moves.empty() || depth == 0 || isWin(bitboard[0]) || isWin(bitboard[1]))
return make_pair(-1, -scoreBoard(bitboard));
if(isMax)
{
int score = INT_MIN;
int column = moves[rand() % moves.size()];
for(int col : moves)
{
long long c_bitboard[2] = {bitboard[0], bitboard[1]};
int c_height[7] = {height[0], height[1], height[2], height[3], height[4], height[5], height[6]};
long long m = 1LL << c_height[col]++;
c_bitboard[1] ^= m;
int new_score = minimax(c_bitboard, c_height, alpha, beta, depth-1, 0).second - 1;
if(new_score > score)
{
score = new_score;
column = col;
}
alpha = max(alpha, score);
if(alpha >= beta)
break;
}
return make_pair(column, score);
}
else
{
int score = INT_MAX;
int column = moves[rand() % moves.size()];
for(int col : moves)
{
long long c_bitboard[2] = {bitboard[0], bitboard[1]};
int c_height[7] = {height[0], height[1], height[2], height[3], height[4], height[5], height[6]};
long long m = 1LL << c_height[col]++;
c_bitboard[0] ^= m;
int new_score = minimax(c_bitboard, c_height, alpha, beta, depth-1, 1).second + 1;
if(new_score < score)
{
score = new_score;
column = col;
}
beta = min(beta, score);
if(alpha >= beta)
break;
}
return make_pair(column, score);
}
}
int scoreBoard(long long bitboard[])
{
if(isWin(bitboard[0]))
return M1;
if(isWin(bitboard[1]))
return -M1;
long long border = -279258638311360LL;
long long free = ~(bitboard[0] | bitboard[1] | border);
int score = 0;
long long directions[] = {1, 7, 6, 8};
for(int i = 0; i < 2; i++)
{
for(int direction : directions)
{
long long x = free << direction, y = free >> direction;
long long a1 = (bitboard[i] & (bitboard[i] >> direction) & (bitboard[i] >> (2LL * direction))) & x;
long long a2 = (bitboard[i] & (bitboard[i] << direction) & (bitboard[i] << (2LL * direction))) & y;
long long a = a1 | a2;
long long b1 = ((bitboard[i] & ~a) & ((bitboard[i] & ~a) >> direction)) & x;
long long b2 = ((bitboard[i] & ~a) & ((bitboard[i] & ~a) << direction)) & y;
long long b = b1 | b2;
score += (M3 * __builtin_popcountll(a) + M2 * __builtin_popcountll(b)) * (i ? -1 : 1);
}
}
return score;
}