game_private_impl.h 85.7 KB
Newer Older
1
/* tarot implements the rules of the tarot game
Vivien Kraus's avatar
Vivien Kraus committed
2
 * Copyright (C) 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
23
24
25
26
27
28
29
30
 */

#ifndef H_TAROT_GAME_PRIVATE_IMPL_INCLUDED
#define H_TAROT_GAME_PRIVATE_IMPL_INCLUDED

#include <tarot/game_private.h>
#include <tarot/cards_private.h>
#include <tarot/game_event_private.h>
#include <tarot/hands_private.h>
#include <tarot/bids_private.h>
#include <tarot/decls_private.h>
#include <tarot/call_private.h>
#include <tarot/doscard_private.h>
#include <tarot/handfuls_private.h>
#include <tarot/tricks_private.h>
#include <tarot/trick_private.h>
31
#include <tarot/counter_private.h>
Vivien Kraus's avatar
Vivien Kraus committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

struct TarotGameHandle
{
  TarotGameOptions opt;
  TarotPlayer *dealt;
  TarotStep *step;
  TarotHandsHandle hands;
  TarotBids *bids;
  TarotDecls *decls;
  TarotCall *call;
  TarotDoscardHandle doscard;
  TarotHandfuls *handfuls;
  TarotTricksHandle tricks;
Vivien Kraus's avatar
Vivien Kraus committed
49
50
51
52
53
54
  int *has_duplicate_deal;
  TarotPlayer *duplicate_deal;
  int *has_duplicate_taker;
  TarotPlayer *duplicate_taker;
  int *has_duplicate_call;
  TarotCard *duplicate_call;
Vivien Kraus's avatar
Vivien Kraus committed
55
56
57
58
59
60
61
62
};

typedef struct TarotGameHandle TarotGameHandle;

static void
game_generalize (TarotGame * game, TarotGameHandle * handle)
{
  memcpy (&(handle->opt), &(game->opt), sizeof (TarotGameOptions));
Vivien Kraus's avatar
Vivien Kraus committed
63
64
65
66
67
68
  handle->has_duplicate_deal = &(game->has_duplicate_deal);
  handle->duplicate_deal = game->duplicate_deal;
  handle->has_duplicate_taker = &(game->has_duplicate_taker);
  handle->duplicate_taker = &(game->duplicate_taker);
  handle->has_duplicate_call = &(game->has_duplicate_call);
  handle->duplicate_call = &(game->duplicate_call);
Vivien Kraus's avatar
Vivien Kraus committed
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
  switch (game->opt.n_players)
    {
    case 3:
      handle->dealt = &(game->u.p3.dealt);
      handle->step = &(game->u.p3.step);
      hands_3_generalize (&(game->u.p3.hands), &(handle->hands));
      handle->bids = &(game->u.p3.bids);
      handle->decls = &(game->u.p3.decls);
      handle->call = NULL;
      doscard_3_generalize (&(game->u.p3.doscard), &(handle->doscard));
      handle->handfuls = &(game->u.p3.handfuls);
      tricks_3_generalize (&(game->u.p3.tricks), &(handle->tricks));
      break;
    case 4:
      handle->dealt = &(game->u.p4.dealt);
      handle->step = &(game->u.p4.step);
      hands_4_generalize (&(game->u.p4.hands), &(handle->hands));
      handle->bids = &(game->u.p4.bids);
      handle->decls = &(game->u.p4.decls);
      handle->call = NULL;
      doscard_4_generalize (&(game->u.p4.doscard), &(handle->doscard));
      handle->handfuls = &(game->u.p4.handfuls);
      tricks_4_generalize (&(game->u.p4.tricks), &(handle->tricks));
      break;
    case 5:
      if (game->opt.with_call)
        {
          handle->dealt = &(game->u.p5c.dealt);
          handle->step = &(game->u.p5c.step);
          hands_5_generalize (&(game->u.p5c.hands), &(handle->hands));
          handle->bids = &(game->u.p5c.bids);
          handle->decls = &(game->u.p5c.decls);
          handle->call = &(game->u.p5c.call);
          doscard_5_generalize (&(game->u.p5c.doscard), &(handle->doscard));
          handle->handfuls = &(game->u.p5c.handfuls);
          tricks_5_generalize (&(game->u.p5c.tricks), &(handle->tricks));
        }
      else
        {
          handle->dealt = &(game->u.p5.dealt);
          handle->step = &(game->u.p5.step);
          hands_5_generalize (&(game->u.p5.hands), &(handle->hands));
          handle->bids = &(game->u.p5.bids);
          handle->decls = &(game->u.p5.decls);
          handle->call = NULL;
          doscard_5_generalize (&(game->u.p5.doscard), &(handle->doscard));
          handle->handfuls = &(game->u.p5.handfuls);
          tricks_5_generalize (&(game->u.p5.tricks), &(handle->tricks));
        }
      break;
119
120
    default:
      assert (0);
Vivien Kraus's avatar
Vivien Kraus committed
121
122
123
    }
}

124
static inline int game_step_advance (TarotGame * game);
Vivien Kraus's avatar
Vivien Kraus committed
125
126
127
128
129
130
131
132
133
134
135
136
137

static inline void
game_set_dealt (TarotGameHandle * handle, int dealt)
{
  *(handle->dealt) = dealt;
}

static inline void
game_set_step (TarotGameHandle * handle, TarotStep step)
{
  *(handle->step) = step;
}

Vivien Kraus's avatar
Vivien Kraus committed
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
static inline void
game_set_duplicate_deal (TarotGameHandle * handle, size_t n,
                         const TarotPlayer * owner)
{
  size_t i;
  assert (n == 78);
  *(handle->has_duplicate_deal) = 1;
  for (i = 0; i < n; i++)
    {
      handle->duplicate_deal[i] = owner[i];
    }
}

static inline void
game_set_duplicate_taker (TarotGameHandle * handle, TarotPlayer taker)
{
  *(handle->has_duplicate_taker) = 1;
  *(handle->duplicate_taker) = taker;
}

static inline void
game_set_duplicate_call (TarotGameHandle * handle, TarotCard call)
{
  *(handle->has_duplicate_call) = 1;
  *(handle->duplicate_call) = call;
}

Vivien Kraus's avatar
Vivien Kraus committed
165
166
167
168
169
170
171
172
173
174
175
176
static inline int
game_get_dealt (TarotGameHandle * handle)
{
  return *(handle->dealt);
}

static inline TarotStep
game_get_step (const TarotGameHandle * handle)
{
  return *(handle->step);
}

Vivien Kraus's avatar
Vivien Kraus committed
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
/* Returns NULL if no duplicate deal */
static inline const TarotPlayer *
game_get_duplicate_deal (const TarotGameHandle * handle, size_t *n)
{
  if (*(handle->has_duplicate_deal))
    {
      *n = 78;
      return handle->duplicate_deal;
    }
  return NULL;
}

static inline const TarotPlayer *
game_get_duplicate_taker (const TarotGameHandle * handle)
{
  if (*(handle->has_duplicate_taker))
    {
      return handle->duplicate_taker;
    }
  return NULL;
}

static inline const TarotCard *
game_get_duplicate_call (const TarotGameHandle * handle)
{
  if (*(handle->has_duplicate_call))
    {
      return handle->duplicate_call;
    }
  return NULL;
}

Vivien Kraus's avatar
Vivien Kraus committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
static inline void
game_initialize (TarotGame * game, size_t n_players, int with_call)
{
  TarotGameHandle handle;
  game->opt.n_players = n_players;
  game->opt.with_call = with_call;
  game_generalize (game, &handle);
  game_set_dealt (&handle, n_players);
  game_set_step (&handle, TAROT_SETUP);
  hands_initialize (&(handle.hands), n_players);
  bids_initialize (handle.bids);
  decls_initialize (handle.decls, n_players);
  if (with_call)
    {
      call_initialize (handle.call);
    }
  doscard_initialize (&(handle.doscard), n_players);
  handfuls_initialize (handle.handfuls, n_players);
  tricks_initialize (&(handle.tricks), n_players);
Vivien Kraus's avatar
Vivien Kraus committed
228
229
230
  *(handle.has_duplicate_deal) = 0;
  *(handle.has_duplicate_taker) = 0;
  *(handle.has_duplicate_call) = 0;
Vivien Kraus's avatar
Vivien Kraus committed
231
232
233
234
235
236
}

