-
Notifications
You must be signed in to change notification settings - Fork 0
/
MancalaAI.h
194 lines (171 loc) · 3.85 KB
/
MancalaAI.h
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
//Author: Kyle Cummins
//Description: Code for basic AI opponents for Mancala game
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#include "MCTSAI.h"
#include "QLearningAI.h"
int aiSize;
int aiHalf;
int aiSeeds;
//Agent that randomly makes a choice
int randomAgent(int player, int *aiBoard)
{
int choice;
int start = (player == 1 ? 0 : aiHalf);
int choiceGot = 0;
while(!choiceGot)
{
choice = rand() % (aiHalf - 1);
if(aiBoard[start + (choice)] != 0)
{
choiceGot = 1;
}
}
//printf("%d", choice);
return choice + 1;
}
//Agent that attempts to maximize its single turn score
int maxAgent(int player, int *aiBoard)
{
int rightBest, rightGot = 0;
int takeBest = 0, takeNum = 0;
int start = (player == 1 ? 0 : aiHalf);
int i;
for(i = start + (aiHalf - 2); i >= start; --i)
{
if(aiBoard[i])
{
if(!rightGot)
{
rightBest = (i - start) + 1;
rightGot = 1;
}
//Record if pieces may be taken.
if(((player == 1 && (i + aiBoard[i]) % aiSize < aiHalf - 1) || (player == 2 && (i + aiBoard[i]) % aiSize < aiSize - 1 && (i + aiBoard[i]) % aiSize > aiHalf - 1)) && aiBoard[(i + aiBoard[i]) % aiSize] == 0)
{
if(aiBoard[(((aiSize - 2) - (i + aiBoard[i])) % aiSize)] > takeNum)
{
takeNum = aiBoard[(((aiSize - 2) - (i + aiBoard[i])) % aiSize)];
takeBest = (i - start) + 1;
}
}
}
}
if(takeBest)
{
return takeBest;
}
return rightBest;
}
//Agent that attempts to maximize its number of turns, then maximize its score
int expertAgent(int player, int *aiBoard)
{
int rightBest, rightGot = 0;
int takeBest = 0, takeNum = 0;
int start = (player == 1 ? 0 : aiHalf);
int i;
for(i = start + (aiHalf - 2); i >= start; --i)
{
if(aiBoard[i])
{
if(!rightGot)
{
rightBest = (i - start) + 1;
rightGot = 1;
}
//If agent can take another turn, do so
if((player == 1 && (i + aiBoard[i]) % aiSize == aiHalf - 1) || (player == 2 && (i + aiBoard[i]) % aiSize == aiSize - 1))
{
return (i - start) + 1;
}
//Record if pieces may be taken.
if(((player == 1 && (i + aiBoard[i]) % aiSize < aiHalf - 1) || (player == 2 && (i + aiBoard[i]) % aiSize < aiSize - 1 && (i + aiBoard[i]) % aiSize > aiHalf - 1)) && aiBoard[(i + aiBoard[i]) % aiSize] == 0)
{
if(aiBoard[(((aiSize - 2) - (i + aiBoard[i])) % aiSize)] > takeNum)
{
takeNum = aiBoard[(((aiSize - 2) - (i + aiBoard[i])) % aiSize)];
takeBest = (i - start) + 1;
}
}
}
}
if(takeBest)
{
return takeBest;
}
return rightBest;
}
//Returns one of a variety of utility calculations
float utility(int player, int type, int* aiBoard)
{
if(player == 1)
{
switch(type)
{
case 0:
return aiBoard[aiHalf - 1];
break;
case 1:
return aiBoard[aiHalf - 1] - aiBoard[aiSize - 1];
break;
case 2:
if(aiBoard[aiSize - 1] == 0)
{
return aiBoard[aiHalf - 1];
}
return (float)aiBoard[aiHalf - 1] / (float)aiBoard[aiSize - 1];
break;
}
}
else
{
switch(type)
{
case 0:
return aiBoard[aiSize - 1];
break;
case 1:
return aiBoard[aiSize - 1] - aiBoard[aiHalf - 1];
break;
case 2:
if(aiBoard[aiHalf - 1] == 0)
{
return aiBoard[aiSize - 1];
}
return (float)aiBoard[aiSize - 1] / (float)aiBoard[aiHalf - 1];
break;
}
}
}
//Initializes agent AI with relevant game settings
void agentInit(int si, int h, int se)
{
aiSize = si;
aiHalf = h;
aiSeeds = se;
//MCTSInit(si, h, se);
QInit(si, h, se);
}
//Returns input from mancala agent of selected difficulty
int agentManager(int difficulty, int player, int *aiBoard)
{
switch(difficulty)
{
case 0:
return randomAgent(player, aiBoard);
break;
case 1:
return maxAgent(player, aiBoard);
break;
case 2:
return expertAgent(player, aiBoard);
break;
case 3:
return qAgent(player, aiBoard);
break;
//case 3:
//return mcAgent(player, aiBoard);
//break;
}
}