Wt examples  3.3.0
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  bool correct = word_->guess(c);
76 
77  if (!correct) {
78  ++badGuesses_;
80  }
81 
82  if (badGuesses_ == MaxGuesses) {
83  WString status(tr("hangman.youHang"));
84  statusText_->setText(status.arg(word_->word()));
85 
86  letters_->hide();
87  language_->show();
88  newGameButton_->show();
89 
90  scoreUpdated_.emit(-10);
91  } else if (word_->won()) {
92  statusText_->setText(tr("hangman.youWin"));
94 
95  letters_->hide();
96  language_->show();
97  newGameButton_->show();
98 
100  }
101 }

Generated on Fri May 31 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.3.1