static inline void
game_copy (TarotGame * dest, const TarotGame * source)
{
  TarotGameHandle hdest, hsource;
Vivien Kraus's avatar
Vivien Kraus committed
237
238
239
240
  size_t n;
  const TarotPlayer *owners;
  const TarotPlayer *taker;
  const TarotPlayer *call;
Vivien Kraus's avatar
Vivien Kraus committed
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
  game_initialize (dest, source->opt.n_players, source->opt.with_call);
  game_generalize (dest, &hdest);
  game_generalize ((TarotGame *) source, &hsource);
  game_set_dealt (&hdest, game_get_dealt (&hsource));
  game_set_step (&hdest, game_get_step (&hsource));
  hands_copy (&(hdest.hands), &(hsource.hands));
  if (bids_copy (hdest.bids, hsource.bids) != 0)
    {
      assert (0);
    }
  if (decls_copy (hdest.decls, hsource.decls) != 0)
    {
      assert (0);
    }
  if (hsource.opt.with_call)
    {
      if (call_copy (hdest.call, hsource.call) != 0)
        {
          assert (0);
        }
    }
  if (doscard_copy (&(hdest.doscard), &(hsource.doscard)) != 0)
    {
      assert (0);
    }
  if (handfuls_copy (hdest.handfuls, hsource.handfuls) != 0)
    {
      assert (0);
    }
  if (tricks_copy (&(hdest.tricks), &(hsource.tricks)) != 0)
    {
      assert (0);
    }
Vivien Kraus's avatar
Vivien Kraus committed
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
  if ((owners = game_get_duplicate_deal (&hsource, &n)) != NULL)
    {
      assert (n == 78);
      game_set_duplicate_deal (&hdest, n, owners);
    }
  else
    {
      *(hdest.has_duplicate_deal) = 0;
    }
  if ((taker = game_get_duplicate_taker (&hsource)) != NULL)
    {
      game_set_duplicate_taker (&hdest, *taker);
    }
  else
    {
      *(hdest.has_duplicate_taker) = 0;
    }
  if ((call = game_get_duplicate_call (&hsource)) != NULL)
    {
      game_set_duplicate_call (&hdest, *call);
    }
  else
    {
      *(hdest.has_duplicate_call) = 0;
    }
Vivien Kraus's avatar
Vivien Kraus committed
299
300
}

301
302
303
static inline TarotGameError
game_copy_as (TarotGame * dest, const TarotGame * source, TarotPlayer who)
{
Vivien Kraus's avatar
Vivien Kraus committed
304
305
306
307
308
309
310
  TarotGame rebuilt;
  TarotGameIterator i;
  const TarotGameEvent *e;
  /* The number of players and call will be overwritten */
  game_initialize (&rebuilt, 4, 0);
  game_iterator_setup (&i, source);
  while ((e = game_iterator_next_value (&i)) != NULL)
311
    {
Vivien Kraus's avatar
Vivien Kraus committed
312
313
314
315
      TarotPlayer dealt;
      size_t n;
      if (event_get_deal (e, &dealt, &n, 0, 0, NULL) == TAROT_EVENT_OK
          || event_get_deal_all (e, &n, 0, 0, NULL) == TAROT_EVENT_OK)
316
        {
Vivien Kraus's avatar
Vivien Kraus committed
317
318
319
320
321
322
323
324
          TarotGameEvent my_deal;
          unsigned int data[78];
          TarotCard my_hand[78];
          if (game_get_deal_of (source, who, &n, 0, 78, my_hand) !=
              TAROT_GAME_OK)
            {
              return TAROT_GAME_NA;
            }
325
          event_init_deal (&my_deal, who, n, my_hand, 78, data);
Vivien Kraus's avatar
Vivien Kraus committed
326
327
328
329
          if (game_add_event (&rebuilt, &my_deal) != TAROT_GAME_OK)
            {
              assert (0);
            }
330
        }
Vivien Kraus's avatar
Vivien Kraus committed
331
      else if (event_get_discard (e, &n, 0, 0, NULL) == TAROT_EVENT_OK)
332
        {
Vivien Kraus's avatar
Vivien Kraus committed
333
334
335
336
337
338
339
340
341
          TarotGameEvent my_discard;
          unsigned int data[6];
          TarotCard my_cards[6];
          TarotPlayer taker;
          if (game_get_taker (source, &taker) != TAROT_GAME_OK)
            {
              assert (0);
            }
          if (taker == who)
342
            {
Vivien Kraus's avatar
Vivien Kraus committed
343
344
              if (game_get_full_discard (source, &n, 0, 6, my_cards) !=
                  TAROT_GAME_OK)
345
                {
Vivien Kraus's avatar
Vivien Kraus committed
346
                  return TAROT_GAME_NA;
347
348
                }
            }
Vivien Kraus's avatar
Vivien Kraus committed
349
          else
350
            {
Vivien Kraus's avatar
Vivien Kraus committed
351
352
              if (game_get_public_discard (source, &n, 0, 6, my_cards) !=
                  TAROT_GAME_OK)
353
                {
Vivien Kraus's avatar
Vivien Kraus committed
354
                  return TAROT_GAME_NA;
355
356
                }
            }
357
          event_init_discard (&my_discard, n, my_cards, 6, data);
Vivien Kraus's avatar
Vivien Kraus committed
358
359
360
361
362
363
364
365
          if (game_add_event (&rebuilt, &my_discard) != TAROT_GAME_OK)
            {
              assert (0);
            }
        }
      else if (game_add_event (&rebuilt, e) != TAROT_GAME_OK)
        {
          assert (0);
366
367
        }
    }
Vivien Kraus's avatar
Vivien Kraus committed
368
  game_copy (dest, &rebuilt);
369
370
371
  return TAROT_GAME_OK;
}

Vivien Kraus's avatar
Vivien Kraus committed
372
373
374
375
376
377
378
379
static inline TarotStep
game_step (const TarotGame * game)
{
  TarotGameHandle handle;
  game_generalize ((TarotGame *) game, &handle);
  return game_get_step (&handle);
}

