Wt examples  3.3.1
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>
10 #include <Wt/WComboBox>
11 #include <Wt/WPushButton>
12 #include <Wt/WText>
13 #include <boost/lexical_cast.hpp>
14 
15 #include "Session.h"
16 #include "WordWidget.h"
17 #include "ImagesWidget.h"
18 #include "LettersWidget.h"
19 
20 using namespace Wt;
21 
22 namespace {
23  const int MaxGuesses = 9;
24 }
25 
26 HangmanWidget::HangmanWidget(const std::string &name, WContainerWidget *parent)
27  : WContainerWidget(parent),
28  name_(name),
29  badGuesses_(0)
30 {
31  setContentAlignment(AlignCenter);
32 
33  title_ = new WText(tr("hangman.readyToPlay"), this);
34 
35  word_ = new WordWidget(this);
36  statusText_ = new WText(this);
37  images_ = new ImagesWidget(MaxGuesses, this);
38 
39  letters_ = new LettersWidget(this);
41 
42  language_ = new WComboBox(this);
43  language_->addItem(tr("hangman.englishWords").arg(18957));
44  language_->addItem(tr("hangman.dutchWords").arg(1688));
45 
46  new WBreak(this);
47 
48  newGameButton_ = new WPushButton(tr("hangman.newGame"), this);
49  newGameButton_->clicked().connect(this, &HangmanWidget::newGame);
50 
51  letters_->hide();
52 }
53 
55 {
56  WString title(tr("hangman.guessTheWord"));
57  title_->setText(title.arg(name_));
58 
59  language_->hide();
60  newGameButton_->hide();
61 
62  /*
63  * Choose a new secret word and reset the game
64  */
65  Dictionary dictionary = (Dictionary) language_->currentIndex();
66  word_->init(RandomWord(dictionary));
67  letters_->reset();
68  badGuesses_ = 0;
70  statusText_->setText("");
71 }
72 
74 {
75  if (badGuesses_ < MaxGuesses) {
76  bool correct = word_->guess(c);
77 
78  if (!correct) {
79  ++badGuesses_;
81  }
82  }
83 
84  if (badGuesses_ == MaxGuesses) {
85  WString status(tr("hangman.youHang"));
86  statusText_->setText(status.arg(word_->word()));
87 
88  letters_->hide();
89  language_->show();
90  newGameButton_->show();
91 
92  scoreUpdated_.emit(-10);
93  } else if (word_->won()) {
94  statusText_->setText(tr("hangman.youWin"));
96 
97  letters_->hide();
98  language_->show();
99  newGameButton_->show();
100 
102  }
103 }
void emit(A1 a1=NoClass::none, A2 a2=NoClass::none, A3 a3=NoClass::none, A4 a4=NoClass::none, A5 a5=NoClass::none, A6 a6=NoClass::none) const
Wt::Signal< int > scoreUpdated_
Definition: HangmanWidget.h:40
WString & arg(const std::wstring &value)
void addItem(const WString &text)
ImagesWidget * images_
Definition: HangmanWidget.h:33
int currentIndex() const
void init(const std::wstring &word)
Definition: WordWidget.C:19
void showImage(int index)
Definition: ImagesWidget.C:38
HangmanWidget(const std::string &name, Wt::WContainerWidget *parent=0)
Definition: HangmanWidget.C:26
Wt::WComboBox * language_
Definition: HangmanWidget.h:37
static const int HURRAY
Definition: ImagesWidget.h:18
WordWidget * word_
Definition: HangmanWidget.h:32
std::wstring word() const
Definition: WordWidget.h:18
bool guess(wchar_t c)
Definition: WordWidget.C:32
Wt::WPushButton * newGameButton_
Definition: HangmanWidget.h:38
Dictionary
Definition: Dictionary.h:13
bool won()
Definition: WordWidget.C:47
LettersWidget * letters_
Definition: HangmanWidget.h:34
std::string name_
Definition: HangmanWidget.h:42
std::wstring RandomWord(Dictionary dictionary)
Definition: Dictionary.C:16
Wt::Signal< char > & letterPushed()
Definition: LettersWidget.h:26
Wt::WText * statusText_
Definition: HangmanWidget.h:36
Wt::WText * title_
Definition: HangmanWidget.h:30
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method)
void registerGuess(char c)
Definition: HangmanWidget.C:73

Generated on Wed Jun 11 2014 for the C++ Web Toolkit (Wt) by doxygen 1.8.7