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 169 of file HangmanWidget.C.

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

void HangmanWidget::resetButtons (  )  [private]

Definition at line 176 of file HangmanWidget.C.

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

void HangmanWidget::registerBadGuess (  )  [private]

Definition at line 135 of file HangmanWidget.C.

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 }

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

Definition at line 151 of file HangmanWidget.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 }

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

Definition at line 121 of file HangmanWidget.C.

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 }

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 Tue Sep 1 17:51:23 2009 for Wt by doxygen 1.5.6