-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.c
168 lines (153 loc) · 4.6 KB
/
simulation.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
/* main.c*/
#include "player.h"
#include "crupier.h"
#include "hand.h"
#include "deck.h"
// load strategies
#include "bet_strategies.h"
#include "play_strategies.h"
#define NUMPLAYERS 1
void usage() {
printf("usage: ./main [games] [filetowrite] [seed]\n");
}
int main(int argc, char** argv) {
int numPartidas;
Table* tableError;
FILE* output;
if (argc > 4) {
usage();
return -1;
}
if (argc < 2){
fprintf(stderr, "Playing a hundred games as default.\n");
numPartidas=100;
}
else numPartidas=atoi(argv[1]);
Table *table = table_ini();
if(!table)
{
fprintf(stderr, "main: error: failed to initialize table\n");
return EXIT_FAILURE;
}
// initialize players
// set the first player to use a random strategy
table = table_addPlayer(table, player_ini(counting_bet, play_basic_17S_DAS_matrix));
if(!table){
fprintf(stderr, "main: ERROR in table_addPlayer.\n");
return EXIT_FAILURE;
}
table = table_addPlayer(table, player_ini(boring_bet, play_basic_17S_DAS_matrix));
if(!table){
fprintf(stderr, "main: ERROR in table_addPlayer.\n");
return EXIT_FAILURE;
}
table = table_addPlayer(table, player_ini(boring_bet, play_like_crupier));
if(!table){
fprintf(stderr, "main: ERROR in table_addPlayer.\n");
return EXIT_FAILURE;
}
table = table_addPlayer(table, player_ini(boring_bet, play_like_crupier));
if(!table){
fprintf(stderr, "main: ERROR in table_addPlayer.\n");
return EXIT_FAILURE;
}
// initialize crupier
Crupier *crupier = crupier_ini();
if (!crupier) {
fprintf(stderr, "main: crupier_ini: error allocating memory for crupier\n");
table_destroy(table);
return 3;
}
table = table_setCrupier(table, crupier);
if(!table){
fprintf(stderr, "main: ERROR setting Crupier.\n");
return EXIT_FAILURE;
}
// initialize deck, seeding with seed if provided
unsigned seed;
if (argc == 4) {
seed = (unsigned) atoi(argv[3]);
} else {
seed = time(NULL);
}
printf("seeded game with %u\n", seed);
Deck *deck = deck_ini(seed);
if (!deck) {
fprintf(stderr, "main: deck_ini: error allocating memory of deck\n");
table_destroy(table);
return 4;
}
table = table_setDeck(table, deck);
if(!table){
fprintf(stderr, "Error setting Deck.\n");
return EXIT_FAILURE;
}
if (argc > 2){
output = fopen(argv[2], "w");
if (!output) return 0;
}
// play a hundred times
for (int k = 0; k < numPartidas; k++) {
/* apostar*/
tableError = table_makeBets(table);
if(!tableError){
fprintf(stderr, "Error in makeBets.\n");
table_printLastGame(stderr, table, k);
return EXIT_FAILURE;
}
/* repartir primera carta */
table = table_dealCard(table);
if(!table){
fprintf(stderr, "Error in dealCard.\n");
return EXIT_FAILURE;
}
/* repartir 1 carta al crupier */
table = table_dealCardToCrupier(table);
if(!table){
fprintf(stderr, "Error in dealCardToCrupier.\n");
return EXIT_FAILURE;
}
/*repartir segunda carta*/
table = table_dealCard(table);
if(!table){
fprintf(stderr, "Error in dealCard2.\n");
return EXIT_FAILURE;
}
/* juegan los jugadores */
table = table_makePlays(table);
if(!table){
fprintf(stderr, "Error in makePlays.\n");
return EXIT_FAILURE;
}
/*juega el crupier*/ // TODO: should we only touch the crupier thorugh table?
crupier = crupier_play(crupier, deck);
if(!crupier){
fprintf(stderr, "Error while crupier is playing.\n");
return EXIT_FAILURE;
}
table = table_distributeEarnings(table);
if(!table){
fprintf(stderr, "Error in distributeEarnings.\n");
return EXIT_FAILURE;
}
table = table_restartTable(table);
if(!table){
fprintf(stderr, "Error in restartTable.\n");
return EXIT_FAILURE;
}
/*table_printLastGame(stdout, table, k);*/
if (argc > 2){
table_printCash(output, table);
}
}
if (argc > 2){
fclose(output);
}
table_printPlayersPercentages(stdout, table);
fprintf(stdout, "\n");
table_printPlayersMoney(stdout, table);
fprintf(stdout, "\n");
table_printPlayersStreakData(stdout, table);
table_destroy(table);
return EXIT_SUCCESS;
}