forked from trolley813/OpenFool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.cpp
606 lines (569 loc) · 21.8 KB
/
table.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
521
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
567
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
#include "table.h"
#include "carditem.h"
#include "player.h"
#include <QParallelAnimationGroup>
#include <QGraphicsSimpleTextItem>
#include <QSequentialAnimationGroup>
#include <QPropertyAnimation>
#include <QGraphicsView>
#include <QDebug>
#include <QThread>
#include <QPushButton>
#include <QGraphicsProxyWidget>
#include <QEventLoop>
#include <QMessageBox>
#include <QApplication>
const int CARD_WIDTH = 360, CARD_HEIGHT = 540;
const QPointF TALON_LOCATION(-4 * CARD_WIDTH, 0);
const QPointF DISCARD_PILE_LOCATION(4 * CARD_WIDTH, 0);
const int PLAYERS_COUNT
= 4; // in future may be adapted to other number of players
const int DEAL_LIMIT = 6;
const QPointF PLAYER_LOCATIONS[PLAYERS_COUNT]
= {{0, 2 * CARD_HEIGHT},
{-3 * CARD_WIDTH, -1.5 * CARD_HEIGHT},
{0, -2.5 * CARD_HEIGHT},
{3 * CARD_WIDTH, -1.5 * CARD_HEIGHT}};
const QPointF BUTTON_LOCATION = {-4 * CARD_WIDTH, 2 * CARD_HEIGHT};
const QString PLAYER_NAMES[PLAYERS_COUNT] = {"Denis", "Girotte", "Mama", "Kot"};
const QPointF DELTA_HUMAN(0.25 * CARD_WIDTH, 0),
DELTA_AI(0.05 * CARD_WIDTH, 0.05 * CARD_WIDTH);
const QPointF CARD_FIELD_LOCATIONS[DEAL_LIMIT]
= {{-1 * CARD_WIDTH, -1 * CARD_HEIGHT},
{0.5 * CARD_WIDTH, -1 * CARD_HEIGHT},
{2 * CARD_WIDTH, -1 * CARD_HEIGHT},
{-1 * CARD_WIDTH, 0.5 * CARD_HEIGHT},
{0.5 * CARD_WIDTH, 0.5 * CARD_HEIGHT},
{2 * CARD_WIDTH, 0.5 * CARD_HEIGHT}};
const QColor DEFAULT_BACKGROUND(86, 156, 30, 230);
Table::Table(QSettings *settings, QObject *parent) : QGraphicsScene(parent)
{
_deck = new CardDeck(this);
setBackgroundBrush(
QBrush(settings->value("rendering/background", DEFAULT_BACKGROUND)
.value<QColor>(),
Qt::BrushStyle::SolidPattern));
setSceneRect(QRectF(QPointF(-5 * CARD_WIDTH, -3 * CARD_HEIGHT),
QPointF(+5 * CARD_WIDTH, +3 * CARD_HEIGHT)));
_sortingMode
= SortingMode(settings->value("rendering/sorting",
SortingMode::ESM_UNSORTED).toInt());
for (Card c : _deck->cards()) {
CardItem *ci
= new CardItem(c, settings->value("cards/deck", "rus").toString());
ci->setPos(TALON_LOCATION);
ci->hide();
QObject::connect(ci, SIGNAL(cardClicked(Card)), this,
SLOT(onCardClick(Card)));
this->addItem(ci);
_cardItems[c] = ci;
}
QFont mainFont("Serif", CARD_HEIGHT / 10, QFont::Bold);
for (int i = 0; i < PLAYERS_COUNT; i++) {
_playerNames << settings->value(QString("players/name%1").arg(i + 1),
PLAYER_NAMES[i]).toString();
_players << new Player(this, i, _playerNames[i]);
QGraphicsSimpleTextItem *nameItem
= new QGraphicsSimpleTextItem(_playerNames[i]);
nameItem->setPos(PLAYER_LOCATIONS[i] - QPointF(0, 0.15 * CARD_HEIGHT));
this->addItem(nameItem);
_nameItems << nameItem;
nameItem->setFont(mainFont);
nameItem->hide();
PlayerBubbleItem *bubble = new PlayerBubbleItem(
PLAYER_LOCATIONS[i] - QPointF(0, 0.5 * CARD_HEIGHT));
this->addItem(bubble);
_playerBubbles << bubble;
bubble->hide();
_outOfPlay << false;
}
_deckCardsItem
= new QGraphicsSimpleTextItem(QString::number(_deck->cards().length()));
_deckCardsItem->setPos(TALON_LOCATION - QPointF(0, 0.15 * CARD_HEIGHT));
_deckCardsItem->setFont(mainFont);
_deckCardsItem->hide();
this->addItem(_deckCardsItem);
// Add action button
_actionButton = new QPushButton(tr("Action"));
_actionButton->setFont(mainFont);
QGraphicsProxyWidget *proxy = this->addWidget(_actionButton);
proxy->setGeometry(
QRectF(BUTTON_LOCATION, QSizeF(CARD_WIDTH, 0.3 * CARD_HEIGHT)));
// Connect signals
connect(_deck, SIGNAL(cardDrawn()), this, SLOT(_updateDeckCardsLabel()));
for (int i = 0; i < PLAYERS_COUNT; i++) {
connect(_players[i], &Player::done, [=]() { this->onPlayerDone(i); });
connect(_players[i], &Player::cardThrown,
[=](Card c) { this->onPlayerThrows(i, c); });
connect(_players[i], &Player::cardBeaten,
[=](Card c) { this->onPlayerBeats(i, c); });
connect(_players[i], &Player::take, [=]() { this->onPlayerTakes(i); });
}
connect(QApplication::instance(), SIGNAL(aboutToQuit()), this,
SLOT(onQuitRequest()));
}
void Table::newGame()
{
// Show player names
for (auto nameItem : _nameItems)
nameItem->show();
_deckCardsItem->show();
// Prepare the deck
for (auto ci : _cardItems.values()) {
ci->setFaceUp(false);
ci->show();
ci->setPos(TALON_LOCATION);
ci->setRotation(0);
}
_deck->reset();
_deck->shuffle();
for (int i = 0; i < _deck->cards().length(); i++) {
_cardItems[_deck->cards()[i]]->setZValue(i);
}
for (Player *p : _players) {
p->clearHand();
_outOfPlay[p->index()] = false;
}
// Determine the trump
Card trump = _deck->cards()[0];
CardItem *trumpItem = _cardItems[trump];
trumpItem->setFaceUp(true);
_trumpSuit = trump.suit();
QParallelAnimationGroup *animGroup = new QParallelAnimationGroup();
QPropertyAnimation *trumpRot
= new QPropertyAnimation(trumpItem, "rotation");
trumpRot->setDuration(100);
trumpRot->setKeyValueAt(0, 0);
trumpRot->setKeyValueAt(1, 90);
animGroup->addAnimation(trumpRot);
QPropertyAnimation *trumpPos = new QPropertyAnimation(trumpItem, "pos");
trumpPos->setDuration(100);
trumpPos->setKeyValueAt(0, trumpItem->pos());
trumpPos->setKeyValueAt(1, trumpItem->pos()
+ QPointF(CARD_HEIGHT, CARD_WIDTH / 4));
animGroup->addAnimation(trumpPos);
animGroup->start(QAbstractAnimation::DeleteWhenStopped);
// Deal cards to players
for (int i = 0; i < PLAYERS_COUNT; i++)
drawCardsFromDeck(i, DEAL_LIMIT);
// Determine first attacker
Rank lowestTrump = RANK_ACE;
int firstAttacker = 0;
for (Player *p : _players) {
for (Card c : p->hand()) {
if (c.suit() == _trumpSuit
&& ((c.rank() != RANK_ACE && c.rank() < lowestTrump)
|| lowestTrump == RANK_ACE)) {
firstAttacker = p->index();
lowestTrump = c.rank();
}
}
}
_currentAttacker = firstAttacker;
_currentThrower = firstAttacker;
// Set game status
emit setGameStatusText(
tr("%1's turn").arg(_players[firstAttacker]->name()));
// If it's an AI, show the trump to the public
if (firstAttacker) {
Card cd(_trumpSuit, lowestTrump);
qDebug() << QString("Lowest trump is %1, which has %2")
.arg(cd.fileName())
.arg(firstAttacker);
CardItem *lowestTrumpItem = _cardItems[cd];
QPropertyAnimation *trumpShow
= new QPropertyAnimation(lowestTrumpItem, "faceUp");
trumpShow->setDuration(2000);
trumpShow->setKeyValueAt(0, 1);
trumpShow->setKeyValueAt(0.9, 1);
trumpShow->setKeyValueAt(1, 0);
trumpShow->start(QAbstractAnimation::DeleteWhenStopped);
}
// Temp: show players' hand values
for (int j = 0; j < PLAYERS_COUNT; j++) {
Player *p = _players[j];
QString handDesc = "";
for (int i = 0; i < p->hand().length(); i++) {
handDesc += p->hand()[i].fileName();
if (i < p->hand().length() - 1)
handDesc += ", ";
}
qDebug() << QString("Player %1 has cards: %2. Value is %3")
.arg(p->index())
.arg(handDesc)
.arg(p->currentHandValue());
}
_isPlayerTaking = false;
_playersSaidDone = 0;
// Adjust buttons for first attack
if (!currentDefender()->index()) {
_actionButton->setText(tr("Take"));
_actionButton->setEnabled(true);
disconnect(_actionButton, 0, 0, 0);
connect(_actionButton, &QPushButton::pressed, _players[0],
&Player::take);
} else {
_actionButton->setText(tr("Done"));
_actionButton->setEnabled(currentDefender()->index() != 2);
disconnect(_actionButton, 0, 0, 0);
connect(_actionButton, &QPushButton::pressed, _players[0],
&Player::done);
}
// Actual game
QEventLoop playerWaitingLoop;
connect(QApplication::instance(), &QCoreApplication::aboutToQuit,
[&playerWaitingLoop] { playerWaitingLoop.exit(1); });
while (!isGameOver()) {
int opponents = !_outOfPlay[_currentAttacker]
+ !_outOfPlay[(_currentAttacker + 2) % PLAYERS_COUNT];
int code = 0;
if (currentAttacker()->index()) {
currentAttacker()->startTurn();
} else {
for (Player *p : _players) {
disconnect(p, SIGNAL(cardThrown(Card)), &playerWaitingLoop,
SLOT(quit()));
}
connect(currentAttacker(), SIGNAL(cardThrown(Card)),
&playerWaitingLoop, SLOT(quit()));
code = playerWaitingLoop.exec();
if (code)
return;
}
int throwLimit = qMin(DEAL_LIMIT, currentDefender()->hand().length());
while (_playersSaidDone < opponents) {
int currentPlayersDone = _playersSaidDone;
if (!_isPlayerTaking
&& _attackCards.length() > _defenseCards.length()) {
if (currentDefender()->index()) {
currentDefender()->tryBeat();
} else {
connect(currentDefender(), SIGNAL(cardBeaten(Card)),
&playerWaitingLoop, SLOT(quit()));
connect(currentDefender(), SIGNAL(take()),
&playerWaitingLoop, SLOT(quit()));
code = playerWaitingLoop.exec();
if (code)
return;
}
}
if (currentDefender()->hand().length() == 0
|| _attackCards.length() == throwLimit)
break;
if (currentThrower()->index()) {
currentThrower()->throwOrDone();
} else {
connect(currentThrower(), SIGNAL(cardThrown(Card)),
&playerWaitingLoop, SLOT(quit()));
connect(currentThrower(), SIGNAL(done()), &playerWaitingLoop,
SLOT(quit()));
code = playerWaitingLoop.exec();
if (code)
return;
}
if (_playersSaidDone > currentPlayersDone) {
_currentThrower += 2;
_currentThrower %= PLAYERS_COUNT;
}
}
QThread::msleep(400);
bool playerTook = _isPlayerTaking;
endTurn(_isPlayerTaking ? currentDefender()->index() : -1);
_currentAttacker += (1 + playerTook);
_currentAttacker %= PLAYERS_COUNT;
_currentThrower = _currentAttacker;
emit setGameStatusText(tr("%1's turn").arg(currentAttacker()->name()));
if (!currentDefender()->index()) {
_actionButton->setText(tr("Take"));
_actionButton->setEnabled(true);
disconnect(_actionButton, 0, 0, 0);
connect(_actionButton, &QPushButton::pressed, _players[0],
&Player::take);
} else {
_actionButton->setText(tr("Done"));
_actionButton->setEnabled(currentDefender()->index() != 2);
disconnect(_actionButton, 0, 0, 0);
connect(_actionButton, &QPushButton::pressed, _players[0],
&Player::done);
}
}
// TODO: Generalise
bool youWon = _outOfPlay[0] && _outOfPlay[2];
bool opponentsWon = _outOfPlay[1] && _outOfPlay[3];
QString messages[]
= {tr("Game is not over"), tr("You win"), tr("You lose"), tr("Draw")};
QMessageBox::information(nullptr, "Game Over",
messages[youWon + 2 * opponentsWon]);
// Disable the action button after finishing the game
_actionButton->setEnabled(false);
}
void Table::endTurn(int playerIdx)
{
QEventLoop loop;
QList<Card> allCards = _attackCards + _defenseCards;
if (playerIdx < 0) {
// Move all cards to discard pile
QParallelAnimationGroup *animGroup = new QParallelAnimationGroup();
CardItem *ci;
for (Card c : allCards) {
ci = _cardItems[c];
ci->setFaceUp(false);
_discardPile.append(c);
ci->setZValue(_discardPile.length() - 1);
QPropertyAnimation *cardPos = new QPropertyAnimation(ci, "pos");
cardPos->setDuration(600);
cardPos->setKeyValueAt(0, ci->pos());
cardPos->setKeyValueAt(1, DISCARD_PILE_LOCATION);
animGroup->addAnimation(cardPos);
}
connect(animGroup, SIGNAL(finished()), &loop, SLOT(quit()));
animGroup->start();
} else {
// Give all the cards to the taking player
QParallelAnimationGroup *animGroup = new QParallelAnimationGroup();
CardItem *ci;
for (Card c : allCards) {
ci = _cardItems[c];
ci->setFaceUp(playerIdx == 0);
_players[playerIdx]->addCard(c);
int ind = _players[playerIdx]->hand().length() - 1;
ci->setZValue(ind);
QPropertyAnimation *cardPos = new QPropertyAnimation(ci, "pos");
QPointF delta = playerIdx ? DELTA_AI : DELTA_HUMAN;
cardPos->setDuration(600);
cardPos->setKeyValueAt(0, ci->pos());
cardPos->setKeyValueAt(1,
PLAYER_LOCATIONS[playerIdx] + ind * delta);
animGroup->addAnimation(cardPos);
}
connect(animGroup, SIGNAL(finished()), &loop, SLOT(quit()));
animGroup->start();
}
loop.exec();
if (playerIdx >= 0)
sortPlayerCards(playerIdx);
_attackCards.clear();
_defenseCards.clear();
// Draw cards to players
if (!_deck->empty()) {
for (int i = 0; i < PLAYERS_COUNT; i++) {
int cardsToDraw = DEAL_LIMIT - _players[i]->hand().length();
if (cardsToDraw > 0) {
drawCardsFromDeck(i, cardsToDraw);
}
if (_deck->empty())
break;
}
}
// Check if someone is out of play
if (_deck->empty()) {
for (int i = 0; i < PLAYERS_COUNT; i++) {
_outOfPlay[i] = _players[i]->hand().length() == 0;
}
}
_isPlayerTaking = false;
updateNameLabels();
for (PlayerBubbleItem *bubble : _playerBubbles)
bubble->hide();
}
QList<Player *> Table::players() const { return _players; }
Suit Table::trumpSuit() const { return _trumpSuit; }
Player *Table::currentAttacker()
{
// if target player is out of play, then his/her partner attacks
if (_outOfPlay[_currentAttacker]) {
return _players[(_currentAttacker + 2) % PLAYERS_COUNT];
}
return _players[_currentAttacker];
}
Player *Table::currentDefender()
{
int currentDefender = (_currentAttacker + 1) % PLAYERS_COUNT;
// if target player is out of play, then his/her partner defends
if (_outOfPlay[currentDefender])
currentDefender = (currentDefender + 2) % PLAYERS_COUNT;
return _players[currentDefender];
}
bool Table::beats(Card playerCard, Card tableCard)
{
return (((tableCard.rank() != RANK_ACE && playerCard > tableCard)
|| (playerCard.rank() == RANK_ACE))
&& (playerCard.suit() == tableCard.suit()))
|| (playerCard.suit() == _trumpSuit
&& tableCard.suit() != _trumpSuit);
}
void Table::onCardClick(Card c)
{
// qDebug() << QString("Card %1 clicked").arg(c.fileName());
// If it's not human player's card
if (!_players[0]->hand().contains(c))
return;
if (!currentThrower()->index()) {
currentThrower()->throwCard(c);
}
if (!currentDefender()->index()
&& _attackCards.length() != _defenseCards.length()) {
currentDefender()->beatWithCard(c);
}
}
void Table::onPlayerDone(int playerIdx)
{
if (_attackCards.length() == 0)
return;
qDebug() << QString("Player %1 says done").arg(playerIdx);
_playersSaidDone++;
_playerBubbles[playerIdx]->setText(tr("Done"));
_playerBubbles[playerIdx]->show();
}
void Table::onPlayerThrows(int playerIdx, Card c)
{
qDebug() << QString("Player %1 throws %2").arg(playerIdx).arg(c.fileName());
_playersSaidDone = 0;
for (PlayerBubbleItem *bubble : _playerBubbles)
bubble->hide();
_attackCards.append(c);
// Reposition throwed card
CardItem *ci = _cardItems[c];
ci->setFaceUp(true);
// ci->setPos(CARD_FIELD_LOCATIONS[_attackCards.length() - 1]);
ci->setZValue(0);
QEventLoop loop;
QPropertyAnimation *cardPos = new QPropertyAnimation(ci, "pos");
cardPos->setDuration(400);
cardPos->setKeyValueAt(0, ci->pos());
cardPos->setKeyValueAt(1, CARD_FIELD_LOCATIONS[_attackCards.length() - 1]);
connect(cardPos, SIGNAL(finished()), &loop, SLOT(quit()));
cardPos->start();
loop.exec();
// Reposition all cards of a player
QPointF delta = playerIdx ? DELTA_AI : DELTA_HUMAN;
for (int i = 0; i < _players[playerIdx]->hand().length(); i++) {
CardItem *cia = _cardItems[_players[playerIdx]->hand()[i]];
cia->setPos(PLAYER_LOCATIONS[playerIdx] + i * delta);
cia->setZValue(i);
}
updateNameLabels();
// No need to sort here
}
void Table::onPlayerBeats(int playerIdx, Card c)
{
qDebug() << QString("Player %1 beats with %2")
.arg(playerIdx)
.arg(c.fileName());
_playersSaidDone = 0;
for (PlayerBubbleItem *bubble : _playerBubbles)
bubble->hide();
_defenseCards.append(c);
// Reposition throwed card
CardItem *ci = _cardItems[c];
ci->setFaceUp(true);
ci->setZValue(1);
// ci->setPos(CARD_FIELD_LOCATIONS[_attackCards.length() - 1] + DELTA_AI
// *
// 2);
QEventLoop loop;
QPropertyAnimation *cardPos = new QPropertyAnimation(ci, "pos");
cardPos->setDuration(400);
cardPos->setKeyValueAt(0, ci->pos());
cardPos->setKeyValueAt(1, CARD_FIELD_LOCATIONS[_attackCards.length() - 1]
+ DELTA_AI * 2);
cardPos->start();
connect(cardPos, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
// Reposition all cards of a player
QPointF delta = playerIdx ? DELTA_AI : DELTA_HUMAN;
for (int i = 0; i < _players[playerIdx]->hand().length(); i++) {
CardItem *cia = _cardItems[_players[playerIdx]->hand()[i]];
cia->setPos(PLAYER_LOCATIONS[playerIdx] + i * delta);
cia->setZValue(i);
}
updateNameLabels();
// No need to sort here
}
void Table::onPlayerTakes(int playerIdx)
{
qDebug() << QString("Player %1 decides to take").arg(playerIdx);
_playersSaidDone = 0;
_isPlayerTaking = true;
updateNameLabels();
_playerBubbles[playerIdx]->setText(tr("I take"));
_playerBubbles[playerIdx]->show();
}
void Table::onQuitRequest() { qDebug() << "Quit requested"; }
void Table::drawCardsFromDeck(int playerIdx, int cardCount)
{
QEventLoop loop;
QPointF delta = playerIdx ? DELTA_AI : DELTA_HUMAN;
QSequentialAnimationGroup *animGroup = new QSequentialAnimationGroup();
for (int i = 0; i < cardCount; i++) {
if (_deck->empty())
break;
Card c = _deck->draw();
// qDebug() << _deck->cards().length();
_players[playerIdx]->addCard(c);
CardItem *cardItem = _cardItems[c];
// qDebug() << c.fileName();
int ind = _players[playerIdx]->hand().length() - 1;
cardItem->setZValue(ind);
cardItem->setRotation(0);
QPropertyAnimation *cardPos = new QPropertyAnimation(cardItem, "pos");
cardPos->setDuration(200);
cardPos->setKeyValueAt(0, cardItem->pos());
cardPos->setKeyValueAt(1, PLAYER_LOCATIONS[playerIdx] + ind * delta);
animGroup->addAnimation(cardPos);
cardItem->setFaceUp(!playerIdx);
}
animGroup->start(QAbstractAnimation::DeleteWhenStopped);
connect(animGroup, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
sortPlayerCards(playerIdx);
}
QList<Card> Table::defenseCards() const { return _defenseCards; }
bool Table::isGameOver()
{
// TODO: Generalise
return (_outOfPlay[0] && _outOfPlay[2]) || (_outOfPlay[1] && _outOfPlay[3]);
}
void Table::updateNameLabels()
{
for (int i = 0; i < PLAYERS_COUNT; i++) {
_nameItems[i]->setText(QString("%1 (%2)%3")
.arg(_players[i]->name())
.arg(_players[i]->hand().length())
.arg(_outOfPlay[i] ? "+" : ""));
}
}
void Table::sortPlayerCards(int playerIdx)
{
// Sort the cards
_players[playerIdx]->sortCards(_sortingMode);
// Reposition all cards of a player
QPointF delta = playerIdx ? DELTA_AI : DELTA_HUMAN;
for (int i = 0; i < _players[playerIdx]->hand().length(); i++) {
CardItem *cia = _cardItems[_players[playerIdx]->hand()[i]];
cia->setPos(PLAYER_LOCATIONS[playerIdx] + i * delta);
cia->setZValue(i);
}
updateNameLabels();
}
QList<Card> Table::attackCards() const { return _attackCards; }
Player *Table::currentThrower()
{
// if target player is out of play, then his/her partner throws
if (_outOfPlay[_currentThrower]) {
return _players[(_currentThrower + 2) % PLAYERS_COUNT];
}
return _players[_currentThrower];
}
void Table::_updateDeckCardsLabel()
{
int remaining = _deck->cards().length();
if (remaining) {
_deckCardsItem->setText(QString::number(remaining));
} else {
QString suitNames[]
= {tr("Spades"), tr("Clubs"), tr("Diamonds"), tr("Hearts")};
_deckCardsItem->setText(
QString(tr("Trump: %1")).arg(suitNames[_trumpSuit]));
}
}