Skip to content

Commit 5530204

Browse files
committed
Merge pull request #71 from Databean/refactor_deck
Refactor deck
2 parents 2683c37 + 5efb497 commit 5530204

File tree

12 files changed

+109
-151
lines changed

12 files changed

+109
-151
lines changed

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>warsofcatan</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

include/Deck.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
#include <vector>
1212
#include <ctime>
1313
#include <algorithm>
14-
#include "DevelopmentCard.h"
14+
//#include "DevelopmentCard.h"
15+
16+
17+
enum DevCardType { KNIGHT, VICTORYPOINT, YEAROFPLENTY, MONOPOLY, ROADBUILDING, NULLTYPE };
18+
1519

1620
/**
1721
* A collection of Settlers of Catan cards, initialized with the cards available in the standard game
@@ -20,8 +24,8 @@
2024
class Deck {
2125

2226
private:
23-
std::vector<DevelopmentCard*> deck;
24-
std::vector<DevelopmentCard*> discardPile;
27+
std::vector<DevCardType> deck;
28+
std::vector<DevCardType> discardPile;
2529

2630
void shuffleDeck();
2731
void reshuffleDeck();
@@ -31,8 +35,8 @@ class Deck {
3135
virtual ~Deck();
3236

3337
int getSize();
34-
DevelopmentCard* drawCard();
35-
void discard(DevelopmentCard* toDiscard);
38+
DevCardType drawCard();
39+
void discard(DevCardType toDiscard);
3640
};
3741

3842
#endif /* DECK_H_ */

include/DevelopmentCard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <stdexcept>
1818

1919

20-
enum DevCardType { KNIGHT, VICTORYPOINT, YEAROFPLENTY, MONOPOLY, ROADBUILDING };
20+
//enum DevCardType { KNIGHT, VICTORYPOINT, YEAROFPLENTY, MONOPOLY, ROADBUILDING };
2121

2222

