Wt examples: HangmanWidget Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HangmanWidget Class Reference

`#include <HangmanWidget.h>`

Inheritance diagram for HangmanWidget:
[legend]

## Public Member Functions
| HangmanWidget (const std::string &name)
Wt::Signal< int > & | scoreUpdated ()

## Private Member Functions
void | registerGuess (char c)
void | newGame ()

## Private Attributes
Wt::WText * | title_ = nullptr
WordWidget * | word_ = nullptr
ImagesWidget * | images_ = nullptr
LettersWidget * | letters_ = nullptr
Wt::WText * | statusText_ = nullptr
Wt::WComboBox * | language_ = nullptr
Wt::WPushButton * | newGameButton_ = nullptr
Wt::Signal< int > | scoreUpdated_
std::string | name_
int | badGuesses_ = 0

## Detailed Description

Definition at line 19 of file HangmanWidget.h.

## Constructor & Destructor Documentation

## ◆ HangmanWidget()

HangmanWidget::HangmanWidget | ( | const std::string & | name | ) |
| explicit

Definition at line 27 of file HangmanWidget.C.

28 : name_(name)
29{
30 setContentAlignment(AlignmentFlag::Center);
31
32 title_ = addNew<WText>(tr("hangman.readyToPlay"));
33
34 word_ = addNew<WordWidget>();
35 statusText_ = addNew<WText>();
36 images_ = addNew<ImagesWidget>(MaxGuesses);
37
38 letters_ = addNew<LettersWidget>();
39 letters_->letterPushed().connect(this, &HangmanWidget::registerGuess);
40
41 language_ = addNew<WComboBox>();
42 language_->addItem(tr("hangman.englishWords").arg(18957));
43 language_->addItem(tr("hangman.dutchWords").arg(1688));
44
45 addNew<WBreak>();
46
47 newGameButton_ = addNew<WPushButton>(tr("hangman.newGame"));
48 newGameButton_->clicked().connect(this, &HangmanWidget::newGame);
49
50 letters_->hide();
51}
HangmanWidget::name_
std::string name_
Definition HangmanWidget.h:39
HangmanWidget::registerGuess
void registerGuess(char c)
Definition HangmanWidget.C:72
HangmanWidget::letters_
LettersWidget * letters_
Definition HangmanWidget.h:31
HangmanWidget::statusText_
Wt::WText * statusText_
Definition HangmanWidget.h:33
HangmanWidget::language_
Wt::WComboBox * language_
Definition HangmanWidget.h:34
HangmanWidget::newGame
void newGame()
Definition HangmanWidget.C:53
HangmanWidget::images_
ImagesWidget * images_
Definition HangmanWidget.h:30
HangmanWidget::title_
Wt::WText * title_
Definition HangmanWidget.h:27
HangmanWidget::word_
WordWidget * word_
Definition HangmanWidget.h:29
HangmanWidget::newGameButton_
Wt::WPushButton * newGameButton_
Definition HangmanWidget.h:35
LettersWidget::letterPushed
Wt::Signal< char > & letterPushed()
Definition LettersWidget.h:27

## Member Function Documentation

## ◆ newGame()

void HangmanWidget::newGame | ( |  | ) |
| private

Definition at line 53 of file HangmanWidget.C.

54{
55 WString title(tr("hangman.guessTheWord"));
56 title_->setText(title.arg(name_));
57
58 language_->hide();
59 newGameButton_->hide();
60
61 /*
62 * Choose a new secret word and reset the game
63 */
64 auto dictionary = static_cast<Dictionary>(language_->currentIndex());
65 word_->init(randomWord(dictionary));
66 letters_->reset();
67 badGuesses_ = 0;
68 images_->showImage(badGuesses_);
69 statusText_->setText("");
70}
randomWord
std::string randomWord(Dictionary dictionary)
Definition Dictionary.C:14
Dictionary
Dictionary
Definition Dictionary.h:13
HangmanWidget::badGuesses_
int badGuesses_
Definition HangmanWidget.h:41
ImagesWidget::showImage
void showImage(int index)
Definition ImagesWidget.C:36
LettersWidget::reset
void reset()
Definition LettersWidget.C:57
WordWidget::init
void init(const std::string &word)
Definition WordWidget.C:16

## ◆ registerGuess()

void HangmanWidget::registerGuess | ( | char | c | ) |
| private

Definition at line 72 of file HangmanWidget.C.

73{
74 if (badGuesses_ < MaxGuesses) {
75 bool correct = word_->guess(c);
76
77 if (!correct) {
78 ++badGuesses_;
79 images_->showImage(badGuesses_);
80 }
81 }
82
83 if (badGuesses_ == MaxGuesses) {
84 WString status = tr("hangman.youHang");
85 statusText_->setText(status.arg(word_->word()));
86
87 letters_->hide();
88 language_->show();
89 newGameButton_->show();
90
91 scoreUpdated_.emit(-10);
92 } else if (word_->won()) {
93 statusText_->setText(tr("hangman.youWin"));
94 images_->showImage(ImagesWidget::HURRAY);
95
96 letters_->hide();
97 language_->show();
98 newGameButton_->show();
99
100 scoreUpdated_.emit(20 - badGuesses_);
101 }
102}
HangmanWidget::scoreUpdated_
Wt::Signal< int > scoreUpdated_
Definition HangmanWidget.h:37
ImagesWidget::HURRAY
static const int HURRAY
Definition ImagesWidget.h:18
WordWidget::guess
bool guess(char c)
Definition WordWidget.C:29
WordWidget::word
std::string word() const
Definition WordWidget.h:18
WordWidget::won
bool won()
Definition WordWidget.C:44

## ◆ scoreUpdated()

Wt::Signal< int > & HangmanWidget::scoreUpdated | ( |  | ) |
| inline

Definition at line 24 of file HangmanWidget.h.

24{ return scoreUpdated_; }

## Member Data Documentation

## ◆ badGuesses_

int HangmanWidget::badGuesses_ = 0
| private

Definition at line 41 of file HangmanWidget.h.

## ◆ images_

ImagesWidget* HangmanWidget::images_ = nullptr
| private

Definition at line 30 of file HangmanWidget.h.

## ◆ language_

Wt::WComboBox* HangmanWidget::language_ = nullptr
| private

Definition at line 34 of file HangmanWidget.h.

## ◆ letters_

LettersWidget* HangmanWidget::letters_ = nullptr
| private

Definition at line 31 of file HangmanWidget.h.

## ◆ name_

std::string HangmanWidget::name_
| private

Definition at line 39 of file HangmanWidget.h.

## ◆ newGameButton_

Wt::WPushButton* HangmanWidget::newGameButton_ = nullptr
| private

Definition at line 35 of file HangmanWidget.h.

## ◆ scoreUpdated_

Wt::Signal<int> HangmanWidget::scoreUpdated_
| private

Definition at line 37 of file HangmanWidget.h.

## ◆ statusText_

Wt::WText* HangmanWidget::statusText_ = nullptr
| private

Definition at line 33 of file HangmanWidget.h.

## ◆ title_

Wt::WText* HangmanWidget::title_ = nullptr
| private

Definition at line 27 of file HangmanWidget.h.

## ◆ word_

WordWidget* HangmanWidget::word_ = nullptr
| private

Definition at line 29 of file HangmanWidget.h.

The documentation for this class was generated from the following files:

- hangman/HangmanWidget.h
- hangman/HangmanWidget.C