-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameData.cpp
More file actions
162 lines (133 loc) · 4.05 KB
/
Copy pathGameData.cpp
File metadata and controls
162 lines (133 loc) · 4.05 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
#include "GameData.h"
#include <algorithm>
#include <random>
#include <chrono>
GameData::GameData()
: _lastMatchedTableauIndex(-1)
, _score(0)
, _combo(0)
, _lastMatchWithJoker(false) {}
GameData::~GameData() {}
void GameData::initialize()
{
_tableauCards.clear();
_handPile.clear();
_lastMatchedTableauIndex = -1;
_score = 0;
_combo = 0;
_lastMatchWithJoker = false;
createDeck();
}
void GameData::createDeck()
{
std::vector<Card> deck;
for (int suit = 0; suit < 4; ++suit)
{
for (int rank = 1; rank <= 13; ++rank)
{
deck.emplace_back(static_cast<Suit>(suit), rank);
}
}
deck.emplace_back(CardType::JOKER_RED);
deck.emplace_back(CardType::JOKER_BLACK);
shuffleDeck(deck);
for (int i = 0; i < 28 && i < static_cast<int>(deck.size()); ++i)
_tableauCards.push_back(deck[i]);
for (int i = 28; i < static_cast<int>(deck.size()); ++i)
_handPile.push_back(deck[i]);
}
void GameData::addScore(int points)
{
_score += points;
}
int GameData::calculateMatchScore(int tableauIndex) const
{
if (tableauIndex < 0 || tableauIndex >= static_cast<int>(_tableauCards.size()))
return 0;
const Card* handTop = getHandTopCard();
if (handTop == nullptr)
return 0;
const Card& tableauCard = _tableauCards[tableauIndex];
int score = BASE_SCORE;
if (tableauCard.isJoker() || handTop->isJoker())
{
score *= JOKER_MULTIPLIER;
}
if (_combo > 0)
{
score = static_cast<int>(score * (1.0f + _combo * 0.1f));
}
return score;
}
void GameData::shuffleDeck(std::vector<Card>& deck)
{
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(deck.begin(), deck.end(), std::default_random_engine(seed));
}
Card& GameData::getTableauCard(int index) { return _tableauCards.at(index); }
const Card& GameData::getTableauCard(int index) const { return _tableauCards.at(index); }
const Card* GameData::getHandTopCard() const
{
if (_handPile.empty()) return nullptr;
return &_handPile.back();
}
Card* GameData::getHandTopCard()
{
if (_handPile.empty()) return nullptr;
return &_handPile.back();
}
bool GameData::canFlipHandCard(int index) const
{
return index >= 0 && index < static_cast<int>(_handPile.size()) - 1;
}
bool GameData::flipHandCard(int index)
{
if (!canFlipHandCard(index)) return false;
Card card = _handPile[index];
_handPile.erase(_handPile.begin() + index);
_handPile.push_back(card);
return true;
}
bool GameData::restoreFlippedCard(int originalIndex)
{
if (_handPile.empty()) return false;
if (originalIndex < 0 || originalIndex >= static_cast<int>(_handPile.size())) return false;
Card card = _handPile.back();
_handPile.pop_back();
_handPile.insert(_handPile.begin() + originalIndex, card);
return true;
}
bool GameData::canMatchTableauCard(int tableauIndex) const
{
if (tableauIndex < 0 || tableauIndex >= static_cast<int>(_tableauCards.size()))
return false;
const Card* handTop = getHandTopCard();
if (handTop == nullptr) return false;
return _tableauCards[tableauIndex].canMatchWith(*handTop);
}
bool GameData::matchTableauCard(int tableauIndex)
{
if (!canMatchTableauCard(tableauIndex)) return false;
Card card = _tableauCards[tableauIndex];
_handPile.push_back(card);
_tableauCards.erase(_tableauCards.begin() + tableauIndex);
_lastMatchedTableauIndex = tableauIndex;
return true;
}
void GameData::removeTableauCard(int index)
{
if (index >= 0 && index < static_cast<int>(_tableauCards.size()))
_tableauCards.erase(_tableauCards.begin() + index);
}
void GameData::setHandTopCard(const Card& card)
{
if (_handPile.empty())
_handPile.push_back(card);
else
_handPile.back() = card;
}
void GameData::restoreTableauCard(int index, const Card& card)
{
if (index >= 0 && index <= static_cast<int>(_tableauCards.size()))
_tableauCards.insert(_tableauCards.begin() + index, card);
}