-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
1482 lines (1350 loc) · 58.2 KB
/
Copy pathmainwindow.cpp
File metadata and controls
1482 lines (1350 loc) · 58.2 KB
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
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
700
701
702
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
728
729
730
731
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "namedialog.h"
#include <stdlib.h>
#include <QInputDialog>
/* MainWindow handles all game logic and displaying the game pieces */
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
deck_(new Deck),
curr_card_(nullptr),
prev_card_(nullptr),
hand_rect_(HAND_X, HAND_Y, HAND_WIDTH, HAND_HEIGHT),
hide_(true),
card_clicked_(false),
coup_fourre_(false),
empty_deck_(false),
draw_(true),
button_effect_(new QGraphicsDropShadowEffect()),
hide_effect_(new QGraphicsDropShadowEffect()),
curr_player_(0)
{
ui->setupUi(this);
scene = new QGraphicsScene;
QGraphicsView* view = ui->gameView;
// Turn off Scroll Bars
view->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
view->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
// Initialize button effects
button_effect_->setBlurRadius(2);
button_effect_->setOffset(3,3);
button_effect_->setColor(QColor("green"));
button_effect_->setEnabled(false);
hide_effect_->setBlurRadius(2);
hide_effect_->setOffset(3,3);
hide_effect_->setColor(QColor("green"));
ui->readyButton->setGraphicsEffect(hide_effect_);
ui->drawButton->setGraphicsEffect(button_effect_);
// Set scenes and sizes
view->setScene(scene);
view->setSceneRect(0,0,view->frameSize().width(), view->frameSize().height());
GameStartPopUps(parent);
InitializeGame();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::InitializeGame() {
// Intialize game objects, cards, deck, and deal cards to players
InitializeCards();
deck_->Shuffle();
deck_->Deal(players_);
BuildInitialGame();
// Initializing game labels / buttons
SetHandLabel();
SetLabels(ui->player_1, ui->dist_button_1, ui->safety_button_1, ui->miles_traveled_1, 0);
SetLabels(ui->player_2, ui->dist_button_2, ui->safety_button_2, ui->miles_traveled_2, 1);
SetLabels(ui->player_3, ui->dist_button_3, ui->safety_button_3, ui->miles_traveled_3, 2);
SetLabels(ui->player_4, ui->dist_button_4, ui->safety_button_4, ui->miles_traveled_4, 3);
SetLabels(ui->player_5, ui->dist_button_5, ui->safety_button_5, ui->miles_traveled_5, 4);
}
/* InitializeCards is a method to instantiate all the cards necessary for the game */
void MainWindow::InitializeCards() {
std::vector<int> hazard_cards = {ACCIDENT_CARDS, OUT_OF_GAS_CARDS, FLAT_TIRE_CARDS, STOP_CARDS, SPEED_LIMIT_CARDS};
std::vector<int> remedy_cards = {REPAIRS_CARDS, GASOLINE_CARDS, SPARE_TIRE_CARDS, GO_CARDS, END_OF_LIMIT_CARDS};
std::vector<int> distance_cards = {DIST_1_CARDS, DIST_2_CARDS, DIST_3_CARDS, DIST_4_CARDS, DIST_5_CARDS};
// Adding hazard cards
for (unsigned int i = 0; i < hazard_cards.size(); i++) {
for (int j = 0; j < hazard_cards[i]; j++) {
Card *accidents = new HazardCard(static_cast<hazards>(i));
connect(accidents, &Card::cardClicked, this, &MainWindow::HazardClickedSlot);
deck_->AddCard(accidents);
}
}
// Adding remedy cards
for (unsigned int i = 0; i < remedy_cards.size(); i++) {
for (int j = 0; j < remedy_cards[i]; j++) {
Card *repairs = new RemedyCard(static_cast<remedies>(i));
connect(repairs, &Card::cardClicked, this, &MainWindow::RemedyClickedSlot);
deck_->AddCard(repairs);
}
}
// Adding safety cards
for (int i = 0; i <= static_cast<int>(safeties::EmergencyVehicle); i++) {
Card *specialty_card = new SafetyCard(static_cast<safeties>(i));
connect(specialty_card, &Card::cardClicked, this, &MainWindow::SafetyClickedSlot);
deck_->AddCard(specialty_card);
}
// Adding distance cards
for (unsigned int i = 0; i < distance_cards.size(); i++) {
for (int j = 0; j < distance_cards[i]; j++) {
Card *distances_card = new DistanceCard(static_cast<distances>(i));
connect(distances_card, &Card::cardClicked, this, &MainWindow::DistanceClickedSlot);
deck_->AddCard(distances_card);
}
}
// Create Empty Action Cards
for (int i = 0; i < num_players_; i++) {
Card* empty_card = new Card;
QPixmap* empty = CardImgFactory::get_image(true, false);
empty_card->set_img(empty);
connect(empty_card, &Card::cardClicked, this, &MainWindow::EmptyClickedSlot);
deck_->AddEmptyCard(empty_card);
}
// Create Back Cards
for (int i = 0; i < MAX_HAND_SIZE; i++) {
// Creates backs of cards array for hiding players hands
card_backs[i] = new Card;
QPixmap* back_img = CardImgFactory::get_image(false, true);
card_backs[i]->set_img(back_img);
}
}
/* on_readyButton_clicked() handles when the "hide/reveal" button is pressed. It either displays the card
* backs to hide a players hand, or displays the players hand and hides the card backs.
*/
void MainWindow::on_readyButton_clicked()
{
for (int i = 0; i < MAX_HAND_SIZE; i++) {
// Reveal players hand
if (hide_) {
card_backs[i]->hide();
QRect rect(HAND_X, HAND_Y + i * H_ZONE_OFFSET, HAND_WIDTH, HAND_HEIGHT);
players_[curr_player_]->get_hand()[static_cast<unsigned long>(i)]->set_rect(rect);
scene->addItem(players_[curr_player_]->get_hand()[static_cast<unsigned long>(i)]);
}
// Hide players hand
else {
scene->removeItem(players_[curr_player_]->get_hand()[static_cast<unsigned long>(i)]);
card_backs[i]->show();
}
}
// Modify button effects and text
if (hide_) {
if (card_clicked_ || draw_) {
button_effect_->setEnabled(true);
ui->drawButton->update();
}
ui->readyButton->setText("Hide Hand");
}
else {
button_effect_->setEnabled(false);
ui->drawButton->update();
ui->readyButton->setText("Show Hand");
}
hide_ = !hide_;
scene->update();
}
/* on_drawButton_clicked handles the logic for the draw / discard button */
void MainWindow::on_drawButton_clicked()
{
if (hide_) {
return;
}
// Handles special case where player will be drawing two cards
if (!coup_fourre_) {
button_effect_->setEnabled(false);
}
Card** temp = players_[curr_player_]->FindEmptyCard();
ui->drawButton->update();
// If Empty Card exists, replace it with new draw card
if (temp && draw_) {
scene->removeItem(*temp);
QRect temp_rect = (*temp)->get_rect();
*temp = deck_->DrawCard();
(*temp)->set_rect(temp_rect);
scene->addItem(*temp);
// Handles case of Coup Fouree where player needs to draw 2 cards
if (coup_fourre_) {
coup_fourre_ = false;
}
else {
draw_ = false;
ui->drawButton->setText("Discard");
}
unsigned long deck_size = deck_->get_draw_deck().size();
if (!deck_size) {
empty_deck_ = true;
}
ui->drawLabel->setText("Draw Pile: " + QString::number(deck_size));
scene->update();
}
else if (!draw_) {
if (curr_card_ && card_clicked_) {
scene->removeItem(curr_card_);
// Setting previous to current to reset indicators
prev_card_ = curr_card_;
ReplaceWithEmpty(players_[curr_player_]->get_empty_card());
ResetIndicators();
deck_->DiscardCard(curr_card_);
ChangeHands();
}
}
}
/* HandCardClicked is a general helper method called whenever any card in the players hand is clicked.
* This method handles the border color changes of the cards based on clicks, and button effects that will need
* to be implemented upon these clicks. Ex: the discard button gets a green shadow when a card is clicked. Lastly,
* this method also accounts for when cards are re-clicked or "unclicked".
*/
void MainWindow::HandCardClicked(Card* hand_card) {
if (draw_) {
return;
}
// Handles clicking a seperate or the same card after clicking one previously
if (card_clicked_) {
curr_card_->set_border_color(QColor("black"));
if (curr_card_ == hand_card) {
button_effect_->setEnabled(false);
ui->drawButton->update();
prev_card_ = curr_card_;
card_clicked_ = false;
hand_card->set_clicked(false);
ResetIndicators();
return;
}
}
// Handle moments where control of a hand card is being given to the user
else {
card_clicked_ = true;
// Discard button is highlighted
button_effect_->setEnabled(true);
ui->drawButton->update();
}
// Card clicked is made the new card to play
hand_card->set_clicked(true);
hand_card->set_border_color(QColor("red"));
prev_card_ = curr_card_;
curr_card_ = hand_card;
curr_rect_ = hand_card->get_rect();
ResetIndicators();
}
/* EmptyClickedSlot handles the early game scenarios when one of the empty cards that each player
* starts with in their action zone is clicked. We handle two main scenarios here: when a player has clicked
* a Go card in their hand, or the player has clicked an Emergency Vehicle card (which gives them an automatic Go card).
*/
void MainWindow::EmptyClickedSlot(Card* empty_card) {
// If current players card is the empty card and a card is clicked (in hand)
if (players_[curr_player_]->get_action_card() == empty_card && card_clicked_) {
if (curr_card_->get_type() == cardType::Remedy) {
// If Go Card is clicked in hand, play it in place of the empty card
if (static_cast<RemedyCard*>(curr_card_)->get_rem() == remedies::Go) {
players_[curr_player_]->set_go(true);
players_[curr_player_]->set_go_card(curr_card_);
PlayActionCard(empty_card, curr_player_);
ChangeHands();
}
}
else if (curr_card_->get_type() == cardType::Safety) {
if (static_cast<SafetyCard*>(curr_card_)->get_safety() == safeties::EmergencyVehicle) {
// Special occasion where a card is instantiated on the fly to accomodate for Emergency Vehicle being played
Card* special_go_card = new RemedyCard(remedies::Go);
connect(special_go_card, &Card::cardClicked, this, &MainWindow::RemedyClickedSlot);
players_[curr_player_]->set_go(true);
players_[curr_player_]->set_go_card(special_go_card);
PlaySafetyCard(static_cast<SafetyCard*>(curr_card_), false);
// Replace empty card with Emergency Vehicle
scene->removeItem(empty_card);
deck_->DiscardCard(empty_card);
prev_card_ = curr_card_;
curr_card_ = special_go_card;
special_go_card->set_rect(players_[curr_player_]->get_action_rect());
players_[curr_player_]->set_action_card(special_go_card);
special_go_card->update();
scene->addItem(special_go_card);
scene->update();
ChangeHands();
}
}
}
}
/* RemedyClickedSlot handles any time a remedy card is clicked in the MainWindow. If a player hasn't drawn, the method does
* nothing. We then handle the logic for a Remedy Card clicked in hand, a remedy card clicked in a players action zone, or
* a remedy card clicked in another players action zone.
*/
void MainWindow::RemedyClickedSlot(Card* rem_card) {
// Initially check if player needs to draw
if (draw_) {
return;
}
// Handles if card in hand is clicked (or re-clicked)
if (players_[curr_player_]->InHand(rem_card)) {
Card* action_card = players_[curr_player_]->get_action_card();
Card* empty_card = deck_->get_empty_action_card(curr_player_);
Card* limit_card = players_[curr_player_]->get_limit_card();
HandCardClicked(rem_card);
if (card_clicked_) {
// Handle all possible remedy cards played from hand
switch (static_cast<RemedyCard*>(rem_card)->get_rem()) {
case remedies::Go: {
// If action card is "Empty", any "Remedy" Card, or a "Stop" Card
if (action_card == empty_card ||
action_card->get_type() == cardType::Remedy ||
(action_card->get_type() == cardType::Hazard &&
static_cast<HazardCard*>(action_card)->get_haz() == hazards::Stop)) {
action_card->set_border_color(QColor("lime"));
}
break;
}
case remedies::Repairs: {
if (action_card->get_type() == cardType::Hazard &&
static_cast<HazardCard*>(action_card)->get_haz() == hazards::Accident) {
action_card->set_border_color(QColor("lime"));
}
break;
}
case remedies::Gasoline: {
if (action_card->get_type() == cardType::Hazard &&
static_cast<HazardCard*>(action_card)->get_haz() == hazards::OutOfGas) {
action_card->set_border_color(QColor("lime"));
}
break;
}
case remedies::SpareTire: {
if (action_card->get_type() == cardType::Hazard &&
static_cast<HazardCard*>(action_card)->get_haz() == hazards::FlatTire) {
action_card->set_border_color(QColor("lime"));
}
break;
}
case remedies::EndOfLimit: {
if (limit_card) {
limit_card->set_border_color(QColor("lime"));
}
break;
}
}
}
}
// Handle case when card is clicked, but not in hand
else if (card_clicked_) {
// Playing a Hazard Card on the clicked card
if (curr_card_->get_type() == cardType::Hazard) {
for (unsigned long i = 0; i < players_.size(); i++) {
Card* action_card = players_[i]->get_action_card();
// If opposing players action card is the clicked Remedy Card and they don't have a blocking Safety Card
if (action_card == rem_card &&
!players_[i]->LookForSafetyBlock(static_cast<HazardCard*>(curr_card_)->get_haz())) {
action_card->set_border_color(QColor("black"));
// Check if opposing player has Coup Fourre
for (auto &card : players_[i]->get_hand()) {
if (card->get_type() == cardType::Safety) {
SafetyCard* safety_card = static_cast<SafetyCard*>(card);
if (FindMatch(safety_card->get_safety(), static_cast<HazardCard*>(curr_card_))) {
CoupeFourre(rem_card, safety_card, i);
return;
}
}
}
// Handles case where opponent doesn't have blocking safety card
if (static_cast<HazardCard*>(curr_card_)->get_haz() == hazards::SpeedLimit) {
Card* curr_limit = players_[i]->get_limit_card();
PlayLimitCard(curr_limit, i);
}
else {
players_[i]->set_go(false);
PlayActionCard(rem_card, i);
}
ResetIndicators();
ChangeHands();
return;
}
}
}
// Playing a Go Card on a Remedy Card
else if (curr_card_->get_type() == cardType::Remedy &&
players_[curr_player_]->get_action_card() == rem_card) {
if (static_cast<RemedyCard*>(curr_card_)->get_rem() == remedies::Go) {
players_[curr_player_]->set_go(true);
PlayActionCard(rem_card, curr_player_);
ChangeHands();
}
}
// Play Safety Card for defense / points
else if (curr_card_->get_type() == cardType::Safety) {
safeties curr_safety = static_cast<SafetyCard*>(curr_card_)->get_safety();
if (FindMatch(curr_safety, static_cast<RemedyCard*>(rem_card))){
SwapForGo();
}
PlaySafetyCard(static_cast<SafetyCard*>(curr_card_), false);
ChangeHands();
}
}
}
/* HazardClickedSlot handles any time a hazard card is clicked in a players hand, in a players action zone, or in another players action zone.
* When a hazard card is clicked in someones hand, the method searching for all possible positions that card can be played and marks the
* corresponding ones with green borders. If a hazard card is clicked in a players action zone, the method checks the card that was clicked in
* the players hand and chooses the appropriate action to take. When a card in another players action zone has been clicked, if the player also
* has clicked a hazard card from their hand, then that card will replace the current hazard.
*/
void MainWindow::HazardClickedSlot(Card* haz_card) {
if (draw_) {
return;
}
// Card clicked is in the current players hand
if (players_[curr_player_]->InHand(haz_card)) {
HandCardClicked(haz_card);
if (card_clicked_) {
for (unsigned long i = 0; i < players_.size(); i++) {
// Skip if current players iteration
if (i == curr_player_) {
continue;
}
Card* curr_action_card = players_[i]->get_action_card();
// If other players action card is Remedy or Hazard, highlight as playable
if (curr_action_card->get_type() == cardType::Remedy || curr_action_card->get_type() == cardType::Hazard) {
// Check for safety cards
if (!players_[i]->LookForSafetyBlock(static_cast<HazardCard*>(haz_card)->get_haz())) {
curr_action_card->set_border_color(QColor("lime"));
}
}
}
}
}
// Card clicked is not in hand, and hand card is a Remedy
else if (card_clicked_ && curr_card_->get_type() == cardType::Remedy) {
HazardCard* curr_haz_card = static_cast<HazardCard*>(haz_card);
switch (static_cast<RemedyCard*>(curr_card_)->get_rem()) {
case remedies::Go: {
if (curr_haz_card->get_haz() == hazards::Stop) {
players_[curr_player_]->set_go(true);
PlayActionCard(haz_card, curr_player_);
ChangeHands();
}
break;
}
case remedies::Repairs: {
if (curr_haz_card->get_haz() == hazards::Accident) {
PlayActionCard(haz_card, curr_player_);
if (players_[curr_player_]->FindSafety(safeties::EmergencyVehicle)) {
players_[curr_player_]->set_go(true);
SwapForGo();
}
ChangeHands();
}
break;
}
case remedies::Gasoline: {
if (curr_haz_card->get_haz() == hazards::OutOfGas) {
PlayActionCard(haz_card, curr_player_);
if (players_[curr_player_]->FindSafety(safeties::EmergencyVehicle)) {
players_[curr_player_]->set_go(true);
SwapForGo();
}
ChangeHands();
}
break;
}
case remedies::SpareTire: {
if (curr_haz_card->get_haz() == hazards::FlatTire) {
PlayActionCard(haz_card, curr_player_);
if (players_[curr_player_]->FindSafety(safeties::EmergencyVehicle)) {
players_[curr_player_]->set_go(true);
SwapForGo();
}
ChangeHands();
}
break;
}
case remedies::EndOfLimit: {
if (curr_haz_card->get_haz() == hazards::SpeedLimit) {
scene->removeItem(curr_haz_card);
scene->removeItem(curr_card_);
deck_->DiscardCard(curr_haz_card);
deck_->DiscardCard(curr_card_);
ReplaceWithEmpty(players_[curr_player_]->get_empty_card());
players_[curr_player_]->set_speed_limit(false);
ChangeHands();
}
break;
}
}
}
// Card clicked is not in hand, and hand card is a Safety Card
else if (card_clicked_ && curr_card_->get_type() == cardType::Safety) {
HazardCard* curr_haz_card = static_cast<HazardCard*>(haz_card);
SafetyCard* curr_safety_card = static_cast<SafetyCard*>(curr_card_);
// Should refactor: safety && remedy || etc
switch (curr_safety_card->get_safety()) {
case safeties::FuelTank: {
if (curr_haz_card->get_haz() == hazards::OutOfGas) {
SwapForGo();
PlaySafetyCard(curr_safety_card, false);
ChangeHands();
}
break;
}
case safeties::DrivingAce: {
if (curr_haz_card->get_haz() == hazards::Accident) {
SwapForGo();
PlaySafetyCard(curr_safety_card, false);
ChangeHands();
}
break;
}
case safeties::PunctureProof: {
if (curr_haz_card->get_haz() == hazards::FlatTire) {
SwapForGo();
PlaySafetyCard(curr_safety_card, false);
ChangeHands();
}
break;
}
case safeties::EmergencyVehicle: {
if (curr_haz_card->get_haz() == hazards::Stop) {
SwapForGo();
PlaySafetyCard(curr_safety_card, false);
ChangeHands();
}
else if (curr_haz_card->get_haz() == hazards::SpeedLimit) {
scene->removeItem(curr_haz_card);
deck_->DiscardCard(curr_haz_card);
players_[curr_player_]->set_speed_limit(false);
PlaySafetyCard(curr_safety_card, false);
ChangeHands();
}
break;
}
}
}
// Card clicked is not in hand, and hand card is a Hazard Card
else if (card_clicked_ && curr_card_->get_type() == cardType::Hazard) {
for (unsigned long i = 0; i < players_.size(); i++) {
if (curr_player_ == i) {
continue;
}
Card* action_card = players_[i]->get_action_card();
HazardCard* hazard_card = static_cast<HazardCard*>(haz_card);
if (action_card == haz_card && !players_[i]->LookForSafetyBlock(hazard_card->get_haz())) {
// Check if opposing player has Coup Fourre
for (auto &card : players_[i]->get_hand()) {
if (card->get_type() == cardType::Safety) {
SafetyCard* safety_card = static_cast<SafetyCard*>(card);
if (FindMatch(safety_card->get_safety(), static_cast<HazardCard*>(curr_card_))) {
CoupeFourre(haz_card, safety_card, i);
return;
}
}
}
action_card->set_border_color(QColor("black"));
if (static_cast<HazardCard*>(curr_card_)->get_haz() == hazards::SpeedLimit) {
PlayLimitCard(players_[i]->get_limit_card(), i);
}
else {
PlayActionCard(haz_card, i);
}
ResetIndicators();
ChangeHands();
return;
}
}
}
}
/* SafetyClickedSlot handles any time a safety card is clicked in a players hand, any other time will result in nothing occuring. The method
* will highlight possible cards that the safety can be played on to erase or if there are no current cards to play on, it will highlight the
* safety box where a player can always play a safety card if they currently have one.
*/
void MainWindow::SafetyClickedSlot(Card* safety_card) {
if (!draw_ && players_[curr_player_]->InHand(safety_card)) {
HandCardClicked(safety_card);
if (card_clicked_) {
// Check if player has appropriate remedy card currently in their action zone
Card* action_card = players_[curr_player_]->get_action_card();
if (action_card->get_type() == cardType::Remedy) {
if (FindMatch(static_cast<SafetyCard*>(safety_card)->get_safety(), static_cast<RemedyCard*>(action_card))) {
action_card->set_border_color(QColor("lime"));
}
}
// Check if player has appropriate hazard card currently in their action zone
// Need to refactor
else if (action_card->get_type() == cardType::Hazard) {
switch (static_cast<SafetyCard*>(safety_card)->get_safety()) {
case safeties::FuelTank: {
if (static_cast<HazardCard*>(action_card)->get_haz() == hazards::OutOfGas) {
action_card->set_border_color(QColor("lime"));
}
break;
}
case safeties::DrivingAce: {
if (static_cast<HazardCard*>(action_card)->get_haz() == hazards::Accident) {
action_card->set_border_color(QColor("lime"));
}
break;
}
case safeties::PunctureProof: {
if (static_cast<HazardCard*>(action_card)->get_haz() == hazards::FlatTire) {
action_card->set_border_color(QColor("lime"));
}
break;
}
// Shows remedy cards as clickable except Go, in case player wants to use Emergency Vehicle to replace Go
case safeties::EmergencyVehicle: {
if (static_cast<HazardCard*>(action_card)->get_haz() == hazards::Stop) {
action_card->set_border_color(QColor("lime"));
}
else if (players_[curr_player_]->get_speed_limit()) {
players_[curr_player_]->get_limit_card()->set_border_color(QColor("lime"));
}
break;
}
}
}
else {
// Handles Empty case where Emergency Vehicle can be played
if (static_cast<SafetyCard*>(safety_card)->get_safety() == safeties::EmergencyVehicle) {
players_[curr_player_]->get_action_card()->set_border_color(QColor("lime"));
}
}
// Handles Box to place safety cards if you aren't inheritly playing them for another card
if (action_card->get_border_color() != QColor("lime")) {
players_[curr_player_]->get_safety_section()->setEnabled(true);
}
}
}
}
/* DistanceClickedSlot handles any time a distance card is clicked on the MainWindow. If a card is clicked in a players hand, the method
* will highlight the distance zone if no other distance cards have been played or it will highlight the last distance card played, if the move
* if valid. The method checks to see if a speed limit is currently being enforced on the player, or if the miles card they would play would put
* them over the goal. If a card is clicked in the distance zone / section, the method checks if the current card is a distance card, and that it
* won't allow the player to go over the goal or violate a speed limit.
*/
void MainWindow::DistanceClickedSlot(Card* dist_card) {
if (draw_) {
return;
}
std::vector<Card*> temp_dist = players_[curr_player_]->get_distance_cards();
bool not_app = false;
int dist_val = static_cast<DistanceCard*>(dist_card)->get_value();
// Card clicked is in hand
if (players_[curr_player_]->InHand(dist_card)) {
// Checks to see if speed limit card is currently on player
if ( (players_[curr_player_]->get_speed_limit() && dist_val > DIST_2) ||
(players_[curr_player_]->get_dist_score() + dist_val > GOAL) )
{
not_app = true;
}
HandCardClicked(dist_card);
// Highlight distance section or distance card
if (players_[curr_player_]->get_go() && !not_app) {
if (card_clicked_ && temp_dist.size() == 0) {
players_[curr_player_]->get_dist_section()->setEnabled(true);
}
else if (card_clicked_) {
temp_dist.back()->set_border_color(QColor("lime"));
}
}
}
// Card clicked is in players distance pile
else if (players_[curr_player_]->InDistCards(dist_card)) {
if (players_[curr_player_]->get_go() && !not_app) {
temp_dist.back()->set_border_color(QColor("black"));
PlayDistCard();
}
}
}
/* GameStartPopUps handles the initial game start up where windows are displayed asking the user how many players will be playing,
* what their names are, and if they are a computer. I used a subclassed "NameDialog" class of the QDialog base class to create the
* text box that users enter their name and check if the player is a computer or not.
*/
void MainWindow::GameStartPopUps(QWidget *parent) {
// Dialog box asking how many players, restricted to 2 - 5
num_players_ = QInputDialog::getInt(parent,"Mille Bornes","How many players: ", 2,2,5);
for (int i = 0; i < num_players_; i++) {
// Initialize Dialog box and set position
NameDialog player_info_box(this, i + 1);
QRect screenGeometry = QGuiApplication::screens().first()->availableGeometry();
int x = (screenGeometry.width() - player_info_box.width()) / 2;
int y = (screenGeometry.height() - player_info_box.height()) / 2;
player_info_box.move(x, y);
bool automated = false;
// Handles dialog box information gathering
if (player_info_box.exec() == QDialog::Accepted) {
names_.push_back(player_info_box.get_name());
automated = player_info_box.get_ai();
}
else {
names_.push_back("");
}
Player* new_player = new Player(names_.back());
new_player->set_ai(automated);
// Set Action / Distance Zones location
QRect zone_dim(ACTION_X, ACTION_Y + (i * V_ZONE_OFFSET), PLAYED_WIDTH, PLAYED_HEIGHT);
new_player->set_action_rect(zone_dim);
zone_dim.moveLeft(zone_dim.x() + 100);
new_player->set_dist_rect(zone_dim);
// Set Safety Zone locations
QRect safety_zone_dim(SAFETY_X, SAFETY_Y + (i * V_ZONE_OFFSET), SAFETY_WIDTH, SAFETY_HEIGHT);
new_player->set_safety_rect(safety_zone_dim);
// Set Limit Zone locations
QRect limit_zone_dim(LIMIT_X, ACTION_Y + (i * V_ZONE_OFFSET), PLAYED_WIDTH, PLAYED_HEIGHT);
new_player->set_limit_rect(limit_zone_dim);
players_.push_back(new_player);
}
}
/* SetLabels is used during MainWindows instantiation to set appropriate names and hide appropriate areas based on number of players */
void MainWindow::SetLabels(QLabel* name, QPushButton* distance_button, QPushButton* safety_button, QLabel* dist_label, int index) {
if (index > num_players_ - 1) {
name->hide();
distance_button->hide();
safety_button->hide();
dist_label->hide();
}
else {
dist_label->setText("Miles Traveled: 0");
name->setText(names_.at(static_cast<unsigned long>(index)));
players_[static_cast<unsigned long>(index)]->set_dist_section(distance_button);
players_[static_cast<unsigned long>(index)]->set_safety_section(safety_button);
players_[static_cast<unsigned long>(index)]->set_dist_label(dist_label);
}
}
/* SetHandLabel is a simple setter method to adjust the label above the current players hand */
void MainWindow::SetHandLabel() {
ui->handLabel->setText(names_[curr_player_] + "'s Hand");
ui->handLabel->setAlignment(Qt::AlignCenter);
}
/* BuildInitialGame is another method called during MainWindows instantiation that displays players initial action zones (empty cards),
* and the card backs that will be replaced by the first players hand when the on_ready button is pressed.
*/
void MainWindow::BuildInitialGame() {
ui->drawLabel->setText("Draw Pile: " + QString::number(deck_->get_draw_deck().size()));
// Display Players Action Zone
for (int i = 0; i < static_cast<int>(players_.size()); i++) {
Card* temp = players_[static_cast<unsigned long>(i)]->get_action_card();
QRect temp_rect = players_[static_cast<unsigned long>(i)]->get_action_rect();
temp->set_rect(temp_rect);
scene->addItem(temp);
}
// Display Card Backs in first player's hand
for (int i = 0; i < MAX_HAND_SIZE; i++) {
QRect temp_rect(hand_rect_);
temp_rect.moveTop(hand_rect_.y() + (i * 90));
card_backs[i]->set_rect(temp_rect);
scene->addItem(card_backs[i]);
}
}
/* ReplaceWithEmpty is a method to handle replacing a played card with an empty card in a players hand. This is acheived by locating the
* index of the clicked card and simply adding an empty card to that location.
*/
void MainWindow::ReplaceWithEmpty(Card* empty_card) {
int index = players_[curr_player_]->FindClickedCard();
players_[curr_player_]->AddCard(empty_card, index);
empty_card->set_rect(curr_rect_);
// Set appropriate draw button display based on draw pile
card_clicked_ = false;
if (empty_deck_) {
draw_ = false;
}
else {
draw_ = true;
ui->drawButton->setText("Draw");
}
button_effect_->setEnabled(true);
ui->drawButton->update();
// In a coupe fourre scenario, the respective method handles add / removing elements from scene because of alternate variables
if (!coup_fourre_) {
scene->addItem(empty_card);
}
scene->update();
}
/* PlayDistCard handles the logic of playing a distance card into a distance section. */
bool MainWindow::PlayDistCard() {
if (card_clicked_ && curr_card_->get_type() == cardType::Distance) {
// Violates "safe trip" standard
if (static_cast<DistanceCard*>(curr_card_)->get_dist() == distances::Dist_5) {
players_[curr_player_]->set_safe_trip(false);
}
// Removes card from hand, modifies its position, and places it in respective action zone
scene->removeItem(curr_card_);
QRect temp = players_[curr_player_]->get_dist_rect();
curr_card_->set_rect(temp);
curr_card_->setZValue(players_[curr_player_]->get_dist_z_value());
curr_card_->set_border_color(QColor("black"));
players_[curr_player_]->add_distance_card(curr_card_);
players_[curr_player_]->IncrDistScore(static_cast<DistanceCard*>(curr_card_)->get_value());
scene->addItem(curr_card_);
scene->update();
ReplaceWithEmpty(players_[curr_player_]->get_empty_card());
QRect new_dist_rect(temp.x() + 20, temp.y(), temp.width(), temp.height());
players_[curr_player_]->set_dist_rect(new_dist_rect);
players_[curr_player_]->incr_dist_z_value();
ChangeHands();
return true;
}
return false;
}
/* PlaySafetyCard handles the logic of moving a safety card from a players hand, to their safety zone. If the parameter coup_fourre is true
* a special empty card is used so that two cards will need to be draw upon the control being handed back to the player whom dealt the card.
*/
void MainWindow::PlaySafetyCard(SafetyCard* curr_safety_card, bool coup_fourre) {
scene->removeItem(curr_safety_card);
if (!coup_fourre) {
ReplaceWithEmpty(players_[curr_player_]->get_empty_card());
curr_safety_card->set_play(true);
}
else {
coup_fourre_ = true;
ReplaceWithEmpty(players_[curr_player_]->get_spec_empty_card());
curr_safety_card->set_coup_fourre(true);
}
curr_safety_card->set_border_color(QColor("black"));
curr_safety_card->set_rect(players_[curr_player_]->get_safety_rect());
scene->addItem(curr_safety_card);
players_[curr_player_]->UpdateSafetyRect();
players_[curr_player_]->add_safety_card(curr_safety_card);
}
/* PlayActionCard handles the logic of moving a card from a players hand to a players action zone. */
void MainWindow::PlayActionCard(Card* action_card, unsigned long index) {
scene->removeItem(action_card);
scene->removeItem(curr_card_);
deck_->DiscardCard(action_card);
prev_card_ = curr_card_;
curr_card_->set_rect(players_[index]->get_action_rect());
curr_card_->set_border_color(QColor("black"));
players_[index]->set_action_card(curr_card_);
scene->addItem(curr_card_);
scene->update();
ReplaceWithEmpty(players_[curr_player_]->get_empty_card());
}
/* PlayLimitCard handles the logic of moving a card from a players hand to a players speed limit area / zone. */
void MainWindow::PlayLimitCard(Card* curr_limit, unsigned long index) {
if (curr_limit) {
deck_->DiscardCard(curr_limit);
scene->removeItem(curr_limit);
}
players_[index]->set_speed_limit(true);
players_[index]->set_limit_card(curr_card_);
players_[index]->get_action_card()->set_border_color(QColor("black"));
scene->removeItem(curr_card_);
prev_card_ = curr_card_;
curr_card_->set_rect(players_[index]->get_limit_rect());
scene->addItem(curr_card_);
scene->update();
ReplaceWithEmpty(players_[curr_player_]->get_empty_card());
}
/* ChangeHands handles passing control to the next player. It first checks to see if the game is not currently over. Then hides the
* hand with card backs, increments the current players turn to a player whom has cards in their hand, sets the appropriate labels and
* calls the AI method if necessary for the specific player.
*/
void MainWindow::ChangeHands() {
if (CheckForGameOver()) {
GameOver();
return;
}
hide_ = false;
curr_card_ = nullptr;
card_clicked_ = false;
// Hide hand
on_readyButton_clicked();
// Find current player with cards to play
do {
if (curr_player_ == static_cast<unsigned long>(num_players_ - 1)) {
curr_player_ = 0;
}
else {
curr_player_++;
}
} while (empty_deck_ && CheckForEmptyHand(players_[curr_player_]));
// If deck is empty, instantiate a new empty card for the player to be able to replace their played card with
if (empty_deck_) {
Card* empty_card = new Card;
QPixmap* empty_img = CardImgFactory::get_image(true, false);
empty_card->set_img(empty_img);
players_[curr_player_]->set_empty_card(empty_card);
}
SetHandLabel();
if (players_[curr_player_]->get_ai()) {
HandleAITurn();
}
}
/* SwapForGo is a method that handles the logic of swapping in a go card in the action zone of a player. This is mostly used
* when playing safety cards that eliminate hazards / remedy cards.
*/
void MainWindow::SwapForGo() {
Card* curr_action_card = players_[curr_player_]->get_action_card();
Card* curr_go_card = players_[curr_player_]->get_go_card();
scene->removeItem(curr_action_card);
deck_->DiscardCard(curr_action_card);
players_[curr_player_]->set_action_card(curr_go_card);
players_[curr_player_]->set_go(true);
scene->addItem(curr_go_card);
scene->update();
}
/* CoupeFourre handles the logic necessary when a player plays a hazard on another player whom has a safety card to combat it.
* This is a special situation where control is then given to that player, they get bonus points, and then draw two cards and play.
* A window is displayed with the name of the player whom plays the card and an image of the card that was played.
*/
void MainWindow::CoupeFourre(Card* played_card, SafetyCard* safety_card, unsigned long i) {
PlayActionCard(played_card, i);
ResetIndicators();
// Don't display window is players is AI
if (!players_[i]->get_ai()) {
QPixmap* icon = safety_card->get_img();
QString message(players_[i]->get_name() + ": Coupe Fourre!");
QMessageBox coupe_message(this);
coupe_message.setText(message);
coupe_message.setIconPixmap(*icon);
coupe_message.exec();
}
hide_ = false;
on_readyButton_clicked();
curr_player_ = i;
players_[curr_player_]->incr_total_score(COUPE_FOURRE);
safety_card->set_clicked(true);
SwapForGo();
PlaySafetyCard(safety_card, true);
SetHandLabel();
// If the player who played the card is AI, have them continue play
if (players_[curr_player_]->get_ai()) {
HandleAITurn();
}
}