-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
144 lines (114 loc) · 3.96 KB
/
board.cpp
File metadata and controls
144 lines (114 loc) · 3.96 KB
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
//
// Created by dpoole on 6/24/24.
//
#include <string>
#include <utility>
#include <vector>
#include <fmt/format.h>
#include <ranges>
#include <fstream>
#include "board.h"
Property::Property(std::string name, std::vector<uint> values)
: Card(std::move(name))
{
if (values.size() != 9) {
std::string msg = fmt::format("expected 9 values but found {}", values.size());
throw std::runtime_error(msg);
}
this->purchase_price = values[0];
this->house_cost = values[1];
std::copy(values.begin() + 2, values.end(), this->rents.begin());
}
Space parse_board_line(const std::string &line) {
// https://www.youtube.com/watch?v=V14xGZAyVKI
auto split_strings = std::ranges::views::split(line, ',');
std::vector<std::string> tokens;
for (const auto &rng: split_strings) {
tokens.emplace_back(rng.begin(), rng.end());
}
// fmt::print("size={}\n", tokens.size());
tokens.clear();
std::ranges::copy(split_strings | std::ranges::views::transform([](auto &&rng) {
std::string s(rng.begin(), rng.end());
return s;
}), std::back_inserter(tokens));
// fmt::print("found len={} tokens. First={}\n", tokens.size(), tokens[0]);
if (tokens.size() == 10) {
// we found a property; convert rest of tokens into integers
std::vector<uint> values;
try {
std::ranges::copy(tokens | std::views::drop(1) | std::views::transform([](auto &&s) -> int {
// fmt::print("stoi({})\n", s);
return std::stoi(s);
}), std::back_inserter(values));
}
catch (std::invalid_argument const &err) {
std::string msg = fmt::format("property {} has invalid argument {}\n", tokens[0], err.what());
throw std::runtime_error(msg);
}
// Space space { std::in_place_type<Property>, tokens[0], values };
//fmt::print("{}\n", std::get<Property>(board.back()).name);
// auto p = std::get<Property>(space);
// fmt::print("{} price={} hotel rent={}\n", p.name, p.get_purchase_price(), p.get_rent(HOTEL));
return Space { std::in_place_type<Property>, tokens[0], values };
}
if (tokens[0] == "Community Chest") {
return Space { std::in_place_type<CommunityChest>, tokens[0] };
}
if (tokens[0] == "Chance") {
return Chance(tokens[0]);
}
if (tokens[0] == "Income Tax") {
return Penalty(tokens[0], PENALTY_INCOME_TAX);
}
if (tokens[0] == "Luxury Tax") {
return Penalty(tokens[0], PENALTY_LUXURY_TAX);
}
fmt::print("railroad? {} {}\n", tokens[0].at(tokens[0].size() - 1), tokens[0].at(tokens[0].size() - 2));
if (tokens[0].at(tokens[0].size() - 1) == 'R' && tokens[0].at(tokens[0].size() - 2) == 'R') {
return Railroad {tokens[0]};
}
if (tokens[0] == "Jail") {
return Utility(tokens[0]);
}
if (tokens[0] == "Go To Jail") {
return Penalty(tokens[0], PENALTY_GO_TO_JAIL);
}
if (tokens[0] == "Electric Company" || tokens[0] == "Water Works") {
return Utility(tokens[0]);
}
if (tokens[0] == "Free Parking") {
return Utility(tokens[0]);
}
if (tokens[0] == "Go") {
return Space { std::in_place_type<GoCard>, tokens[0] };
}
auto msg = fmt::format("unidentified card \"{}\"", tokens[0]);
throw std::runtime_error(msg);
}
std::vector<Space> load_board(const std::string &file_name)
{
std::ifstream input_file(file_name);
if (input_file.fail()) {
auto msg = fmt::format("failed to open {} {}", file_name, std::strerror(errno));
throw std::runtime_error(msg);
}
std::vector<Space> board;
std::string line;
while (std::getline(input_file, line)) {
Space s = parse_board_line(line);
board.push_back(std::move(s));
Space& last = board.back();
if (const Property *p = std::get_if<Property>(&last) ) {
fmt::print("add property {} to the board\n", p->name);
}
// https://en.cppreference.com/w/cpp/utility/variant/visit
// var_t w = std::visit([](auto&& arg) -> var_t { return arg + arg; }, v);
std::string name = std::visit([](auto&& arg) -> std::string { return arg.name; }, s);
fmt::print("add {} to the board\n", name);
}
return board;
}
std::string get_name(const Space& space) {
return std::visit([](auto &&arg) -> std::string { return arg.name; }, space);
}