tricks_private_impl.h 9.03 KB
Newer Older
1
2
/* tarot implements the rules of the tarot game
 * Copyright (C) 2017, 2018, 2019  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
 */

#include <tarot/tricks_private.h>

19
20
21
#ifndef H_TAROT_TRICKS_PRIVATE_IMPL_INCLUDED
#define H_TAROT_TRICKS_PRIVATE_IMPL_INCLUDED

Vivien Kraus's avatar
Vivien Kraus committed
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
static inline size_t
tricks_get_i_current (const TarotTricksHandle * tricks)
{
  return *(tricks->i_current);
}

static inline void
tricks_set_i_current (TarotTricksHandle * tricks, size_t value)
{
  *(tricks->i_current) = value;
}

static inline void
tricks_3_generalize (TarotTricks3 * tricks, TarotTricksHandle * gen)
{
  size_t i;
  gen->n_players = 3;
  gen->n_tricks = 24;
  gen->i_current = &(tricks->i_current);
  for (i = 0; i < 24; i++)
    {
      trick_3_generalize (&(tricks->tricks[i]), &(gen->tricks[i]));
    }
}

static inline void
tricks_4_generalize (TarotTricks4 * tricks, TarotTricksHandle * gen)
{
  size_t i;
  gen->n_players = 4;
  gen->n_tricks = 18;
  gen->i_current = &(tricks->i_current);
  for (i = 0; i < 18; i++)
    {
      trick_4_generalize (&(tricks->tricks[i]), &(gen->tricks[i]));
    }
}

static inline void
tricks_5_generalize (TarotTricks5 * tricks, TarotTricksHandle * gen)
{
  size_t i;
  gen->n_players = 5;
  gen->n_tricks = 15;
  gen->i_current = &(tricks->i_current);
  for (i = 0; i < 15; i++)
    {
      trick_5_generalize (&(tricks->tricks[i]), &(gen->tricks[i]));
    }
}

static inline void
tricks_initialize (TarotTricksHandle * tricks, size_t n_players)
{
  size_t i = 0;
  size_t n;
  switch (n_players)
    {
    case 3:
      n = 24;
      break;
    case 4:
      n = 18;
      break;
    case 5:
      n = 15;
      break;
    default:
90
91
      /* Invalid number of players */
      assert (0);
Vivien Kraus's avatar
Vivien Kraus committed
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
284
285
286
287
288
289
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
      break;
    }
  for (i = 0; i < n; ++i)
    {
      trick_initialize (&(tricks->tricks[i]), n_players);
    }
  trick_set_last (&(tricks->tricks[n - 1]), 1);
  tricks->n_players = n_players;
  tricks_set_i_current (tricks, 0);
}

static inline int
tricks_copy (TarotTricksHandle * dest, const TarotTricksHandle * source)
{
  size_t i;
  assert (dest->n_players == source->n_players);
  dest->n_tricks = source->n_tricks;
  tricks_set_i_current (dest, tricks_current (source));
  for (i = 0; i < source->n_tricks; ++i)
    {
      if (trick_copy (&(dest->tricks[i]), &(source->tricks[i])) != 0)
        {
          assert (0);
        }
    }
  return 0;
}

static inline size_t
tricks_size (const TarotTricksHandle * tricks)
{
  return tricks->n_tricks;
}

static inline size_t
tricks_current (const TarotTricksHandle * tricks)
{
  return tricks_get_i_current (tricks);
}

static inline size_t
tricks_count_remaining_cards (const TarotTricksHandle * tricks,
                              TarotPlayer who)
{
  size_t current = tricks_current (tricks);
  size_t max = tricks->n_tricks;
  if (current == max)
    {
      return 0;
    }
  if (trick_has_played (&(tricks->tricks[current]), who))
    {
      return max - current - 1;
    }
  else
    {
      return max - current;
    }
  assert (0);
  return 0;
}

static inline int
tricks_has_next (const TarotTricksHandle * tricks)
{
  return (tricks_get_i_current (tricks) < tricks->n_tricks
          &&
          (trick_n_cards
           (&(tricks->tricks[tricks_get_i_current (tricks)])) <
           tricks->n_players));
}

static inline TarotPlayer
tricks_next (const TarotTricksHandle * tricks)
{
  return trick_next (&(tricks->tricks[tricks_get_i_current (tricks)]));
}

static inline int
tricks_points (const TarotTricksHandle * tricks, TarotPlayer taker)
{
  int ret = 0;
  size_t i = 0;
  size_t i_current = tricks_get_i_current (tricks);
  for (i = 0; i < i_current; ++i)
    {
      if (trick_taker (&(tricks->tricks[i])) == taker)
        {
          ret += trick_points (&(tricks->tricks[i]));
        }
    }
  return ret;
}

static inline int
tricks_oudlers (const TarotTricksHandle * tricks, TarotPlayer taker)
{
  int ret = 0;
  size_t i = 0;
  size_t i_current = tricks_get_i_current (tricks);
  for (i = 0; i < i_current; ++i)
    {
      if (trick_taker (&(tricks->tricks[i])) == taker)
        {
          ret += trick_oudlers (&(tricks->tricks[i]));
        }
    }
  return ret;
}