2323
/**

include/GameBoard.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#include "GameDice.h"
2020
#include "Deck.h"
2121

22-
#include "DevelopmentCard.h"
22+
//#include "DevelopmentCard.h"
23+
2324

2425
class GameVisitor;
2526

@@ -36,7 +37,7 @@ class GameBoard {
3637

3738
Deck deck;
3839

39-
std::map<Coordinate, std::vector<std::shared_ptr<Road>>>roads;
40+
std::map<Coordinate, std::vector<std::shared_ptr<Road>>> roads;
4041

4142
std::vector<std::unique_ptr<Player>> players;
4243
Coordinate robber;
@@ -45,11 +46,12 @@ class GameBoard {
4546
int maxVictoryPoints;
4647
int winner;
4748

48-
void addResource(int x, int y, resourceType res, int val);
49-
bool checkRolls(int* rolls);
49+
void addResource(int x, int y, resourceType res, int val);
50+
bool checkRolls(int* rolls);
5051

5152
bool isValidBoard() const;
5253

54+
5355
bool outOfBounds(const Coordinate& coord) const;
5456
bool roadExists(Coordinate start, Coordinate end) const;
5557
bool isRoadConnectionPoint(Coordinate point, Player& Owner) const;
@@ -63,9 +65,9 @@ class GameBoard {
6365
void createRing(Coordinate topRight, int sideLength, std::vector<resourceType>& resources, std::vector<int>& rolls);
6466
void insertTile(Coordinate location, std::vector<resourceType>& resources, std::vector<int>& rolls);
6567

66-
std::pair<int, int> startTurn();
67-
void enableRobber();
68-
void payoutResources(int roll);
68+
std::pair<int, int> startTurn();
69+
void enableRobber();
70+
void payoutResources(int roll);
6971

7072
public:
7173
GameBoard(const std::vector<std::string>& playerNames);
@@ -82,7 +84,7 @@ class GameBoard {
8284
void save(std::ostream& out);
8385

8486
void buyCard(Player& owner);
85-
void discardCard(DevelopmentCard * card);
87+
void discardCard(DevCardType card);
8688

8789
ResourceTile& getResourceTile(Coordinate location) const;
8890

@@ -138,11 +140,11 @@ class GameBoard {
138140
int getNoOfPlayers();
139141
Player& getPlayer(int index);
140142

141-
bool testRollChecking(int* rolls);
143+
bool testRollChecking(int* rolls);
142144

143-
bool moveRobber(Coordinate newRobber);
144-
Coordinate getRobber() const;
145-
bool canRobberRob(Player& opponent, Coordinate location);
145+
bool moveRobber(Coordinate newRobber);
146+
Coordinate getRobber() const;
147+
bool canRobberRob(Player& opponent, Coordinate location);
146148

147149
};
148150

include/Player.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "tinyxml2.h"
1818
#include "Util.h"
1919
#include "GameVisitor.h"
20+
#include "Deck.h"
2021

2122
// WHEAT, SHEEP, STONE, BRICK, WOOD is the order because it matches the enum in GamePiece.h
2223
const int WHEAT_INDEX = 0;
@@ -26,8 +27,8 @@ const int BRICK_INDEX = 3;
2627
const int WOOD_INDEX = 4;
2728

2829

29-
class DevelopmentCard;
30-
class Deck;
30+
//class DevelopmentCard;
31+
//class Deck;
3132

3233

3334
class Player {
@@ -75,7 +76,7 @@ class Player {
7576

7677
int getDevCardsInHand();
7778

78-
bool buyCard(std::unique_ptr<DevelopmentCard> &card);
79+
bool buyCard(DevCardType card);
7980
std::string getName() const;
8081
GameBoard* getBoard();
8182
void setBoard(GameBoard* newboard);

src/Deck.cpp

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,25 @@
1313
*/
1414
Deck::Deck()
1515
{
16-
// TODO Auto-generated constructor stub
1716
for(int i = 0; i < 15; i++)
1817
{
19-
DevelopmentCard* card = new KnightCard();
20-
this->deck.push_back(card);
18+
this->deck.push_back(KNIGHT);
2119
}
2220
for(int i = 0; i < 4; i++)
2321
{
24-
DevelopmentCard* card = new VictoryPointCard();
25-
this->deck.push_back(card);
22+
this->deck.push_back(VICTORYPOINT);
2623
}
2724
for(int i = 0; i < 2; i++)
2825
{
29-
DevelopmentCard* card = new YearOfPlentyCard();
30-
this->deck.push_back(card);
26+
this->deck.push_back(YEAROFPLENTY);
3127
}
3228
for(int i = 0; i < 2; i++)
3329
{
34-
DevelopmentCard* card = new MonopolyCard();
35-
this->deck.push_back(card);
30+
this->deck.push_back(MONOPOLY);
3631
}
3732
for(int i = 0; i < 2; i++)
3833
{
39-
DevelopmentCard* card = new RoadBuildingCard();
40-
this->deck.push_back(card);
34+
this->deck.push_back(ROADBUILDING);
4135
}
4236

4337
shuffleDeck();
@@ -49,19 +43,7 @@ Deck::Deck()
4943
Deck::~Deck() {
5044
// TODO Auto-generated destructor stub
5145

52-
while(!this->deck.empty())
53-
{
54-
delete this->deck.back();
55-
this->deck.pop_back();
56-
//std::cout<<":";
57-
}
58-
59-
while(!this->discardPile.empty())
60-
{
61-
delete this->discardPile.back();
62-
this->discardPile.pop_back();
63-
//std::cout<<":";
64-
}
46+
6547
}
6648

6749
/**
@@ -77,7 +59,7 @@ int Deck::getSize()
7759
* Pull a random card from the deck. If the deck is empty, reshuffle it.
7860
* @return An owning raw pointer to a random card from the deck.
7961
*/
80-
DevelopmentCard* Deck::drawCard()
62+
DevCardType Deck::drawCard()
8163
{
8264
if(this->getSize() == 0)
8365
{
@@ -86,10 +68,10 @@ DevelopmentCard* Deck::drawCard()
8668

8769
if(this->getSize() == 0)
8870
{
89-
return NULL;
71+
return NULLTYPE;
9072
}
9173

92-
DevelopmentCard* card = this->deck.back();
74+
DevCardType card = this->deck.back();
9375
this->deck.pop_back();
9476
return card;
9577
}
@@ -120,7 +102,7 @@ void Deck::reshuffleDeck()
120102
* Return a played card to the discard pile.
121103
* @param toDiscard An owning raw pointer to the card to discard.
122104
*/
123-
void Deck::discard(DevelopmentCard* toDiscard)
105+
void Deck::discard(DevCardType toDiscard)
124106
{
125107
discardPile.push_back(toDiscard);
126108
}

src/GameBoard.cpp

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,47 +1276,20 @@ void GameBoard::payoutResources(int roll) {
12761276
void GameBoard::buyCard(Player& owner) {
12771277
if (owner.canBuyCard() && deck.getSize() > 0) {
12781278

1279-
DevelopmentCard * card_ptr = deck.drawCard();
1280-
1281-
std::unique_ptr<DevelopmentCard> knight = std::unique_ptr<
1282-
DevelopmentCard>(new KnightCard());
1283-
std::unique_ptr<DevelopmentCard> victorypoint = std::unique_ptr<
1284-
DevelopmentCard>(new VictoryPointCard());
1285-
std::unique_ptr<DevelopmentCard> monopoly = std::unique_ptr<
1286-
DevelopmentCard>(new MonopolyCard());
1287-
std::unique_ptr<DevelopmentCard> yearofplenty = std::unique_ptr<
1288-
DevelopmentCard>(new YearOfPlentyCard());
1289-
std::unique_ptr<DevelopmentCard> roadbuilding = std::unique_ptr<
1290-
DevelopmentCard>(new RoadBuildingCard());
1291-
1292-
switch (card_ptr->getType()) {
1293-
case KNIGHT:
1294-
owner.buyCard(knight);
1295-
break;
1296-
case VICTORYPOINT:
1297-
owner.buyCard(victorypoint);
1298-
break;
1299-
case MONOPOLY:
1300-
owner.buyCard(monopoly);
1301-
break;
1302-
case YEAROFPLENTY:
1303-
owner.buyCard(yearofplenty);
1304-
break;
1305-
case ROADBUILDING:
1306-
owner.buyCard(roadbuilding);
1307-
break;
1308-
default:
1309-
break;
1310-
}
1279+
DevCardType card = deck.drawCard();
1280+
1281+
1282+
owner.buyCard(card);
13111283

1312-
delete (card_ptr);
13131284
}
13141285
}
13151286

13161287
/**
13171288
* Discards a card back into the deck
13181289
*/
1319-
void GameBoard::discardCard(DevelopmentCard * card) {
1290+
1291+
void GameBoard::discardCard(DevCardType card){
1292+
13201293
deck.discard(card);
13211294
}
13221295

src/Player.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <iostream>
1818

1919

20-
#include "DevelopmentCard.h"
2120
#include "GameBoard.h"
2221

2322

@@ -325,10 +324,10 @@ int Player::getVictoryPoints()
325324
* @param card An owning pointer to the card the player acquired.
326325
* @return True if the player successfully bought a card, or false if the player had insufficient resources
327326
*/
328-
bool Player::buyCard(std::unique_ptr<DevelopmentCard>& card)
327+
bool Player::buyCard(DevCardType card)
329328
{
330329
if(getWheat() > 0 && getOre() > 0 && getWool() > 0){
331-
developmentCards[card->getType()]++;
330+
developmentCards[card]++;
332331
addWheat(-1);
333332
addOre(-1);
334333
addWool(-1);
@@ -489,7 +488,7 @@ bool Player::playYearOfPlenty(int resourceType){
489488
if(developmentCards[YEAROFPLENTY] > 0){
490489
developmentCards[YEAROFPLENTY]--;
491490
addResource(resourceType, 2);
492-
board.discardCard(new YearOfPlentyCard());
491+
board.discardCard(YEAROFPLENTY);
493492
return true;
494493
}
495494
return false;
@@ -509,7 +508,7 @@ bool Player::playMonopoly(int resourceType){
509508
for(auto& player : board.getPlayers()) {
510509
addResource(resourceType, player->giveAllResources(resourceType));
511510
}
512-
board.discardCard(new MonopolyCard());
511+
board.discardCard(MONOPOLY);
513512
return true;
514513
}
515514
return false;
@@ -532,7 +531,7 @@ bool Player::playRoadBuilding(Coordinate start1, Coordinate end1, Coordinate sta
532531
board.PlaceRoad(start2, end2, *this);
533532
}
534533
developmentCards[ROADBUILDING]--;
535-
board.discardCard(new RoadBuildingCard());
534+
board.discardCard(ROADBUILDING);
536535
return true;
537536
}
538537
}

tests/testSerialization.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "Serialization.h"
99
#include "GameBoard.h"
10-
#include "DevelopmentCard.h"
1110
#include "Player.h"
1211

1312
#include "tinyxml2.h"
@@ -41,17 +40,12 @@ TEST(SerializationTest, testCardSerialization) {
4140
Player& testPlayer = testBoard.getPlayer(0);
4241

4342

44-
std::unique_ptr<DevelopmentCard> knight_card = unique_ptr<DevelopmentCard>(new KnightCard());
45-
std::unique_ptr<DevelopmentCard> victory_card = unique_ptr<DevelopmentCard>(new VictoryPointCard());
46-
std::unique_ptr<DevelopmentCard> plenty_card = unique_ptr<DevelopmentCard>(new YearOfPlentyCard());
47-
std::unique_ptr<DevelopmentCard> monopoly_card = unique_ptr<DevelopmentCard>(new MonopolyCard());
48-
std::unique_ptr<DevelopmentCard> road_card = std::unique_ptr<DevelopmentCard>(new RoadBuildingCard());
4943

50-
testPlayer.buyCard(knight_card);
51-
testPlayer.buyCard(victory_card);
52-
testPlayer.buyCard(plenty_card);
53-
testPlayer.buyCard(monopoly_card);
54-
testPlayer.buyCard(road_card);
44+
testPlayer.buyCard(KNIGHT);
45+
testPlayer.buyCard(VICTORYPOINT);
46+
testPlayer.buyCard(YEAROFPLENTY);
47+
testPlayer.buyCard(MONOPOLY);
48+
testPlayer.buyCard(ROADBUILDING);
5549

5650
stringstream stream;
5751
testBoard.save(stream);

0 commit comments

Comments
 (0)