Notice
Recent Posts
Recent Comments
NeuroWhAI의 잡블로그
[Algorithm] Minimax - Tic Tac Toe 본문
틱택토 인공지능 입니다.
https://www.neverstopbuilding.com/blog/2013/12/13/tic-tac-toe-understanding-the-minimax-algorithm13/
이거 보고 공부했습니당.
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
|
#include <iostream> #include <vector>
using namespace std;
enum class Pawn
{
None,
O,
X
};
class Game
{
public:
Game()
: m_turn(Pawn::O)
{
for (int y = 0; y<3; ++y)
{
for (int x = 0; x<3; ++x)
{
m_board[y][x] = Pawn::None;
}
}
}
public:
void move(int x, int y)
{
m_board[y][x] = m_turn;
if (m_turn == Pawn::O)
{
m_turn = Pawn::X;
}
else
{
m_turn = Pawn::O;
}
}
bool canMove(int x, int y) const
{
return (m_board[y][x] == Pawn::None);
}
Pawn turn() const
{
return m_turn;
}
bool isFull() const
{
for (int y = 0; y<3; ++y)
{
for (int x = 0; x<3; ++x)
{
if (m_board[y][x] == Pawn::None)
{
return false;
}
}
}
return true;
}
Pawn whoWin() const
{
for (int i = 0; i<3; ++i)
{
if (m_board[i][0] == m_board[i][1] && m_board[i][1] == m_board[i][2])
{
return m_board[i][0];
}
if (m_board[0][i] == m_board[1][i] && m_board[1][i] == m_board[2][i])
{
return m_board[0][i];
}
}
if (m_board[0][0] == m_board[1][1] && m_board[1][1] == m_board[2][2])
{
return m_board[0][0];
}
if (m_board[2][0] == m_board[1][1] && m_board[1][1] == m_board[0][2])
{
return m_board[2][0];
}
return Pawn::None;
}
void autoAI()
{
minimax(0);
}
public:
void print() const
{
cout << "---" << endl;
for (int y = 0; y<3; ++y)
{
for (int x = 0; x<3; ++x)
{
switch (m_board[y][x])
{
case Pawn::O:
cout << "O";
break;
case Pawn::X:
cout << "X";
break;
default:
cout << " ";
break;
}
}
cout << endl;
}
cout << "---" << endl;
}
private:
Pawn m_board[3][3];
Pawn m_turn;
private:
int minimax(int depth)
{
Pawn winner = whoWin();
if (winner == Pawn::O)
{
return depth - 10;
}
else if (winner == Pawn::X)
{
return 10 - depth;
}
else if (isFull())
{
return 0;
}
int maxScore = -10000000;
int maxX = -1, maxY = -1;
int minScore = 10000000;
int minX = -1, minY = -1;
for (int y = 0; y<3; ++y)
{
for (int x = 0; x<3; ++x)
{
if (canMove(x, y))
{
Game newGame = *this;
newGame.move(x, y);
int score = newGame.minimax(depth + 1);
if (score > maxScore)
{
maxScore = score;
maxX = x;
maxY = y;
}
if (score < minScore)
{
minScore = score;
minX = x;
minY = y;
}
}
}
}
if (turn() == Pawn::X)
{
move(maxX, maxY);
return maxScore;
}
else
{
move(minX, minY);
return minScore;
}
return 0;
}
};
int main()
{
Game game;
while (game.isFull() == false)
{
game.print();
int x = 0, y = 0;
cin >> x >> y;
if (game.canMove(x, y))
{
game.move(x, y);
game.autoAI();
Pawn winner = game.whoWin();
if (winner == Pawn::O)
{
cout << "Win!" << endl;
break;
}
else if (winner == Pawn::X)
{
cout << "Lose!" << endl;
break;
}
}
}
cout << "Game Over!" << endl;
return 0;
}
|
cs |
'개발 및 공부 > 알고리즘' 카테고리의 다른 글
[Rust] 퍼셉트론 (0) | 2018.07.13 |
---|---|
[Keras] Attention 매커니즘 간단한 예제 (0) | 2018.06.02 |
[C++] Q-Learning : Frozen Lake 코드 (0) | 2018.06.02 |
알고리즘 문제 풀땐 왠만하면 전용 입출력 라이브러리를 사용해야 할듯 (0) | 2018.02.24 |
[Rust] 경사 하강법 (0) | 2018.01.03 |
Comments