Wt examples  4.0.0
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.h>
10 #include <Wt/WComboBox.h>
11 #include <Wt/WPushButton.h>
12 #include <Wt/WText.h>
13 #include <Wt/WAny.h>
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)
28  : WContainerWidget(),
29  name_(name),
30  badGuesses_(0)
31 {
32  setContentAlignment(AlignmentFlag::Center);
33 
34  title_ = addWidget(cpp14::make_unique<WText>(tr("hangman.readyToPlay")));
35 
36  word_ = addWidget(cpp14::make_unique<WordWidget>());
37  statusText_ = addWidget(cpp14::make_unique<WText>());
38  images_ = addWidget(cpp14::make_unique<ImagesWidget>(MaxGuesses));
39 
40  letters_ = addWidget(cpp14::make_unique<LettersWidget>());
42 
43  language_ = addWidget(cpp14::make_unique<WComboBox>());
44  language_->addItem(tr("hangman.englishWords").arg(18957));
45  language_->addItem(tr("hangman.dutchWords").arg(1688));
46 
47  addWidget(cpp14::make_unique<WBreak>());
48 
49  newGameButton_ = addWidget(cpp14::make_unique<WPushButton>(tr("hangman.newGame")));
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 }
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)
Signal< char > & letterPushed()
Definition: LettersWidget.h:28
void emit(A...args) const
ImagesWidget * images_
Definition: HangmanWidget.h:31
int currentIndex() const
void init(const std::wstring &word)
Definition: WordWidget.C:19
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
void showImage(int index)
Definition: ImagesWidget.C:36
bool setText(const WString &text)
Wt::WComboBox * language_
Definition: HangmanWidget.h:35
Wt::Signals::connection connect(const F &function)
static const int HURRAY
Definition: ImagesWidget.h:20
WordWidget * word_
Definition: HangmanWidget.h:30
std::wstring word() const
Definition: WordWidget.h:20
bool guess(wchar_t c)
Definition: WordWidget.C:32
Wt::WPushButton * newGameButton_
Definition: HangmanWidget.h:36
Dictionary
Definition: Dictionary.h:15
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
HangmanWidget(const std::string &name)
Definition: HangmanWidget.C:27
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
virtual void addWidget(std::unique_ptr< WWidget > widget)
void registerGuess(char c)
Definition: HangmanWidget.C:74

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11