-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_game.cpp
More file actions
154 lines (123 loc) · 2.82 KB
/
text_game.cpp
File metadata and controls
154 lines (123 loc) · 2.82 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
144
145
146
147
148
149
150
151
152
153
154
#include <print>
#include <string>
#include <termios.h>
#include <unistd.h>
#include "ansi.h"
#include "text_gallows.h"
#include "text_game.h"
#include "word.h"
#include "wordlist.h"
namespace
{
std::string bad_guesses(const std::vector<char> &list)
{
std::string out{ANSI::reset + "Guesses: " + ANSI::light_red};
for (char ch : list)
{
out += std::format("{} ", static_cast<char>(toupper(ch)));
}
return out + "\n";
}
void clear_screen()
{
std::print(ANSI::clear);
}
void cursor_home()
{
std::print(ANSI::home);
}
// Get a character from the keyboard without requiring enter afterward.
#ifdef _WIN32
#include <conio.h>
char getchar_immediate()
{
return _getch();
}
#else
char getchar_immediate()
{
struct termios oldterm, newterm;
char ch;
tcgetattr(STDIN_FILENO, &oldterm);
newterm = oldterm;
newterm.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newterm);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldterm);
return ch;
}
#endif
}
Game::Game(size_t min_length)
: words_{"../assets/words-2025-5-16.txt"}
, gallows_{}
, state_{GameState::PLAYING}
, min_length_{min_length}
, word_{}
{
}
void Game::run()
{
while (state_ != GameState::COMPLETE)
{
clear_screen();
word_ = words_.random(min_length_);
gallows_.reset();
do
{
drawPlaying();
updatePlaying();
} while (state_ == GameState::PLAYING);
clear_screen();
if (state_ == GameState::SUCCESS)
{
drawSuccess();
}
else
{
drawFailure();
}
drawEnding();
updateEnding();
}
}
void Game::updatePlaying()
{
auto ch = getchar_immediate();
if (isalpha(ch) && !word_.guessed(ch) && !word_.guess(ch))
gallows_.next();
if (word_.done())
{
state_ = GameState::SUCCESS;
}
else if (word_.bad_letters.size() == gallows_.stages() - 1)
{
state_ = GameState::FAILURE;
}
}
void Game::updateEnding()
{
char ch = getchar_immediate();
if (static_cast<char>(tolower(ch)) == 'n')
state_ = GameState::COMPLETE;
}
void Game::drawPlaying() const
{
gallows_.draw();
std::print("{}{}\n{}{}\n\n=> ", ANSI::move_cursor(26, 1), bad_guesses(word_.bad_letters), ANSI::reset, word_);
}
void Game::drawSuccess() const
{
gallows_.draw_state();
std::println("{}{}\nYou got it: {}{}", ANSI::move_cursor(26, 1), ANSI::light_green, word_.display(), ANSI::reset);
}
void Game::drawFailure() const
{
gallows_.draw_state();
std::println(
"{}{}\nBad luck! It was {}{}", ANSI::move_cursor(26, 1), ANSI::light_red, word_.display(), ANSI::reset);
}
void Game::drawEnding() const
{
std::print("\n{}play again? {}", ANSI::light_blue, ANSI::reset);
}