Vivien Kraus's avatar
Vivien Kraus committed
380
381
382
383
384
385
386
387
388
389
390
391
392
static inline TarotGameError
game_duplicate_set_deal (TarotGame * game, size_t n_owners,
                         const TarotPlayer * owners)
{
  TarotGameHandle handle;
  TarotStep step;
  game_generalize ((TarotGame *) game, &handle);
  step = game_get_step (&handle);
  if (step == TAROT_SETUP || step == TAROT_DEAL)
    {
      game_set_duplicate_deal (&handle, n_owners, owners);
      if (step == TAROT_DEAL)
        {
393
          int err = game_step_advance (game);
Vivien Kraus's avatar
Vivien Kraus committed
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
          if (err != 0)
            {
              return TAROT_GAME_INVEV;
            }
        }
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_TOOLATE;
}

static inline TarotGameError
game_duplicate_set_taker (TarotGame * game, TarotPlayer taker)
{
  TarotGameHandle handle;
  TarotStep step;
  size_t n_bids;
  int has_bids;
  game_generalize ((TarotGame *) game, &handle);
  step = game_get_step (&handle);
  has_bids = (game_get_bids (game, &n_bids, 0, 0, NULL) == TAROT_GAME_OK);
  if (step == TAROT_BIDS)
    {
      assert (has_bids);
    }
  if (step == TAROT_SETUP || step == TAROT_DEAL
      || (step == TAROT_BIDS && n_bids == 0))
    {
      game_set_duplicate_taker (&handle, taker);
      if (step == TAROT_BIDS)
        {
424
          int err = game_step_advance (game);
Vivien Kraus's avatar
Vivien Kraus committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
          if (err != 0)
            {
              return TAROT_GAME_INVEV;
            }
        }
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_TOOLATE;
}

static inline TarotGameError
game_duplicate_set_call (TarotGame * game, TarotCard call)
{
  TarotGameHandle handle;
  TarotStep step;
  game_generalize ((TarotGame *) game, &handle);
  step = game_get_step (&handle);
  if (step == TAROT_SETUP || step == TAROT_DEAL || step == TAROT_BIDS
      || step == TAROT_DECLS || step == TAROT_CALL)
    {
      game_set_duplicate_call (&handle, call);
      if (step == TAROT_CALL)
        {
448
          int err = game_step_advance (game);
Vivien Kraus's avatar
Vivien Kraus committed
449
450
451
452
453
454
455
456
457
458
          if (err != 0)
            {
              return TAROT_GAME_INVEV;
            }
        }
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_TOOLATE;
}

Vivien Kraus's avatar
Vivien Kraus committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
static inline void
game_iterator_setup (TarotGameIterator * iterator, const TarotGame * game)
{
  iterator->handle = game;
  iterator->step = TAROT_SETUP;
}

static inline size_t
game_n_players (const TarotGame * game)
{
  return game->opt.n_players;
}

static inline int
game_with_call (const TarotGame * game)
{
  return game->opt.with_call;
}

static inline TarotGameError
game_get_next (const TarotGame * game, TarotPlayer * next)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  switch (game_get_step (&h))
    {
    case TAROT_SETUP:
      return TAROT_GAME_NA;
    case TAROT_DEAL:
      return TAROT_GAME_NA;
    case TAROT_BIDS:
      assert (bids_has_next (h.bids, h.opt.n_players));
      *next = bids_next (h.bids);
      return TAROT_GAME_OK;
    case TAROT_DECLS:
      assert (decls_has_next (h.decls, h.opt.n_players));
      *next = decls_next (h.decls);
      return TAROT_GAME_OK;
    case TAROT_CALL:
      assert (call_waiting (h.call));
      *next = bids_taker (h.bids);
      return TAROT_GAME_OK;
    case TAROT_DOG:
      assert (doscard_expects_dog (&(h.doscard)));
      return TAROT_GAME_NA;
    case TAROT_DISCARD:
      assert (doscard_expects_discard (&(h.doscard)));
      *next = bids_taker (h.bids);
      return TAROT_GAME_OK;
    case TAROT_TRICKS:
      assert (handfuls_has_next (h.handfuls)
              || tricks_has_next (&(h.tricks)));
      if (handfuls_has_next (h.handfuls))
        {
          assert (handfuls_next (h.handfuls, h.opt.n_players)
                  == tricks_next (&(h.tricks)));
        }
      *next = tricks_next (&(h.tricks));
      return TAROT_GAME_OK;
    case TAROT_END:
      return TAROT_GAME_NA;
520
521
    default:
      assert (0);
Vivien Kraus's avatar
Vivien Kraus committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_main_player (const TarotGame * game, TarotPlayer * main)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  *main = game_get_dealt (&h);
  if (*main >= game->opt.n_players)
    {
      return TAROT_GAME_NA;
    }
  return TAROT_GAME_OK;
}

static inline int
game_get_deal_of_aux (const TarotGameHandle * game, TarotPlayer p,
                      TarotHand * dest)
{
  int error = 0;
  error = error || (hands_known (&(game->hands), p) == 0);
  hand_copy (dest, &(game->hands.hands[p]));
  if (error == 0)
    {
      unsigned int i_trick, current_trick;
      current_trick = tricks_current (&(game->tricks));
      for (i_trick = 0; i_trick < current_trick; i_trick++)
        {
          const TarotTrickHandle *trick = &(game->tricks.tricks[i_trick]);
          TarotCard played = trick_get_card_of (trick, p);
          error = error || hand_insert (dest, played);
        }
      if (current_trick < game->tricks.n_tricks)
        {
          const TarotTrickHandle *current =
            &(game->tricks.tricks[current_trick]);
          if (trick_has_played (current, p))
            {
              TarotCard played = trick_get_card_of (current, p);
              error = error || hand_insert (dest, played);
            }
        }
      if (bids_has_taker (game->bids, game->opt.n_players)
567
          && bids_taker (game->bids) == p
Vivien Kraus's avatar
Vivien Kraus committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
          && bids_discard_allowed (game->bids, game->opt.n_players)
          && doscard_discard_known (&(game->doscard)))
        {
          hand_swap (dest, &(game->doscard.discard), &(game->doscard.dog));
        }
    }
  return error;
}

static inline TarotGameError
game_get_deal_all (const TarotGame * game, size_t *n, size_t start,
                   size_t max, TarotPlayer * owners)
{
  TarotGameError ret = TAROT_GAME_NA;
  TarotPlayer buffer[78];
  TarotCard owned_cards[24];
  size_t n_owned;
  size_t i = 0;
  size_t n_players = game_n_players (game);
  *n = sizeof (buffer) / sizeof (buffer[0]);
  for (i = 0; i < sizeof (buffer) / sizeof (buffer[0]); i++)
    {
      buffer[i] = n_players;
    }
  for (i = 0; i < n_players; i++)
    {
      size_t j;
      ret =
        game_get_deal_of (game, i, &n_owned, 0,
                          sizeof (owned_cards) / sizeof (owned_cards[0]),
                          owned_cards);
      if (ret != TAROT_GAME_OK)
        {
          break;
        }
      for (j = 0; j < n_owned; j++)
        {
          TarotCard owned = owned_cards[j];
          buffer[owned] = i;
        }
    }
  for (i = 0; i < max && i + start < sizeof (buffer) / sizeof (*buffer); i++)
    {
      owners[i] = buffer[i + start];
    }
  return ret;
}

static inline void
query_hand (const TarotHand * hand, size_t *n, size_t start, size_t max,
            TarotCard * dest)
{
  size_t n_available = *(hand->n);
  if (start > n_available)
    {
      start = n_available;
    }
  if (start + max > n_available)
    {
      max = n_available - start;
    }
  *n = n_available;
630
  assert (start + max <= n_available);
Vivien Kraus's avatar
Vivien Kraus committed
631
632
633
634
  if (max != 0)
    {
      memcpy (dest, hand->buffer + start, max * sizeof (TarotCard));
    }
Vivien Kraus's avatar
Vivien Kraus committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
}

static inline TarotGameError
game_get_deal_of (const TarotGame * game, TarotPlayer who, size_t *n,
                  size_t start, size_t max, TarotCard * deal)
{
  TarotHand24 hand;
  TarotHand hand_handle = {
    .n = &(hand.n),
    .n_max = sizeof (hand.buffer) / sizeof (TarotCard),
    .buffer = hand.buffer
  };
  TarotGameHandle handle;
  TarotGameError ret;
  game_generalize ((TarotGame *) game, &handle);
  if (game_get_deal_of_aux (&handle, who, &hand_handle) != 0)
    {
      ret = TAROT_GAME_NA;
    }
  else
    {
      query_hand (&hand_handle, n, start, max, deal);
      ret = TAROT_GAME_OK;
    }
  return ret;
}

static inline int
game_get_cards_aux (const TarotGameHandle * game, TarotPlayer who,
                    TarotHand * dest)
{
  int error = hands_known (&(game->hands), who) == 0;
  if (error == 0)
    {
      hand_copy (dest, &(game->hands.hands[who]));
    }
  return error;
}

static inline size_t
game_count_cards_aux (const TarotGameHandle * game, TarotPlayer who)
{
  return tricks_count_remaining_cards (&(game->tricks), who);
}

static inline TarotGameError
game_get_cards (const TarotGame * game, TarotPlayer who, size_t *n,
                size_t start, size_t max, TarotCard * cards)
{
  TarotHand24 hand;
  TarotHand hand_handle = {
    .n = &hand.n,
    .n_max = sizeof (hand.buffer) / sizeof (TarotCard),
    .buffer = hand.buffer
  };
  TarotGameHandle h;
  TarotGameError ret;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_cards_aux (&h, who, &hand_handle) != 0)
    {
      ret = TAROT_GAME_NA;
      *n = game_count_cards_aux (&h, who);
    }
  else
    {
700
701
702
      assert (*(hand_handle.n) == hand.n);
      assert (*(hand_handle.n) <= 24);
      assert (hand_handle.n_max == 24);
Vivien Kraus's avatar
Vivien Kraus committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
      query_hand (&hand_handle, n, start, max, cards);
      ret = TAROT_GAME_OK;
    }
  return ret;
}

static inline TarotGameError
game_get_bids (const TarotGame * game, size_t *n, TarotPlayer start,
               size_t max, TarotBid * bids)
{
  TarotGameHandle h;
  TarotGameError ret = TAROT_GAME_NA;
  *n = 0;
  game_generalize ((TarotGame *) game, &h);
  if (h.bids->has_started)
    {
      *n = h.bids->n_bids;
      if (start > *n)
        {
          start = *n;
        }
      if (start + max > *n)
        {
          max = *n - start;
        }
Vivien Kraus's avatar
Vivien Kraus committed
728
729
730
731
      if (max != 0)
        {
          memcpy (bids, h.bids->bids + start, max * sizeof (TarotBid));
        }
Vivien Kraus's avatar
Vivien Kraus committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
      ret = TAROT_GAME_OK;
    }
  return ret;
}

static inline TarotGameError
game_get_taker (const TarotGame * game, TarotPlayer * taker)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  *taker = bids_taker (h.bids);
  if (bids_has_taker (h.bids, h.opt.n_players))
    {
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_declarations (const TarotGame * game, size_t *n, TarotPlayer start,
                       size_t max, int *decls)
{
  TarotGameHandle h;
  size_t i;
  game_generalize ((TarotGame *) game, &h);
  *n = 0;
  if (!(bids_has_taker (h.bids, h.opt.n_players)))
    {
      return TAROT_GAME_NA;
    }
  if (game_get_step (&h) >= TAROT_DECLS)
    {
      if (decls_has_next (h.decls, h.opt.n_players))
        {
          *n = decls_next (h.decls);
        }
      else
        {
          *n = game->opt.n_players;
        }
    }
  else
    {
      return TAROT_GAME_NA;
    }
  for (i = 0; i < max && i + start < *n; i++)
    {
      decls[i] = 0;
      if (decls_has_declarant (h.decls, h.opt.n_players)
          && decls_declarant (h.decls) == i + start)
        {
          decls[i] = 1;
        }
    }
  return TAROT_GAME_OK;
}

static inline TarotGameError
game_get_declarant (const TarotGame * game, TarotPlayer * declarant)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) < TAROT_DECLS)
    {
      *declarant = 0;
      return TAROT_GAME_NA;
    }
  if (decls_has_next (h.decls, h.opt.n_players))
    {
      *declarant = decls_next (h.decls);
      return TAROT_GAME_NA;
    }
  if (decls_has_declarant (h.decls, h.opt.n_players))
    {
      *declarant = decls_declarant (h.decls);
      return TAROT_GAME_OK;
    }
  *declarant = game->opt.n_players;
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_call (const TarotGame * game, TarotCard * card)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  TarotGameError ret;
  if (game->opt.with_call && call_card_known (h.call))
    {
      *card = call_card (h.call);
      ret = TAROT_GAME_OK;
    }
  else
    {
      ret = TAROT_GAME_NA;
    }
  return ret;
}

