-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.c
265 lines (221 loc) · 6.89 KB
/
minimax.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include "tictactoe.h"
#define CLEAR() printf("\033[H\033[J")
/* A quick and non optimized version of minimax for tictactoe */
void displayGame(gData *p, char player);
struct movesInfo{
int index;
int score;
};
/* Set whom is whom */
char aiPlayer;
char bioPlayer;
int isWinning(char *gameState, char currPlayer);
int returnAvailableMoves();
int miniMax(char *gameState, char player, struct movesInfo *moves);
int selectBestMove(struct movesInfo *moves);
int main(void)
{
srand(time(NULL));
char name[] = "/gameState";
int smfd = shm_open (name, O_RDWR, 0660);
if(smfd == -1){
perror("Unable to open shared memory\n");
return 1;
}
gData *p;
p = mmap(NULL, sizeof(gData), PROT_WRITE, MAP_SHARED, smfd, 0);
if(p < 0){
perror("mmap failed.\n");
return 1;
}
printf("p->game read from master process = %s\n", p->game);
char player;
printf("Trying to be player X..\n");
if(p->playerX == '*'){
p->playerX = 'X';
player = 'X';
}else if(p->playerO == '*'){
p->playerO = 'O';
player = 'O';
}else{
printf("No available player slots\n");
return 1;
}
printf("This instance is player %c\n", player);
aiPlayer = player;
if(aiPlayer == 'O')
bioPlayer = 'X';
else
bioPlayer = 'O';
/* Display once, save some terminal cpu */
int displayed = 0;
/* Main loop */
while(1){
char nextMove[10];
if(p->playerTurn == player && p->nextMove[0] == '*'){
displayGame(p, player);
char gameState[10];
memcpy(gameState, p->game, 9);
gameState[9] = '\0';
struct movesInfo moves[9];
/* Truncate moves.. */
int j;
for(j = 0; j < 9; j++){
moves[j].index = -1;
moves[j].score = -1;
}
/* Run minimax */
miniMax(gameState, aiPlayer, moves);
/* Select best move */
int bestMove = selectBestMove(moves);
/* Convert move to human readable form */
if(bestMove == 0)
memcpy(nextMove, "a1", 2);
else if(bestMove == 1)
memcpy(nextMove, "b1", 2);
else if(bestMove == 2)
memcpy(nextMove, "c1", 2);
else if(bestMove == 3)
memcpy(nextMove, "a2", 2);
else if(bestMove == 4)
memcpy(nextMove, "b2", 2);
else if(bestMove == 5)
memcpy(nextMove, "c2", 2);
else if(bestMove == 6)
memcpy(nextMove, "a3", 2);
else if(bestMove == 7)
memcpy(nextMove, "b3", 2);
else if(bestMove == 8)
memcpy(nextMove, "c3", 2);
memcpy(p->nextMove, nextMove, 3);
p->nextMove[2] = '\0';
/* Truncate moves.. */
int i;
for(i = 0; i < 9; i++){
moves[i].index = -1;
moves[i].score = -1;
}
displayed = 0;
}else{
if(displayed == 0){
displayGame(p, player);
displayed = 1;
}
}
usleep(UDELAY);
}
munmap(&p, sizeof(gData));
return 0;
}
/*returns int:
* 0: no winner (yet)
* 1: currPlayer wins (X | O)*/
int isWinning(char *gameState, char currPlayer)
{
if((gameState[0] == currPlayer && gameState[1] == currPlayer && gameState[2] == currPlayer)\
|| (gameState[3] == currPlayer && gameState[4] == currPlayer && gameState[5] == currPlayer)\
|| (gameState[6] == currPlayer && gameState[7] == currPlayer && gameState[8] == currPlayer)\
|| (gameState[0] == currPlayer && gameState[3] == currPlayer && gameState[6] == currPlayer)\
|| (gameState[1] == currPlayer && gameState[4] == currPlayer && gameState[7] == currPlayer)\
|| (gameState[2] == currPlayer && gameState[5] == currPlayer && gameState[8] == currPlayer)\
|| (gameState[0] == currPlayer && gameState[4] == currPlayer && gameState[8] == currPlayer)\
|| (gameState[2] == currPlayer && gameState[4] == currPlayer && gameState[6] == currPlayer))
return 1;
else
return 0;
}
/* Fills array of ints with indices of available moves */
int returnAvailableMoves(char *gameState, int *avMoves)
{
int i;
int j = 0;
for(i = 0; i < 9; i++){
if(gameState[i] == '*'){
avMoves[j] = i;
j++;
}
}
avMoves[j] = -1;
return j;
}
int miniMax(char *gameState, char player, struct movesInfo *moves)
{
/* Return available moves */
int avMoves[9];
int i = 0;
int avMvCnt = returnAvailableMoves(gameState, avMoves);
/* Base case: return terminal gameState; win, lose, tie */
if(isWinning(gameState, bioPlayer))
return 1;
else if(isWinning(gameState, aiPlayer))
return -1;
else if(avMvCnt == 0)
return 0;
else if(avMvCnt == 9){
int rnd = rand() % 9;
moves[avMoves[i]].index = rnd;
moves[avMoves[i]].score = 1000;
return 0;
}
/* Loop over all available moves */
while(avMoves[i] != -1)
{
/* Store the index in movesInfo struct array */
moves[avMoves[i]].index = avMoves[i];
/* Make a move on gameState for curr player */
gameState[avMoves[i]] = player;
int result;
/* Recurse minimax and save scores
* In this version, scores are added or
* substracted, resulting in best move
* with the highest score */
if(player == aiPlayer){
result = miniMax(gameState, bioPlayer, moves);
moves[avMoves[i]].score -= result;
}else{
result = miniMax(gameState, aiPlayer, moves);
moves[avMoves[i]].score += result;
}
/* Reset the move */
gameState[avMoves[i]] = '*';
i++;
}
return 0;
}
/* Returns the best move gamestate index */
int selectBestMove(struct movesInfo *moves)
{
int i;
int currBest = -1;
int bestIndex;
for(i = 0; i < 9; i++){
if(moves[i].score >= currBest && moves[i].index != -1){
bestIndex = moves[i].index;
currBest = moves[i].score;
}
}
return bestIndex;
}
void displayGame(gData *p, char player)
{
CLEAR();
printf("Minimax player: %c\n\n", player);
int i;
int j = 9;
for(i = 3; i > 0; i--){
printf("%i %c | %c | %c \n", i, p->game[j - 3], p->game[j - 2], p->game[j - 1]);
if(i > 1)
printf(" ---|---|---\n");
j -= 3;
}
printf("\n a b c\n\n");
printf("%s\n", p->bcastMsg);
}