summaryrefslogtreecommitdiff
path: root/Userland/Games/MasterWord/WordGame.h
blob: 4b7fa90eb8ae01c7c803834578ddae0691ce9fb2 (plain)
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
/*
 * Copyright (c) 2022, Joe Petrus <joe@petrus.io>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/DeprecatedString.h>
#include <AK/Forward.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibCore/Timer.h>
#include <LibGUI/Frame.h>
#include <LibGfx/Rect.h>

namespace MasterWord {

class WordGame : public GUI::Frame {
    C_OBJECT(WordGame);

public:
    virtual ~WordGame() override = default;

    void reset();
    void set_use_system_theme(bool b);
    void set_check_guesses_in_dictionary(bool b);
    void set_word_length(size_t length);
    void set_max_guesses(size_t max_guesses);
    Gfx::IntSize game_size() const;

    Optional<DeprecatedString> random_word(size_t length);
    size_t shortest_word();
    size_t longest_word();
    bool is_checking_guesses() const;

    void add_guess(AK::StringView guess);
    bool is_in_dictionary(AK::StringView guess);

    Function<void(Optional<StringView>)> on_message;

private:
    WordGame();
    void read_words();
    void show_message(StringView message) const;
    void clear_message() const;

    virtual void paint_event(GUI::PaintEvent&) override;
    virtual void keydown_event(GUI::KeyEvent&) override;
    virtual void resize_event(GUI::ResizeEvent&) override;
    void pick_font();

    Gfx::IntRect letter_rect(size_t guess_number, size_t letter_number) const;

    size_t m_max_guesses { 6 };
    size_t m_num_letters { 5 };
    bool m_check_guesses { false };
    bool m_last_word_invalid { false };
    static constexpr int m_letter_width { 40 };
    static constexpr int m_letter_spacing { 5 };
    static constexpr int m_outer_margin { 20 };
    static constexpr int m_letter_height { 60 };

    Color m_right_letter_wrong_spot_color { Color::from_rgb(0xb59f3b) };
    Color m_right_letter_right_spot_color { Color::from_rgb(0x538d4e) };
    Color m_border_color { Color::from_rgb(0x3a3a3c) };
    Color m_wrong_letter_color { m_border_color };
    Color m_background_color { Color::from_rgb(0x121213) };
    Color m_text_color { Color::White };
    Color m_word_not_in_dict_color { Color::from_argb(0x40aa0000) };

    enum LetterState {
        Correct,
        WrongSpot,
        Incorrect
    };

    struct Guess {
        AK::DeprecatedString text;
        AK::Vector<LetterState> letter_states;
    };

    AK::Vector<Guess> m_guesses;
    AK::DeprecatedString m_current_guess;
    AK::DeprecatedString m_current_word;

    HashMap<size_t, AK::Vector<DeprecatedString>> m_words;

    NonnullRefPtr<Core::Timer> m_clear_message_timer;
};

}