static inline int
tricks_is_in_team (TarotPlayer taker, TarotPlayer partner, TarotPlayer needle)
{
  return (needle == taker || needle == partner);
}

static inline int
tricks_pab_check_normal (const TarotTricksHandle * tricks, TarotPlayer taker,
                         TarotPlayer partner)
{
  const TarotTrickHandle *last = &(tricks->tricks[tricks->n_tricks - 1]);
  int has_petit = trick_has_petit (last);
  TarotPlayer last_taker = trick_taker (last);
  int taker_is_attacker = tricks_is_in_team (taker, partner, last_taker);
  if (has_petit && taker_is_attacker)
    {
      return +1;
    }
  else if (has_petit)
    {
      return -1;
    }
  return 0;
}

static inline TarotPlayer
tricks_owner_of_petit_in_penultimate (const TarotTricksHandle * tricks)
{
  const TarotTrickHandle *trick = &(tricks->tricks[tricks->n_tricks - 2]);
  size_t pos = 0;
  int error = 0;
  error = trick_locate (trick, TAROT_PETIT, &pos);
  assert (error == 0);
  (void) error;
  return trick_get_player (trick, pos);
}

static inline int
  tricks_owner_of_petit_in_penultimate_has_played_excuse_in_last
  (const TarotTricksHandle * tricks)
{
  const TarotTrickHandle *last = &(tricks->tricks[tricks->n_tricks - 1]);
  TarotPlayer oopip = tricks_owner_of_petit_in_penultimate (tricks);
  TarotCard card_of_oopip = trick_get_card_of (last, oopip);
  return card_of_oopip == TAROT_EXCUSE;
}

static inline int
tricks_pab (const TarotTricksHandle * tricks, TarotPlayer taker,
            TarotPlayer partner)
{
  const TarotTrickHandle *last, *penultimate;
  assert (tricks_get_i_current (tricks) >= tricks->n_tricks);
  assert (tricks->n_tricks >= 2);
  last = &(tricks->tricks[tricks->n_tricks - 1]);
  penultimate = &(tricks->tricks[tricks->n_tricks - 2]);
  if (trick_taken_by_excuse (last)
      && trick_has_petit (penultimate)
      &&
      tricks_owner_of_petit_in_penultimate_has_played_excuse_in_last (tricks))
    {
      TarotPlayer slammer = trick_taker (last);
      if (tricks_is_in_team (taker, partner, slammer))
        {
          return +1;
        }
      else
        {
          return -1;
        }
    }
  return tricks_pab_check_normal (tricks, taker, partner);
}

static inline int
tricks_slam (const TarotTricksHandle * tricks, TarotPlayer taker,
             TarotPlayer partner)
{
  size_t i = 0;
  int attack = 1, defence = 1;
  for (i = 0; i < tricks_get_i_current (tricks); ++i)
    {
      if (tricks_is_in_team
          (taker, partner, trick_taker (&(tricks->tricks[i]))))
        {
          defence = 0;
        }
      else
        {
          attack = 0;
        }
    }
  return attack - defence;
}

static inline int
tricks_locate (const TarotTricksHandle * tricks, TarotCard needle,
               size_t *i_trick, size_t *position)
{
  int error = 0;
  size_t i;
  size_t i_current = tricks_get_i_current (tricks);
  for (i = 0; i <= i_current && i < tricks->n_tricks; ++i)
    {
      error = trick_locate (&(tricks->tricks[i]), needle, position);
      if (error == 0)
        {
          *i_trick = i;
          return 0;
        }
    }
  return 1;
}

static inline int
tricks_set_leader (TarotTricksHandle * tricks, TarotPlayer leader)
{
  return trick_set_leader (&(tricks->tricks[0]), leader);
}

static inline int
tricks_next_is_slamming (const TarotTricksHandle * tricks, TarotPlayer taker,
                         TarotPlayer partner)
{
  TarotPlayer next = tricks_next (tricks);
  int slam_team;
  slam_team = tricks_slam (tricks, taker, partner);
  if (tricks_is_in_team (taker, partner, next))
    {
      return slam_team > 0;
    }
  else
    {
      return slam_team < 0;
    }
  return 0;
}

int
tricks_add (TarotTricksHandle * tricks, TarotPlayer taker,
            TarotPlayer partner, TarotCard card)
{
  int force = 0;
  int error = 0;
  TarotTrickHandle *current;
  size_t i_current = tricks_get_i_current (tricks);
  if (card == TAROT_EXCUSE && i_current + 1 == tricks->n_tricks
      && tricks_next_is_slamming (tricks, taker, partner))
    {
      force = 1;
    }
  current = &(tricks->tricks[i_current]);
  error = trick_add (current, card, force);
  if (error == 0 && trick_n_cards (current) == tricks->n_players)
    {
      if (i_current + 1 < tricks->n_tricks)
        {
          TarotPlayer taker = trick_taker (current);
          error = trick_set_leader (&(tricks->tricks[i_current + 1]), taker);
        }
      tricks_set_i_current (tricks, i_current + 1);
    }
  return error;
}
366
367

#endif /* not H_TAROT_TRICKS_PRIVATE_IMPL_INCLUDED */