-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
520 lines (468 loc) · 15.5 KB
/
Engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
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
520
/*=============================================================================
* TarotClub - Engine.cpp
*=============================================================================
* Main Tarot engine
*=============================================================================
* TarotClub ( http://www.tarotclub.fr ) - This file is part of TarotClub
* Copyright (C) 2003-2999 - Anthony Rabine
*
* TarotClub is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TarotClub 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
* along with TarotClub. If not, see <http://www.gnu.org/licenses/>.
*
*=============================================================================
*/
#include <chrono>
#include <random>
#include <sstream>
#include <iostream>
#include "Engine.h"
#include "DealGenerator.h"
#include "Identity.h"
#include "Util.h"
#include "Log.h"
#include "System.h"
#include "JsonReader.h"
/*****************************************************************************/
Engine::Engine()
: mSequence(STOPPED)
, mPosition(0U)
, mTrickCounter(0U)
{
std::chrono::system_clock::rep seed = std::chrono::system_clock::now().time_since_epoch().count(); // rep is long long
mSeed = static_cast<std::uint32_t>(seed);
mCtx.Initialize();
for (std::uint8_t i = 0U; i < 5U; i++)
{
mHandleAsked[i] = false;
}
}
/*****************************************************************************/
Engine::~Engine()
{
}
/*****************************************************************************/
/**
* @brief Engine::Initialize
* Call this method before clients connections
*/
void Engine::Initialize()
{
mSequence = STOPPED;
}
/*****************************************************************************/
void Engine::CreateTable(std::uint8_t nbPlayers)
{
// Save parameters
mCtx.mNbPlayers = nbPlayers;
// 1. Initialize internal states
mCtx.Initialize();
// Choose the dealer
mDealer = DealGenerator::RandomPlace(mCtx.mNbPlayers);
// Wait for ready
mSequence = WAIT_FOR_PLAYERS;
}
/*****************************************************************************/
void Engine::NewGame()
{
mSequence = WAIT_FOR_READY;
}
/*****************************************************************************/
Tarot::Distribution Engine::NewDeal(const Tarot::Distribution &shuffle)
{
Tarot::Distribution shReturned = shuffle;
// 1. Initialize internal states
mCtx.Initialize();
mPosition = 0U;
currentTrick.Clear();
// 2. Choose the dealer and the first player to start the bid
mDealer = mDealer.Next(mCtx.mNbPlayers);
mCurrentPlayer = mDealer.Next(mCtx.mNbPlayers); // The first player on the dealer's right begins the bid
// 3. Give cards to all players
CreateDeal(shReturned);
// 4. Prepare the wait for ack
mSequence = WAIT_FOR_CARDS;
return shReturned;
}
/*****************************************************************************/
/**
* @brief Engine::StartDeal
* @return The first player to play
*/
Place Engine::StartDeal()
{
mTrickCounter = 0U;
mPosition = 0U;
for (std::uint8_t i = 0U; i < 5U; i++)
{
mHandleAsked[i] = false;
}
// In case of slam, the first player to play is the taker.
// Otherwise, it is the player on the right of the dealer
if (mCtx.mBid.slam == true)
{
mCurrentPlayer = mCtx.mBid.taker;
}
else
{
mCurrentPlayer = mDealer.Next(mCtx.mNbPlayers); // The first player on the dealer's right
}
std::stringstream ss;
ss << "Taker: " << mCtx.mBid.taker.ToString() << " / ";
ss << "Contract: " << mCtx.mBid.contract.ToString();
TLogInfo(ss.str());
mCtx.mFirstPlayer = mCurrentPlayer;
return mCurrentPlayer;
}
/*****************************************************************************/
bool Engine::SetDiscard(const Deck &discard)
{
bool valid = mPlayers[mCtx.mBid.taker.Value()].TestDiscard(discard, mCtx.mDog, mCtx.mNbPlayers);
if (valid)
{
// Add the dog to the player's deck, and then filter the discard
mPlayers[mCtx.mBid.taker.Value()] += mCtx.mDog;
mPlayers[mCtx.mBid.taker.Value()].RemoveDuplicates(discard);
mCtx.mDiscard = discard;
mCtx.mDiscard.SetOwner(Team(Team::ATTACK));
std::stringstream ss;
ss << "Received discard: " << discard.ToString() << " / ";
ss << "Taker's deck after the discard: " << mPlayers[mCtx.mBid.taker.Value()].ToString();
TLogInfo(ss.str());
mSequence = WAIT_FOR_START_DEAL;
}
return valid;
}
/*****************************************************************************/
/**
* @brief Engine::SetHandle
* @param handle
* @param p
* @return true if the handle is valid, otherwise false
*/
bool Engine::SetHandle(const Deck &handle, Place p)
{
bool valid = mPlayers[p.Value()].TestHandle(handle);
if (valid)
{
mCtx.SetHandle(handle, p);
mSequence = WAIT_FOR_SHOW_HANDLE;
}
return valid;
}
/*****************************************************************************/
bool Engine::SetCard(const Card &c, Place p)
{
bool ret = false;
if (mPlayers[p.Value()].CanPlayCard(c, currentTrick))
{
currentTrick.Append(c);
mPlayers[p.Value()].Remove(c);
std::stringstream ss;
ss << "Turn: " << (int)mTrickCounter + 1 << ", Tick: " << currentTrick.ToString() << ", Player " << p.ToString() << " played " << c.ToString() << " Engine player deck is: " << mPlayers[p.Value()].ToString();
TLogInfo(ss.str());
// ------- PREPARE NEXT ONE
mPosition++; // done for this player
mCurrentPlayer = mCurrentPlayer.Next(mCtx.mNbPlayers); // next player!
mSequence = WAIT_FOR_SHOW_CARD;
ret = true;
}
else
{
std::stringstream ss;
ss << "The player " << p.ToString() << " cannot play the card: " << c.ToString()
<< " on turn " << (int)mTrickCounter + 1 << " Engine deck is: " << mPlayers[p.Value()].ToString();
TLogError(ss.str());
}
return ret;
}
/*****************************************************************************/
Contract Engine::SetBid(Contract c, bool slam, Place p)
{
c = mCtx.SetBid(c, slam, p);
// ------- PREPARE NEXT ONE
mPosition++; // done for this player
mCurrentPlayer = mCurrentPlayer.Next(mCtx.mNbPlayers); // next player!
mSequence = WAIT_FOR_SHOW_BID;
return c;
}
/*****************************************************************************/
bool Engine::SetKingCalled(const Card &c)
{
bool success = false;
Deck::Statistics stats;
mPlayers[mCtx.mBid.taker.Value()].AnalyzeSuits(stats);
if (mCtx.CheckKingCall(c, stats))
{
// Appel au roi dans le chien ou dans le deck du preneur ?
if (mCtx.mDog.HasCard(c) || mPlayers[mCtx.mBid.taker.Value()].HasCard(c))
{
// Il est tout seul car il a appelé une carte à lui ou au chien
mCtx.mBid.partner = mCtx.mBid.taker;
success = true;
}
else
{
// On recherche son partenaire
Place partner = mCtx.mBid.taker.Next(5);
for (uint32_t i = 0; i < 4; i++)
{
if (mPlayers[partner.Value()].HasCard(c))
{
mCtx.mBid.partner = partner;
success = true;
break;
}
partner = partner.Next(5);
}
}
mCtx.mKingCalled = c; // sauvegarde du roi appelé
mSequence = Engine::WAIT_FOR_SHOW_KING_CALL;
}
return success;
}
/*****************************************************************************/
void Engine::Stop()
{
mSequence = STOPPED;
}
/*****************************************************************************/
Deck Engine::GetDeck(Place p)
{
Deck deck;
if (p < Place(Place::NOWHERE))
{
deck = mPlayers[p.Value()];
}
return deck;
}
/*****************************************************************************/
Points Engine::GetCurrentGamePoints()
{
return mCurrentPoints;
}
/*****************************************************************************/
/**
* @brief Engine::GameSequence
* @return true if the trick is finished
*/
void Engine::GameSequence()
{
// If end of trick, prepare next one
if (IsEndOfTrick())
{
TLogInfo("----------------------------------------------------\n");
// The current trick winner will begin the next trick
mCurrentPlayer = mCtx.SetTrick(currentTrick, mTrickCounter);
currentTrick.Clear();
mSequence = WAIT_FOR_END_OF_TRICK;
}
// Special case of first round: players can declare a handle
else if ((mTrickCounter == 0U) &&
(!mHandleAsked[mCurrentPlayer.Value()]))
{
mHandleAsked[mCurrentPlayer.Value()] = true;
mSequence = WAIT_FOR_HANDLE;
}
else
{
std::stringstream message;
message << "Turn: " << (std::uint32_t)mTrickCounter << " player: " << mCurrentPlayer.ToString();
TLogInfo(message.str());
mSequence = WAIT_FOR_PLAYED_CARD;
}
}
/*****************************************************************************/
void Engine::EndOfDeal(JsonObject &json)
{
mCurrentPoints.Clear();
mCtx.AnalyzeGame(mCurrentPoints);
mCtx.SaveToJson(json);
mSequence = WAIT_FOR_END_OF_DEAL;
}
/*****************************************************************************/
void Engine::ManageAfterBidSequence()
{
if (mCtx.ManageDogAfterBid())
{
// Show the dog to all the players
mSequence = WAIT_FOR_SHOW_DOG;
}
else
{
// We do not display the dog and start the deal immediatly
mSequence = WAIT_FOR_START_DEAL;
}
}
/*****************************************************************************/
/**
* @brief Engine::BidSequence
* @return The next sequence to go
*/
void Engine::BidSequence()
{
// If a slam has been announced, we start immediately the deal
if (IsEndOfTrick() || mCtx.mBid.slam)
{
if (mCtx.mBid.contract == Contract::PASS)
{
// All the players have passed, deal again new cards
mSequence = WAIT_FOR_ALL_PASSED;
}
// On a terminé les enchères, on bascule sur le choix du roi
// dans le cas du jeu à 5 joueurs
else if ((mCtx.mNbPlayers == 5) && (mSequence == WAIT_FOR_SHOW_BID))
{
mCtx.mBid.partner = mCtx.mBid.taker; // par défaut, le partenaire est le preneur (cas à 5 joueurs que l'on tente
// de généraliser pour tous les autres modes de jeu
mSequence = WAIT_FOR_KING_CALL;
}
else
{
ManageAfterBidSequence();
}
}
else
{
mSequence = WAIT_FOR_BID;
}
}
/*****************************************************************************/
void Engine::DiscardSequence()
{
mSequence = WAIT_FOR_DISCARD;
}
/*****************************************************************************/
bool Engine::IsEndOfTrick()
{
bool endOfTrick = false;
if (mPosition >= mCtx.mNbPlayers)
{
// Trick as ended, all the players have played
mPosition = 0U;
mTrickCounter++;
endOfTrick = true;
}
return endOfTrick;
}
/*****************************************************************************/
void Engine::CreateDeal(Tarot::Distribution &shuffle)
{
DealGenerator editor;
bool random = true;
if (shuffle.mType == Tarot::Distribution::CUSTOM_DEAL)
{
std::string fullPath;
// If not an absolute path, then it is a path relative to the home directory
if (!Util::FileExists(shuffle.mFile))
{
fullPath = System::HomePath() + shuffle.mFile;
}
else
{
fullPath = shuffle.mFile;
}
if (!editor.LoadFile(fullPath))
{
// Fall back to default mode
TLogError("Cannot load custom deal file: " + fullPath);
}
else if (editor.IsValid(mCtx.mNbPlayers))
{
random = false;
// Override the current player
mCurrentPlayer = editor.GetFirstPlayer();
mDealer = mCurrentPlayer.Previous(mCtx.mNbPlayers);
}
else
{
// Fall back to default mode
TLogError("Invalid deal file");
}
}
if (random)
{
bool valid = true;
do
{
if (shuffle.mType == Tarot::Distribution::NUMBERED_DEAL)
{
valid = editor.CreateRandomDeal(mCtx.mNbPlayers, shuffle.mSeed);
if (!valid)
{
// The provided seed does dot generate a valid deal, so switch to a random one
shuffle.mType = Tarot::Distribution::RANDOM_DEAL;
}
}
else
{
valid = editor.CreateRandomDeal(mCtx.mNbPlayers);
}
}
while (!valid);
// Save the seed
shuffle.mSeed = editor.GetSeed();
}
#ifdef UNIT_TEST
editor.SaveFile("unit_test_current_deal.json");
#endif
// Copy deal editor cards to engine
for (std::uint32_t i = 0U; i < mCtx.mNbPlayers; i++)
{
Place p(i);
mPlayers[i].Clear();
mPlayers[i].Append(editor.GetPlayerDeck(p));
TLogInfo( "Player " + p.ToString() + " deck: " + mPlayers[i].ToString());
#ifdef UNIT_TEST
std::cout << "Player " + p.ToString() + " deck: " + mPlayers[i].ToString() << std::endl;
#endif
}
mCtx.mDog = editor.GetDogDeck();
TLogInfo("Dog deck: " + editor.GetDogDeck().ToString());
}
/*****************************************************************************/
bool Engine::LoadGameDealLog(const std::string &fileName)
{
bool ret = false;
JsonValue json;
#ifdef TAROT_DEBUG
std::cout << "File: " << fileName << std::endl;
#endif
if (JsonReader::ParseFile(json, fileName))
{
ret = mCtx.LoadFromJson(json);
}
else
{
TLogError("Cannot open Json deal file");
}
return ret;
}
/*****************************************************************************/
bool Engine::LoadGameDeal(const std::string &buffer)
{
bool ret = false;
JsonValue json;
if (JsonReader::ParseString(json, buffer))
{
ret = mCtx.LoadFromJson(json);
}
else
{
TLogError("Cannot analyze JSON buffer");
}
return ret;
}
//=============================================================================
// End of file Engine.cpp
//=============================================================================