-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattle_engine.c
More file actions
172 lines (136 loc) · 4.12 KB
/
battle_engine.c
File metadata and controls
172 lines (136 loc) · 4.12 KB
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
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "engines.h"
#include "player_types.h"
#include "game.h"
#include "player_interaction.h"
#include "util.h"
#define THRESHOLD 10000
struct sh_count_t {
struct player_type_t player;
int count;
};
static void shuffle(struct player_type_t *players, int nplayers)
{
int i, r;
struct player_type_t t;
if (nplayers > 1) {
for (i = 0; i < nplayers; i++) {
r = rand() / ( RAND_MAX / nplayers + 1 );
t = players[i];
players[i] = players[r];
players[r] = t;
}
}
}
static void init_sh_counts(struct player_type_t ptypes[], struct sh_count_t counts[])
{
struct sh_count_t random;
random.player = ptypes[0];
random.count = 0;
counts[0] = random;
struct sh_count_t low;
low.player = ptypes[1];
low.count = 0;
counts[1] = low;
struct sh_count_t pyro;
pyro.player = ptypes[2];
pyro.count = 0;
counts[2] = pyro;
}
static void add_sh(char *name, struct sh_count_t counts[])
{
if (strcmp(name, "Random") == 0)
counts[0].count++;
else if (strcmp(name, "Low") == 0)
counts[1].count++;
else
counts[2].count++;
}
static int score_order(const void *count1, const void *count2)
{
const struct sh_count_t *icount1 = (const struct sh_count_t *) count1;
const struct sh_count_t *icount2 = (const struct sh_count_t *) count2;
return icount1->count > icount2->count;
}
static void show_counts(struct sh_count_t counts[], int ngames)
{
float percent;
qsort(counts, 3, sizeof(struct sh_count_t), score_order);
int i;
for (i = 0; i < 3; i++) {
percent = (double)counts[i].count / (double)ngames * 100.0;
printf("%-20s%-10d%.0f%%\n", counts[i].player.name, counts[i].count, percent);
}
}
void run_battle_engine(int ngames)
{
int game = 0;
int nplayers = 3;
int ncards = 3;
int stalemates = 0;
struct player_type_t ptypes[nplayers];
struct sh_count_t sh_counts[nplayers];
struct player_type_t random;
random.type = 'r';
strcpy(random.name, "Random");
struct player_type_t low;
low.type = 'l';
strcpy(low.name, "Low");
struct player_type_t pyro;
pyro.type = 'p';
strcpy(pyro.name, "Pyro");
ptypes[0] = random;
ptypes[1] = low;
ptypes[2] = pyro;
init_sh_counts(ptypes, sh_counts);
clock_t start = clock();
for (game = 0; game < ngames; game++) {
char names[MAX_NUM_PLAYERS][MAX_NAME_LEN];
char types[MAX_NUM_PLAYERS];
char shithead[MAX_NAME_LEN];
shuffle(ptypes, 3);
int i;
for (i = 0; i < nplayers; i++) {
strcpy(names[i], ptypes[i].name);
types[i] = ptypes[i].type;
}
struct game_t game = make_game(nplayers, names, types, ncards);
init_game(&game);
perform_swap(&game);
first_move(&game);
int moves = 0;
while (continue_play(&game) && moves < THRESHOLD) {
if (player_on_last_cards(&game))
perform_last_cards_move(&game);
else
perform_move(&game);
moves++;
}
if (moves == THRESHOLD) {
stalemates++;
}
get_shithead(&game, shithead);
add_sh(shithead, sh_counts);
}
clock_t end = clock();
double totalmillis = ((double) (end - start)) / (CLOCKS_PER_SEC / 1000);
char fmt_elapsed[100];
format_millis(totalmillis, fmt_elapsed);
float avg_game_millis = totalmillis / ngames;
float stalemate_percent = (double)stalemates / (double)ngames * 100.0;
printf("\n");
printf("SUMMARY\n");
printf("Total games : %d\n", ngames);
printf("Total time : ");
printf("%s\n", fmt_elapsed);
printf("Average game: %.2f milliseconds\n", avg_game_millis);
printf("Stalemates : %d, %.2f%%\n", stalemates, stalemate_percent);
printf("\n");
printf("SCORES\n");
printf("%-20s%-10s%s\n", "Name", "Shithead", "Lose rate");
printf("------------------------------------------\n");
show_counts(sh_counts, ngames);
printf("\n");
}