-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotAI.c
425 lines (370 loc) · 9.82 KB
/
BotAI.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Globais.h"
//--------------------------------------------------------------
//returns number of cards of value "number"
int playerHasNumber(int number)
{
int i,c=0;
for(i=0;i<5;i++)
if(cardNumber[player[i]]==number)
c++;
return c;
}
//--------------------------------------------------------------
//checks if bot has number 1 for first bot play
//if exists return that position, if not return -1
int botHasNumber1()
{
int i;
for(i=0;i<5;i++)
{
if(botInfo[0][i]==1)
if(cardNumber[bot[i]]==1)
return i+1;
}
return -1;
}
//--------------------------------------------------------------
//check if bot has card whith number and color given
//return position of card or -1 if no card found
int botHasCard(int number,int color)
{
int i;
for(i=0;i<5;i++)
{
if(botInfo[0][i]==1 && botInfo[1][i]==1)
if(cardNumber[bot[i]]==number && cardColor[bot[i]]==color)
return i+1;
}
return -1;
}
//--------------------------------------------------------------
//check if player has a card to play
//return the position of that card or -1 if no card playable
int playerHasCard(int number,int color)
{
int i;
for(i=0;i<5;i++)
{
if(cardNumber[player[i]]==number && cardColor[player[i]]==color)
return i+1;
}
return -1;
}
//--------------------------------------------------------------
//count the number of cards of "int number" that player has in the hand
//and returns that amount
int countPlayerCardsNumber(int number)
{
int count=0,i;
for(i=0;i<5;i++)
if(cardNumber[player[i]]==number)
count++;
return count;
}
//--------------------------------------------------------------
//count the number of cards of "int color" that player has in hand
//and returns that amount
int countPlayerCardsColor(int color)
{
int count=0,i;
for(i=0;i<5;i++)
if(cardColor[player[i]]==color)
count++;
return count;
}
//--------------------------------------------------------------
//returns the small number with no clues on player hand
//return 6 if all player cards has a number clue
int smallPlayerNumber()
{
int i,menor=6;
for(i=0;i<5;i++)
if(cardNumber[player[i]]<menor && playerInfo[0][i]==0)
menor=cardNumber[player[i]];
return menor;
}
//--------------------------------------------------------------
//checks how many cards in player hand dont have any color clue
//returns that amount
int howManyNoCluesColor()
{
int i,count=0;
for(i=0;i<5;i++)
if(playerInfo[1][i]==0)
count++;
return count;
}
//--------------------------------------------------------------
//checks the first card without clues from bot hand
//returns the position of that card ,-1 if it doesnt exist
int botFirstNoClueCard()
{
int i;
for(i=4;i>=0;i--)
{
if(botInfo[0][i]==0 && botInfo[1][i]==0)
return i+1;
}
return 0;
}
//--------------------------------------------------------------
//checks the smallest number in table
//returns that number
int smallTableNumber()
{
int i,smallest=6;
for(i=0;i<5;i++)
{
if(table[i]==-1)
{
smallest=0;
return smallest;
}
if(cardNumber[table[i]]<smallest)
smallest=cardNumber[table[i]];
}
return smallest;
}
//--------------------------------------------------------------
//check if bot have all cards with color and number clue,return 1
//if some card doesnt have a clue return 0
int botHasAllClues()
{
int i;
for(i=0;i<5;i++)
{
if(botInfo[0][i]==0 || botInfo[1][i]==0){
return 0;
}
return 1;
}
}
//--------------------------------------------------------------
//count how many clues the player has
//return that amount
int countPlayerClues()
{
int i,count=0;
for(i=0;i<5;i++)
count=count+playerInfo[0][i]+playerInfo[1][i];
return count;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
//main function for bot turn
//--------------------------------------------------------------
//--------------------------------------------------------------
void botAction()
{
int position=0,i,number,aux;
//first play
//check if table is empty and bot has 1
if(tableScore()==0)
{
position=botHasNumber1();
if(position!=-1)
{
indCard=bot[position-1];
tablePlay(bot,botInfo,position);
strcpy(status,"Bot: Jogou uma carta 1");
return;
}
}
//check if table is empty and if player has 1
if(tableScore()==0 && playerHasNumber(1)>0)
{
numberClues(player,playerInfo,1);
clues--;
strcpy(status,"Bot: Deu dica de numero 1");
return;
}
//end of first play
//check if bot can play a card to table
for(i=0;i<5;i++)
{
if(table[i]==-1)
number=0;
else
number=cardNumber[table[i]];
color=cardColor[i*10];
position=botHasCard(number+1,color);
if(position!=-1)
{
indCard=bot[position-1];
tablePlay(bot,botInfo,position);
sprintf(status,"Bot: Jogou Carta %d %s",cardNumber[indCard],colorName[indCard/10]);
return;
}
}
//check if bot can discard non playable card
//colocado aqui para melhorar a IA do bot
if(clues<8 && countPlayerClues()>2) //o player ja tem 3 dicas nao necessita de mais :)
{
position=0;
aux=smallTableNumber();
if(aux!=0)
{
for(i=0;i<5;i++)
{
if(botInfo[0][i]==1)
if(cardNumber[bot[i]]<=aux)
{
aux=cardNumber[bot[i]];
position=i+1;
}
}
if(position!=0)
{
indCard=bot[position-1];
discardAct(bot,botInfo,position);
sprintf(status,"Bot: Descartou Carta %d %s",cardNumber[indCard],colorName[indCard/10]);
return;
}
}
}
//no cards playable
if(clues>0)
{
for(i=0;i<5;i++)
{
if(table[i]==-1)
number=0;
else
number=cardNumber[table[i]];
color=cardColor[i*10];
position=playerHasCard(number+1,color);
if(position!=-1)
{
if(playerInfo[0][i]==0 && playerInfo[1][i]==0) //if card is playable and is without clues
{
if(countPlayerCardsNumber(number+1)==1)
{
numberClues(player,playerInfo,cardNumber[player[position-1]]);
sprintf(status,"Bot: Tens um unico numero %d",number+1);
clues--;
return;
}
else
if(countPlayerCardsColor(color)==1)
{
colorClue(player,playerInfo,cardColor[player[position-1]]);
sprintf(status,"Bot: Tens uma unica carta %s",colorName[i]);
clues--;
return;
}
}
else
if(playerInfo[0][i]==1)
{
colorClue(player,playerInfo,cardColor[player[position-1]]);
sprintf(status,"Bot: Deu dica de cor %s",colorName[i]);
clues--;
return;
}
else
if(playerInfo[1][i]==1)
{
numberClues(player,playerInfo,cardNumber[player[position-1]]);
sprintf(status,"Bot: Deu dica de numero %d",number+1);
clues--;
return;
}
else
if(rand()%2==1)
{
numberClues(player,playerInfo,cardNumber[player[position-1]]);
sprintf(status,"Bot: Deu dica de numero %d",number+1);
clues--;
return;
}
else
{
colorClue(player,playerInfo,cardColor[player[position-1]]);
sprintf(status,"Bot: Deu dica de cor %s",colorName[i]);
clues--;
return;
}
}// end if player has playable card
}//end of for
//going to check if bot can give a clue of the smallest number of player
int menor=smallPlayerNumber();
if(menor!=6)
{
numberClues(player,playerInfo,menor);
sprintf(status,"Bot: Deu dica de numero %d",menor);
clues--;
return;
}
//going to check if bot can give a clue of a random color
if(howManyNoCluesColor()!=0)
{
while(1)
{
aux=rand()%5;
if(playerInfo[1][aux]==0)
{
colorClue(player,playerInfo,cardColor[player[aux]]);
sprintf(status,"Bot: Deu dica de cor %s",colorName[player[aux]/10]);
clues--;
return;
}
}
}
}//end of if(clues>0)
//no clues left bot makes a discard
//discards the first no clue card
position=botFirstNoClueCard();
if(position!=0)
{
indCard=bot[position-1];
discardAct(bot,botInfo,position);
sprintf(status,"Bot: Descartou Carta %d %s",cardNumber[indCard],colorName[indCard/10]);
return;
}
//checking if can discard any
position=0;
aux=smallTableNumber();
if(aux!=0)
{
for(i=0;i<5;i++)
{
if(botInfo[0][i]==1)
if(cardNumber[bot[i]]<=aux)
{
aux=cardNumber[bot[i]];
position=i+1;
}
}
if(position!=0)
{
indCard=bot[position-1];
discardAct(bot,botInfo,position);
sprintf(status,"Bot: Descartou Carta %d %s",cardNumber[indCard],colorName[indCard/10]);
return;
}
}
//checks if bot has a card without all clues and discards it randomly
if(botHasAllClues()!=1)
{
while(1)
{
position=rand()%5+1;
if(botInfo[0][position-1]==0 || botInfo[1][position-1]==0)
{
indCard=bot[position-1];
discardAct(bot,botInfo,position);
sprintf(status,"Bot: Descartou Carta %d %s",cardNumber[indCard],colorName[indCard/10]);
return;
}
}
}
//bot will discard a random card
position=rand()%5+1;
indCard=bot[position-1];
discardAct(bot,botInfo,position);
sprintf(status,"Bot: Descartou Carta %d %s",cardNumber[indCard],colorName[indCard/10]);
return;
}//end of botAction