00001
00002
00003
00004
00005
00006
00007 #include "HangmanWidget.h"
00008
00009 #include <Wt/WBreak>
00010 #include <Wt/WCssDecorationStyle>
00011 #include <Wt/WTable>
00012 #include <Wt/WText>
00013 #include <Wt/WTableCell>
00014 #include <Wt/WLineEdit>
00015 #include <Wt/WPushButton>
00016 #include <Wt/WImage>
00017 #include <Wt/WSignalMapper>
00018 #include <boost/lexical_cast.hpp>
00019
00020 #include "Dictionary.h"
00021 #include "HangmanDb.h"
00022
00023 HangmanWidget::HangmanWidget(std::wstring user, Dictionary dict,
00024 WContainerWidget *parent):
00025 WContainerWidget(parent),
00026 MaxGuesses(9),
00027 User(user),
00028 Dict(dict)
00029 {
00030 setContentAlignment(AlignCenter);
00031
00032 Title = new WText(L"Guess the word!", this);
00033 Title->decorationStyle().font().setSize(WFont::XLarge);
00034
00035 WordContainer = new WContainerWidget(this);
00036 WordContainer->setMargin(20, Top | Bottom);
00037 WordContainer->setContentAlignment(AlignCenter);
00038 WCssDecorationStyle& style = WordContainer->decorationStyle();
00039 style.setBorder(WBorder(WBorder::Solid));
00040 style.font().setFamily(WFont::Monospace, L"courier");
00041 style.font().setSize(WFont::XXLarge);
00042
00043 StatusText = new WText(this);
00044 new WBreak(this);
00045 createHangmanImages(this);
00046 createAlphabet(this);
00047 new WBreak(this);
00048 NewGameButton = new WPushButton(L"New Game", this);
00049 NewGameButton->clicked.connect(SLOT(this, HangmanWidget::newGame));
00050
00051
00052 newGame();
00053 }
00054
00055 void HangmanWidget::createHangmanImages(WContainerWidget *parent)
00056 {
00057 for(unsigned int i = 0; i <= MaxGuesses; ++i) {
00058 std::string fname = "icons/hangman";
00059 fname += boost::lexical_cast<std::string>(i) + ".png";
00060 WImage *theImage = new WImage(fname, parent);
00061 HangmanImages.push_back(theImage);
00062
00063
00064
00065 theImage->resize(256, 256);
00066 }
00067
00068 HurrayImage = new WImage("icons/hangmanhurray.png", parent);
00069 resetImages();
00070 }
00071
00072 void HangmanWidget::createAlphabet(WContainerWidget *parent)
00073 {
00074 LetterButtonLayout = new WTable(parent);
00075
00076
00077 LetterButtonLayout->resize(13*30, WLength());
00078
00079 WSignalMapper<WPushButton *> *mapper
00080 = new WSignalMapper<WPushButton *>(this);
00081
00082 for(unsigned int i = 0; i < 26; ++i) {
00083 std::wstring c(1, 'A' + i);
00084 WPushButton *character =
00085 new WPushButton(c, LetterButtonLayout->elementAt(i / 13, i % 13));
00086 LetterButtons.push_back(character);
00087 character->resize(30, WLength());
00088 mapper->mapConnect(character->clicked, character);
00089 }
00090
00091 mapper->mapped.connect(SLOT(this, HangmanWidget::processButton));
00092 }
00093
00094 void HangmanWidget::newGame()
00095 {
00096 Word = RandomWord(Dict);
00097 Title->setText(L"Guess the word, " + User + L"!");
00098 NewGameButton->hide();
00099
00100
00101 resetImages();
00102 resetButtons();
00103 BadGuesses = DisplayedLetters = 0;
00104 HangmanImages[0]->show();
00105
00106
00107 WordContainer->clear();
00108 WordLetters.clear();
00109 for(unsigned int i = 0; i < Word.size(); ++i) {
00110 WText *c = new WText(L"-", WordContainer);
00111 WordLetters.push_back(c);
00112 }
00113
00114
00115 WordContainer->resize(WLength(Word.size() * 1.5, WLength::FontEx),
00116 WLength());
00117
00118 StatusText->setText(L"");
00119 }
00120
00121 void HangmanWidget::processButton(WPushButton *button)
00122 {
00123 if (!button->isEnabled())
00124 return;
00125
00126 const wchar_t *txt = button->text().value().c_str();
00127 wchar_t c = txt[0];
00128 if(std::find(Word.begin(), Word.end(), c) != Word.end())
00129 registerCorrectGuess(c);
00130 else
00131 registerBadGuess();
00132 button->disable();
00133 }
00134
00135 void HangmanWidget::registerBadGuess()
00136 {
00137 if(BadGuesses < MaxGuesses) {
00138 HangmanImages[BadGuesses]->hide();
00139 BadGuesses++;
00140 HangmanImages[BadGuesses]->show();
00141 if(BadGuesses == MaxGuesses) {
00142 StatusText->setText(L"You hang... <br />"
00143 L"The correct answer was: " + Word);
00144 LetterButtonLayout->hide();
00145 NewGameButton->show();
00146 HangmanDb::addToScore(User, -10);
00147 }
00148 }
00149 }
00150
00151 void HangmanWidget::registerCorrectGuess(wchar_t c)
00152 {
00153 for(unsigned int i = 0; i < Word.size(); ++i) {
00154 if(Word[i] == c) {
00155 DisplayedLetters++;
00156 WordLetters[i]->setText(std::wstring(1, c));
00157 }
00158 }
00159 if(DisplayedLetters == Word.size()) {
00160 StatusText->setText(L"You win!");
00161 HangmanImages[BadGuesses]->hide();
00162 HurrayImage->show();
00163 LetterButtonLayout->hide();
00164 NewGameButton->show();
00165 HangmanDb::addToScore(User, 20 - BadGuesses);
00166 }
00167 }
00168
00169 void HangmanWidget::resetImages()
00170 {
00171 HurrayImage->hide();
00172 for(unsigned int i = 0; i < HangmanImages.size(); ++i)
00173 HangmanImages[i]->hide();
00174 }
00175
00176 void HangmanWidget::resetButtons()
00177 {
00178 for(unsigned int i = 0; i < LetterButtons.size(); ++i) {
00179 LetterButtons[i]->enable();
00180 }
00181 LetterButtonLayout->show();
00182 }