Wt examples  4.10.0
Loading...
Searching...
No Matches
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 : 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>();
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}
52
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;
69 statusText_->setText("");
70}
71
73{
74 if (badGuesses_ < MaxGuesses) {
75 bool correct = word_->guess(c);
76
77 if (!correct) {
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"));
95
96 letters_->hide();
97 language_->show();
98 newGameButton_->show();
99
100 scoreUpdated_.emit(20 - badGuesses_);
101 }
102}
std::string randomWord(Dictionary dictionary)
Definition Dictionary.C:14
Dictionary
Definition Dictionary.h:13
std::string name_
void registerGuess(char c)
LettersWidget * letters_
Wt::WText * statusText_
Wt::WComboBox * language_
ImagesWidget * images_
Wt::WText * title_
HangmanWidget(const std::string &name)
Wt::Signal< int > scoreUpdated_
WordWidget * word_
Wt::WPushButton * newGameButton_
void showImage(int index)
static const int HURRAY
Wt::Signal< char > & letterPushed()
void init(const std::string &word)
Definition WordWidget.C:16
bool guess(char c)
Definition WordWidget.C:29
std::string word() const
Definition WordWidget.h:18
bool won()
Definition WordWidget.C:44