Wt examples: /home/koen/project/wt/public-git/wt/examples/hangman/HangmanWidget.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/hangman/HangmanWidget.C
Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
    00003  *
    00004  * See the LICENSE file for terms of use.
    00005  */
    00006
    00007 #include "HangmanWidget.h"
    00008
    00009 #include <Wt/WBreak>
    00010 #include <Wt/WComboBox>
    00011 #include <Wt/WPushButton>
    00012 #include <Wt/WText>
    00013 #include <boost/lexical_cast.hpp>
    00014
    00015 #include "Session.h"
    00016 #include "WordWidget.h"
    00017 #include "ImagesWidget.h"
    00018 #include "LettersWidget.h"
    00019
    00020 using namespace Wt;
    00021
    00022 namespace {
    00023   const int MaxGuesses = 9;
    00024 }
    00025
    00026 HangmanWidget::HangmanWidget(const std::string &name, WContainerWidget *parent)
    00027   : WContainerWidget(parent),
    00028     name_(name),
    00029     badGuesses_(0)
    00030 {
    00031   setContentAlignment(AlignCenter);
    00032
    00033   title_ = new WText(tr("hangman.readyToPlay"), this);
    00034
    00035   word_ = new WordWidget(this);
    00036   statusText_ = new WText(this);
    00037   images_ = new ImagesWidget(MaxGuesses, this);
    00038
    00039   letters_ = new LettersWidget(this);
    00040   letters_->letterPushed().connect(this, &HangmanWidget::registerGuess);
    00041
    00042   language_ = new WComboBox(this);
    00043   language_->addItem(tr("hangman.englishWords").arg(18957));
    00044   language_->addItem(tr("hangman.dutchWords").arg(1688));
    00045
    00046   new WBreak(this);
    00047
    00048   newGameButton_ = new WPushButton(tr("hangman.newGame"), this);
    00049   newGameButton_->clicked().connect(this, &HangmanWidget::newGame);
    00050
    00051   letters_->hide();
    00052 }
    00053
    00054 void HangmanWidget::newGame()
    00055 {
    00056   WString title(tr("hangman.guessTheWord"));
    00057   title_->setText(title.arg(name_));
    00058
    00059   language_->hide();
    00060   newGameButton_->hide();
    00061
    00062   /*
    00063    * Choose a new secret word and reset the game
    00064    */
    00065   Dictionary dictionary = (Dictionary) language_->currentIndex();
    00066   word_->init(RandomWord(dictionary));
    00067   letters_->reset();
    00068   badGuesses_ = 0;
    00069   images_->showImage(badGuesses_);
    00070   statusText_->setText("");
    00071 }
    00072
    00073 void HangmanWidget::registerGuess(char c)
    00074 {
    00075   bool correct = word_->guess(c);
    00076
    00077   if (!correct) {
    00078     ++badGuesses_;
    00079     images_->showImage(badGuesses_);
    00080   }
    00081
    00082   if (badGuesses_ == MaxGuesses) {
    00083     WString status(tr("hangman.youHang"));
    00084     statusText_->setText(status.arg(word_->word()));
    00085
    00086     letters_->hide();
    00087     language_->show();
    00088     newGameButton_->show();
    00089
    00090     scoreUpdated_.emit(-10);
    00091   } else if (word_->won()) {
    00092     statusText_->setText(tr("hangman.youWin"));
    00093     images_->showImage(ImagesWidget::HURRAY);
    00094
    00095     letters_->hide();
    00096     language_->show();
    00097     newGameButton_->show();
    00098
    00099     scoreUpdated_.emit(20 - badGuesses_);
    00100   }
    00101 }