Skip to content

Commit 9929dfd

Browse files
committed
Fixed File Import and Export
1 parent cdba1fe commit 9929dfd

4 files changed

Lines changed: 32 additions & 41 deletions

File tree

FileExport.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void exportFile(std::vector<tile>& tiles){
88
std::ofstream csv ("Tiles.csv");
99

1010
for(tile t : tiles){
11-
csv<<t.tileIndex<<";"<<t.ownerId<<";"<<t.buyable<<";"<<t.buyPrice<<";"<<t.isMortgaged<<";"<<t.housePrice<<";"<<t.upgradeStage<<";"<<t.price0<<";"<<t.price1<<";"<<t.price2<<";"<<t.price3<<";"<<t.price4<<";"<<t.price5<<";"<<t.color<<";"<<t.tileName<<";"<<t.shortName<<"\n";
11+
csv<<t.tileIndex<<";"<<t.ownerId<<";"<<t.buyable<<";"<<t.buyPrice<<";"<<t.isMortgaged<<";"<<t.housePrice<<";"<<t.upgradeStage<<";"<<t.price0<<";"<<t.price1<<";"<<t.price2<<";"<<t.price3<<";"<<t.price4<<";"<<t.price5<<";"<<static_cast<int>(t.color)<<";"<<t.tileName<<";"<<t.shortName<<"\n";
1212
}
1313
csv.close();
1414
}
@@ -18,6 +18,7 @@ void exportFile(std::vector<player>& players){
1818
std::ofstream ownedStreetsFile ("OwnedStreets.csv");
1919

2020
for(player p : players){
21+
csv<<p.playerId<<";"<<p.symbol<<";"<<p.name<<";"<<p.money<<";"<<p.currentPosition<<";"<<p.jailed<<";"<<static_cast<int>(p.color)<<";"<<p.jailCounter<<";"<<p.jailFreeCard<<";"<<p.bankrupt<<"\n";
2122
for (int street : p.ownedStreets){
2223
ownedStreetsFile<<p.playerId<<";"<<street<<"\n";
2324
}
@@ -31,7 +32,7 @@ void exportFile(int freeParkingMoney, int currentPlayerTurn, std::vector<std::si
3132

3233
csv<<freeParkingMoney<<"\n";
3334
csv<<currentPlayerTurn<<"\n";
34-
for(std::size_t i : turnOrder){
35+
for(int i : turnOrder){
3536
csv<<i<<"\n";
3637
}
3738
csv.close();

FileImport.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ inline void importFile(std::vector<tile>& tiles) {
7070
start = pos + del.length();
7171

7272
pos = line.find(del,start);
73-
std::string colorStr = line.substr(start, pos-start);
74-
s.color = stringColorGroupMatcher(colorStr);
73+
int colorInt;
74+
colorInt = std::stoi(line.substr(start, pos-start));
75+
s.color = static_cast<ColorGroup>(colorInt);
7576
start = pos + del.length();
7677

7778
pos = line.find(del,start);
@@ -123,8 +124,9 @@ inline void importFile(std::vector<player>& players) {
123124
start = pos + del.length();
124125

125126
pos = line.find(del,start);
126-
std::string colorStr = line.substr(start, pos-start);
127-
s.color = stringColorGroupMatcher(colorStr);
127+
int colorInt;
128+
colorInt = std::stoi(line.substr(start, pos-start));
129+
s.color = static_cast<ColorGroup>(colorInt);
128130
start = pos + del.length();
129131

130132
pos = line.find(del,start);
@@ -163,7 +165,7 @@ inline void importFile(std::vector<player>& players) {
163165
ownedStreetsFile.close();
164166
}
165167

166-
inline void importFile(int freeParkingMoney, int currentPlayerTurn, std::vector<std::size_t>& turnOrder) {
168+
inline void importFile(int& freeParkingMoney, int& currentPlayerTurn, std::vector<std::size_t>& turnOrder) {
167169
std::ifstream csv;
168170
csv.open("otherData.csv");
169171
std::string line;

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ cmake --build build
2828
```
2929

3030
Windows support is not available due to an utf-8 encoding issue with the player model.
31+
32+
## Missing features
33+
34+
There is no implementation for player vs bot since it is not easily feesible to implement with the broad decision needed to give any challange in this case.
35+
A pure random generator for doing actions and accessing menu's is not a good idea.

monopoly.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,6 @@ const std::vector<std::pair<std::string, ColorGroup>> colorCodes = {
6666
{"\033[38;2;102;0;255m", PURPLE}
6767
};
6868

69-
inline ColorGroup stringColorGroupMatcher(const std::string& colorStr) {
70-
if (colorStr == "brown") return ColorGroup::BROWN;
71-
if (colorStr == "blue") return ColorGroup::BLUE;
72-
if (colorStr == "pink") return ColorGroup::PINK;
73-
if (colorStr == "orange") return ColorGroup::ORANGE;
74-
if (colorStr == "red") return ColorGroup::RED;
75-
if (colorStr == "yellow") return ColorGroup::YELLOW;
76-
if (colorStr == "green") return ColorGroup::GREEN;
77-
if (colorStr == "darkblue") return ColorGroup::DARK_BLUE;
78-
if (colorStr == "gray") return ColorGroup::GRAY;
79-
if (colorStr == "purple") return ColorGroup::PURPLE;
80-
81-
return ColorGroup::NONE; // safer default
82-
}
83-
8469
void clearInputBuffer() {
8570
std::cin.clear();
8671
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@@ -1327,36 +1312,34 @@ int main(){
13271312
<<std::endl;
13281313
std::cin>>sel;
13291314
} else { sel = 0; }
1315+
1316+
std::vector<std::size_t> turnOrder;
1317+
int index;
13301318
if(sel == 1 && std::cin.good()){
13311319
importFile(gameBoard);
13321320
importFile(players);
1321+
importFile(freeParkingFunds, index, turnOrder);
13331322
}else{
13341323
gameBoard = initializeGameBoard();
13351324
players = initializePlayers();
1325+
// Randomize turn order
1326+
// create vector with player indices
1327+
turnOrder.resize(players.size());
1328+
// fill vector with values 0, 1, ..., players.size() - 1
1329+
std::iota(turnOrder.begin(), turnOrder.end(), 0);
1330+
// shuffle the vector
1331+
std::shuffle(turnOrder.begin(), turnOrder.end(), gen);
1332+
index = 0;
13361333
}
13371334
chanceCards = initializeChanceCards();
13381335
communityCards = initializeCommunityCards();
13391336

1340-
// Randomize turn order
1341-
// create vector with player indices
1342-
std::vector<std::size_t> turnOrder(players.size());
1343-
// fill vector with values 0, 1, ..., players.size() - 1
1344-
std::iota(turnOrder.begin(), turnOrder.end(), 0);
1345-
// shuffle the vector
1346-
std::shuffle(turnOrder.begin(), turnOrder.end(), gen);
1347-
1348-
int index = 0;
1349-
1350-
if(sel == 1 && std::cin.good()){
1351-
importFile(freeParkingFunds, currentPlayerTurn, turnOrder);
1352-
index = currentPlayerTurn;
1353-
}
1354-
13551337
//Display Gameboard
13561338
displayGameBoard();
13571339

13581340
do {
1359-
player &currentPlayer = players[index];
1341+
std::size_t playerIdx = turnOrder[index];
1342+
player &currentPlayer = players[playerIdx];
13601343

13611344
// Skip bankrupt players
13621345
if (!currentPlayer.bankrupt) {
@@ -1397,7 +1380,7 @@ int main(){
13971380
}
13981381
} else {
13991382
std::cout << "Game ended early.\n"
1400-
<<"Do you want to export the game state?\n"
1383+
<<"Do you want to export the game state? (You turn ends with this action)\n"
14011384
<<"┌────────┬────────┐\n"
14021385
<<"│ 1: YES │ 0: NO │\n"
14031386
<<"└────────┴────────┘\n"
@@ -1406,15 +1389,15 @@ int main(){
14061389
if(sel == 1 && std::cin.good()){
14071390
exportFile(players);
14081391
exportFile(gameBoard);
1409-
exportFile(freeParkingFunds, currentPlayerTurn, turnOrder);
1392+
exportFile(freeParkingFunds, ((currentPlayerTurn + 1) % turnOrder.size()), turnOrder);
14101393
std::cout<<"Exited with saving"<<std::endl;
14111394

14121395
}else if(sel == 0 && std::cin.good()){
14131396
std::cout<<"Exited without saving"<<std::endl;
14141397
}else{
14151398
exportFile(players);
14161399
exportFile(gameBoard);
1417-
exportFile(freeParkingFunds, currentPlayerTurn, turnOrder);
1400+
exportFile(freeParkingFunds, ((currentPlayerTurn + 1) % turnOrder.size()), turnOrder);
14181401
std::cout<<"Saved you :)"<<std::endl;
14191402
}
14201403
}

0 commit comments

Comments
 (0)