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#include "hangmangame.h"
52#include <QFile>
53#include <QDebug>
54#include <QBuffer>
55#include <QRandomGenerator>
56#include <QtConcurrent/QtConcurrentRun>
57
58#include <mutex>
59
60HangmanGame::HangmanGame(QObject *parent)
61 : QObject(parent)
62 , m_vowelsUnlocked(false)
63{
64 connect(sender: this, signal: &HangmanGame::vowelBought, receiver: this, slot: &HangmanGame::registerLetterBought);
65
66 m_initWordListfuture = QtConcurrent::run(object: this, fn: &HangmanGame::initWordList);
67
68 m_vowelsUnlocked = m_persistentSettings.value(key: "Hangman/vowelsUnlocked", defaultValue: false).toBool();
69 m_vowelsAvailable = m_persistentSettings.value(key: "Hangman/vowelsAvailable", defaultValue: 0).toInt();
70 m_wordsGiven = m_persistentSettings.value(key: "Hangman/wordsGiven", defaultValue: 0).toInt();
71 m_wordsGuessedCorrectly = m_persistentSettings.value(key: "Hangman/wordsGuessedCorrectly", defaultValue: 0).toInt();
72 m_score = m_persistentSettings.value(key: "Hangman/score", defaultValue: 0).toInt();
73}
74
75HangmanGame::~HangmanGame(){
76 m_initWordListfuture.cancel();
77 m_initWordListfuture.waitForFinished();
78}
79
80void HangmanGame::reset()
81{
82 m_lettersOwned.clear();
83 emit lettersOwnedChanged();
84 emit errorCountChanged();
85 chooseRandomWord();
86}
87
88void HangmanGame::reveal()
89{
90 m_lettersOwned += vowels() + consonants();
91 emit errorCountChanged();
92 emit lettersOwnedChanged();
93}
94
95void HangmanGame::gameOverReveal()
96{
97 m_lettersOwned += vowels() + consonants();
98 emit lettersOwnedChanged();
99}
100
101void HangmanGame::requestLetter(const QString &letterString)
102{
103 Q_ASSERT(letterString.size() == 1);
104 QChar letter = letterString.at(i: 0);
105 registerLetterBought(letter);
106}
107
108void HangmanGame::guessWord(const QString &word)
109{
110 if (word.compare(s: m_word, cs: Qt::CaseInsensitive) == 0) {
111 //Determine how many vowels were earned
112 setVowelsAvailable(m_vowelsAvailable + calculateEarnedVowels());
113 //score is number of remaining consonants + remaining errors
114 setScore(m_score + calculateEarnedPoints());
115 m_lettersOwned += m_word.toUpper();
116 } else {
117 // Small hack to get an additional penalty for guessing wrong
118 static int i=0;
119 Q_ASSERT(i < 10);
120 m_lettersOwned += QString::number(i++);
121 emit errorCountChanged();
122 }
123 emit lettersOwnedChanged();
124}
125
126bool HangmanGame::isVowel(const QString &letter)
127{
128 Q_ASSERT(letter.size() == 1);
129 QChar letterChar = letter.at(i: 0);
130 return vowels().contains(c: letterChar);
131}
132
133QString HangmanGame::vowels() const
134{
135 return QStringLiteral("AEIOU");
136}
137
138QString HangmanGame::consonants() const
139{
140 return QStringLiteral("BCDFGHJKLMNPQRSTVWXYZ");
141}
142
143int HangmanGame::errorCount() const
144{
145 int count = 0;
146 for (QChar c : m_lettersOwned) {
147 if (!m_word.contains(c))
148 ++count;
149 }
150 return count;
151}
152
153bool HangmanGame::vowelsUnlocked() const
154{
155 return m_vowelsUnlocked;
156}
157
158void HangmanGame::setVowelsUnlocked(bool vowelsUnlocked)
159{
160 if (m_vowelsUnlocked != vowelsUnlocked) {
161 m_vowelsUnlocked = vowelsUnlocked;
162 m_persistentSettings.setValue(key: "Hangman/vowelsUnlocked", value: m_vowelsUnlocked);
163 emit vowelsUnlockedChanged(unlocked: m_vowelsUnlocked);
164 }
165}
166
167int HangmanGame::vowelsAvailable() const
168{
169 return m_vowelsAvailable;
170}
171
172int HangmanGame::wordsGiven() const
173{
174 return m_wordsGiven;
175}
176
177int HangmanGame::wordsGuessedCorrectly() const
178{
179 return m_wordsGuessedCorrectly;
180}
181
182int HangmanGame::score() const
183{
184 return m_score;
185}
186
187void HangmanGame::setScore(int score)
188{
189 if (m_score != score) {
190 m_score = score;
191 m_persistentSettings.setValue(key: "Hangman/score", value: m_score);
192 emit scoreChanged(arg: score);
193 }
194}
195
196void HangmanGame::setWordsGiven(int count)
197{
198 if (m_wordsGiven != count) {
199 m_wordsGiven = count;
200 m_persistentSettings.setValue(key: "Hangman/wordsGiven", value: m_wordsGiven);
201 emit wordsGivenChanged(arg: count);
202 }
203}
204
205void HangmanGame::setWordsGuessedCorrectly(int count)
206{
207 if (m_wordsGuessedCorrectly != count) {
208 m_wordsGuessedCorrectly = count;
209 m_persistentSettings.setValue(key: "Hangman/wordsGuessedCorrectly", value: m_wordsGuessedCorrectly);
210 emit wordsGuessedCorrectlyChanged(arg: count);
211 }
212}
213
214void HangmanGame::setVowelsAvailable(int count)
215{
216 if (m_vowelsAvailable != count) {
217 m_vowelsAvailable = count;
218 m_persistentSettings.setValue(key: "Hangman/vowelsAvailable", value: m_vowelsAvailable);
219 emit vowelsAvailableChanged(arg: count);
220 }
221}
222
223void HangmanGame::registerLetterBought(const QChar &letter)
224{
225 if (m_lettersOwned.contains(c: letter))
226 return;
227
228 m_lettersOwned.append(c: letter);
229 emit lettersOwnedChanged();
230
231 if (!m_word.contains(c: letter))
232 emit errorCountChanged();
233}
234
235void HangmanGame::chooseRandomWord()
236{
237 const std::lock_guard<QRecursiveMutex> locker(m_lock);
238 if (m_wordList.isEmpty())
239 return;
240
241 m_word = m_wordList.at(i: QRandomGenerator::global()->bounded(highest: m_wordList.size()));
242 emit wordChanged();
243}
244
245void HangmanGame::initWordList()
246{
247 const std::lock_guard<QRecursiveMutex> locker(m_lock);
248 QFile file(":/enable2.txt");
249 if (file.open(flags: QIODevice::ReadOnly)) {
250 QByteArray allData = file.readAll();
251 QBuffer buffer(&allData);
252 if (!buffer.open(openMode: QIODevice::ReadOnly))
253 qFatal(msg: "Couldn't open buffer for reading!");
254
255 while (!buffer.atEnd()) {
256 QByteArray ba = buffer.readLine().trimmed().toUpper();
257 if (!ba.isEmpty() && ba.length() < 10)
258 m_wordList.append(t: QString::fromLatin1(str: ba));
259 }
260 }
261
262 chooseRandomWord();
263}
264
265int HangmanGame::calculateEarnedVowels()
266{
267 int total = 0;
268 for (int i = 0; i < m_word.length(); ++i) {
269 if (isVowel(letter: QString(m_word[i])) && !m_lettersOwned.contains(s: QString(m_word[i])))
270 total++;
271 }
272 return total;
273}
274
275int HangmanGame::calculateEarnedPoints()
276{
277 int total = 0;
278 for (int i = 0; i < m_word.length(); ++i) {
279 if (consonants().contains(s: QString(m_word[i])) && !m_lettersOwned.contains(s: QString(m_word[i])))
280 total++;
281 }
282 total += 8 - errorCount();
283 return total;
284}
285

source code of qtpurchasing/examples/purchasing/qthangman/hangmangame.cpp