Wt examples 4.8.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.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
21using namespace Wt;
22
23namespace {
24 const int MaxGuesses = 9;
25}
26
27HangmanWidget::HangmanWidget(const std::string &name)
28 : WContainerWidget(),
29 name_(name),
30 badGuesses_(0)
31{
32 setContentAlignment(AlignmentFlag::Center);
33
34 title_ = addWidget(std::make_unique<WText>(tr("hangman.readyToPlay")));
35
36 word_ = addWidget(std::make_unique<WordWidget>());
37 statusText_ = addWidget(std::make_unique<WText>());
38 images_ = addWidget(std::make_unique<ImagesWidget>(MaxGuesses));
39
40 letters_ = addWidget(std::make_unique<LettersWidget>());
42
43 language_ = addWidget(std::make_unique<WComboBox>());
44 language_->addItem(tr("hangman.englishWords").arg(18957));
45 language_->addItem(tr("hangman.dutchWords").arg(1688));
46
47 addWidget(std::make_unique<WBreak>());
48
49 newGameButton_ = addWidget(std::make_unique<WPushButton>(tr("hangman.newGame")));
50 newGameButton_->clicked().connect(this, &HangmanWidget::newGame);
51
52 letters_->hide();
53}
54
56{
57 WString title(tr("hangman.guessTheWord"));
58 title_->setText(title.arg(name_));
59
60 language_->hide();
61 newGameButton_->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) {
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();
91 newGameButton_->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
102 scoreUpdated_.emit(20 - badGuesses_);
103 }
104}
std::wstring RandomWord(Dictionary dictionary)
Definition: Dictionary.C:16
Dictionary
Definition: Dictionary.h:15
std::string name_
Definition: HangmanWidget.h:40
void registerGuess(char c)
Definition: HangmanWidget.C:74
LettersWidget * letters_
Definition: HangmanWidget.h:32
Wt::WText * statusText_
Definition: HangmanWidget.h:34
Wt::WComboBox * language_
Definition: HangmanWidget.h:35
ImagesWidget * images_
Definition: HangmanWidget.h:31
Wt::WText * title_
Definition: HangmanWidget.h:28
HangmanWidget(const std::string &name)
Definition: HangmanWidget.C:27
Wt::Signal< int > scoreUpdated_
Definition: HangmanWidget.h:38
WordWidget * word_
Definition: HangmanWidget.h:30
Wt::WPushButton * newGameButton_
Definition: HangmanWidget.h:36
void showImage(int index)
Definition: ImagesWidget.C:36
static const int HURRAY
Definition: ImagesWidget.h:20
Signal< char > & letterPushed()
Definition: LettersWidget.h:29
std::wstring word() const
Definition: WordWidget.h:20
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

Generated on Fri Sep 23 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.5