HangmanWidget Class Reference

#include <HangmanWidget.h>

Inheritance diagram for HangmanWidget:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 HangmanWidget (std::wstring user, Dictionary dict, WContainerWidget *parent=0)

Private Slots

void processButton (WPushButton *button)
void newGame ()

Private Member Functions

void createAlphabet (WContainerWidget *parent)
void createHangmanImages (WContainerWidget *parent)
void resetImages ()
void resetButtons ()
void registerBadGuess ()
void registerCorrectGuess (wchar_t c)

Private Attributes

WTextTitle
WTableLetterButtonLayout
std::vector< WPushButton * > LetterButtons
std::vector< WImage * > HangmanImages
WImageHurrayImage
WContainerWidgetWordContainer
WTextStatusText
std::vector< WText * > WordLetters
WPushButtonNewGameButton
const unsigned int MaxGuesses
unsigned int BadGuesses
unsigned int DisplayedLetters
std::wstring Word
std::wstring User
Dictionary Dict


Detailed Description

Definition at line 28 of file HangmanWidget.h.


Constructor & Destructor Documentation

HangmanWidget::HangmanWidget ( std::wstring  user,
Dictionary  dict,
WContainerWidget parent = 0 
)

Definition at line 23 of file HangmanWidget.C.

00024                                                       :
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    // prepare for first game
00052    newGame();
00053 }


Member Function Documentation

void HangmanWidget::createAlphabet ( WContainerWidget parent  )  [private]

Definition at line 72 of file HangmanWidget.C.

00073 {
00074    LetterButtonLayout = new WTable(parent);
00075 
00076    // The default width of a table is 100%...
00077    LetterButtonLayout->resize(13*30, WLength::Auto);
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::Auto);
00088       mapper->mapConnect(character->clicked(), character);
00089    }
00090 
00091    mapper->mapped().connect(SLOT(this, HangmanWidget::processButton));
00092 }

void HangmanWidget::createHangmanImages ( WContainerWidget parent  )  [private]

Definition at line 55 of file HangmanWidget.C.

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       // Although not necessary, we can avoid flicker (on konqueror)
00064       // by presetting the image size.
00065       theImage->resize(256, 256);
00066    }
00067 
00068    HurrayImage = new WImage("icons/hangmanhurray.png", parent);
00069    resetImages(); // Hide all images
00070 }

void HangmanWidget::resetImages (  )  [private]

Definition at line 168 of file HangmanWidget.C.

00169 {
00170     HurrayImage->hide();
00171     for(unsigned int i = 0; i < HangmanImages.size(); ++i)
00172        HangmanImages[i]->hide();
00173 }

void HangmanWidget::resetButtons (  )  [private]

Definition at line 175 of file HangmanWidget.C.

00176 {
00177    for(unsigned int i = 0; i < LetterButtons.size(); ++i) {
00178       LetterButtons[i]->enable();
00179    }
00180    LetterButtonLayout->show();
00181 }

void HangmanWidget::registerBadGuess (  )  [private]

Definition at line 134 of file HangmanWidget.C.

00135 {
00136    if(BadGuesses < MaxGuesses) {
00137       HangmanImages[BadGuesses]->hide();
00138       BadGuesses++;
00139       HangmanImages[BadGuesses]->show();
00140       if(BadGuesses == MaxGuesses) {
00141          StatusText->setText(L"You hang... <br />"
00142                              L"The correct answer was: " + Word);
00143          LetterButtonLayout->hide();
00144          NewGameButton->show();
00145          HangmanDb::addToScore(User, -10);
00146       }
00147    }
00148 }

void HangmanWidget::registerCorrectGuess ( wchar_t  c  )  [private]

Definition at line 150 of file HangmanWidget.C.

00151 {
00152    for(unsigned int i = 0; i < Word.size(); ++i) {
00153       if(Word[i] == c) {
00154          DisplayedLetters++;
00155          WordLetters[i]->setText(std::wstring(1, c));
00156       }
00157    }
00158    if(DisplayedLetters == Word.size()) {
00159       StatusText->setText(L"You win!");
00160       HangmanImages[BadGuesses]->hide();
00161       HurrayImage->show();
00162       LetterButtonLayout->hide();
00163       NewGameButton->show();
00164       HangmanDb::addToScore(User, 20 - BadGuesses);
00165    }
00166 }

void HangmanWidget::processButton ( WPushButton button  )  [private, slot]

Definition at line 121 of file HangmanWidget.C.

00122 {
00123    if (!button->isEnabled())
00124      return;
00125 
00126    wchar_t c = button->text().value().c_str()[0];
00127    if(std::find(Word.begin(), Word.end(), c) != Word.end())
00128       registerCorrectGuess(c);
00129    else
00130       registerBadGuess();
00131    button->disable();
00132 }

void HangmanWidget::newGame (  )  [private, slot]

Definition at line 94 of file HangmanWidget.C.

00095 {
00096    Word = RandomWord(Dict);
00097    Title->setText(L"Guess the word, " + User + L"!");
00098    NewGameButton->hide(); // don't let the player chicken out
00099 
00100    // Bring widget to initial state
00101    resetImages();
00102    resetButtons();
00103    BadGuesses = DisplayedLetters = 0;
00104    HangmanImages[0]->show();
00105 
00106    // Prepare the widgets for the new word
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    // resize appropriately so that the border nooks nice.
00115    WordContainer->resize(WLength(Word.size() * 1.5, WLength::FontEx),
00116                          WLength::Auto);
00117 
00118    StatusText->setText(L"");
00119 }


Member Data Documentation

Definition at line 35 of file HangmanWidget.h.

Definition at line 36 of file HangmanWidget.h.

std::vector<WPushButton *> HangmanWidget::LetterButtons [private]

Definition at line 37 of file HangmanWidget.h.

std::vector<WImage *> HangmanWidget::HangmanImages [private]

Definition at line 38 of file HangmanWidget.h.

Definition at line 39 of file HangmanWidget.h.

Definition at line 40 of file HangmanWidget.h.

Definition at line 41 of file HangmanWidget.h.

std::vector<WText *> HangmanWidget::WordLetters [private]

Definition at line 42 of file HangmanWidget.h.

Definition at line 43 of file HangmanWidget.h.

const unsigned int HangmanWidget::MaxGuesses [private]

Definition at line 45 of file HangmanWidget.h.

unsigned int HangmanWidget::BadGuesses [private]

Definition at line 46 of file HangmanWidget.h.

unsigned int HangmanWidget::DisplayedLetters [private]

Definition at line 47 of file HangmanWidget.h.

std::wstring HangmanWidget::Word [private]

Definition at line 48 of file HangmanWidget.h.

std::wstring HangmanWidget::User [private]

Definition at line 49 of file HangmanWidget.h.

Definition at line 50 of file HangmanWidget.h.


The documentation for this class was generated from the following files:

Generated on Fri Mar 26 17:12:12 2010 for Wt by doxygen 1.5.6