Skip to content

Commit 2888b9b

Browse files
committed
Small dice refactoring
1 parent 5c0c027 commit 2888b9b

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

include/GameDice.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
#ifndef GAMEDICE_H
22
#define GAMEDICE_H
33

4-
54
class GameVisitor;
65

6+
/**
7+
* The two dice used to determine the tiles that give resources.
8+
*/
79
class GameDice {
8-
private:
9-
int first;
10-
int second;
11-
12-
13-
public:
14-
int getFirst();
15-
int getSecond();
16-
bool shown;
17-
void setFirst(int newFirst);
18-
void setSecond(int newSecond);
19-
virtual void accept(GameVisitor& visitor);
20-
21-
22-
10+
private:
11+
int first;
12+
int second;
13+
public:
14+
int roll();
15+
int getFirst() const;
16+
int getSecond() const;
17+
virtual void accept(GameVisitor& visitor);
2318
};
2419

2520
#endif

src/GameBoard.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -940,21 +940,15 @@ Player& GameBoard::getPlayer(int index)
940940
* When a player begins their turn, this rolls the dice and takes the required action (paying resources or enabling robber movement)
941941
* @return A pair of the values of the dice.
942942
*/
943-
std::pair<int, int> GameBoard::startTurn()
944-
{
945-
int die1 = std::rand() % 6 + 1;
946-
int die2 = std::rand() % 6 + 1;
947-
int roll = die1+die2;
948-
949-
dice.setFirst(die1);
950-
dice.setSecond(die2);
951-
if (roll==7)
952-
enableRobber();
953-
954-
else
955-
payoutResources(roll);
956-
957-
return std::make_pair(die1, die2);
943+
std::pair<int, int> GameBoard::startTurn() {
944+
int roll = dice.roll();
945+
if (roll==7) {
946+
enableRobber();
947+
} else {
948+
payoutResources(roll);
949+
}
950+
951+
return std::make_pair(dice.getFirst(), dice.getSecond());
958952
}
959953

960954
/**

src/GameDice.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
#include "GameDice.h"
22
#include "GameVisitor.h"
33

4-
int GameDice::getFirst(){
5-
return first;
6-
}
4+
#include <cstdlib>
75

8-
int GameDice::getSecond(){
9-
return second;
6+
using std::rand;
7+
8+
/**
9+
* Re-roll the dice.
10+
* @return The sum of the two dice.
11+
*/
12+
int GameDice::roll() {
13+
first = rand() % 6 + 1;
14+
second = rand() % 6 + 1;
15+
16+
return first + second;
1017
}
1118

12-
void GameDice::setFirst(int newFirst){
13-
first = newFirst;
19+
/**
20+
* The value of the first die.
21+
* @return The first die's value.
22+
*/
23+
int GameDice::getFirst() const {
24+
return first;
1425
}
1526

16-
void GameDice::setSecond(int newSecond){
17-
second = newSecond;
27+
/**
28+
* The value of the second die.
29+
* @return The second die's value.
30+
*/
31+
int GameDice::getSecond() const {
32+
return second;
1833
}
1934

35+
/**
36+
* Double-dispatch method for GameVisitor.
37+
* @param visitor The visitor.
38+
*/
2039
void GameDice::accept(GameVisitor& visitor) {
2140
visitor.visit(*this);
2241
}

0 commit comments

Comments
 (0)