static inline TarotGameError
game_get_partner (const TarotGame * game, TarotPlayer * partner)
{
  TarotGameHandle h;
  TarotGameError ret;
  game_generalize ((TarotGame *) game, &h);
  if (game->opt.with_call && call_partner_known (h.call))
    {
      *partner = call_partner (h.call);
      assert (*partner < game->opt.n_players);
      ret = TAROT_GAME_OK;
    }
  else
    {
      game_get_taker (game, partner);
      ret = TAROT_GAME_NA;
    }
  return ret;
}

static inline TarotGameError
game_get_dog (const TarotGame * game, size_t *n, size_t start, size_t max,
              TarotCard * cards)
{
  TarotHand6 dog;
  TarotHand hand = {
    .n = &dog.n,
    .n_max = 6,
    .buffer = dog.buffer
  };
  TarotGameHandle h;
  TarotGameError ret = TAROT_GAME_NA;
  game_generalize ((TarotGame *) game, &h);
  dog.n = 0;
  if (doscard_dog_known (&(h.doscard)))
    {
      if (doscard_dog (&(h.doscard), &hand) != 0)
        {
          assert (0);
        }
      ret = TAROT_GAME_OK;
    }
873
874
875
  assert (*(hand.n) == dog.n);
  assert (*(hand.n) <= 6);
  assert (hand.n_max == 6);
Vivien Kraus's avatar
Vivien Kraus committed
876
877
878
879
880
881
882
883
884
885
  query_hand (&hand, n, start, max, cards);
  return ret;
}

static inline TarotGameError
game_get_full_discard (const TarotGame * game, size_t *n, size_t start,
                       size_t max, TarotCard * cards)
{
  TarotHand6 h6;
  TarotHand hand = {
886
    .n = &(h6.n),
Vivien Kraus's avatar
Vivien Kraus committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
    .n_max = 6,
    .buffer = h6.buffer
  };
  TarotGameHandle h;
  TarotGameError ret = TAROT_GAME_NA;
  game_generalize ((TarotGame *) game, &h);
  h6.n = 0;
  if (doscard_discard_known (&(h.doscard)))
    {
      if (doscard_discard (&(h.doscard), &hand) != 0)
        {
          assert (0);
        }
      ret = TAROT_GAME_OK;
    }
902
903
904
  assert (*(hand.n) == h6.n);
  assert (*(hand.n) <= 6);
  assert (hand.n_max == 6);
Vivien Kraus's avatar
Vivien Kraus committed
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
  query_hand (&hand, n, start, max, cards);
  return ret;
}

static inline TarotGameError
game_get_public_discard (const TarotGame * game, size_t *n, size_t start,
                         size_t max, TarotCard * cards)
{
  TarotHand6 h6;
  TarotHand hand = {
    .n = &h6.n,
    .n_max = 6,
    .buffer = h6.buffer
  };
  TarotGameHandle h;
  TarotGameError ret = TAROT_GAME_NA;
  game_generalize ((TarotGame *) game, &h);
  h6.n = 0;
  if (doscard_discard_known (&(h.doscard)))
    {
      if (doscard_discard_public (&(h.doscard), &hand) != 0)
        {
          assert (0);
        }
      ret = TAROT_GAME_OK;
    }
  query_hand (&hand, n, start, max, cards);
  return ret;
}

static inline TarotGameError
game_get_handful (const TarotGame * game, TarotPlayer p, int *size, size_t *n,
                  size_t start, size_t max, TarotCard * cards)
{
  TarotHand24 h24;
  TarotHand hand = {
    .n = &h24.n,
    .n_max = 24,
    .buffer = h24.buffer
  };
  TarotGameHandle h;
  TarotGameError ret = TAROT_GAME_NA;
  game_generalize ((TarotGame *) game, &h);
  h24.n = 0;
  *size = 0;
  if (handfuls_has_handful (h.handfuls, p, h.opt.n_players))
    {
      *size = handfuls_handful_size_of (h.handfuls, p, h.opt.n_players);
      if (handfuls_handful_of (h.handfuls, p, &hand) != 0)
        {
          assert (0);
        }
      ret = TAROT_GAME_OK;
    }
  query_hand (&hand, n, start, max, cards);
  return ret;
}

static inline size_t
game_n_tricks (const TarotGame * game)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  return h.tricks.n_tricks;
}

