Wt examples  4.10.4
WordWidget.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 "WordWidget.h"
8 
9 #include <Wt/WText.h>
10 
12 {
13  addStyleClass("wordcontainer");
14 }
15 
16 void WordWidget::init(const std::string &word)
17 {
18  word_ = word;
20 
21  clear();
22  wordLetters_.clear();
23  for (const char ch : word_) {
24  auto c = addNew<Wt::WText>("-");
25  wordLetters_.push_back(c);
26  }
27 }
28 
29 bool WordWidget::guess(char c)
30 {
31  bool correct = false;
32 
33  for (std::size_t i = 0; i < word_.size(); ++i) {
34  if(word_[i] == c) {
36  wordLetters_[i]->setText(std::string(1, c));
37  correct = true;
38  }
39  }
40 
41  return correct;
42 }
43 
45 {
46  return displayedLetters_ == word_.size();
47 }
void init(const std::string &word)
Definition: WordWidget.C:16
bool guess(char c)
Definition: WordWidget.C:29
std::vector< Wt::WText * > wordLetters_
Definition: WordWidget.h:26
std::string word() const
Definition: WordWidget.h:18
unsigned displayedLetters_
Definition: WordWidget.h:29
bool won()
Definition: WordWidget.C:44
std::string word_
Definition: WordWidget.h:27
virtual void clear()
virtual void addStyleClass(const WString &styleClass, bool force=false) override