| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2021 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Purchasing module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #ifndef HANGMANGAME_H |
| 52 | #define HANGMANGAME_H |
| 53 | |
| 54 | #include <QObject> |
| 55 | #include <QStringList> |
| 56 | #include <QMutex> |
| 57 | #include <QtPurchasing> |
| 58 | #include <QSettings> |
| 59 | #include <QFuture> |
| 60 | |
| 61 | class HangmanGame : public QObject |
| 62 | { |
| 63 | Q_OBJECT |
| 64 | Q_PROPERTY(QString word READ word NOTIFY wordChanged) |
| 65 | Q_PROPERTY(QString lettersOwned READ lettersOwned NOTIFY lettersOwnedChanged) |
| 66 | Q_PROPERTY(QString vowels READ vowels CONSTANT) |
| 67 | Q_PROPERTY(QString consonants READ consonants CONSTANT) |
| 68 | Q_PROPERTY(int errorCount READ errorCount NOTIFY errorCountChanged) |
| 69 | Q_PROPERTY(bool vowelsUnlocked READ vowelsUnlocked WRITE setVowelsUnlocked NOTIFY vowelsUnlockedChanged) |
| 70 | Q_PROPERTY(int vowelsAvailable READ vowelsAvailable WRITE setVowelsAvailable NOTIFY vowelsAvailableChanged) |
| 71 | Q_PROPERTY(int wordsGiven READ wordsGiven WRITE setWordsGiven NOTIFY wordsGivenChanged) |
| 72 | Q_PROPERTY(int wordsGuessedCorrectly READ wordsGuessedCorrectly WRITE setWordsGuessedCorrectly NOTIFY wordsGuessedCorrectlyChanged) |
| 73 | Q_PROPERTY(int score READ score WRITE setScore NOTIFY scoreChanged) |
| 74 | |
| 75 | public: |
| 76 | explicit HangmanGame(QObject *parent = 0); |
| 77 | ~HangmanGame(); |
| 78 | Q_INVOKABLE void reset(); |
| 79 | Q_INVOKABLE void reveal(); |
| 80 | Q_INVOKABLE void gameOverReveal(); |
| 81 | Q_INVOKABLE void requestLetter(const QString &letterString); |
| 82 | Q_INVOKABLE void guessWord(const QString &word); |
| 83 | Q_INVOKABLE bool isVowel(const QString &letter); |
| 84 | Q_INVOKABLE void setVowelsAvailable(int count); |
| 85 | Q_INVOKABLE void setWordsGiven(int count); |
| 86 | Q_INVOKABLE void setWordsGuessedCorrectly(int count); |
| 87 | Q_INVOKABLE void setScore(int score); |
| 88 | |
| 89 | QString word() const { return m_word; } |
| 90 | QString lettersOwned() const { return m_lettersOwned; } |
| 91 | QString vowels() const; |
| 92 | QString consonants() const; |
| 93 | int errorCount() const; |
| 94 | bool vowelsUnlocked() const; |
| 95 | void setVowelsUnlocked(bool vowelsUnlocked); |
| 96 | int vowelsAvailable() const; |
| 97 | int wordsGiven() const; |
| 98 | int wordsGuessedCorrectly() const; |
| 99 | int score() const; |
| 100 | |
| 101 | signals: |
| 102 | void wordChanged(); |
| 103 | void lettersOwnedChanged(); |
| 104 | void errorCountChanged(); |
| 105 | void vowelBought(const QChar &vowel); |
| 106 | void purchaseWasSuccessful(bool wasSuccessful); |
| 107 | void vowelsUnlockedChanged(bool unlocked); |
| 108 | void vowelsAvailableChanged(int arg); |
| 109 | void wordsGivenChanged(int arg); |
| 110 | void wordsGuessedCorrectlyChanged(int arg); |
| 111 | void scoreChanged(int arg); |
| 112 | |
| 113 | private slots: |
| 114 | void registerLetterBought(const QChar &letter); |
| 115 | |
| 116 | private: |
| 117 | void chooseRandomWord(); |
| 118 | void initWordList(); |
| 119 | int calculateEarnedVowels(); |
| 120 | int calculateEarnedPoints(); |
| 121 | |
| 122 | QFuture<void> m_initWordListfuture; |
| 123 | QString m_word; |
| 124 | QString m_lettersOwned; |
| 125 | QStringList m_wordList; |
| 126 | QRecursiveMutex m_lock; |
| 127 | bool m_vowelsUnlocked; |
| 128 | QSettings m_persistentSettings; |
| 129 | int m_vowelsAvailable; |
| 130 | int m_wordsGiven; |
| 131 | int m_wordsGuessedCorrectly; |
| 132 | int m_score; |
| 133 | }; |
| 134 | |
| 135 | #endif // HANGMANGAME_H |
| 136 | |