-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingBots.c
145 lines (127 loc) · 5.41 KB
/
testingBots.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
/*
This is for testing the bots of different difficulties against the random bot
to validate that they perform better than random.
The results of our tests are at the end of the report.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bot.c"
#include "checking.c"
#include "spellmap.c"
#include <time.h>
int main(){
int DIFFICULTY = 3; //0 is random, 1 is easy, 2 is medium, 3 is hard, ...
int RandomWins = 0;
int LostByNoMoreMoves = 0;
int LostByIllegalMoveWordUnavailable = 0;
int LostByIllegalMoveWordAlreadyUsed = 0;
int LostByIllegalMoveWordsDoNotMatch = 0;
int BotWins = 0;
int WonByNoMoreMoves = 0;
int WonByIllegalMoveWordUnavailable = 0;
int WonByIllegalMoveWordAlreadyUsed = 0;
int WonByIllegalMoveWordsDoNotMatch = 0;
int TotalGames = 100;
srand(time(NULL));
int coin = rand()%2; // 0 for player 1, and 1 for player 2 (bot);
for(int g = 0; g < TotalGames; g++){
struct Spell * spellMap[26]; //array of 26 linked lists of spells, each entry starting with a certain letter
int tally[26]; //keeps track of how many spells we have starting with each letter which are playable (not used yet)
for(int i = 0; i < 26; i++){
spellMap[i] = NULL;
tally[i] = 0;
}
readSpells(spellMap, tally, "spells.txt");
// reads from file all spells, and places them in the array accoridng to first letter
// also prints the spells as desired.
coin = (coin + 1) %2;
int c = 0; //counter for how many turns have been played
char * prev = (char *) malloc(30 * sizeof(char)); //assuming 30 is largest
while(1){
int turn = ((c + coin) % 2); //calculates whose turn it is: 0 for player 1, 1 for player 2.
if(!canPlay(tally, prev) && c!=0){
//Player loses because he has no more possible moves.
char * winner, *loser;
if(turn == 0){
BotWins++;
WonByNoMoreMoves++;
}
else{
RandomWins++;
LostByNoMoreMoves++;
}
break;
}
char word [30]; //again, assuming longest word is of 30 characters
if(turn == 0){
if(c > 0){
playBot(spellMap, tally, prev, 0, word);
}
else{
for(char i = 'a'; i <= 'z'; i++){
if(tally[i-'a'] != 0){
char str[] = {i, '\0'};
if(playBot(spellMap, tally, str, 0, word) != -1){ //choose a word which does not make you lose
break;
}
}
}
}
}
else {
if(c > 0){
playBot(spellMap, tally, prev, DIFFICULTY, word);
}
else{
for(char i = 'a'; i <= 'z'; i++){
if(tally[i-'a'] != 0){
char str[] = {i, '\0'};
if(playBot(spellMap, tally, str, DIFFICULTY, word) != -1){ //choose a word which does not make you lose
break;
}
}
}
}
}
int t = isLegal(spellMap, tally, prev, word, c);
if(t < 0){
//Player loses for one of 3 reasons according to return value
char * winner, *loser;
if(turn == 0){
BotWins++;
if(t == -1)
WonByIllegalMoveWordsDoNotMatch++;
else if(t == -2)
WonByIllegalMoveWordUnavailable++;
else
WonByIllegalMoveWordAlreadyUsed++;
}
else{
RandomWins++;
if(t == -1)
LostByIllegalMoveWordsDoNotMatch++;
else if(t == -2)
LostByIllegalMoveWordUnavailable++;
else
LostByIllegalMoveWordAlreadyUsed++;
}
break;
}
strcpy(prev, word); //store copy of word in prev to check conditions next turn
c++;
}
free(prev);
for (int i = 0; i < 26; i++) {
struct Spell *current = spellMap[i];
while (current != NULL) {
struct Spell *next = current->next;
free(current->name);
free(current);
current = next;
}
}
}
printf("\nTotal Number Of Games: %d\nWon By Random Bot: %d\nWon By Difficulty %d Bot: %d\n\nWon By No More Moves: %d\nWon By No Such Word: %d\nWon By Does Not Match: %d\nWon By Already Used: %d\n\nLost By No More Moves: %d\nLost By No Such Word: %d\nLost By Does Not Match: %d\nLost By Already Used: %d\n\n%d", TotalGames, RandomWins, DIFFICULTY, BotWins, WonByNoMoreMoves, WonByIllegalMoveWordUnavailable, WonByIllegalMoveWordsDoNotMatch, WonByIllegalMoveWordAlreadyUsed, LostByNoMoreMoves, LostByIllegalMoveWordUnavailable, LostByIllegalMoveWordsDoNotMatch, LostByIllegalMoveWordAlreadyUsed, 0);
return 0;
}