-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversi.cpp
More file actions
163 lines (153 loc) · 4.17 KB
/
Copy pathreversi.cpp
File metadata and controls
163 lines (153 loc) · 4.17 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
#include <qlist.h>
#include <cstdlib>
#include "reversi.h"
#define DATA(point) data.pos[(point).y()][(point).x()]
void Reversi::reset(void)
{
BoardGame::reset();
data.pos[3][3] = White;
data.pos[4][4] = White;
data.pos[3][4] = Black;
data.pos[4][3] = Black;
}
int Reversi::check(const QPoint& point, const QPoint& direction, const int player) const
{
int cnt;
if ((cnt = count(point + direction, direction, !player)) != 0) {
cnt++;
if (pointValid(point + cnt * direction) && DATA(point + cnt * direction) == player)
return cnt - 1;
}
return 0;
}
bool Reversi::available(const QPoint& point, const int player) const
{
if (DATA(point) != -1)
return false;
for (int d = 1; d > -2; d -= 2) {
QPoint direction;
direction = QPoint(1, 0) * d;
if (check(point, direction, player))
return true;
direction = QPoint(1, 1) * d;
if (check(point, direction, player))
return true;
direction = QPoint(0, 1) * d;
if (check(point, direction, player))
return true;
direction = QPoint(-1, 1) * d;
if (check(point, direction, player))
return true;
}
return false;
}
QPointArray Reversi::availableSpaces(const int player) const
{
QPointArray spaces;
for (int r = 0; r < size().height(); r++)
for (int c = 0; c < size().width(); c++)
if (available(QPoint(c, r), player))
spaces.putPoints(spaces.size(), 1, c, r);
return spaces;
}
void Reversi::reverse(const QPoint& point, const QPoint& direction, const int cnt, const int player)
{
QPoint p(point);
for (int c = cnt + 1; c--; p += direction)
DATA(p) = player;
}
bool Reversi::place(const QPoint& point, const int player)
{
if (DATA(point) != -1)
return false;
for (int d = 1; d > -2; d -= 2) {
int cnt;
QPoint direction;
direction = QPoint(1, 0) * d;
if ((cnt = check(point, direction, player)) != 0)
reverse(point, direction, cnt, player);
direction = QPoint(1, 1) * d;
if ((cnt = check(point, direction, player)) != 0)
reverse(point, direction, cnt, player);
direction = QPoint(0, 1) * d;
if ((cnt = check(point, direction, player)) != 0)
reverse(point, direction, cnt, player);
direction = QPoint(-1, 1) * d;
if ((cnt = check(point, direction, player)) != 0)
reverse(point, direction, cnt, player);
}
if (DATA(point) == -1)
return false;
return true;
}
int Reversi::win(void)
{
bool spaces = false;
int cnt[playerCount()];
for (int i = 0; i < playerCount(); i++)
cnt[i] = 0;
for (int r = 0; r < size().height(); r++)
for (int c = 0; c < size().width(); c++) {
if (data.pos[r][c] == -1) {
spaces = true;
continue;
}
cnt[data.pos[r][c]]++;
}
if (cnt[Black] == 0)
return White;
if (cnt[White] == 0)
return Black;
if (spaces)
return -1;
if (cnt[Black] == cnt[White])
return -2;
return cnt[Black] > cnt[White] ? Black : White;
}
int Reversi::reversible(const QPoint& point, const int player)
{
int cnt = 0;
for (int d = 1; d > -2; d -= 2) {
QPoint direction;
direction = QPoint(1, 0) * d;
cnt += check(point, direction, player);
direction = QPoint(1, 1) * d;
cnt += check(point, direction, player);
direction = QPoint(0, 1) * d;
cnt += check(point, direction, player);
direction = QPoint(-1, 1) * d;
cnt += check(point, direction, player);
}
return cnt;
}
QPoint Reversi::ai(const int player)
{
const int choices[8][8] = {
{100, -100, 50, 30, 30, 50, -100, 100,},
{-100, -100, -50, -50, -50, -50, -100, -100,},
{50, -50, 10, 10, 10, 10, -50, 50,},
{30, -50, 10, 0, 0, 10, -50, 30,},
{30, -50, 10, 0, 0, 10, -50, 30,},
{50, -50, 10, 10, 10, 10, -50, 50,},
{-100, -100, -50, -50, -50, -50, -100, -100,},
{100, -100, 50, 30, 30, 50, -100, 100,},
};
QPointArray spaces = availableSpaces(player);
if (spaces.size() == 0)
return BoardGame::ai(player);
int values[spaces.size()];
for (unsigned int i = 0; i < spaces.size(); i++) {
QPoint p(spaces[i]);
values[i] = choices[p.y()][p.x()] + reversible(p, player) * 5;
}
int max = 0;
for (unsigned int i = 0; i < spaces.size(); i++)
if (values[i] > values[max])
max = i;
QList<int> equals;
equals.setAutoDelete(true);
for (unsigned int i = 0; i < spaces.size(); i++)
if (values[i] == values[max])
equals.append(new int(i));
return spaces[*equals.at(rand() % equals.count())];
}