• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kanagram

kanagramgame.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com>              *
00003  *             (C) 2007 by Jeremy Whiting <jeremy@scitools.com>            *
00004  *                                                                         *
00005  *   Portions of this code taken from KMessedWords by Reuben Sutton        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
00021  ***************************************************************************/
00022 
00023 #include "kanagramgame.h"
00024 
00025 #include <QFile>
00026 #include <QFileInfo>
00027 
00028 #include <kurl.h>
00029 #include <kdebug.h>
00030 #include <kmessagebox.h>
00031 #include <kstandarddirs.h>
00032 #include <klocale.h>
00033 
00034 #include <sharedkvtmlfiles.h>
00035 #include <keduvocdocument.h>
00036 #include <keduvocexpression.h>
00037 
00038 #include "kanagramsettings.h"
00039 
00040 
00041 KanagramGame::KanagramGame() : m_index(0), m_doc(NULL)
00042 {
00043     // first get the list of vocabularies
00044     refreshVocabList();
00045 
00046     // then load the default vocab
00047     loadDefaultVocab();
00048 }
00049 
00050 KanagramGame::~KanagramGame()
00051 {
00052     if (m_doc != NULL)
00053     {
00054         delete m_doc;
00055         m_doc = NULL;
00056     }
00057 }
00058 
00059 void KanagramGame::checkFile()
00060 {
00061     if (!QFile::exists(KStandardDirs::locate("data", m_filename)))
00062     {
00063         emit fileError(m_filename);
00064     }
00065 }
00066 
00067 void KanagramGame::loadDefaultVocab()
00068 {
00069     m_filename = KanagramSettings::defaultVocab();
00070     if (m_filename.isEmpty() || !QFileInfo(m_filename).exists())
00071     {
00072         refreshVocabList();
00073         nextVocab();
00074     }
00075 
00076     if ( m_doc != 0 ) 
00077     {
00078         delete m_doc;
00079         m_doc = NULL;
00080     }
00081     m_doc = new KEduVocDocument(this);
00082 
00084     int result = m_doc->open(KUrl(KStandardDirs::locate("data", m_filename)));
00085     if (result != 0) {
00086         KMessageBox::error(0, m_doc->errorDescription(result));
00087     }
00088     nextAnagram();
00089 }
00090 
00091 bool KanagramGame::refreshVocabList()
00092 {
00093     bool retval = false;
00094     QString oldFilename = m_filename;
00095     m_fileList = SharedKvtmlFiles::fileNames(KanagramSettings::dataLanguage());
00096     if ( m_doc ) {
00097         useVocab(m_doc->title());
00098     }
00099     return oldFilename != m_filename;
00100 }
00101 
00103 QStringList KanagramGame::getVocabsList()
00104 {
00105     return SharedKvtmlFiles::titles(KanagramSettings::dataLanguage());
00106 }
00107 
00109 void KanagramGame::useVocab(const QString &vocabname)
00110 {
00111     QStringList titles = getVocabsList();
00112     int vocab = titles.indexOf(vocabname);
00113     if (vocab > 0)
00114     {
00115         m_index = vocab;
00116         m_filename = m_fileList[vocab];
00117     }
00118     else
00119     {
00120         m_index = 0;
00121         m_filename = m_fileList[0];
00122     }
00123 }
00124 
00125 void KanagramGame::updateIndex()
00126 {
00127     m_index = 0;
00128     for (int i = 0; i < m_fileList.size(); i++)
00129     {
00130         if (m_filename == m_fileList[i])
00131         {
00132             m_index = i;
00133         }
00134     }
00135 }
00136 
00137 void KanagramGame::previousVocab()
00138 {
00139     if (--m_index < 0)
00140     {
00141         m_index = m_fileList.size() - 1;
00142     }
00143 
00144     m_filename = m_fileList[m_index];
00145     checkFile();
00146     if ( m_doc != 0 ) {
00147         delete m_doc;
00148     }
00149     m_doc = new KEduVocDocument(this);
00151     m_doc->open(KUrl(KStandardDirs::locate("data", m_filename)));
00152     m_answeredWords.clear();
00153 }
00154 
00155 void KanagramGame::nextVocab()
00156 {
00157     if (++m_index >= m_fileList.size())
00158     {
00159         m_index = 0;
00160     }
00161 
00162     if (!m_fileList.isEmpty())
00163     {
00164         m_filename = m_fileList[m_index];
00165         checkFile();
00166         if ( m_doc != 0 ) {
00167             delete m_doc;
00168         }
00169         m_doc = new KEduVocDocument(this);
00171         m_doc->open(KUrl(KStandardDirs::locate("data", m_filename)));
00172         m_answeredWords.clear();
00173     }
00174 }
00175 
00176 void KanagramGame::nextAnagram()
00177 {
00178     checkFile();
00179 
00180     int totalWords = m_doc->lesson()->entryCount(KEduVocLesson::Recursive);
00181     int wordNumber = m_random.getLong(totalWords);
00182 
00183 
00184     if (totalWords == (int)m_answeredWords.size())
00185     {
00186         m_answeredWords.clear();
00187     }
00188 
00189     if (totalWords > 0)
00190     {
00191         while (m_answeredWords.indexOf(m_doc->lesson()->entries(KEduVocLesson::Recursive).value(wordNumber)->translation(0)->text()) != -1)
00192         {
00193             wordNumber = m_random.getLong(totalWords);
00194         }
00195 
00196         // lowercase the entry text so german words that start capitalized will be lowercased
00197         m_originalWord = m_doc->lesson()->entries(KEduVocLesson::Recursive).value(wordNumber)->translation(0)->text().toLower();
00198         m_answeredWords.append(m_originalWord);
00199         createAnagram();
00200         m_hint = m_doc->lesson()->entries(KEduVocLesson::Recursive).value(wordNumber)->translation(0)->comment();
00201 
00202         if (m_hint.isEmpty())
00203         {
00204             m_hint = i18n("No hint");
00205         }
00206     }
00207     else
00208     {
00209         // this file has no entries
00210         m_originalWord = "";
00211         createAnagram();
00212         m_hint = "";
00213         // TODO: add some error reporting here
00214     }
00215 }
00216 
00217 QString KanagramGame::getFilename()
00218 {
00219     return m_fileList.empty() ? m_filename : m_fileList[m_index];
00220 }
00221 
00222 QString KanagramGame::getAnagram()
00223 {
00224     return m_anagram;
00225 }
00226 
00227 QString KanagramGame::getHint()
00228 {
00229     return m_hint;
00230 }
00231 
00232 QString KanagramGame::getWord()
00233 {
00234     return m_originalWord;
00235 }
00236 
00237 void KanagramGame::restoreWord()
00238 {
00239     m_anagram = m_originalWord;
00240 }
00241 
00242 void KanagramGame::createAnagram()
00243 {
00244     QStringList objData = m_originalWord.split(QString(""));
00245     QString insaneData;
00246     do
00247     {
00248         insaneData = "";
00249         int count;
00250 
00251         for (int i = 0; (count = objData.count()); i++)
00252         {
00253             int objChunk;
00254             if ((i == 0) && (count > 1))
00255             {
00256                 objChunk = 1 + m_random.getLong(count - 1);
00257             }
00258             else
00259             {
00260                 objChunk = m_random.getLong(count);
00261             }
00262 
00263             QString sd = objData.at(objChunk);
00264             objData.removeAt(objChunk);
00265             insaneData += sd;
00266         }
00267         // go again if we got the original word
00268     } while (insaneData == m_originalWord);
00269 
00270     m_anagram = insaneData;
00271 }
00272 
00273 QString KanagramGame::getDocTitle()
00274 {
00275     if (m_doc) 
00276     {
00277         return m_doc->title();
00278     }
00279     return QString();
00280 }
00281 
00282 #include "kanagramgame.moc"

kanagram

Skip menu "kanagram"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal