Wt examples: hangman/WordWidget.C Source File

Wt examples 4.10.4

Loading...
Searching...
No Matches
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
11WordWidget::WordWidget()
12{
13 addStyleClass("wordcontainer");
14}
15
16 void WordWidget::init(const std::string &word)
17{
18 word_ = word;
19 displayedLetters_ = 0;
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) {
35 displayedLetters_++;
36 wordLetters_[i]->setText(std::string(1, c));
37 correct = true;
38 }
39 }
40
41 return correct;
42}
43
44 bool WordWidget::won()
45{
46 return displayedLetters_ == word_.size();
47}
WordWidget.h
WordWidget::WordWidget
WordWidget()
Definition WordWidget.C:11
WordWidget::init
void init(const std::string &word)
Definition WordWidget.C:16
WordWidget::guess
bool guess(char c)
Definition WordWidget.C:29
WordWidget::wordLetters_
std::vector< Wt::WText * > wordLetters_
Definition WordWidget.h:26
WordWidget::word
std::string word() const
Definition WordWidget.h:18
WordWidget::displayedLetters_
unsigned displayedLetters_
Definition WordWidget.h:29
WordWidget::won
bool won()
Definition WordWidget.C:44
WordWidget::word_
std::string word_
Definition WordWidget.h:27