guess corrector
This commit is contained in:
		@@ -59,7 +59,7 @@ void Game::Run() {
 | 
			
		||||
    SDL_Quit();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Game::Game() : guessed() {
 | 
			
		||||
Game::Game() {
 | 
			
		||||
    std::random_device random_device{};
 | 
			
		||||
    std::mt19937 rng(random_device());
 | 
			
		||||
 | 
			
		||||
@@ -71,6 +71,7 @@ Game::Game() : guessed() {
 | 
			
		||||
    std::shuffle(all_words.begin(), all_words.end(), rng);
 | 
			
		||||
    word = all_words.back();
 | 
			
		||||
    all_words.pop_back();
 | 
			
		||||
    guess_corrector = GuessCorrector(word);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Game::handle_key(SDL_Keycode event) {
 | 
			
		||||
 
 | 
			
		||||
@@ -4,13 +4,14 @@
 | 
			
		||||
#include <SDL_events.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include "GuessCorrector.hpp"
 | 
			
		||||
 | 
			
		||||
const SDL_Point SCREEN_SIZE{800, 800};
 | 
			
		||||
 | 
			
		||||
class Game {
 | 
			
		||||
private:
 | 
			
		||||
    std::vector<const char *> all_words;
 | 
			
		||||
    std::vector<std::optional<char>> guessed;
 | 
			
		||||
    GuessCorrector guess_corrector;
 | 
			
		||||
    const char *word;
 | 
			
		||||
 | 
			
		||||
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 "Game.hpp"
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    std::cout << "Hello, World!" << std::endl;
 | 
			
		||||
    return 0;
 | 
			
		||||
    Game::Run();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user