Wt examples 4.6.1
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HangmanWidget Class Reference

#include <HangmanWidget.h>

Inheritance diagram for HangmanWidget:
Inheritance graph
[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_
 
WordWidgetword_
 
ImagesWidgetimages_
 
LettersWidgetletters_
 
Wt::WText * statusText_
 
Wt::WComboBox * language_
 
Wt::WPushButton * newGameButton_
 
Wt::Signal< int > scoreUpdated_
 
std::string name_
 
int badGuesses_
 

Detailed Description

Definition at line 20 of file HangmanWidget.h.

Constructor & Destructor Documentation

◆ HangmanWidget()

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

Definition at line 27 of file HangmanWidget.C.

28 : WContainerWidget(),
29 name_(name),
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}
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
WordWidget * word_
Definition: HangmanWidget.h:30
Wt::WPushButton * newGameButton_
Definition: HangmanWidget.h:36
Signal< char > & letterPushed()
Definition: LettersWidget.h:29

Member Function Documentation

◆ newGame()

void HangmanWidget::newGame ( )
private

Definition at line 55 of file HangmanWidget.C.

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}
std::wstring RandomWord(Dictionary dictionary)
Definition: Dictionary.C:16
Dictionary
Definition: Dictionary.h:15
void showImage(int index)
Definition: ImagesWidget.C:36
void init(const std::wstring &word)
Definition: WordWidget.C:19

◆ registerGuess()

void HangmanWidget::registerGuess ( char  c)
private

Definition at line 74 of file HangmanWidget.C.

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}
Wt::Signal< int > scoreUpdated_
Definition: HangmanWidget.h:38
static const int HURRAY
Definition: ImagesWidget.h:20
std::wstring word() const
Definition: WordWidget.h:20
bool guess(wchar_t c)
Definition: WordWidget.C:32
bool won()
Definition: WordWidget.C:47

◆ scoreUpdated()

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

Definition at line 25 of file HangmanWidget.h.

25{ return scoreUpdated_; }

Member Data Documentation

◆ badGuesses_

int HangmanWidget::badGuesses_
private

Definition at line 42 of file HangmanWidget.h.

◆ images_

ImagesWidget* HangmanWidget::images_
private

Definition at line 31 of file HangmanWidget.h.

◆ language_

Wt::WComboBox* HangmanWidget::language_
private

Definition at line 35 of file HangmanWidget.h.

◆ letters_

LettersWidget* HangmanWidget::letters_
private

Definition at line 32 of file HangmanWidget.h.

◆ name_

std::string HangmanWidget::name_
private

Definition at line 40 of file HangmanWidget.h.

◆ newGameButton_

Wt::WPushButton* HangmanWidget::newGameButton_
private

Definition at line 36 of file HangmanWidget.h.

◆ scoreUpdated_

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

Definition at line 38 of file HangmanWidget.h.

◆ statusText_

Wt::WText* HangmanWidget::statusText_
private

Definition at line 34 of file HangmanWidget.h.

◆ title_

Wt::WText* HangmanWidget::title_
private

Definition at line 28 of file HangmanWidget.h.

◆ word_

WordWidget* HangmanWidget::word_
private

Definition at line 30 of file HangmanWidget.h.


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

Generated on Fri Jan 28 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.3