File indexing completed on 2024-04-14 03:40:24

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com>              *
0003  *             (C) 2007-2021 by Jeremy Whiting <jpwhiting@kde.org>         *
0004  *             (C) 2012 by Laszlo Papp <lpapp@kde.org>                     *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, write to the                         *
0018  *   Free Software Foundation, Inc.,                                       *
0019  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0020  ***************************************************************************/
0021 
0022 #ifndef KANAGRAMGAME_H
0023 #define KANAGRAMGAME_H
0024 
0025 #include <QObject>
0026 #include <QHash>
0027 #include <QLoggingCategory>
0028 #include <QStringList>
0029 #include <QUrl>
0030 
0031 #ifdef HAVE_SPEECH
0032 class QTextToSpeech;
0033 #endif
0034 
0035 Q_DECLARE_LOGGING_CATEGORY(KANAGRAM)
0036 
0037 class KEduVocDocument;
0038 
0039 namespace Sonnet {
0040     class Speller;
0041 }
0042 /** @brief game api
0043  * @author Joshua Keel <joshuakeel@gmail.com>
0044  * @author Jeremy Whiting <jpwhiting@kde.org>
0045  */
0046 class KanagramGame : public QObject
0047 {
0048     Q_OBJECT
0049     // Get the current anagram, word, hint, picture, and audio
0050     Q_PROPERTY(QStringList anagram READ anagram NOTIFY wordChanged)
0051     Q_PROPERTY(QString word READ word NOTIFY wordChanged)
0052     Q_PROPERTY(QString hint READ hint NOTIFY wordChanged)
0053     Q_PROPERTY(QUrl picHint READ picHint NOTIFY wordChanged)
0054     Q_PROPERTY(QUrl audio READ audioFile NOTIFY wordChanged)
0055     // Get the individual word list
0056     Q_PROPERTY(QStringList userAnswer READ userAnswer NOTIFY userAnswerChanged)
0057     // Get the current title
0058     Q_PROPERTY(QString title READ documentTitle NOTIFY titleChanged)
0059 
0060     // Get list of vocabularies (document titles), language names, and current language)
0061     Q_PROPERTY(QStringList vocabularies READ vocabularyList)
0062     Q_PROPERTY(QStringList languages READ languageNames)
0063     Q_PROPERTY(QString dataLanguage READ dataLanguage WRITE setDataLanguage NOTIFY dataLanguageChanged)
0064 
0065     Q_PROPERTY(int currentPlayer READ getPlayerNumber WRITE setPlayerNumber NOTIFY currentPlayerChanged)
0066     Q_PROPERTY(bool singlePlayer READ singlePlayerMode WRITE setSinglePlayerMode NOTIFY singlePlayerChanged)
0067 
0068     Q_PROPERTY(int score READ totalScore NOTIFY scoreChanged)
0069     Q_PROPERTY(int score2 READ totalScore2 NOTIFY scoreChanged)
0070 
0071     Q_PROPERTY(bool useSounds READ useSounds NOTIFY useSoundsChanged)
0072 
0073     public:
0074         /** Default constructor */
0075         KanagramGame();
0076 
0077         /** Default destructor */
0078         ~KanagramGame();
0079 
0080         /** Get the anagram to show */
0081         QStringList anagram() const;
0082 
0083         /** Get this anagram's hint */
0084         QString hint() const;
0085 
0086         /** Get this anagram's answer */
0087         QString word() const;
0088 
0089         /** Get this anagram's picture hint URL */
0090         QUrl picHint();
0091 
0092         /** Get this anagram's audio URL */
0093         QUrl audioFile();
0094 
0095         /** Get the current vocabulary file's title */
0096         QString documentTitle() const;
0097 
0098         /** Get the current vocabulary file's filename */
0099         QString filename() const;
0100 
0101         /** Get the user's current guess */
0102         QStringList userAnswer() const;
0103 
0104         /** Get the list of vocabularies */
0105         QStringList vocabularyList() const;
0106 
0107         /** Return the language names found available in the system */
0108         QStringList languageNames();
0109 
0110         /** Get the current data language */
0111         QString dataLanguage() const;
0112 
0113         /** Get the sanitized data language used */
0114         QString sanitizedDataLanguage() const;
0115 
0116         // These accessor and mutator methods are not needed once the
0117         // kconfig_compiler can generate Q_INVOKABLE methods, slots or/and
0118         // properties
0119 
0120         Q_INVOKABLE int hintHideTime();
0121 
0122         Q_INVOKABLE int resolveTime();
0123 
0124         Q_INVOKABLE int scoreTime();
0125 
0126         Q_INVOKABLE void moveLetterToUserAnswer(int position);
0127 
0128         Q_INVOKABLE void moveLetterToAnagram(int position);
0129 
0130         /** Reset the anagram, move all userAnswer letters back to the anagram */
0131         Q_INVOKABLE void resetAnagram();
0132 
0133         /** Move the given letter from anagram to answer or back if there's no
0134          * instances of the given letter in the anagram currently */
0135         Q_INVOKABLE void moveLetter(const QString &letter);
0136 
0137         Q_INVOKABLE bool singlePlayerMode();
0138 
0139         /** Get the current score */
0140         int totalScore();
0141 
0142         /** Get the current score of player 2*/
0143         int totalScore2();
0144 
0145         /** Check word answer against the current word */
0146         Q_INVOKABLE bool checkWord();
0147 
0148         bool useSounds();
0149 
0150     public Q_SLOTS:
0151 
0152         /** Checks if in single-player mode*/
0153         void setSinglePlayerMode(bool);
0154 
0155         /** Get */
0156         int getPlayerNumber();
0157 
0158         /** Set */
0159         void setPlayerNumber(int);
0160 
0161         /** Set the vocabulary to use according to the vocabulary name */
0162         void useVocabulary(const QString &vocabularyname);
0163 
0164         /** Set the vocabulary to use according to the desired index value*/
0165         void useVocabulary(int index);
0166 
0167         /** Set the data language */
0168         void setDataLanguage(const QString& dataLanguage);
0169 
0170         /** Refresh the list of vocabulary files available
0171          * from what we find on disk
0172          *@returns true if the current vocabulary has changed so the ui can refresh
0173          */
0174         bool refreshVocabularyList();
0175 
0176         /** Load the default vocabulary file */
0177         void loadDefaultVocabulary();
0178 
0179         /** Set the index to the next word */
0180         void nextAnagram();
0181 
0182         /** Use the next vocabulary file in the list */
0183         void nextVocabulary();
0184 
0185         /** Use the previous vocabulary file in the list */
0186         void previousVocabulary();
0187 
0188 #ifdef HAVE_SPEECH
0189         /** The word was revealed (or correctly entered), so play the audio, say it, or play right.ogg */
0190         void wordRevealed();
0191 #endif
0192 
0193         /** Reset the current score */
0194         void resetTotalScore();
0195 
0196         /** Adjust the current score by points */
0197         void adjustScore(int points);
0198 
0199         /** Set the anagram to the original word for a time */
0200         void revealWord();
0201 
0202         void reloadSettings();
0203 
0204         /** Slots to adjust score accordingly */
0205         void answerCorrect();
0206         void answerIncorrect();
0207         void answerSkipped();
0208         void answerRevealed();
0209 
0210     Q_SIGNALS:
0211 
0212         /** Signal the ui that a there's a file error of some kind */
0213         void fileError(const QString &filename);
0214 
0215         /** Signal the ui that the data language has changed */
0216         void dataLanguageChanged();
0217 
0218         /** Signal the ui that the player has changed when in 2-player Mode*/
0219         void currentPlayerChanged();
0220 
0221         /** Signal the ui that the mode has changed*/
0222         void singlePlayerChanged();
0223 
0224         /** Signal the ui that the current document title has changed */
0225         void titleChanged();
0226 
0227         /** Signal the ui that the anagram, word, hint, picHint, and audioUrl changed */
0228         void wordChanged();
0229 
0230         /** Signal the ui that the score has changed */
0231         void scoreChanged();
0232 
0233 
0234         //Signal the UI that the word is broken into alphabets
0235         void userAnswerChanged();
0236 
0237         /** Signal the ui that sound enabled has changed */
0238         void useSoundsChanged();
0239     private:
0240 
0241         /** Make the word into an anagram */
0242         void createAnagram();
0243 
0244         /** Check the current file */
0245         bool checkFile();
0246 
0247 #ifdef HAVE_SPEECH
0248         /** speak the word
0249          *@param text the word that is to be converted from text to speech
0250          */
0251         void say(const QString &text);
0252 #endif
0253 
0254         /** Get the value of a numeric setting from it's string */
0255         int getNumericSetting(const QString &settingString);
0256 
0257         /** Check if enteredword is an anagram of word */
0258         bool isAnagram(const QString &enteredword, const QString &word);
0259 
0260         /** Remove accent characters from a word */
0261         QString stripAccents(const QString &original);
0262 
0263         /** Load score settings into local variables */
0264         void loadSettings();
0265 
0266         /** The current scrambled word */
0267         QString m_anagram;
0268 
0269         //The current word scambled word list
0270         QString m_userAnswer;
0271 
0272         /** The current anagram's hint */
0273         QString m_hint;
0274 
0275         /** The current anagram's picture if any */
0276         QUrl m_picHintUrl;
0277 
0278         /** The current anagram's audio if any */
0279         QUrl m_audioUrl;
0280 
0281         /** The current anagram's answer */
0282         QString m_originalWord;
0283 
0284         /** The list of vocabulary files */
0285         QStringList m_fileList;
0286 
0287         /** Which index the current filename is in m_fileList */
0288         int m_fileIndex;
0289 
0290         /** The list of words that have been answered */
0291         QStringList m_answeredWords;
0292 
0293         /** The current vocabulary's filename */
0294         QString m_filename;
0295 
0296         /** The current document */
0297         KEduVocDocument* m_document;
0298 
0299         /** The hash of the language code and name */
0300         QHash<QString, QString> m_languageCodeNameHash;
0301 
0302 #ifdef HAVE_SPEECH
0303         /** Text to Speech API */
0304         QTextToSpeech *m_speech;
0305 #endif
0306 
0307         /** current total score */
0308         int m_totalScore;
0309 
0310         /** current total score of player 2*/
0311         int m_totalScore2;
0312 
0313         /** current player number in 2-player mode*/
0314         int m_currentPlayerNumber;
0315 
0316         /** Speller object to check correct spelling */
0317         Sonnet::Speller *m_speller;
0318 
0319         /** Values for settings */
0320         int m_correctAnswerScore;
0321         int m_incorrectAnswerScore;
0322         int m_revealAnswerScore;
0323         int m_skippedWordScore;
0324 };
0325 
0326 #endif
0327