tarot_private.h 7.34 KB
Newer Older
1
/* tarot implements the rules of the tarot game
2
 * Copyright (C) 2017, 2018, 2019, 2020  Vivien Kraus
Vivien Kraus's avatar
Vivien Kraus committed
3
 *
4
5
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, version 3 of the License.
Vivien Kraus's avatar
Vivien Kraus committed
7
8
9
10
11
12
13
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Vivien Kraus's avatar
Vivien Kraus committed
15
16
17
18
19
20
21
22
 */

#ifndef H_TAROT_PRIVATE_INCLUDED
#define H_TAROT_PRIVATE_INCLUDED

#include <tarot.h>
#include <stdalign.h>
#include <nettle/yarrow.h>
Vivien Kraus's avatar
Vivien Kraus committed
23
#include "julien.h"
Vivien Kraus's avatar
Vivien Kraus committed
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

#ifdef __cplusplus
extern "C"
{
#endif                          /* __cplusplus */

  typedef struct
  {
    size_t n_max;
    size_t *n;
    TarotCard *buffer;
  } TarotHand;

  typedef struct
  {
    size_t n;
    TarotCard buffer[3];
  } TarotHand3;

  typedef struct
  {
    size_t n;
    TarotCard buffer[6];
  } TarotHand6;

  typedef struct
  {
    size_t n;
    TarotCard buffer[15];
  } TarotHand15;

  typedef struct
  {
    size_t n;
    TarotCard buffer[18];
  } TarotHand18;

  typedef struct
  {
    size_t n;
    TarotCard buffer[24];
  } TarotHand24;

  typedef struct
  {
    TarotHand24 hands[3];
    int known[3];
  } TarotHands3;

  typedef struct
  {
    TarotHand18 hands[4];
    int known[4];
  } TarotHands4;

  typedef struct
  {
    TarotHand15 hands[5];
    int known[5];
  } TarotHands5;

  typedef struct
  {
    size_t n_players;
    TarotHand hands[5];
    int *known;
  } TarotHandsHandle;

  typedef struct
  {
    size_t n_bids;
    int has_started;
    TarotPlayer taker;
    TarotBid bids[5];
  } TarotBids;

  typedef struct
  {
    size_t n_decls;
    int has_started;
    TarotPlayer slammer;
  } TarotDecls;

  typedef struct
  {
    int has_started;
    int has_card;
    int has_player;
    TarotCard call;
    TarotPlayer partner;
    TarotPlayer taker;
  } TarotCall;

  typedef struct
  {
    int has_started;
    TarotPlayer taker;
    TarotBid contract;
    int dog_done;
    int discard_done;
  } TarotDoscardHeader;

  typedef struct
  {
    TarotDoscardHeader d;
    TarotHand6 dog;
    TarotHand6 discard;
  } TarotDoscard34;

  typedef struct
  {
    TarotDoscardHeader d;
    TarotHand3 dog;
    TarotHand3 discard;
  } TarotDoscard5;

  typedef struct
  {
    size_t n_players;
    TarotDoscardHeader *doscard;
    TarotHand dog;
    TarotHand discard;
  } TarotDoscardHandle;

  typedef struct
  {
    int has_started;
    size_t n_decisions;
    int has_next;
    TarotPlayer leader;
    TarotPlayer shower_of[22];
  } TarotHandfuls;

  typedef struct
  {
    size_t n_cards;
    int last;
    TarotPlayer leader;
    int has_forced_taker;
    TarotPlayer forced_taker;
  } TarotTrickHeader;

  typedef struct
  {
    TarotTrickHeader t;
    TarotCard cards[3];
  } TarotTrick3;

  typedef struct
  {
    TarotTrickHeader t;
    TarotCard cards[4];
  } TarotTrick4;

  typedef struct
  {
    TarotTrickHeader t;
    TarotCard cards[5];
  } TarotTrick5;

  typedef struct
  {
    size_t n_players;
    TarotTrickHeader *trick;
    TarotCard *cards;
  } TarotTrickHandle;

  typedef struct
  {
    size_t i_current;
    TarotTrick3 tricks[24];
  } TarotTricks3;

  typedef struct
  {
    size_t i_current;
    TarotTrick4 tricks[18];
  } TarotTricks4;

  typedef struct
  {
    size_t i_current;
    TarotTrick5 tricks[15];
  } TarotTricks5;

  typedef struct
  {
    size_t n_players;
    size_t n_tricks;
    size_t *i_current;
    TarotTrickHandle tricks[24];
  } TarotTricksHandle;

  typedef struct
  {
    TarotPlayer dealt;
    TarotStep step;
    TarotHands3 hands;
    TarotBids bids;
    TarotDecls decls;
    TarotDoscard34 doscard;
    TarotHandfuls handfuls;
    TarotTricks3 tricks;
  } TarotGame3;

  typedef struct
  {
    TarotPlayer dealt;
    TarotStep step;
    TarotHands4 hands;
    TarotBids bids;
    TarotDecls decls;
    TarotDoscard34 doscard;
    TarotHandfuls handfuls;
    TarotTricks4 tricks;
  } TarotGame4;

  typedef struct
  {
    TarotPlayer dealt;
    TarotStep step;
    TarotHands5 hands;
    TarotBids bids;
    TarotDecls decls;
    TarotDoscard5 doscard;
    TarotHandfuls handfuls;
    TarotTricks5 tricks;
  } TarotGame5;

  typedef struct
  {
    TarotPlayer dealt;
    TarotStep step;
    TarotHands5 hands;
    TarotBids bids;
    TarotDecls decls;
    TarotCall call;
    TarotDoscard5 doscard;
    TarotHandfuls handfuls;
    TarotTricks5 tricks;
  } TarotGame5c;

  typedef struct
  {
    size_t n_players;
    int with_call;
  } TarotGameOptions;

  typedef union
  {
    TarotGame3 p3;
    TarotGame4 p4;
    TarotGame5 p5;
    TarotGame5c p5c;
  } TarotGameU;

  struct TarotGame
  {
    TarotGameOptions opt;
    TarotGameU u;
Vivien Kraus's avatar
Vivien Kraus committed
284
285
286
287
288
289
    int has_duplicate_deal;
    TarotPlayer duplicate_deal[78];
    int has_duplicate_taker;
    TarotPlayer duplicate_taker;
    int has_duplicate_call;
    TarotCard duplicate_call;
Vivien Kraus's avatar
Vivien Kraus committed
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
  };

  typedef union
  {
    TarotGameOptions setup;
    TarotPlayer deal;
    TarotBid bid;
    int decl;
    TarotCard call;
    TarotCard card;
  } TarotGameEventU;

  struct TarotGameEvent
  {
    TarotGameEventT t;
    TarotGameEventU u;
    size_t n;
    unsigned int *data;
  };

  struct TarotGameIterator
  {
    const TarotGame *handle;
    TarotStep step;
    size_t i_player;
    size_t i_trick;
    int handful_done;
    TarotGameEvent last_event;
    unsigned int event_data[78];
  };

  typedef struct
  {
    size_t n_cards_remaining;
    TarotNumber max_of[5];
  } TarotCounterPlayer;

  struct TarotCounter
  {
    size_t n_players;
    TarotCounterPlayer by_player[5];
    TarotPlayer current_owner_of[78];
    int may_be_discarded[78];
    /* Discarded, or still in the unrevealed dog.  A card in the dog
       that may have been discarded is "owned" by the taker, but it
       may be discarded. */
    TarotPlayer taker;
  };

  struct TarotMctsNode;
  typedef struct TarotMctsNode TarotMctsNode;

  struct TarotMctsNode
  {
    TarotMctsNode *up;
    TarotMctsNode *left;
    TarotMctsNode *right;
    int sum_scores;
    size_t n_simulations;
    TarotPlayer myself;
    TarotGameEvent event;
  };

  struct TarotMcts
  {
    TarotMctsNode *root;
    TarotGame base;
    struct yarrow256_ctx generator;
    double tune_parameter;
    double tune_simulation_random;
    double tune_simulation_agreed;
    char *pool_tail;
    size_t pool_remaining;
    char *pool_head;
  };

Vivien Kraus's avatar
Vivien Kraus committed
366
367
368
369
  struct TarotSolo
  {
    TarotGame game;
    TarotPlayer myself;
Vivien Kraus's avatar
Vivien Kraus committed
370
    TarotAi *ai;
Vivien Kraus's avatar
Vivien Kraus committed
371
372
  };

Vivien Kraus's avatar
Vivien Kraus committed
373
374
375
376
377
378
379
380
381
382
383
  struct TarotPerceptron
  {
    size_t n_layers;
    size_t *layer_sizes;
    double learning_rate;
    struct julien *jl;
    TarotGameEvent best_event;
    unsigned int event_data[78];
    double best_score;
  };

384
385
386
387
388
389
390
391
392
393
394
395
396
  struct TarotAi
  {
    void *user_data;
    void (*eval) (void *, const TarotGame *, size_t, TarotGameEvent **,
                  size_t, size_t, double *);
    void (*learn) (void *, const TarotGame *, const TarotGameEvent *, double);
    void *(*dup) (void *);
    void (*destruct) (void *);
    TarotGameEvent best_event;
    unsigned int event_data[78];
    double best_score;
  };

Vivien Kraus's avatar
Vivien Kraus committed
397
398
399
400
#ifdef __cplusplus
}
#endif                          /* __cplusplus */
#endif                          /* not H_TAROT_PRIVATE_INCLUDED */