-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.h
145 lines (113 loc) · 3.9 KB
/
Game.h
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
// Author: Annie Berend (5033782) - Jonathan Verbeek (5058288)
#pragma once
#include <QObject>
#include <QTimer>
#include <QTextStream>
#include "HoldingStack.h"
#include "FinalStack.h"
#include "Card.h"
#include "DrawStack.h"
// Contains the scoring attributes for the game
namespace GameScoringAttributes
{
// DrawStack is empty after a move and has to be recycled
static const int RECYCLING_DRAW_PILE = -100;
// Moving a card form the DrawStack to a HoldingStack
static const int WASTE_PILE_TO_TABLEAU = 5;
// Moving a card from the DrawStack to a FinalStack
static const int WASTE_PILE_TO_FOUNDATION = 10;
// Moving a card from a HoldingStack to a FinalStack
static const int TABLEAU_TO_FOUNDATION = 10;
// Flipping a card form a HoldingStack when previous card is moved
static const int TURN_OVER_TABLEAU_CARD = 5;
// Moving a card back from a FinalStack to a HoldingStack
static const int FOUNDATION_TO_TABLEAU = -15;
}
// A transaction is an action by the user, such as move a card
struct Transaction
{
// The type of this transaction
enum ETransactionType {
StackToStack,
DrawFromDrawStack,
DrawToStack
} type;
// The involved cards and stacks
CCardStack* stack1 = nullptr;
CCardStack* stack2 = nullptr;
QList<CCard*> cards;
// The score before and after this transaction
int scoreBefore = 0;
int scoreAfter = 0;
// If type == StackToStack, whether the card before was flipped or not
bool flipCardBefore = false;
// ToString method
QString toString() {
QString str;
QTextStream stream(&str);
// Format type and symbol strings
QString typeStr;
switch (type) {
case ETransactionType::StackToStack: typeStr = "Stack<->Stack"; break;
case ETransactionType::DrawFromDrawStack: typeStr = "DrawFromDrawStack"; break;
case ETransactionType::DrawToStack: typeStr = "DrawToStack"; break;
}
// Create final string
stream << "(Transaction " << this
<< " type=" << typeStr
<< " stack1=" << stack1
<< " stack2=" << stack2
<< " cards=" << cards.length()
<< " scoreBefore=" << scoreBefore
<< " scoreAfter=" << scoreAfter
<< " flipCardBefore=" << flipCardBefore
<< ")";
return str;
}
};
// Implements the game logic
class CGame : public QObject
{
Q_OBJECT
public:
// How many steps we allow to undo
static int MaxUndoSteps;
public:
// Constructor
CGame(QObject* parent = nullptr);
// The initial setup of the game board, which shuffles the card deck, fills all card stacks
// and calls the GameWindow to display everything
void setUp();
// Moves a card, when clicked and a movement is possible
bool moveCard(CCard* cardToDrop, CCardStack* srcStack);
// Evaluates which scoring attribute should be added to the score
void evaluateScore(CCardStack* srcStack, CCardStack* destStack);
// Actually changes the variable score
void addScore(int points);
int getScore();
// Checks whether the game has ended (all finalStacks have 13 cards)
void checkHasEnded();
bool hasEnded();
// Registers a new transaction
void addTransaction(Transaction newTransaction);
// Undoes the last move
void undoLastMove();
signals:
// The "new game" menu item
void onScoreChanged();
public slots:
void restartGame();
private:
// Reference to the draw stack
CDrawStack* drawStack;
// The final stacks at the top
QList<CFinalStack*> finalStacks;
QList<CHoldingStack*> holdingStacks;
// The deck contains all cards in the whole game
QList<CCard*> deck;
// List of latest transactions the player has made
QList<Transaction> transactions;
// The score of the game
int score = 0;
bool isWon;
};