#include <HangmanWidget.h>

Public Member Functions | |
| HangmanWidget (std::wstring user, Dictionary dict, WContainerWidget *parent=0) | |
Private Member Functions | |
| void | createAlphabet (WContainerWidget *parent) |
| void | createHangmanImages (WContainerWidget *parent) |
| void | resetImages () |
| void | resetButtons () |
| void | registerBadGuess () |
| void | registerCorrectGuess (wchar_t c) |
| void | processButton (WPushButton *button) |
| void | newGame () |
Private Attributes | |
| WText * | Title |
| WTable * | LetterButtonLayout |
| std::vector< WPushButton * > | LetterButtons |
| std::vector< WImage * > | HangmanImages |
| WImage * | HurrayImage |
| WContainerWidget * | WordContainer |
| WText * | StatusText |
| std::vector< WText * > | WordLetters |
| WPushButton * | NewGameButton |
| const unsigned int | MaxGuesses |
| unsigned int | BadGuesses |
| unsigned int | DisplayedLetters |
| std::wstring | Word |
| std::wstring | User |
| Dictionary | Dict |
Definition at line 28 of file HangmanWidget.h.
| HangmanWidget::HangmanWidget | ( | std::wstring | user, |
| Dictionary | dict, | ||
| WContainerWidget * | parent = 0 |
||
| ) |
Definition at line 23 of file HangmanWidget.C.
: WContainerWidget(parent), MaxGuesses(9), User(user), Dict(dict) { setContentAlignment(AlignCenter); Title = new WText("Guess the word!", this); Title->decorationStyle().font().setSize(WFont::XLarge); WordContainer = new WContainerWidget(this); WordContainer->setMargin(20, Top | Bottom); WordContainer->setContentAlignment(AlignCenter); WCssDecorationStyle& style = WordContainer->decorationStyle(); style.setBorder(WBorder(WBorder::Solid)); style.font().setFamily(WFont::Monospace, "courier"); style.font().setSize(WFont::XXLarge); StatusText = new WText(this); new WBreak(this); createHangmanImages(this); createAlphabet(this); new WBreak(this); NewGameButton = new WPushButton("New Game", this); NewGameButton->clicked().connect(this, &HangmanWidget::newGame); // prepare for first game newGame(); }
| void HangmanWidget::createAlphabet | ( | WContainerWidget * | parent ) | [private] |
Definition at line 72 of file HangmanWidget.C.
{
LetterButtonLayout = new WTable(parent);
// The default width of a table is 100%...
LetterButtonLayout->resize(13*30, WLength::Auto);
WSignalMapper<WPushButton *> *mapper
= new WSignalMapper<WPushButton *>(this);
for(unsigned int i = 0; i < 26; ++i) {
std::wstring c(1, 'A' + i);
WPushButton *character =
new WPushButton(c, LetterButtonLayout->elementAt(i / 13, i % 13));
LetterButtons.push_back(character);
character->resize(30, WLength::Auto);
mapper->mapConnect(character->clicked(), character);
}
mapper->mapped().connect(this, &HangmanWidget::processButton);
}
| void HangmanWidget::createHangmanImages | ( | WContainerWidget * | parent ) | [private] |
Definition at line 55 of file HangmanWidget.C.
{
for(unsigned int i = 0; i <= MaxGuesses; ++i) {
std::string fname = "icons/hangman";
fname += boost::lexical_cast<std::string>(i) + ".png";
WImage *theImage = new WImage(fname, parent);
HangmanImages.push_back(theImage);
// Although not necessary, we can avoid flicker (on konqueror)
// by presetting the image size.
theImage->resize(256, 256);
}
HurrayImage = new WImage("icons/hangmanhurray.png", parent);
resetImages(); // Hide all images
}
| void HangmanWidget::newGame | ( | ) | [private] |
Definition at line 94 of file HangmanWidget.C.
{
Word = RandomWord(Dict);
Title->setText(L"Guess the word, " + User + L"!");
NewGameButton->hide(); // don't let the player chicken out
// Bring widget to initial state
resetImages();
resetButtons();
BadGuesses = DisplayedLetters = 0;
HangmanImages[0]->show();
// Prepare the widgets for the new word
WordContainer->clear();
WordLetters.clear();
for(unsigned int i = 0; i < Word.size(); ++i) {
WText *c = new WText("-", WordContainer);
WordLetters.push_back(c);
}
// resize appropriately so that the border nooks nice.
WordContainer->resize(WLength(Word.size() * 1.5, WLength::FontEx),
WLength::Auto);
StatusText->setText("");
}
| void HangmanWidget::processButton | ( | WPushButton * | button ) | [private] |
Definition at line 121 of file HangmanWidget.C.
{
if (!button->isEnabled())
return;
wchar_t c = button->text().value().c_str()[0];
if(std::find(Word.begin(), Word.end(), c) != Word.end())
registerCorrectGuess(c);
else
registerBadGuess();
button->disable();
}
| void HangmanWidget::registerBadGuess | ( | ) | [private] |
Definition at line 134 of file HangmanWidget.C.
{
if(BadGuesses < MaxGuesses) {
HangmanImages[BadGuesses]->hide();
BadGuesses++;
HangmanImages[BadGuesses]->show();
if(BadGuesses == MaxGuesses) {
StatusText->setText(L"You hang... <br />"
L"The correct answer was: " + Word);
LetterButtonLayout->hide();
NewGameButton->show();
HangmanDb::addToScore(User, -10);
}
}
}
| void HangmanWidget::registerCorrectGuess | ( | wchar_t | c ) | [private] |
Definition at line 150 of file HangmanWidget.C.
{
for(unsigned int i = 0; i < Word.size(); ++i) {
if(Word[i] == c) {
DisplayedLetters++;
WordLetters[i]->setText(std::wstring(1, c));
}
}
if(DisplayedLetters == Word.size()) {
StatusText->setText("You win!");
HangmanImages[BadGuesses]->hide();
HurrayImage->show();
LetterButtonLayout->hide();
NewGameButton->show();
HangmanDb::addToScore(User, 20 - BadGuesses);
}
}
| void HangmanWidget::resetButtons | ( | ) | [private] |
Definition at line 175 of file HangmanWidget.C.
{
for(unsigned int i = 0; i < LetterButtons.size(); ++i) {
LetterButtons[i]->enable();
}
LetterButtonLayout->show();
}
| void HangmanWidget::resetImages | ( | ) | [private] |
Definition at line 168 of file HangmanWidget.C.
{
HurrayImage->hide();
for(unsigned int i = 0; i < HangmanImages.size(); ++i)
HangmanImages[i]->hide();
}
unsigned int HangmanWidget::BadGuesses [private] |
Definition at line 46 of file HangmanWidget.h.
Dictionary HangmanWidget::Dict [private] |
Definition at line 50 of file HangmanWidget.h.
unsigned int HangmanWidget::DisplayedLetters [private] |
Definition at line 47 of file HangmanWidget.h.
std::vector<WImage *> HangmanWidget::HangmanImages [private] |
Definition at line 38 of file HangmanWidget.h.
WImage* HangmanWidget::HurrayImage [private] |
Definition at line 39 of file HangmanWidget.h.
WTable* HangmanWidget::LetterButtonLayout [private] |
Definition at line 36 of file HangmanWidget.h.
std::vector<WPushButton *> HangmanWidget::LetterButtons [private] |
Definition at line 37 of file HangmanWidget.h.
const unsigned int HangmanWidget::MaxGuesses [private] |
Definition at line 45 of file HangmanWidget.h.
WPushButton* HangmanWidget::NewGameButton [private] |
Definition at line 43 of file HangmanWidget.h.
WText* HangmanWidget::StatusText [private] |
Definition at line 41 of file HangmanWidget.h.
WText* HangmanWidget::Title [private] |
Definition at line 35 of file HangmanWidget.h.
std::wstring HangmanWidget::User [private] |
Definition at line 49 of file HangmanWidget.h.
std::wstring HangmanWidget::Word [private] |
Definition at line 48 of file HangmanWidget.h.
WContainerWidget* HangmanWidget::WordContainer [private] |
Definition at line 40 of file HangmanWidget.h.
std::vector<WText *> HangmanWidget::WordLetters [private] |
Definition at line 42 of file HangmanWidget.h.
1.7.2