Wt examples  3.3.5
Public Member Functions | Private Member Functions | Private Attributes
HangmanWidget Class Reference

#include <HangmanWidget.h>

List of all members.

Public Member Functions

 HangmanWidget (const std::string &name, Wt::WContainerWidget *parent=0)
Wt::Signal< int > & scoreUpdated ()

Private Member Functions

void registerGuess (char c)
void newGame ()

Private Attributes

Wt::WText * title_
WordWidgetword_
ImagesWidgetimages_
LettersWidgetletters_
Wt::WText * statusText_
Wt::WComboBox * language_
Wt::WPushButton * newGameButton_
Wt::Signal< int > scoreUpdated_
std::string name_
int badGuesses_

Detailed Description

Definition at line 20 of file HangmanWidget.h.


Constructor & Destructor Documentation

HangmanWidget::HangmanWidget ( const std::string &  name,
Wt::WContainerWidget *  parent = 0 
)

Definition at line 27 of file HangmanWidget.C.

  : WContainerWidget(parent),
    name_(name),
    badGuesses_(0)
{
  setContentAlignment(AlignCenter);
  
  title_ = new WText(tr("hangman.readyToPlay"), this);

  word_ = new WordWidget(this);
  statusText_ = new WText(this);
  images_ = new ImagesWidget(MaxGuesses, this);

  letters_ = new LettersWidget(this);
  letters_->letterPushed().connect(this, &HangmanWidget::registerGuess);

  language_ = new WComboBox(this);
  language_->addItem(tr("hangman.englishWords").arg(18957));
  language_->addItem(tr("hangman.dutchWords").arg(1688));

  new WBreak(this);

  newGameButton_ = new WPushButton(tr("hangman.newGame"), this);
  newGameButton_->clicked().connect(this, &HangmanWidget::newGame);

  letters_->hide();
}

Member Function Documentation

void HangmanWidget::newGame ( ) [private]

Definition at line 55 of file HangmanWidget.C.

{
  WString title(tr("hangman.guessTheWord"));
  title_->setText(title.arg(name_));

  language_->hide();
  newGameButton_->hide();

  /*
   * Choose a new secret word and reset the game
   */
  Dictionary dictionary = (Dictionary) language_->currentIndex();
  word_->init(RandomWord(dictionary));
  letters_->reset();
  badGuesses_ = 0;
  images_->showImage(badGuesses_);
  statusText_->setText("");
}
void HangmanWidget::registerGuess ( char  c) [private]

Definition at line 74 of file HangmanWidget.C.

{
  if (badGuesses_ < MaxGuesses) {
    bool correct = word_->guess(c);

    if (!correct) {
      ++badGuesses_;
      images_->showImage(badGuesses_);
    }
  }

  if (badGuesses_ == MaxGuesses) {
    WString status(tr("hangman.youHang"));
    statusText_->setText(status.arg(word_->word()));

    letters_->hide();
    language_->show();
    newGameButton_->show();

    scoreUpdated_.emit(-10);
  } else if (word_->won()) {
    statusText_->setText(tr("hangman.youWin"));
    images_->showImage(ImagesWidget::HURRAY);

    letters_->hide();
    language_->show();
    newGameButton_->show();

    scoreUpdated_.emit(20 - badGuesses_);
  }
}
Wt::Signal<int>& HangmanWidget::scoreUpdated ( ) [inline]

Definition at line 25 of file HangmanWidget.h.

{ return scoreUpdated_; }

Member Data Documentation

Definition at line 42 of file HangmanWidget.h.

Definition at line 31 of file HangmanWidget.h.

Wt::WComboBox* HangmanWidget::language_ [private]

Definition at line 35 of file HangmanWidget.h.

Definition at line 32 of file HangmanWidget.h.

std::string HangmanWidget::name_ [private]

Definition at line 40 of file HangmanWidget.h.

Wt::WPushButton* HangmanWidget::newGameButton_ [private]

Definition at line 36 of file HangmanWidget.h.

Wt::Signal<int> HangmanWidget::scoreUpdated_ [private]

Definition at line 38 of file HangmanWidget.h.

Wt::WText* HangmanWidget::statusText_ [private]

Definition at line 34 of file HangmanWidget.h.

Wt::WText* HangmanWidget::title_ [private]

Definition at line 28 of file HangmanWidget.h.

Definition at line 30 of file HangmanWidget.h.


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

Generated on Wed Mar 23 2016 for the C++ Web Toolkit (Wt) by doxygen 1.7.6.1