Wt examples 3.3.12
HangmanWidget.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include "HangmanWidget.h"
8
9#include <Wt/WBreak>
10#include <Wt/WComboBox>
11#include <Wt/WPushButton>
12#include <Wt/WText>
13#include <boost/lexical_cast.hpp>
14
15#include "Session.h"
16#include "Dictionary.h"
17#include "WordWidget.h"
18#include "ImagesWidget.h"
19#include "LettersWidget.h"
20
21using namespace Wt;
22
23namespace {
24 const int MaxGuesses = 9;
25}
26
27HangmanWidget::HangmanWidget(const std::string &name, WContainerWidget *parent)
28 : WContainerWidget(parent),
29 name_(name),
30 badGuesses_(0)
31{
33
34 title_ = new WText(tr("hangman.readyToPlay"), this);
35
36 word_ = new WordWidget(this);
37 statusText_ = new WText(this);
38 images_ = new ImagesWidget(MaxGuesses, this);
39
40 letters_ = new LettersWidget(this);
42
43 language_ = new WComboBox(this);
44 language_->addItem(tr("hangman.englishWords").arg(18957));
45 language_->addItem(tr("hangman.dutchWords").arg(1688));
46
47 new WBreak(this);
48
49 newGameButton_ = new WPushButton(tr("hangman.newGame"), this);
51
52 letters_->hide();
53}
54
56{
57 WString title(tr("hangman.guessTheWord"));
58 title_->setText(title.arg(name_));
59
60 language_->hide();
62
63 /*
64 * Choose a new secret word and reset the game
65 */
67 word_->init(RandomWord(dictionary));
68 letters_->reset();
69 badGuesses_ = 0;
72}
73
75{
76 if (badGuesses_ < MaxGuesses) {
77 bool correct = word_->guess(c);
78
79 if (!correct) {
82 }
83 }
84
85 if (badGuesses_ == MaxGuesses) {
86 WString status(tr("hangman.youHang"));
87 statusText_->setText(status.arg(word_->word()));
88
89 letters_->hide();
90 language_->show();
92
94 } else if (word_->won()) {
95 statusText_->setText(tr("hangman.youWin"));
97
98 letters_->hide();
99 language_->show();
101
103 }
104}
std::wstring RandomWord(Dictionary dictionary)
Definition Dictionary.C:16
Dictionary
Definition Dictionary.h:13
std::string name_
void registerGuess(char c)
LettersWidget * letters_
Wt::WText * statusText_
Wt::WComboBox * language_
ImagesWidget * images_
Wt::WText * title_
HangmanWidget(const std::string &name, Wt::WContainerWidget *parent=0)
Wt::Signal< int > scoreUpdated_
WordWidget * word_
Wt::WPushButton * newGameButton_
void showImage(int index)
static const int HURRAY
Wt::Signal< char > & letterPushed()
std::wstring word() const
Definition WordWidget.h:18
bool guess(wchar_t c)
Definition WordWidget.C:32
void init(const std::wstring &word)
Definition WordWidget.C:19
bool won()
Definition WordWidget.C:47
void emit(A1 a1=NoClass::none, A2 a2=NoClass::none, A3 a3=NoClass::none, A4 a4=NoClass::none, A5 a5=NoClass::none, A6 a6=NoClass::none) const
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method)
int currentIndex() const
void addItem(const WString &text)
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
EventSignal< WMouseEvent > & clicked()
WString & arg(const std::wstring &value)
bool setText(const WString &text)
static WString tr(const char *key)
AlignCenter

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8