WordWidget.C 883 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include "WordWidget.h"
  7. #include <Wt/WText>
  8. using namespace Wt;
  9. WordWidget::WordWidget(WContainerWidget *parent) :
  10. WContainerWidget(parent)
  11. {
  12. addStyleClass("wordcontainer");
  13. }
  14. void WordWidget::init(const std::wstring &word)
  15. {
  16. word_ = word;
  17. displayedLetters_ = 0;
  18. clear();
  19. wordLetters_.clear();
  20. for(unsigned int i = 0; i < word_.size(); ++i) {
  21. WText *c = new WText("-", this);
  22. wordLetters_.push_back(c);
  23. }
  24. }
  25. bool WordWidget::guess(wchar_t c)
  26. {
  27. bool correct = false;
  28. for(unsigned int i = 0; i < word_.size(); ++i) {
  29. if(word_[i] == c) {
  30. displayedLetters_++;
  31. wordLetters_[i]->setText(std::wstring(1, c));
  32. correct = true;
  33. }
  34. }
  35. return correct;
  36. }
  37. bool WordWidget::won()
  38. {
  39. return displayedLetters_ == word_.size();
  40. }