Skip to content

Commit 23b9d84

Browse files
committed
Some more input validation
1 parent 9929dfd commit 23b9d84

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

monopoly.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ bool trading_menue(player &p){
10071007
for(player pl : players){
10081008
if(pl.playerId != p.playerId){
10091009
otherplayers.push_back(pl.playerId);
1010-
std::cout<< std::string(i < 10 ? "0" : "") << i << " | " << pl.name << "\n";
1010+
std::cout<< colorCodes[pl.color].first << std::string(i < 10 ? "0" : "") << i << " | " << pl.name << RESET_COLOR <<"\n";
10111011
i++;
10121012
}
10131013
}
@@ -1114,21 +1114,23 @@ bool trading_menue(player &p){
11141114
std::cout<<colorCodes[players[otherplayers[sel]].color].first << players[otherplayers[sel]].symbol << " " << players[otherplayers[sel]].name << RESET_COLOR << " do you accept the trade?\n"
11151115
<<"You give: $" <<amount2;
11161116
for(int i : tiles2){
1117-
std::cout<<", "<<colorCodes[gameBoard[i].color].first<<gameBoard[i].tileName;
1117+
std::cout<<", "<<colorCodes[gameBoard[i].color].first<<gameBoard[i].tileName << RESET_COLOR;
11181118
}
11191119
std::cout<<std::endl;
11201120
std::cout<<"You get: $"<<amount1;
11211121
for(int i : tiles1){
1122-
std::cout<<", "<<colorCodes[gameBoard[i].color].first<<gameBoard[i].tileName;
1122+
std::cout<<", "<<colorCodes[gameBoard[i].color].first<<gameBoard[i].tileName << RESET_COLOR;
11231123
}
11241124
std::cout<<std::endl;
11251125
std::cout
11261126
<<"┌────────┬────────┐\n"
11271127
<<"│ 1: YES │ 0: NO │\n"
11281128
<<"└────────┴────────┘\n"
11291129
<<std::endl;
1130+
clearInputBuffer();
11301131
std::cin>>desicion;
1131-
}while (true);
1132+
}while (std::cin.fail() || (desicion != 0 && desicion != 1));
1133+
clearInputBuffer();
11321134
if(!desicion){
11331135
displayGameBoard();
11341136
std::cout<<"They didn't want your offer! 😡"<<std::endl;
@@ -1320,6 +1322,10 @@ int main(){
13201322
importFile(players);
13211323
importFile(freeParkingFunds, index, turnOrder);
13221324
}else{
1325+
if (std::cin.fail() || sel != 0) {
1326+
std::cout << "Invalid Input, continuing with normal setup!" << std::endl;
1327+
}
1328+
clearInputBuffer();
13231329
gameBoard = initializeGameBoard();
13241330
players = initializePlayers();
13251331
// Randomize turn order
@@ -1357,6 +1363,7 @@ int main(){
13571363
if (sel == 77) break;
13581364
if (!control) sel = -1;
13591365
}
1366+
clearInputBuffer();
13601367
} while (sel);
13611368
}
13621369

0 commit comments

Comments
 (0)