-
Notifications
You must be signed in to change notification settings - Fork 0
/
NormalAI.cpp
108 lines (106 loc) · 2.96 KB
/
NormalAI.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "NormalAI.hpp"
NormalAIPlayer::NormalAIPlayer()
{
name = L"Robot";
finished = false;
}
void NormalAIPlayer::makeMove(std::vector<int> &positions, int id, int delta)
{
// moves ranks
int ranks[4] = {4, 4, 4, 4};
// check players pieces
for (int i = 0; i < 4; i++)
{
// is in holder
if (positions[id * 4 + i] == -1)
{
// dice 6
if (delta == 6)
{
// own pieces on position 0
if (positions[id * 4] == 0 || positions[id * 4 + 1] == 0 || positions[id * 4 + 2] == 0 || positions[id * 4 + 3] == 0)
{
ranks[i] = 0;
}
else
{
ranks[i] = 6;
}
}
else
{
ranks[i] = 0;
}
}
// after move beyond board
else if (positions[id * 4 + i] + delta > 43)
{
ranks[i] = 0;
}
else
{
// move from 0 position
if (positions[id * 4 + i] == 0)
{
ranks[i] = 5;
}
// collision with other pieces
for (int j = 0; j < 16; j++)
{
// not in holder and home
if (positions[j] != -1 && positions[j] < 40 && positions[id * 4 + i] < 40 && (positions[id * 4 + i] + delta + id * 10) % 40 == (positions[j] + (j / 4) * 10) % 40)
{
// own piece
if (j / 4 == id)
{
ranks[i] = 0;
break;
}
// other player's piece
else
{
ranks[i] = 10;
break;
}
}
// own collision in home
else if (j / 4 == id && positions[id * 4 + i] + delta == positions[j])
{
ranks[i] = 0;
}
}
}
}
int move = 0;
// choose best
for (int i = 1; i < 4; i++)
{
if (ranks[move] < ranks[i])
move = i;
}
// move
if (positions[id * 4 + move] == -1)
{
for (int i = 0; i < 16; i++)
{
if (i / 4 != id && positions[i] != -1 && positions[i] < 40 && (positions[i] + (i / 4) * 10) % 40 == (id * 10) % 40)
{
positions[i] = -1;
break;
}
}
positions[id * 4 + move] = 0;
}
else
{
for (int i = 0; i < 16; i++)
{
if (positions[i] != -1 && positions[i] < 40 && positions[id * 4 + move] + delta < 40 && (positions[id * 4 + move] + delta + id * 10) % 40 == (positions[i] + (i / 4) * 10) % 40)
{
positions[i] = -1;
break;
}
}
positions[id * 4 + move] += delta;
}
}