guess corrector
This commit is contained in:
parent
7d992f3b30
commit
9ea691f7e6
@ -18,6 +18,8 @@ add_executable(hang_man src/main.cpp
|
|||||||
src/words.hpp
|
src/words.hpp
|
||||||
src/words.cpp
|
src/words.cpp
|
||||||
src/utils.hpp
|
src/utils.hpp
|
||||||
|
src/GuessCorrector.cpp
|
||||||
|
src/GuessCorrector.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(hang_man PRIVATE
|
target_link_libraries(hang_man PRIVATE
|
||||||
|
@ -59,7 +59,7 @@ void Game::Run() {
|
|||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::Game() : guessed() {
|
Game::Game() {
|
||||||
std::random_device random_device{};
|
std::random_device random_device{};
|
||||||
std::mt19937 rng(random_device());
|
std::mt19937 rng(random_device());
|
||||||
|
|
||||||
@ -71,6 +71,7 @@ Game::Game() : guessed() {
|
|||||||
std::shuffle(all_words.begin(), all_words.end(), rng);
|
std::shuffle(all_words.begin(), all_words.end(), rng);
|
||||||
word = all_words.back();
|
word = all_words.back();
|
||||||
all_words.pop_back();
|
all_words.pop_back();
|
||||||
|
guess_corrector = GuessCorrector(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::handle_key(SDL_Keycode event) {
|
void Game::handle_key(SDL_Keycode event) {
|
||||||
|
@ -4,13 +4,14 @@
|
|||||||
#include <SDL_events.h>
|
#include <SDL_events.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include "GuessCorrector.hpp"
|
||||||
|
|
||||||
const SDL_Point SCREEN_SIZE{800, 800};
|
const SDL_Point SCREEN_SIZE{800, 800};
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
private:
|
private:
|
||||||
std::vector<const char *> all_words;
|
std::vector<const char *> all_words;
|
||||||
std::vector<std::optional<char>> guessed;
|
GuessCorrector guess_corrector;
|
||||||
const char *word;
|
const char *word;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
44
src/GuessCorrector.cpp
Normal file
44
src/GuessCorrector.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <cstring>
|
||||||
|
#include "GuessCorrector.hpp"
|
||||||
|
|
||||||
|
GuessCorrector::GuessCorrector(const char *word)
|
||||||
|
: m_word(word),
|
||||||
|
m_word_length(strlen(word)),
|
||||||
|
m_parts_guessed(std::make_unique<std::optional<char>[]>(m_word_length)) {
|
||||||
|
for (size_t i = 0; i < m_word_length; i++) {
|
||||||
|
m_parts_guessed[i] = std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GuessCorrector::has_char(const char to_check) const {
|
||||||
|
for (int i = 0; i < m_word_length; ++i) {
|
||||||
|
char c = m_word[i];
|
||||||
|
if (c == to_check)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GuessCorrector::add(const char to_add) {
|
||||||
|
for (int i = 0; i < m_word_length; i++) {
|
||||||
|
char c = m_word[i];
|
||||||
|
if (c == to_add)
|
||||||
|
m_parts_guessed[i] = to_add;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GuessCorrector::is_filled_out() const {
|
||||||
|
for (int i = 0; i < m_word_length; i++)
|
||||||
|
if (m_parts_guessed[i] == std::nullopt)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::weak_ptr<std::optional<char>[]> GuessCorrector::guessed() const {
|
||||||
|
m_parts_guessed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
23
src/GuessCorrector.hpp
Normal file
23
src/GuessCorrector.hpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class GuessCorrector {
|
||||||
|
const char *m_word;
|
||||||
|
size_t m_word_length;
|
||||||
|
std::unique_ptr<std::optional<char>[]> m_parts_guessed;
|
||||||
|
|
||||||
|
public:
|
||||||
|
GuessCorrector(const char *word);
|
||||||
|
|
||||||
|
GuessCorrector() = default;
|
||||||
|
|
||||||
|
[[nodiscard]] bool has_char(char to_check) const;
|
||||||
|
|
||||||
|
void add(char to_add);
|
||||||
|
|
||||||
|
[[nodiscard]] bool is_filled_out() const;
|
||||||
|
|
||||||
|
[[nodiscard]] std::weak_ptr<std::optional<char>[]> guessed() const;
|
||||||
|
};
|
@ -1,6 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "Game.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!" << std::endl;
|
Game::Run();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user