static inline TarotGameError
game_get_current_trick (const TarotGame * game, size_t *current)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  *current = tricks_current (&(h.tricks));
  if (game_get_step (&h) == TAROT_TRICKS
      || (game_get_step (&h) == TAROT_END
          && bids_contract (h.bids) != TAROT_PASS))
    {
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_lead_suit (const TarotGame * game, size_t i_trick,
                    TarotSuit * lead_suit)
{
  TarotGameHandle h;
  const TarotTrickHandle *trick;
  game_generalize ((TarotGame *) game, &h);
  trick = &(h.tricks.tricks[i_trick]);
  if (trick_has_lead_suit (trick))
    {
      *lead_suit = trick_lead_suit (trick);
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_max_trump (const TarotGame * game, size_t i_trick,
                    TarotNumber * max_trump)
{
  TarotGameHandle h;
  const TarotTrickHandle *trick;
  game_generalize ((TarotGame *) game, &h);
  trick = &(h.tricks.tricks[i_trick]);
  *max_trump = 0;
  if (trick_has_lead_suit (trick))
    {
      *max_trump = trick_max_trump (trick);
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_trick_leader (const TarotGame * game, size_t i_trick,
                       TarotPlayer * leader)
{
  TarotGameHandle h;
  const TarotTrickHandle *trick;
  game_generalize ((TarotGame *) game, &h);
  trick = &(h.tricks.tricks[i_trick]);
  if ((game_get_step (&h) == TAROT_TRICKS
       || game_get_step (&h) == TAROT_END) && trick_has_leader (trick))
    {
      *leader = trick_leader (trick);
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_trick_taker (const TarotGame * game, size_t i_trick,
                      TarotPlayer * taker)
{
  TarotGameHandle h;
  const TarotTrickHandle *trick;
  game_generalize ((TarotGame *) game, &h);
  trick = &(h.tricks.tricks[i_trick]);
  *taker = h.opt.n_players;
  if (trick_has_leader (trick))
    {
      *taker = trick_taker (trick);
      if (trick_n_cards (trick) == h.opt.n_players)
        {
          return TAROT_GAME_OK;
        }
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_trick_cards (const TarotGame * game, size_t i_trick,
                      size_t *n_cards,
                      size_t start, size_t max, TarotGameCardAnalysis * cards)
{
  TarotGameHandle h;
  const TarotTrickHandle *trick;
  size_t i;
  game_generalize ((TarotGame *) game, &h);
  if ((game_get_step (&h) == TAROT_TRICKS
       || game_get_step (&h) == TAROT_END)
1067
      && i_trick <= *(h.tricks.i_current) && i_trick < h.tricks.n_tricks)
Vivien Kraus's avatar
Vivien Kraus committed
1068
    {
1069
      size_t my_n_cards;
Vivien Kraus's avatar
Vivien Kraus committed
1070
      trick = &(h.tricks.tricks[i_trick]);
1071
1072
      my_n_cards = trick->trick->n_cards;
      if (start > my_n_cards)
Vivien Kraus's avatar
Vivien Kraus committed
1073
        {
1074
          start = my_n_cards;
Vivien Kraus's avatar
Vivien Kraus committed
1075
        }
1076
      for (i = 0; i < my_n_cards - start && i < max; i++)
Vivien Kraus's avatar
Vivien Kraus committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
        {
          TarotPlayer p = (trick_leader (trick) + i);
          if (p >= game->opt.n_players)
            {
              p -= game->opt.n_players;
            }
          cards[i].card = trick_get_card (trick, i + start);
          cards[i].cannot_follow = trick_cannot_follow (trick, p);
          cards[i].cannot_overtrump = trick_cannot_overtrump (trick, p);
          cards[i].cannot_undertrump = trick_cannot_undertrump (trick, p);
        }
1088
      *n_cards = my_n_cards;
Vivien Kraus's avatar
Vivien Kraus committed
1089
1090
1091
1092
1093
1094
      return TAROT_GAME_OK;
    }
  *n_cards = 0;
  return TAROT_GAME_NA;
}

1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
static inline TarotGameError
game_get_one_trick_card (const TarotGame * game, size_t i_trick, size_t i,
                         TarotGameCardAnalysis * out)
{
  size_t n_cards;
  TarotGameCardAnalysis my_out;
  TarotGameError base =
    game_get_trick_cards (game, i_trick, &n_cards, i, 1, &my_out);
  if (base == TAROT_GAME_OK && n_cards <= i)
    {
      base = TAROT_GAME_NA;
    }
  if (base == TAROT_GAME_OK)
    {
      *out = my_out;
    }
  return base;
}

Vivien Kraus's avatar
Vivien Kraus committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
static inline TarotGameError
game_get_points_in_trick (const TarotGame * game, size_t i_trick,
                          unsigned int *halfpoints, unsigned int *oudlers)
{
  TarotGameHandle h;
  const TarotTrickHandle *trick;
  game_generalize ((TarotGame *) game, &h);
  *halfpoints = 0;
  *oudlers = 0;
  if (game_get_step (&h) == TAROT_TRICKS && i_trick <= *(h.tricks.i_current))
    {
      trick = &(h.tricks.tricks[i_trick]);
      *halfpoints = trick_points (trick);
      *oudlers = trick_oudlers (trick);
      if (trick_n_cards (trick) == h.opt.n_players)
        {
          return TAROT_GAME_OK;
        }
    }
  return TAROT_GAME_NA;
}

static inline int
game_base_score (const TarotGameHandle * game)
{
  int ret = 0, hp = 0, od = 0, bar = 0, hfs, declarant, slammer;
  TarotPlayer taker = bids_taker (game->bids);
  TarotPlayer partner = (game->opt.with_call
                         && (call_partner_known (game->call))
                         ? call_partner (game->call) : taker);
  static const int bars[4] = { 56, 51, 41, 36 };
  if (game_get_step (game) != TAROT_END
      || bids_contract (game->bids) == TAROT_PASS)
    {
      return 0;
    }
  hp = tricks_points (&(game->tricks), taker);
  od = tricks_oudlers (&(game->tricks), taker);
  if (partner != taker)
    {
      hp += tricks_points (&(game->tricks), partner);
      od += tricks_oudlers (&(game->tricks), partner);
    }
  if (bids_discard_counted (game->bids, game->opt.n_players))
    {
      hp += doscard_points (&(game->doscard));
      od += doscard_oudlers (&(game->doscard));
    }
  assert (od >= 0 && od < 4);
  bar = 2 * bars[od];
  if (hp >= bar)
    {
      int diff = hp - bar;
      ret = (diff - diff / 2) + 25;
      hfs = +1;
    }
  else
    {
      int diff = bar - hp;
      ret = -(diff - diff / 2) - 25;
      hfs = -1;
    }
  ret += 10 * tricks_pab (&(game->tricks), taker, partner);
  ret *= bids_multiplier (game->bids, game->opt.n_players);
  ret += hfs * handfuls_bonus (game->handfuls, game->opt.n_players);
  if (decls_has_declarant (game->decls, game->opt.n_players))
    {
      TarotPlayer d = decls_declarant (game->decls);
      if (d == taker || d == partner)
        {
          declarant = +1;
        }
      else
        {
          declarant = -1;
        }
    }
  else
    {
      declarant = 0;
    }
  slammer = tricks_slam (&(game->tricks), taker, partner);
  if (declarant != 0 && slammer != 0)
    {
      ret += slammer * 400;
    }
  else if (declarant != 0)
    {
      ret -= declarant * 200;
    }
  else if (slammer != 0)
    {
      ret += declarant * 200;
    }
  return ret;
}

static inline TarotGameError
game_get_scores (const TarotGame * game, size_t *n, size_t start,
                 size_t max, int *scores)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  *n = 0;
  if (game_get_step (&h) == TAROT_END)
    {
      size_t i;
      size_t n_players = h.opt.n_players;
      int base = game_base_score (&h);
      TarotPlayer taker, partner;
      *n = n_players;
      game_get_taker (game, &taker);
      game_get_partner (game, &partner);
      if (start > n_players)
        {
          start = n_players;
        }
      if (start + max > n_players)
        {
          max = n_players - start;
        }
      for (i = 0; i < max && i + start < n_players; i++)
        {
          TarotPlayer p = i + start;
          if (p == partner || p == taker)
            {
              if (p == partner && p == taker)
                {
                  scores[i] = (n_players - 1) * base;
                }
              else if (p == partner)
                {
                  scores[i] = base;
                }
              else
                {
                  scores[i] = 2 * base;
                }
            }
          else
            {
              scores[i] = -base;
            }
        }
      if (start == 0 && max >= n_players)
        {
          int sum = 0;
          for (i = 0; i < n_players; i++)
            {
              sum += scores[i];
            }
          assert (sum == 0);
        }
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_bid (const TarotGame * game,
                   TarotPlayer * player, TarotBid * mini)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) == TAROT_BIDS)
    {
      *player = bids_next (h.bids);
      *mini = bids_contract (h.bids) + 1;
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_decl (const TarotGame * game,
                    TarotPlayer * player, int *allowed)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) == TAROT_DECLS)
    {
      *player = decls_next (h.decls);
      *allowed = 1;
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_call (const TarotGame * game, TarotPlayer * player,
                    TarotNumber * mini)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) == TAROT_CALL)
    {
      *player = bids_taker (h.bids);
      *mini = TAROT_JACK;
      if (hands_known (&(h.hands), *player))
        {
          *mini = hand_hint_call (&(h.hands.hands[*player]));
        }
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_discard_aux (const TarotGame * game,
                           TarotPlayer * player, size_t *n,
                           TarotHand * prio, TarotHand * additional)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) == TAROT_DISCARD
      && doscard_expects_discard (&(h.doscard)))
    {
      const TarotHand *dog = &(h.doscard.dog);
      *player = bids_taker (h.bids);
      hand_set (prio, NULL, 0);
      hand_set (additional, NULL, 0);
      *n = hand_max (dog);
      if (hands_known (&(h.hands), *player))
        {
          const TarotHand *takers_hand = &(h.hands.hands[*player]);
          if (hand_hint_discard (takers_hand, dog, prio, additional) != 0)
            {
              return TAROT_GAME_NA;
            }
        }
      return TAROT_GAME_OK;
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_discard (const TarotGame * game, TarotPlayer * player,
                       size_t *n_cards, size_t *n_prio, size_t start_prio,
                       size_t max_prio, TarotCard * prio,
                       size_t *n_additional, size_t start_additional,
                       size_t max_additional, TarotCard * additional)
{
  TarotCard buffer_prio[78];
  TarotCard buffer_additional[78];
  size_t real_n_prio = 0, real_n_additional = 0;
  TarotHand h_prio = {
    .n = &real_n_prio,
    .n_max = 78,
    .buffer = buffer_prio
  };
  TarotHand h_additional = {
    .n = &real_n_additional,
    .n_max = 78,
    .buffer = buffer_additional
  };
  TarotGameError ret =
    game_get_hint_discard_aux (game, player, n_cards, &h_prio, &h_additional);
  if (ret != TAROT_GAME_OK)
    {
      real_n_prio = 0;
      real_n_additional = 0;
    }
  query_hand (&h_prio, n_prio, start_prio, max_prio, prio);
  query_hand (&h_additional, n_additional, start_additional, max_additional,
              additional);
  return ret;
}

static inline TarotGameError
game_get_hint_handful_aux (const TarotGame * game,
                           TarotPlayer * player, size_t *n_simple,
                           size_t *n_double, size_t *n_triple,
                           TarotHand * prio, TarotHand * additional)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) == TAROT_TRICKS && handfuls_has_next (h.handfuls))
    {
      unsigned int n[4] = { 0 };
      handfuls_sizes (h.opt.n_players, n);
      *n_simple = n[1];
      *n_double = n[2];
      *n_triple = n[3];
      *player = handfuls_next (h.handfuls, h.opt.n_players);
      hand_set (prio, NULL, 0);
      hand_set (additional, NULL, 0);
      if (hands_known (&(h.hands), *player))
        {
          const TarotHand *next_hand = &(h.hands.hands[*player]);
          if (bids_discard_allowed (h.bids, h.opt.n_players)
              && doscard_discard_known (&(h.doscard))
              && *player == bids_taker (h.bids))
            {
              const TarotHand *discard = &(h.doscard.discard);
              if (hand_hint_handful_with_discard
                  (next_hand, discard, prio, additional) != 0)
                {
                  return TAROT_GAME_NA;
                }
              return TAROT_GAME_OK;
            }
          else
            {
              if (hand_hint_handful_without_discard
                  (next_hand, prio, additional) != 0)
                {
                  return TAROT_GAME_NA;
                }
              return TAROT_GAME_OK;
            }
        }
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_handful (const TarotGame * game, TarotPlayer * player,
                       size_t *n_simple, size_t *n_double, size_t *n_triple,
                       size_t *n_prio, size_t start_prio, size_t max_prio,
                       TarotCard * prio, size_t *n_additional,
                       size_t start_additional, size_t max_additional,
                       TarotCard * additional)
{
  TarotCard buffer_prio[78];
  TarotCard buffer_additional[78];
  size_t real_n_prio = 0, real_n_additional = 0;
  TarotHand h_prio = {
    .n = &real_n_prio,
    .n_max = 78,
    .buffer = buffer_prio
  };
  TarotHand h_additional = {
    .n = &real_n_additional,
    .n_max = 78,
    .buffer = buffer_additional
  };
  TarotGameError ret =
    game_get_hint_handful_aux (game, player, n_simple, n_double, n_triple,
                               &h_prio, &h_additional);
  if (ret != TAROT_GAME_OK)
    {
      real_n_prio = 0;
      real_n_additional = 0;
    }
  query_hand (&h_prio, n_prio, start_prio, max_prio, prio);
  query_hand (&h_additional, n_additional, start_additional, max_additional,
              additional);
  return ret;
}

static inline TarotGameError
game_get_hint_card_aux (const TarotGame * game,
                        TarotPlayer * player, TarotHand * playable)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  if (game_get_step (&h) == TAROT_TRICKS)
    {
      *player = tricks_next (&(h.tricks));
      hand_set (playable, NULL, 0);
      if (hands_known (&(h.hands), *player))
        {
          const TarotHand *next_hand = &(h.hands.hands[*player]);
          size_t current = tricks_current (&(h.tricks));
          const TarotTrickHandle *current_trick = &(h.tricks.tricks[current]);
          if (trick_has_lead_suit (current_trick))
            {
              TarotSuit lead_suit = trick_lead_suit (current_trick);
              TarotNumber max_trump = trick_max_trump (current_trick);
              assert (max_trump <= 21);
              if (hand_hint_card_with_lead (next_hand, lead_suit, max_trump,
                                            playable) != 0)
                {
                  return TAROT_GAME_NA;
                }
            }
          else if (h.opt.with_call && tricks_current (&(h.tricks)) == 0)
            {
              TarotCard c = call_card (h.call);
              if (hand_hint_card_without_lead_first (next_hand, c, playable)
                  != 0)
                {
                  return TAROT_GAME_NA;
                }
            }
          else
            {
              if (hand_hint_card_without_lead_not_first (next_hand, playable)
                  != 0)
                {
                  return TAROT_GAME_NA;
                }
            }
          return TAROT_GAME_OK;
        }
    }
  return TAROT_GAME_NA;
}

static inline TarotGameError
game_get_hint_card (const TarotGame * game, TarotPlayer * player,
                    size_t *n_playable, size_t start_playable,
                    size_t max_playable, TarotCard * playable)
{
  TarotCard buffer[78];
  size_t real_n = 0;
  TarotHand h = {
    .n = &real_n,
    .n_max = 78,
    .buffer = buffer
  };
  TarotGameError ret = game_get_hint_card_aux (game, player, &h);
  if (ret != TAROT_GAME_OK)
    {
      real_n = 0;
    }
  query_hand (&h, n_playable, start_playable, max_playable, playable);
  return ret;
}

1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
static inline int
game_deal_check_number_of_cards (const TarotGameHandle * game,
                                 const TarotPlayer * owners)
{
  size_t n_players = game->opt.n_players;
  TarotPlayer p;
  for (p = 0; p < n_players; p++)
    {
      size_t n_owned = 0;
      TarotCard c;
      for (c = 0; c < 78; c++)
        {
          if (owners[c] == p)
            {
              n_owned++;
            }
        }
      if (n_owned != game->hands.hands[p].n_max)
        {
          /* Player p has been given n_owned cards, not the expected
           * number */
          return 0;
        }
    }
  return 1;
}

Vivien Kraus's avatar
Vivien Kraus committed
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
static inline int
game_deal_has_petit_sec (const TarotPlayer * owners)
{
  TarotCard other_trump;
  int ret = 1;
  for (other_trump = TAROT_PETIT + 1; other_trump < 78; ++other_trump)
    {
      ret = ret && (owners[TAROT_PETIT] != owners[other_trump]);
    }
  return ret;
}

static inline int
game_check_deal_all (const TarotGameHandle * game, const TarotPlayer * owners)
{
  return ((game_get_step (game) == TAROT_DEAL
           || game_get_step (game) == TAROT_SETUP)
1578
          && game_deal_check_number_of_cards (game, owners)
Vivien Kraus's avatar
Vivien Kraus committed
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
          && !game_deal_has_petit_sec (owners));
}

static inline int
game_check_deal (const TarotGameHandle * game,
                 TarotPlayer myself, const TarotHand * cards)
{
  return ((game_get_step (game) == TAROT_DEAL
           || game_get_step (game) == TAROT_SETUP)
          && myself < game->opt.n_players
          && hand_size (cards) == game->hands.hands[0].n_max
          && !hand_has_petit_sec (cards));
}

static inline int
game_check_bid (const TarotGameHandle * game, TarotBid bid)
{
  return (game_get_step (game) == TAROT_BIDS
          && bids_check (game->bids, bid, game->opt.n_players));
}

static inline int
game_check_decl (const TarotGameHandle * game, int decl)
{
  return (game_get_step (game) == TAROT_DECLS
          && decls_check (game->decls, decl, game->opt.n_players));
}

static inline int
1608
1609
game_check_call (const TarotGame * game_for_counter,
                 const TarotGameHandle * game, TarotCard call)
Vivien Kraus's avatar
Vivien Kraus committed
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
{
  int ok = (game_get_step (game) == TAROT_CALL);
  TarotNumber n;
  TarotSuit s;
  ok = ok && (decompose (call, &n, &s) == 0);
  if (ok)
    {
      TarotPlayer taker = bids_taker (game->bids);
      const TarotHand *takers_hand = &(game->hands.hands[taker]);
      if (hands_known (&(game->hands), taker))
        {
          ok = hand_check_call (takers_hand, n);
        }
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
      else
        {
          TarotNumber superior;
          TarotCounter validator;
          counter_load_from_game (&validator, game_for_counter);
          /* Check that this is a face and that the taker may own all
           * superior faces */
          if (n < TAROT_JACK || n > TAROT_KING || s == TAROT_TRUMPS)
            {
              ok = 0;
            }
          for (superior = n + 1; superior <= TAROT_KING; superior++)
            {
              TarotCard superior_card;
              if (of (superior, s, &superior_card) != 0)
                {
                  assert (0);
                }
              if (!counter_may_own (&validator, 1, &taker, superior_card))
                {
                  ok = 0;
                }
            }
        }
Vivien Kraus's avatar
Vivien Kraus committed
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
    }
  return ok;
}

static inline int
game_check_dog (const TarotGameHandle * game, const TarotHand * dog)
{
  int not_in_hands;
  size_t i = 0;
  TarotPlayer owner;
  size_t n = *(dog->n);
  for (i = 0; i < n; i++)
    {
      not_in_hands = hands_locate (&(game->hands), dog->buffer[i], &owner);
      if (not_in_hands)
        {
          /* This card may be in the dog */
        }
      else
        {
          return 0;
        }
    }
  return (game_get_step (game) == TAROT_DOG
          && doscard_check_dog (&(game->doscard), dog));
}

static inline int
1675
1676
game_check_discard (const TarotGame * game_for_counter,
                    const TarotGameHandle * game, const TarotHand * discard)
Vivien Kraus's avatar
Vivien Kraus committed
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
{
  int ok = (game_get_step (game) == TAROT_DISCARD);
  TarotPlayer taker = bids_taker (game->bids);
  if (ok)
    {
      const TarotHand *takers_hand = &(game->hands.hands[taker]);
      if (hands_known (&(game->hands), taker))
        {
          const TarotHand *dog = &(game->doscard.dog);
          ok = (hand_check_discard (takers_hand, dog, discard)
                && doscard_check_discard (&(game->doscard), taker, discard));
        }
      else
        {
          ok =
            doscard_check_partial_discard (&(game->doscard), taker, discard);
        }
    }
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
  else if (ok)
    {
      size_t i_card;
      TarotCounter validator;
      counter_load_from_game (&validator, game_for_counter);
      for (i_card = 0; i_card < *(discard->n); i_card++)
        {
          if (!counter_may_own
              (&validator, 1, &taker, discard->buffer[i_card]))
            {
              ok = 0;
            }
        }
    }
Vivien Kraus's avatar
Vivien Kraus committed
1709
1710
1711
1712
  return ok;
}

static inline int
1713
1714
game_check_handful (const TarotGame * game_for_counter,
                    const TarotGameHandle * game, const TarotHand * handful)
Vivien Kraus's avatar
Vivien Kraus committed
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
{
  size_t i_current = tricks_current (&(game->tricks));
  int ok = (game_get_step (game) == TAROT_TRICKS && i_current == 0);
  TarotPlayer next = tricks_next (&(game->tricks));
  if (ok)
    {
      ok = handfuls_check (game->handfuls, next, handful,
                           game->opt.n_players);
    }
  if (ok && hands_known (&(game->hands), next))
    {
      TarotPlayer taker = bids_taker (game->bids);
      const TarotHand *next_hand = &(game->hands.hands[next]);
      if (bids_discard_allowed (game->bids, game->opt.n_players)
          && doscard_discard_known (&(game->doscard)) && taker == next)
        {
          const TarotHand *discard = &(game->doscard.discard);
          ok = hand_check_handful_with_discard (next_hand, discard, handful);
        }
      else
        {
          ok = hand_check_handful_without_discard (next_hand, handful);
        }
    }
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
  else if (ok)
    {
      size_t i_card;
      TarotCounter validator;
      counter_load_from_game (&validator, game_for_counter);
      for (i_card = 0; i_card < *(handful->n); i_card++)
        {
          if (!counter_may_own
              (&validator, 1, &next, handful->buffer[i_card]))
            {
              ok = 0;
            }
        }
    }
Vivien Kraus's avatar
Vivien Kraus committed
1753
1754
1755
1756
  return ok;
}

static inline int
1757
1758
game_check_card_full_aux (const TarotGame * game_for_counter,
                          const TarotGameHandle * game, TarotCard played,
Vivien Kraus's avatar
Vivien Kraus committed
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
                          int *cnf, TarotSuit * lead_suit, int *cno,
                          TarotNumber * max_trump, int *cnu)
{
  size_t i_current = tricks_current (&(game->tricks));
  int ok = (game_get_step (game) == TAROT_TRICKS);
  TarotPlayer next = tricks_next (&(game->tricks));
  const TarotTrickHandle *current = &(game->tricks.tricks[i_current]);
  if (ok && hands_known (&(game->hands), next))
    {
      const TarotHand *next_hand = &(game->hands.hands[next]);
      if (trick_has_lead_suit (current))
        {
          ok =
            hand_check_card_with_lead (next_hand,
                                       trick_lead_suit (current),
                                       trick_max_trump (current), played);
        }
      else if (i_current == 0 && game->opt.with_call)
        {
          ok =
            hand_check_card_without_lead_first (next_hand,
                                                call_card
                                                (game->call), played);
        }
      else
        {
          ok = hand_check_card_without_lead_not_first (next_hand, played);
        }
    }
1788
1789
1790
1791
1792
1793
1794
1795
  else if (ok)
    {
      /* We need to check if this card does not violate the rules.
       * This is subtle: what if the player trumps clubs but later it
       * is found that she has clubs?  For this we need a counter.
       * Fortunately, the counter API does not call this function
       * during its construction. */
      TarotCounter validator;
1796
      TarotCard call;
1797
1798
1799
      counter_initialize (&validator);
      counter_load_from_game (&validator, game_for_counter);
      ok = (counter_may_own (&validator, 1, &next, played));
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
      if (game_get_call (game_for_counter, &call) == TAROT_GAME_OK)
        {
          size_t current_trick;
          if (game_get_current_trick (game_for_counter, &current_trick) ==
              TAROT_GAME_OK)
            {
              if (current_trick == 0)
                {
                  TarotCard first_trick_lead_suit;
                  if (game_get_lead_suit
                      (game_for_counter, current_trick,
                       &first_trick_lead_suit) == TAROT_GAME_NA)
                    {
                      /* We do not allow cards of the call suit,
                       * except the called card itself */
                      TarotNumber played_number;
                      TarotCard played_suit;
                      TarotNumber call_number;
                      TarotCard call_suit;
                      if (decompose (call, &call_number, &call_suit) != 0)
                        {
                          assert (0);
                        }
                      if (decompose (played, &played_number, &played_suit) ==
                          0)
                        {
                          if (played_suit == call_suit
                              && played_number != call_number)
                            {
                              ok = 0;
                            }
                        }
                    }
                }
            }
        }
1836
    }
Vivien Kraus's avatar
Vivien Kraus committed
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
  if (ok)
    {
      assert (played < 78);
      trick_check_card_full (current, played, cnf, lead_suit, cno,
                             max_trump, cnu);
    }
  return ok;
}

static inline int
game_check_card (const TarotGame * game, TarotCard played,
                 int *cnf, TarotSuit * lead_suit, int *cno,
                 TarotNumber * max_trump, int *cnu)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
  assert (played < 78);
1854
1855
  return game_check_card_full_aux (game, &h, played, cnf, lead_suit, cno,
                                   max_trump, cnu);
Vivien Kraus's avatar
Vivien Kraus committed
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
}

static void
copy_cards (TarotHand * dest, size_t n, const TarotCard * buffer)
{
  size_t i = 0;
  *(dest->n) = 0;
  for (i = 0; i < n; i++)
    {
      if (hand_insert (dest, buffer[i]) != 0)
        {
          assert (0);
        }
    }
}

static inline int
1873
1874
game_check_event_aux (const TarotGame * game_not_generalized,
                      const TarotGameHandle * game,
Vivien Kraus's avatar
Vivien Kraus committed
1875
1876
1877
1878
1879
1880
                      const TarotGameEvent * event)
{
  size_t n_players, n_in_hand = 0;
  int with_call, decl;
  int error = 0;
  TarotPlayer myself;
1881
  TarotPlayer owners[78];
Vivien Kraus's avatar
Vivien Kraus committed
1882
1883
1884
1885
  TarotHand hand;
  TarotBid bid;
  TarotCard card;
  TarotCard buffer[78];
1886
1887
  TarotCard event_data[78];
  size_t n_event_data;
Vivien Kraus's avatar
Vivien Kraus committed
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
  hand.n = &n_in_hand;
  hand.n_max = 78;
  hand.buffer = buffer;
  switch (event_type (event))
    {
    case TAROT_SETUP_EVENT:
      error =
        (event_get_setup (event, &n_players, &with_call) != TAROT_EVENT_OK);
      assert (error == 0);
      return ((game_get_step (game) == TAROT_SETUP)
              && n_players >= 3 && n_players <= 5
              && (!with_call || n_players == 5));
    case TAROT_DEAL_EVENT:
1901
1902
1903
1904
1905
1906
      if (event_get_deal (event, &myself, &n_event_data, 0, 78, event_data) !=
          TAROT_EVENT_OK)
        {
          assert (0);
        }
      copy_cards (&hand, n_event_data, event_data);
Vivien Kraus's avatar
Vivien Kraus committed
1907
1908
      return game_check_deal (game, myself, &hand);
    case TAROT_DEAL_ALL_EVENT:
1909
1910
1911
1912
1913
      if (event_get_deal_all (event, &n_players, 0, 78, owners) !=
          TAROT_EVENT_OK)
        {
          assert (0);
        }
Vivien Kraus's avatar
Vivien Kraus committed
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
      return (n_players == 78 && game_check_deal_all (game, owners));
    case TAROT_BID_EVENT:
      error = event_get_bid (event, &bid);
      assert (error == 0);
      return game_check_bid (game, bid);
    case TAROT_DECL_EVENT:
      error = event_get_decl (event, &decl);
      assert (error == 0);
      return game_check_decl (game, decl);
    case TAROT_CALL_EVENT:
      error = event_get_call (event, &card);
      assert (error == 0);
1926
      return game_check_call (game_not_generalized, game, card);
Vivien Kraus's avatar
Vivien Kraus committed
1927
    case TAROT_DOG_EVENT:
1928
1929
1930
1931
1932
1933
      if (event_get_dog (event, &n_event_data, 0, 78, event_data) !=
          TAROT_EVENT_OK)
        {
          assert (0);
        }
      copy_cards (&hand, n_event_data, event_data);
Vivien Kraus's avatar
Vivien Kraus committed
1934
1935
      return game_check_dog (game, &hand);
    case TAROT_DISCARD_EVENT:
1936
1937
1938
1939
1940
1941
      if (event_get_discard (event, &n_event_data, 0, 78, event_data) !=
          TAROT_EVENT_OK)
        {
          assert (0);
        }
      copy_cards (&hand, n_event_data, event_data);
1942
      return game_check_discard (game_not_generalized, game, &hand);
Vivien Kraus's avatar
Vivien Kraus committed
1943
    case TAROT_HANDFUL_EVENT:
1944
1945
1946
1947
1948
1949
      if (event_get_handful (event, &n_event_data, 0, 78, event_data) !=
          TAROT_EVENT_OK)
        {
          assert (0);
        }
      copy_cards (&hand, n_event_data, event_data);
1950
      return game_check_handful (game_not_generalized, game, &hand);
Vivien Kraus's avatar
Vivien Kraus committed
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
    case TAROT_CARD_EVENT:
      error = event_get_card (event, &card);
      assert (error == TAROT_EVENT_OK);
      assert (card < 78);
      {
        int cnf;
        TarotSuit lead_suit;
        int cno;
        TarotNumber max_trump;
        int cnu;
1961
1962
1963
        return game_check_card_full_aux (game_not_generalized, game, card,
                                         &cnf, &lead_suit, &cno, &max_trump,
                                         &cnu);
Vivien Kraus's avatar
Vivien Kraus committed
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
      }
    }
  (void) error;
  return 0;
}

static inline int
game_check_event (const TarotGame * game, const TarotGameEvent * event)
{
  TarotGameHandle h;
  game_generalize ((TarotGame *) game, &h);
1975
  return game_check_event_aux (game, &h, event);
Vivien Kraus's avatar
Vivien Kraus committed
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
}

static inline TarotHand
game_create_hand (size_t n_max, size_t *n, TarotCard * buffer)
{
  TarotHand ret;
  ret.n_max = n_max;
  ret.n = n;
  ret.buffer = buffer;
  return ret;
}

1988
1989
static inline int game_add_deal_all (TarotGame * game_not_generalized,
                                     TarotGameHandle * game,
Vivien Kraus's avatar
Vivien Kraus committed
1990
                                     const TarotPlayer * owners);
1991
1992
1993
1994
static inline int game_add_bid (TarotGame * game_not_generalized,
                                TarotGameHandle * game, TarotBid bid);
static inline int game_add_call (TarotGame * game_not_generalized,
                                 TarotGameHandle * game, TarotCard call);
Vivien Kraus's avatar
Vivien Kraus committed
1995

Vivien Kraus's avatar
Vivien Kraus committed
1996
static inline int
1997
game_step_advance (TarotGame * game_not_generalized)
Vivien Kraus's avatar
Vivien Kraus committed
1998
{
1999
2000
2001
  TarotGameHandle game_handle;
  TarotGameHandle *game = &game_handle;
  game_generalize (game_not_generalized, game);
Vivien Kraus's avatar
Vivien Kraus committed
2002
2003
2004
2005
  switch (game_get_step (game))
    {
    case TAROT_SETUP:
      game_set_step (game, TAROT_DEAL);
2006
      return game_step_advance (game_not_generalized);
Vivien Kraus's avatar
Vivien Kraus committed
2007
2008
2009
2010
2011
2012
      break;
    case TAROT_DEAL:
      do
        {
          TarotPlayer i;
          int ok = 0;
Vivien Kraus's avatar
Vivien Kraus committed
2013
2014
2015
2016
2017
2018
          const TarotPlayer *owners;
          size_t n_owners;
          owners = game_get_duplicate_deal (game, &n_owners);
          if (owners != NULL)
            {
              assert (n_owners == 78);
2019
              return game_add_deal_all (game_not_generalized, game, owners);
Vivien Kraus's avatar
Vivien Kraus committed
2020
            }
Vivien Kraus's avatar
Vivien Kraus committed
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
          for (i = 0; i < game->opt.n_players; ++i)
            {
              if (hands_known (&(game->hands), i))
                {
                  ok = 1;
                }
            }
          if (ok)
            {
              int error = 0;
              error = bids_start (game->bids);
              assert (error == 0);
              (void) error;
              game_set_step (game, TAROT_BIDS);
2035
              return game_step_advance (game_not_generalized);
Vivien Kraus's avatar
Vivien Kraus committed
2036
2037
2038
2039
2040
            }
        }
      while (0);
      break;
    case TAROT_BIDS:
Vivien Kraus's avatar
Vivien Kraus committed
2041
2042
2043
2044
2045
2046
2047
      if (bids_has_next (game->bids, game->opt.n_players))
        {
          const TarotPlayer *forced_taker = game_get_duplicate_taker (game);
          if (forced_taker != NULL)
            {
              if (*forced_taker == bids_next (game->bids))
                {
2048
2049
                  return game_add_bid (game_not_generalized, game,
                                       TAROT_PUSH);
Vivien Kraus's avatar
Vivien Kraus committed
2050
2051
2052
                }
              else
                {
2053
2054
                  return game_add_bid (game_not_generalized, game,
                                       TAROT_PASS);
Vivien Kraus's avatar
Vivien Kraus committed
2055
2056
2057
2058
                }
            }
        }
      else
Vivien Kraus's avatar
Vivien Kraus committed
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
        {
          if (bids_has_taker (game->bids, game->opt.n_players))
            {
              int error = 0;
              error = decls_start (game->decls);
              assert (error == 0);
              (