Skip to content

Commit 7fb2f25

Browse files
committed
Merge pull request #15 from Databean/RoadDevCard
Road dev card
2 parents b833033 + 040a3c5 commit 7fb2f25

File tree

10 files changed

+106
-9
lines changed

10 files changed

+106
-9
lines changed

include/DevelopmentCard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "Player.h"
1515
#include "GameVisitor.h"
1616

17+
#include <stdexcept>
18+
1719

1820
enum DevCardType { KNIGHT, VICTORYPOINT, YEAROFPLENTY, MONOPOLY, ROADBUILDING };
1921

@@ -97,6 +99,7 @@ class RoadBuildingCard : public DevelopmentCard {
9799

98100
virtual DevCardType getType() const;
99101
virtual void playCard();
102+
void playCard(Coordinate start1, Coordinate end1, Coordinate start2, Coordinate end2);
100103
};
101104

102105

include/GameBoard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <iostream>
1010
#include <fstream>
1111
#include <cstddef>
12+
#include <stdexcept>
1213

1314
#include "Util.h"
1415
#include "GamePiece.h"
@@ -37,6 +38,7 @@ class GameBoard {
3738

3839
bool isValidBoard() const;
3940

41+
4042
bool verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner) const;
4143
bool outOfBounds(const Coordinate& coord) const;
4244
bool roadExists(Coordinate start, Coordinate end) const;
@@ -82,7 +84,6 @@ class GameBoard {
8284

8385
bool buyRoad(Coordinate start, Coordinate end, Player& Owner);
8486

85-
8687
//void PlaceSettlement(Coordinate location, Player& Owner);
8788
void PlaceCity(Coordinate location, Player& Owner);
8889
bool PlaceRoad(Coordinate start, Coordinate end, Player& Owner);

include/Player.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const int WOOL_INDEX = 4;
2727

2828
class DevelopmentCard;
2929
class Deck;
30-
30+
class GameBoard;
3131

3232

3333

@@ -40,7 +40,7 @@ class Player {
4040
int armySize;
4141
int longestRoad;
4242
int victoryPoints;
43-
// GameBoard *board;
43+
GameBoard* board;
4444
int resources[5];
4545

4646

@@ -63,6 +63,9 @@ class Player {
6363
void buyCard(std::unique_ptr<DevelopmentCard> card);
6464
std::string getName() const;
6565

66+
GameBoard* getBoard();
67+
void setBoard(GameBoard* newboard);
68+
6669
void playCard(DevelopmentCard* card);
6770

6871
bool canBuyRoad();

include/Road.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Player.h"
66
#include <vector>
77
#include <cmath>
8+
#include <stdexcept>
89

910
#include "Player.h"
1011

src/DevelopmentCard.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "DevelopmentCard.h"
1010
#include "Util.h"
11+
#include "GameBoard.h"
1112

1213

1314
//Base Class
@@ -104,8 +105,17 @@ DevCardType RoadBuildingCard::getType() const
104105
void RoadBuildingCard::playCard()
105106
{
106107
//Call function to build a road twice
108+
//Perhaps this function could signal to the controller how it should receive input next.
107109
}
108110

111+
void RoadBuildingCard::playCard(Coordinate start1, Coordinate end1, Coordinate start2, Coordinate end2){
112+
if (!(getOwner()->getBoard()->PlaceRoad(start1, end1, *getOwner()))){
113+
throw std::invalid_argument("The first road passed was not valid, no roads placed");
114+
}
115+
if ((!getOwner()->getBoard()->PlaceRoad(start2, end2, *getOwner()))){
116+
throw std::invalid_argument("The second road passed was not valid, only the first road was placed");
117+
}
118+
}
109119

110120

111121

src/GameBoard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ bool GameBoard::PlaceRoad(Coordinate start, Coordinate end, Player& Owner) {
358358
std::shared_ptr<Road> newRoad;
359359
try {
360360
newRoad = std::shared_ptr<Road>(new Road(start, end, Owner));
361-
} catch (int n) {
361+
} catch (std::invalid_argument& e) {
362362
//Coordinates did not meet the criteria for a valid road
363363
return false;
364364
}

src/Player.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ int Player::getVictoryPoints()
118118
return victoryPoints;
119119
}
120120

121+
GameBoard* Player::getBoard(){
122+
return board;
123+
}
124+
125+
void Player::setBoard(GameBoard * newboard){
126+
board = newboard;
127+
}
121128

122129
void Player::buyCard(std::unique_ptr<DevelopmentCard> card)
123130
{

src/Road.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Road::Road(Coordinate start, Coordinate end, Player& Owner) : owner(Owner) {
1313

1414
//If the input is bad, throw an exception so bad roads won't be built
1515
if(!checkRoad()){
16-
throw -1;
16+
throw std::invalid_argument("Road start on one corner and go to a corner exactly 1 away");
1717
}
1818
}
1919

tests/test_DevelopmentCards.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <vector>
2+
#include <memory>
3+
#include <map>
4+
#include <stdexcept>
5+
6+
#include "GameBoard.h"
7+
#include "GamePiece.h"
8+
#include "DevelopmentCard.h"
9+
#include "Util.h"
10+
#include "UnitTest++.h"
11+
12+
13+
TEST(RoadBuildingCard_good){
14+
std::vector<std::unique_ptr<Player>> players {};
15+
players.emplace_back(new Player("tester"));
16+
Player& test_player = *players[0];
17+
GameBoard * test_board = new GameBoard(std::move(players));
18+
test_player.setBoard(test_board);
19+
20+
test_board->PlaceSettlement(Coordinate(0,0), test_player);
21+
22+
RoadBuildingCard test_card(&test_player);
23+
24+
try{
25+
test_card.playCard(Coordinate(0,0), Coordinate(-1,1), Coordinate(0,0), Coordinate(1,0));
26+
CHECK(true);
27+
} catch (std::invalid_argument& e){
28+
std::cout << e.what();
29+
CHECK(false);
30+
}
31+
}
32+
33+
TEST(RoadBuildingCard_bad){
34+
std::vector<std::unique_ptr<Player>> players {};
35+
players.emplace_back(new Player("tester"));
36+
Player& test_player = *players[0];
37+
GameBoard * test_board = new GameBoard(std::move(players));
38+
test_player.setBoard(test_board);
39+
40+
test_board->PlaceSettlement(Coordinate(0,0), test_player);
41+
42+
RoadBuildingCard test_card(&test_player);
43+
44+
try{
45+
test_card.playCard(Coordinate(0,0), Coordinate(0,2), Coordinate(0,0), Coordinate(1,0));
46+
CHECK(false);
47+
} catch (std::invalid_argument& e){
48+
CHECK(true);
49+
}
50+
51+
try{
52+
test_card.playCard(Coordinate(0,0), Coordinate(0,1), Coordinate(0,0), Coordinate(2,2));
53+
CHECK(false);
54+
} catch (std::invalid_argument& e){
55+
CHECK(true);
56+
}
57+
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+

tests/test_Roads.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ TEST(road_constuctor_bad){
2727
try {
2828
Road test_road_1(start, start, test_player);
2929
CHECK(false);
30-
} catch (int n) {
31-
CHECK(n == -1);
30+
} catch (const std::invalid_argument& e) {
31+
CHECK(true);
3232
}
3333
//test road too long
3434
try {
3535
Road test_road_2(start, end, test_player);
3636
CHECK(false);
37-
} catch (int n) {
38-
CHECK(n == -1);
37+
} catch (const std::invalid_argument& e) {
38+
CHECK(true);
3939
}
4040

4141
}

0 commit comments

Comments
 (0)