diff --git a/src/Game.cpp b/src/Game.cpp index 829e6c5..08b246e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -8,11 +8,13 @@ #include "words.hpp" #include "utils.hpp" #include "default_font.hpp" +#include "State.hpp" const int CHAR_SIZE = 30; const int STEP_SIZE = CHAR_SIZE + CHAR_SIZE / 2; const int UNDERSCORE_DY = 10; const SDL_Color TEXT_COLOR = {255, 255, 255}; +const int MAX_GUESSES = 8; void Game::Run() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { @@ -67,7 +69,7 @@ void Game::Run() { SDL_Quit(); } -Game::Game() { +Game::Game() : m_wrong_guesses(0), m_game_state(State::PLAY) { const char *defaultFontPath = getDefaultFontPath(); if (defaultFontPath == nullptr) { std::stringstream ss; @@ -101,7 +103,19 @@ Game::Game() { } void Game::handle_key(SDL_Keycode event) { - + if (!isalpha(event)) + return; + bool is_valid = guess_corrector->has_char(event); + if (is_valid) { + guess_corrector->add(event); + if (guess_corrector->is_filled_out()) + m_game_state = State::WIN; + } else { + m_wrong_guesses += 1; + if (m_wrong_guesses >= MAX_GUESSES) { + m_game_state = State::GAME_OVER; + } + } } void Game::draw(SDL_Renderer *renderer) { diff --git a/src/Game.hpp b/src/Game.hpp index 9def6a9..720cd9d 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -7,6 +7,7 @@ #include "SDL_render.h" #include "SDL_ttf.h" #include "GuessCorrector.hpp" +#include "State.hpp" const SDL_Point SCREEN_SIZE{800, 800}; @@ -16,6 +17,8 @@ private: std::unique_ptr guess_corrector; const char *word; _TTF_Font *font; + int m_wrong_guesses; + State m_game_state; public: static void Run(); @@ -28,5 +31,6 @@ public: private: void draw_guesses(SDL_Renderer *renderer); + };