-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
187 lines (150 loc) · 5.31 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// davep 20240615 I'm bored. Let's play with C++ and write a monopoly simulator.
// After having played with Rust for a while, can I do C++ without any dynamic memory?
// Can I do C++ without any OOP-y-ness (which I hear has fallen out of fashion)
#include <iostream>
#include <ranges>
#include <vector>
#include <fmt/format.h>
#include <cassert>
#include "board.h"
#include "player.h"
void calculate_penalty(Penalty& penalty, Player& player)
{
switch (penalty.penalty_type) {
case PENALTY_INCOME_TAX:
player.pay_tax(200);
break;
case PENALTY_LUXURY_TAX:
player.pay_tax(100);
break;
case PENALTY_GO_TO_JAIL:
player.go_to_jail();
break;
default:
std::string msg = fmt::format("{} unhandled PenaltyType {}",
__func__, static_cast<int>(penalty.penalty_type));
throw std::runtime_error(msg);
}
}
int main()
{
std::cout << "Hello, World!" << std::endl;
const std::string board_filename { "/home/dpoole/src/monop/board.txt" };
std::vector<Space> board = load_board(board_filename);
assert(board.size() == BOARD_SIZE && "The board must contain 40 elements.");
std::vector<Player> player_list;
player_list.emplace_back( "Dave", 1500 );
player_list.emplace_back( "Sarah", 1500 );
player_list.emplace_back( "Matthew", 1500 );
player_list.emplace_back( "Brendan", 1500 );
for ( uint i=0 ; i<100 ; i++ ) {
for ( uint player_idx=0 ; player_idx<player_list.size() ; player_idx++ ) {
while (true) {
Player &player = player_list.at(player_idx);
auto roll = player.roll();
fmt::print("Player {} rolled {} {}\n", player.name, roll.first, roll.second);
bool rolled_doubles = roll.first == roll.second;
if (rolled_doubles) {
// TODO goto jail on rolling three doubles
fmt::print("{} rolled doubles!\n", player.name);
if (player.in_jail() ) {
fmt::print("{} is no longer in jail\n", player.name);
player.leave_jail();
// end of turn; go to next player
break;
}
}
if (player.in_jail() ) {
if (player.do_jail_turn()) {
// player has had three turns in jail
// so time to leave
player.leave_jail();
}
// end of turn; go to next player
break;
}
uint pos = player.move();
Space& land = board.at(pos);
std::visit([&player](auto &&arg) { fmt::print("player {} landed on {}\n", player.name, arg.name); }, land);
if (player.passed_go()) {
fmt::print("player {} has passed Go and earns 200!\n", player.name);
player.add_money(200);
}
if (std::holds_alternative<Property>(land)) {
auto& property = std::get<Property>(land);
std::optional<uint> owner = get_owner<Property>(property);
if (owner) {
auto& owner_player = player_list.at(owner.value());
fmt::print("{} is owned by {}\n", property.name, owner_player.name);
if ( owner != player_idx ) {
// owner is not self so rent must be paid
uint rent = property.get_rent(NO_HOUSES);
fmt::print("{} must pay {} {} in rent\n", player.name, owner_player.name, rent);
owner_player.earn_rent( player.pay_rent(rent));
}
}
else {
fmt::print("{} is unowned\n", property.name);
if (player.buys(property)) {
property.set_owner(player_idx);
}
}
}
else if (std::holds_alternative<Penalty>(land)) {
auto& penalty = std::get<Penalty>(land);
fmt::print("{} lands on a penalty {}\n", player.name, penalty.name);
calculate_penalty(penalty,player);
}
else if (std::holds_alternative<Railroad>(land)) {
auto& rr = std::get<Railroad>(land);
std::optional<uint> owner = get_owner<Railroad>(rr);
if (owner) {
auto& owner_player = player_list.at(owner.value());
fmt::print("{} is owned by {}\n", rr.name, owner_player.name);
if ( owner != player_idx ) {
// owner is not self so rent must be paid
// uint rent = property.get_rent(NO_HOUSES);
uint rent = Railroad::get_rent(owner_player.railroads_owned());
fmt::print("{} must pay {} {} in rent on Railroad(s)\n", player.name, owner_player.name, rent);
owner_player.earn_rent( player.pay_rent(rent));
}
}
else {
fmt::print("{} is unowned\n", rr.name);
if (player.buys(rr)) {
rr.set_owner(player_idx);
}
}
}
else if (std::holds_alternative<CommunityChest>(land)) {
fmt::print("{} lands on Community Chest", player.name);
}
else if (std::holds_alternative<Chance>(land)) {
fmt::print("{} lands on Chance", player.name);
}
if (!rolled_doubles) {
fmt::print("{} has completed their turn\n", player.name);
break;
}
fmt::print("{} has rolled doubles and gets another turn\n", player.name);
}
}
}
fmt::print("game is over!\n");
for (Player& player : player_list) {
for (auto property_idx = player.owned_cbegin() ; property_idx < player.owned_cend() ; property_idx++) {
Space& space = board.at(*property_idx);
// std::string name = std::visit([](auto&& arg) -> std::string { return arg.name; }, space);
std::string name = get_name(space);
// auto& property = std::get<Property>(space);
fmt::print("{} owns {}\n", player.name, name);
}
}
for (Player& player : player_list) {
fmt::print("{} stats {}\n", player.name, player.get_stats());
}
for (const Space &space : board ) {
fmt::print("{}\n", get_name(space));
}
return 0;
}