Wt examples  3.7.1
HangmanWidget.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Emweb bv, Herent, 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 
21 using namespace Wt;
22 
23 namespace {
24  const int MaxGuesses = 9;
25 }
26 
27 HangmanWidget::HangmanWidget(const std::string &name, WContainerWidget *parent)
28  : WContainerWidget(parent),
29  name_(name),
30  badGuesses_(0)
31 {
32  setContentAlignment(AlignCenter);
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  */
66  Dictionary dictionary = (Dictionary) language_->currentIndex();
67  word_->init(RandomWord(dictionary));
68  letters_->reset();
69  badGuesses_ = 0;
71  statusText_->setText("");
72 }
73 
75 {
76  if (badGuesses_ < MaxGuesses) {
77  bool correct = word_->guess(c);
78 
79  if (!correct) {
80  ++badGuesses_;
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 
93  scoreUpdated_.emit(-10);
94  } else if (word_->won()) {
95  statusText_->setText(tr("hangman.youWin"));
97 
98  letters_->hide();
99  language_->show();
100  newGameButton_->show();
101 
103  }
104 }
std::wstring word() const
Definition: WordWidget.h:18
Wt::Signal< int > scoreUpdated_
Definition: HangmanWidget.h:38
WString & arg(const std::wstring &value)
EventSignal< WMouseEvent > & clicked()
static WString tr(const char *key)
void addItem(const WString &text)
ImagesWidget * images_
Definition: HangmanWidget.h:31
void init(const std::wstring &word)
Definition: WordWidget.C:19
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
void emit(A... args) const
void showImage(int index)
Definition: ImagesWidget.C:38
bool setText(const WString &text)
HangmanWidget(const std::string &name, Wt::WContainerWidget *parent=0)
Definition: HangmanWidget.C:27
Wt::WComboBox * language_
Definition: HangmanWidget.h:35
static const int HURRAY
Definition: ImagesWidget.h:18
WordWidget * word_
Definition: HangmanWidget.h:30
bool guess(wchar_t c)
Definition: WordWidget.C:32
Wt::WPushButton * newGameButton_
Definition: HangmanWidget.h:36
Dictionary
Definition: Dictionary.h:13
bool won()
Definition: WordWidget.C:47
LettersWidget * letters_
Definition: HangmanWidget.h:32
std::string name_
Definition: HangmanWidget.h:40
std::wstring RandomWord(Dictionary dictionary)
Definition: Dictionary.C:16
int currentIndex() const
Wt::Signal< char > & letterPushed()
Definition: LettersWidget.h:26
Wt::WText * statusText_
Definition: HangmanWidget.h:34
Wt::WText * title_
Definition: HangmanWidget.h:28
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method) override
void registerGuess(char c)
Definition: HangmanWidget.C:74

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13