moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kmessedwords / kmessedwords / kmwview.cpp
blob7e7132e1e8eb7c81069210cc0f792d8b6d94b720
1 /*
2 * Copyright (C) 2001 by Primoz Anzur
3 (C) 2004 Anne-Marie Mahfouf <annma@kde.org>
4 (C) 2004 Reuben Sutton <reubens@gmail.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of version 2 of the GNU General Public
8 License as published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //project headers
21 #include "answer.h"
22 #include "mainwindow.h"
23 #include "kmwview.h"
24 #include "prefs.h"
26 #include <qlineedit.h>
27 #include <qlabel.h>
29 #include <kapplication.h>
30 #include <kdebug.h>
32 KmwView::KmwView(MainWindow *parent, const char *name)
33 : FrontEnd(parent, name)
35 m_guesses=0;
36 m_tGuesses=0;
37 m_solvedWords=0;
38 m_messups=0;
39 btnMess->setFocus(); //focus on "Messup" button
40 m_levelString.append(i18n("Easy"));
41 m_levelString.append(i18n("Medium"));
42 m_levelString.append(i18n("Hard"));
43 levelBox->insertStringList(m_levelString);
44 levelBox->setCurrentItem(m_levelString.findIndex(Prefs::level()));
45 connect(btnMess, SIGNAL(clicked()), this, SLOT(slot_messup()));
46 connect(btnTry, SIGNAL(clicked()), this, SLOT(slot_try()));
47 connect(guessedText, SIGNAL(returnPressed()), this, SLOT(slot_try()));
48 connect(levelBox, SIGNAL(activated(int)), this, SLOT(slot_changeLevel(int)));
49 //mainScreen->setAutoMask(true);
52 KmwView::~KmwView()
56 void KmwView::slot_try()
59 if(!guessedText->text().isEmpty())
61 m_guesses++;
62 m_tGuesses++;
64 if(QString::compare(guessedText->text().stripWhiteSpace(),m_cleanWord.stripWhiteSpace())==0)
66 kdDebug() << "correct" << endl;
67 if (KMessageBox::questionYesNo(this,i18n("Congratulations! You got the right answer after %1 attempts.\n\nPlay again?").arg(m_guesses), "KMessedWords", i18n("Play Again"), i18n("Restart Your Game"))==4)
69 //scoring final calc thingy goes here :)
70 m_solvedWords++;
71 //TODO add this: write_scores();
72 QString justtext=i18n("Name: %1\n").arg(Prefs::gamerName()) + i18n("Solved: %1\n").arg(m_solvedWords)+ i18n ("Rounds: %1\n").arg(m_messups);
73 KMessageBox::information(this,justtext);
74 resetKMW();
77 else
78 { //here goes on...
79 //scoring calc thingy goes here :)
80 m_solvedWords++;
81 m_guesses=0;
82 guessedText->clear();
83 slot_messup();
86 else
87 { //if you are wrong...
88 kdDebug() << "wrong" << endl;
89 KMessageBox::information(this,i18n("Sorry! You got that wrong! Try again!"), "KMessedWords"); //this should be replaced by a graphical box [?]
90 guessedText->clear();
95 void KmwView::slot_messup()
97 // enable Buttons on first button click
98 if (!guessedText->isEnabled())
100 guessedText->setEnabled(true);
101 txtGuess->setEnabled(true);
102 btnTry->setEnabled(true);
103 btnMess->setDefault(false);
105 randomizer(levelBox->currentItem());
106 messWord();
108 //prevents clean words to be displayed...
109 if (m_cleanWord.stripWhiteSpace()==m_messedWord.stripWhiteSpace())
111 slot_messup();
112 //m_messups ++; //this is for nothing more than monitoring the completenumbers of messed and clean words...
114 else
116 m_messups++;
117 mainScreen->setText(m_messedWord.stripWhiteSpace());
118 guessedText->setFocus();
122 void KmwView::randomizer(int level)
124 reloadStructure();
125 if(level==0)
127 m_lines=configInputData.easyDict->numLines();
128 m_cleanWord=configInputData.easyDict->textLine( m_random.getLong(m_lines) ).stripWhiteSpace();
130 if(level==1)
132 m_lines=configInputData.mediumDict->numLines();
133 m_cleanWord=configInputData.mediumDict->textLine( m_random.getLong(m_lines) ).stripWhiteSpace();
135 if(level==2)
137 m_lines=configInputData.hardDict->numLines();
138 m_cleanWord=configInputData.hardDict->textLine( m_random.getLong(m_lines) ).stripWhiteSpace();
142 void KmwView::messWord()
144 QPtrList <QChar> messList;
145 messList.setAutoDelete(true);
147 // Put all letters of the word in the list
148 for(unsigned int i = 0; i < m_cleanWord.length(); i++)
150 messList.append(new QChar( m_cleanWord[i] ));
153 // Shuffle the list
154 m_random.randomize(&messList);
156 // Give the messed up string the right length.
157 m_messedWord.fill(' ', m_cleanWord.length());
159 // Copy all letters to the messed u-p string.
160 for(unsigned int i = 0; i < m_cleanWord.length(); i++)
162 m_messedWord[i] = *(messList.at(i));
166 void KmwView::reloadStructure(){
167 //this too is a-must-have for the paths and file checkings and stuff...
168 QString setLocalDictionary=locate("data","kmessedwords/"); //<-this NEEDS to be there due to the info, that is in the Locate path [graphics and files]
169 QFile f(setLocalDictionary+"easy.txt");
170 if (! f.exists())
172 setLocalDictionary=locate("data","kmessedwords/");
175 //this is just a patch.. It reloads the files into the dictionary buffer...
176 QFile openEasyFile(setLocalDictionary+"easy.txt");
177 openEasyFile.open(IO_ReadOnly);
178 QTextStream readEasyStr(&openEasyFile);
179 configInputData.easyDict->setText(readEasyStr.read().stripWhiteSpace());
180 openEasyFile.close();
182 QFile openMediumFile(setLocalDictionary+"medium.txt");
183 openMediumFile.open(IO_ReadOnly);
184 QTextStream readMediumStr(&openMediumFile);
185 configInputData.mediumDict->setText(readMediumStr.read().stripWhiteSpace());
186 openMediumFile.close();
188 QFile openHardFile(setLocalDictionary+"hard.txt");
189 openHardFile.open(IO_ReadOnly);
190 QTextStream readHardStr(&openHardFile);
191 configInputData.hardDict->setText(readHardStr.read().stripWhiteSpace());
192 openHardFile.close();
195 void KmwView::slot_changeLevel(int id)
197 resetKMW();
198 btnMess->setDefault(true);
199 Prefs::setLevel(m_levelString[id]);
200 Prefs::writeConfig();
201 emit changeLevel(m_levelString[id]);
204 void KmwView::resetKMW()
206 m_messups=0;
207 m_solvedWords=0;
208 m_guesses=0;
209 m_tGuesses=0;
210 guessedText->clear();
211 mainScreen->setText("KMessedWords");
212 guessedText->setEnabled(false);
213 txtGuess->setEnabled(false);
214 btnTry->setEnabled(false);
217 void KmwView::slotEnd()
219 m_solvedWords++;
220 QString justtext=i18n("Name: %1\n").arg(Prefs::gamerName()) + i18n("Solved: %1\n").arg(m_solvedWords)
221 + i18n ("Rounds: %1\n").arg(m_messups);
222 KMessageBox::information(this, justtext);
223 kapp->quit();
226 void KmwView::slotReply()
228 m_solvedWords++;
229 m_guesses=0;
230 guessedText->setText("");
231 slot_messup();
234 #include "kmwview.moc"