Public Member Functions | Private Slots | Private Member Functions | Private Attributes

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.

                                                      :
   WContainerWidget(parent),
   MaxGuesses(9),
   User(user),
   Dict(dict)
{
   setContentAlignment(AlignCenter);

   Title = new WText(L"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, L"courier");
   style.font().setSize(WFont::XXLarge);

   StatusText = new WText(this);
   new WBreak(this);
   createHangmanImages(this);
   createAlphabet(this);
   new WBreak(this);
   NewGameButton = new WPushButton(L"New Game", this);
   NewGameButton->clicked().connect(SLOT(this, HangmanWidget::newGame));

   // prepare for first game
   newGame();
}

Member Function Documentation

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(SLOT(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, slot]

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(L"-", 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(L"");
}
void HangmanWidget::processButton ( WPushButton button ) [private, slot]

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(L"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();
}

Member Data Documentation

unsigned int HangmanWidget::BadGuesses [private]

Definition at line 46 of file HangmanWidget.h.

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.

Definition at line 39 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.

const unsigned int HangmanWidget::MaxGuesses [private]

Definition at line 45 of file HangmanWidget.h.

Definition at line 43 of file HangmanWidget.h.

Definition at line 41 of file HangmanWidget.h.

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.

Definition at line 40 of file HangmanWidget.h.

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

Definition at line 42 of file HangmanWidget.h.


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

Generated on Sat Dec 4 2010 06:32:31 for Wt by doxygen 1.7.2