Wt examples
4.14.4
Loading...
Searching...
No Matches
hangman
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
11
WordWidget::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
const
std::size_t wordLength =
word_
.size();
24
for
(std::size_t i = 0; i < wordLength; ++i) {
25
auto
c = addNew<Wt::WText>(
"-"
);
26
wordLetters_
.push_back(c);
27
}
28
}
29
30
bool
WordWidget::guess
(
char
c)
31
{
32
bool
correct =
false
;
33
34
for
(std::size_t i = 0; i <
word_
.size(); ++i) {
35
if
(
word_
[i] == c) {
36
displayedLetters_
++;
37
wordLetters_
[i]->setText(std::string(1, c));
38
correct =
true
;
39
}
40
}
41
42
return
correct;
43
}
44
45
bool
WordWidget::won
()
46
{
47
return
displayedLetters_
==
word_
.size();
48
}
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:30
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:45
WordWidget::word_
std::string word_
Definition
WordWidget.h:27
Wt::WContainerWidget::clear
virtual void clear()
Wt::WWebWidget::addStyleClass
virtual void addStyleClass(const WString &styleClass, bool force=false) override
Generated on Thu Oct 1 2026 for
the C++ Web Toolkit (Wt)
by
1.9.8