Skip to content

Commit 60bb1e1

Browse files
committed
Work on lesson 02
1 parent 0630783 commit 60bb1e1

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

src/game_components.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ struct Game
128128
Character player;
129129
std::function<void(Game &)> start_game;
130130

131-
std::map<std::string, Variable> variables;
131+
// enable transparent comparators for std::string
132+
std::map<std::string, Variable, std::less<>> variables;
132133
std::vector<std::string> display_variables;
133134
std::string current_map;
134135
std::chrono::milliseconds clock;

src/game_hacking_lesson_02.cpp

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Game_Map make_map()// NOLINT cognitive complexity
2929
fill(pixels, Color{ 0, 0, 250, 255 });// NOLINT magic number
3030
};
3131

32-
32+
const std::string location_string = fmt::format("{}:{}", __FILE__, __LINE__);
3333
auto wall_draw = [colors_used]([[maybe_unused]] Vector2D_Span<Color> &pixels,
3434
[[maybe_unused]] const Game &game,
3535
[[maybe_unused]] Point map_location,
@@ -58,13 +58,18 @@ Game_Map make_map()// NOLINT cognitive complexity
5858
// static constexpr auto MyPrettyBlue = Color{10,24, 200, 255}; // for example
5959
//
6060
// Also consider naming the other colors used!
61+
62+
static constexpr auto mint_green = Color{ 64, 128, 64, 255 };
63+
6164
switch ((game.clock.count() / 1000) % 2) {// NOLINT magic number
6265
case 0:
63-
fill(pixels, Color{ 64, 128, 64, 255 });// NOLINT magic number
66+
fill(pixels, mint_green);
6467
break;
6568
case 1:
6669
fill(pixels, Color{ 128, 64, 64, 255 });// NOLINT magic number
6770
break;
71+
72+
// When you add a new color, try to not use // NOLINT!
6873
}
6974

7075
colors_used->insert(pixels.at(Point{ 3, 3 }));
@@ -108,18 +113,72 @@ Game_Map make_map()// NOLINT cognitive complexity
108113
map.locations.at(Point{ 8, 6 }) = Flashing_Tile;// NOLINT magic numbers
109114
map.locations.at(Point{ 5, 5 }) = Flashing_Tile;// NOLINT magic numbers
110115

116+
static constexpr auto test_and_set =
117+
[](Game &game, const std::string &key_to_test, const std::string &key_to_set) -> bool// NOLINT easily swappable
118+
{
119+
if (const auto &value = game.variables.find(key_to_test);
120+
value != game.variables.end() && std::get<bool>(value->second)) {
121+
game.variables[key_to_set] = true;
122+
return true;
123+
} else {
124+
return false;
125+
}
126+
};
127+
111128
map.locations.at(Point{ 2, 1 }).enter_action = [](Game &game, Point, Direction) {
112129
game.last_message = "What is a Magic Number? {2,3}";
130+
game.variables["clue1"] = true;
113131
};
114132

115133
map.locations.at(Point{ 2, 3 }).enter_action = [](Game &game, Point, Direction) {
116-
game.last_message = "Magic Number? https://en.wikipedia.org/wiki/Magic_number_(programming) {4, 3}";
134+
if (test_and_set(game, "clue1", "clue2")) {
135+
game.last_message = "Magic Number? https://en.wikipedia.org/wiki/Magic_number_(programming) {4, 3}";
136+
}
117137
};
118138

119139
map.locations.at(Point{ 4, 3 }).enter_action = [](Game &game, Point, Direction) {
120-
game.last_message = "It's a hard coded constant. This is generally a 'Code Smell'";
140+
if (test_and_set(game, "clue2", "clue3")) {
141+
game.last_message = "It's a hard coded constant. This is generally a 'Code Smell' {3, 5}";
142+
}
143+
};
144+
145+
map.locations.at(Point{ 3, 5 }).enter_action = [](Game &game, Point, Direction) {// NOLINT magic number
146+
if (test_and_set(game, "clue3", "clue4")) {
147+
game.last_message = "'Code Smells' might indicate other problems in your code {1, 8}";
148+
}
149+
};
150+
151+
map.locations.at(Point{ 1, 8 }).enter_action = [](Game &game, Point, Direction) {// NOLINT magic number
152+
if (test_and_set(game, "clue4", "clue5")) {
153+
game.last_message = "This code has many // NOLINT comments to prevent 'magic number' warnings {8, 1}";
154+
}
155+
};
156+
157+
map.locations.at(Point{ 8, 1 }).enter_action = [](Game &game, Point, Direction) {// NOLINT magic number
158+
if (test_and_set(game, "clue5", "clue6")) {
159+
game.last_message = fmt::format("Most of these are for colors and locations. Look in {}. {{8, 7}}", __FILE__);
160+
}
161+
};
162+
163+
map.locations.at(Point{ 8, 7 }).enter_action = [](Game &game, Point, Direction) {// NOLINT magic number
164+
if (test_and_set(game, "clue6", "clue7")) {
165+
game.last_message = "Named constants avoid this warning. Ex: const auto Blue = Color{0,0,255,255}; {7, 8}";
166+
}
121167
};
122168

169+
map.locations.at(Point{ 7, 8 }).enter_action = [](Game &game, Point, Direction) {// NOLINT magic number
170+
if (test_and_set(game, "clue7", "clue8")) {
171+
game.last_message = "To access the buttom corner, you need to display *more than 2* colors {5, 6}";
172+
}
173+
};
174+
175+
map.locations.at(Point{ 5, 6 }).enter_action = [=](Game &game, Point, Direction) {// NOLINT magic number
176+
if (test_and_set(game, "clue8", "clue9")) {
177+
game.last_message = fmt::format("Check out {} for more info on how to add colors.", location_string);
178+
}
179+
};
180+
181+
123182
map.locations.at(special_location) = Flashing_Tile;
124183
map.locations.at(special_location).can_enter =
125184
[colors_used]([[maybe_unused]] const Game &game, Point, [[maybe_unused]] Direction direction) {

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,13 @@ int main(int argc, const char **argv)
472472
fmt::format("{} {}",
473473
travels::cmake::project_name,
474474
travels::cmake::project_version));// version string, acquired
475-
// from config.hpp via CMake
475+
// from config.hpp via CMake
476476

477477
// to start the lessons, comment out this line
478478
auto game = lefticus::travels::make_game(resource_search_directories());
479479

480480
// and uncomment this line
481-
// auto game = lefticus::travels::hacking::lesson_00::make_lesson();
481+
// auto game = lefticus::travels::hacking::lesson_02::make_lesson();
482482

483483
// we want to take over as the main spdlog sink
484484
auto log_sink = std::make_shared<lefticus::travels::log_sink<std::mutex>>();

0 commit comments

Comments
 (0)