#include <HangmanWidget.h>
Inherits Wt::WContainerWidget.
Public Member Functions | |
HangmanWidget (std::wstring user, Dictionary dict, WContainerWidget *parent=0) | |
Private Slots | |
void | processButton (WPushButton *button) |
void | newGame () |
Private Member Functions | |
void | createAlphabet (WContainerWidget *parent) |
void | createHangmanImages (WContainerWidget *parent) |
void | resetImages () |
void | resetButtons () |
void | registerBadGuess () |
void | registerCorrectGuess (wchar_t c) |
Private Attributes | |
WText * | Title |
WTable * | LetterButtonLayout |
std::vector< WPushButton * > | LetterButtons |
std::vector< WImage * > | HangmanImages |
WImage * | HurrayImage |
WContainerWidget * | WordContainer |
WText * | StatusText |
std::vector< WText * > | WordLetters |
WPushButton * | NewGameButton |
const unsigned int | MaxGuesses |
unsigned int | BadGuesses |
unsigned int | DisplayedLetters |
std::wstring | Word |
std::wstring | User |
Dictionary | Dict |
Definition at line 28 of file HangmanWidget.h.
HangmanWidget::HangmanWidget | ( | std::wstring | user, | |
Dictionary | dict, | |||
WContainerWidget * | parent = 0 | |||
) |
Definition at line 23 of file HangmanWidget.C.
00025 : 00026 WContainerWidget(parent), 00027 MaxGuesses(9), 00028 User(user), 00029 Dict(dict) 00030 { 00031 setContentAlignment(AlignCenter); 00032 00033 Title = new WText(L"Guess the word!", this); 00034 Title->decorationStyle().font().setSize(WFont::XLarge); 00035 00036 WordContainer = new WContainerWidget(this); 00037 WordContainer->setMargin(20, Top | Bottom); 00038 WordContainer->setContentAlignment(AlignCenter); 00039 WCssDecorationStyle& style = WordContainer->decorationStyle(); 00040 style.setBorder(WBorder(WBorder::Solid)); 00041 style.font().setFamily(WFont::Monospace, L"courier"); 00042 style.font().setSize(WFont::XXLarge); 00043 00044 StatusText = new WText(this); 00045 new WBreak(this); 00046 createHangmanImages(this); 00047 createAlphabet(this); 00048 new WBreak(this); 00049 NewGameButton = new WPushButton(L"New Game", this); 00050 NewGameButton->clicked().connect(SLOT(this, HangmanWidget::newGame)); 00051 00052 // prepare for first game 00053 newGame(); }
void HangmanWidget::createAlphabet | ( | WContainerWidget * | parent | ) | [private] |
Definition at line 72 of file HangmanWidget.C.
00073 { 00074 LetterButtonLayout = new WTable(parent); 00075 00076 // The default width of a table is 100%... 00077 LetterButtonLayout->resize(13*30, WLength::Auto); 00078 00079 WSignalMapper<WPushButton *> *mapper 00080 = new WSignalMapper<WPushButton *>(this); 00081 00082 for(unsigned int i = 0; i < 26; ++i) { 00083 std::wstring c(1, 'A' + i); 00084 WPushButton *character = 00085 new WPushButton(c, LetterButtonLayout->elementAt(i / 13, i % 13)); 00086 LetterButtons.push_back(character); 00087 character->resize(30, WLength::Auto); 00088 mapper->mapConnect(character->clicked(), character); 00089 } 00090 00091 mapper->mapped().connect(SLOT(this, HangmanWidget::processButton)); 00092 }
void HangmanWidget::createHangmanImages | ( | WContainerWidget * | parent | ) | [private] |
Definition at line 55 of file HangmanWidget.C.
00056 { 00057 for(unsigned int i = 0; i <= MaxGuesses; ++i) { 00058 std::string fname = "icons/hangman"; 00059 fname += boost::lexical_cast<std::string>(i) + ".png"; 00060 WImage *theImage = new WImage(fname, parent); 00061 HangmanImages.push_back(theImage); 00062 00063 // Although not necessary, we can avoid flicker (on konqueror) 00064 // by presetting the image size. 00065 theImage->resize(256, 256); 00066 } 00067 00068 HurrayImage = new WImage("icons/hangmanhurray.png", parent); 00069 resetImages(); // Hide all images 00070 }
void HangmanWidget::newGame | ( | ) | [private, slot] |
Definition at line 94 of file HangmanWidget.C.
00095 { 00096 Word = RandomWord(Dict); 00097 Title->setText(L"Guess the word, " + User + L"!"); 00098 NewGameButton->hide(); // don't let the player chicken out 00099 00100 // Bring widget to initial state 00101 resetImages(); 00102 resetButtons(); 00103 BadGuesses = DisplayedLetters = 0; 00104 HangmanImages[0]->show(); 00105 00106 // Prepare the widgets for the new word 00107 WordContainer->clear(); 00108 WordLetters.clear(); 00109 for(unsigned int i = 0; i < Word.size(); ++i) { 00110 WText *c = new WText(L"-", WordContainer); 00111 WordLetters.push_back(c); 00112 } 00113 00114 // resize appropriately so that the border nooks nice. 00115 WordContainer->resize(WLength(Word.size() * 1.5, WLength::FontEx), 00116 WLength::Auto); 00117 00118 StatusText->setText(L""); 00119 }
void HangmanWidget::processButton | ( | WPushButton * | button | ) | [private, slot] |
Definition at line 121 of file HangmanWidget.C.
00122 { 00123 if (!button->isEnabled()) 00124 return; 00125 00126 wchar_t c = button->text().value().c_str()[0]; 00127 if(std::find(Word.begin(), Word.end(), c) != Word.end()) 00128 registerCorrectGuess(c); 00129 else 00130 registerBadGuess(); 00131 button->disable(); 00132 }
void HangmanWidget::registerBadGuess | ( | ) | [private] |
Definition at line 134 of file HangmanWidget.C.
00135 { 00136 if(BadGuesses < MaxGuesses) { 00137 HangmanImages[BadGuesses]->hide(); 00138 BadGuesses++; 00139 HangmanImages[BadGuesses]->show(); 00140 if(BadGuesses == MaxGuesses) { 00141 StatusText->setText(L"You hang... <br />" 00142 L"The correct answer was: " + Word); 00143 LetterButtonLayout->hide(); 00144 NewGameButton->show(); 00145 HangmanDb::addToScore(User, -10); 00146 } 00147 } 00148 }
void HangmanWidget::registerCorrectGuess | ( | wchar_t | c | ) | [private] |
Definition at line 150 of file HangmanWidget.C.
00151 { 00152 for(unsigned int i = 0; i < Word.size(); ++i) { 00153 if(Word[i] == c) { 00154 DisplayedLetters++; 00155 WordLetters[i]->setText(std::wstring(1, c)); 00156 } 00157 } 00158 if(DisplayedLetters == Word.size()) { 00159 StatusText->setText(L"You win!"); 00160 HangmanImages[BadGuesses]->hide(); 00161 HurrayImage->show(); 00162 LetterButtonLayout->hide(); 00163 NewGameButton->show(); 00164 HangmanDb::addToScore(User, 20 - BadGuesses); 00165 } 00166 }
void HangmanWidget::resetButtons | ( | ) | [private] |
Definition at line 175 of file HangmanWidget.C.
00176 { 00177 for(unsigned int i = 0; i < LetterButtons.size(); ++i) { 00178 LetterButtons[i]->enable(); 00179 } 00180 LetterButtonLayout->show(); 00181 }
void HangmanWidget::resetImages | ( | ) | [private] |
Definition at line 168 of file HangmanWidget.C.
00169 { 00170 HurrayImage->hide(); 00171 for(unsigned int i = 0; i < HangmanImages.size(); ++i) 00172 HangmanImages[i]->hide(); 00173 }
unsigned int HangmanWidget::BadGuesses [private] |
Definition at line 46 of file HangmanWidget.h.
Dictionary HangmanWidget::Dict [private] |
Definition at line 50 of file HangmanWidget.h.
unsigned int HangmanWidget::DisplayedLetters [private] |
Definition at line 47 of file HangmanWidget.h.
std::vector<WImage *> HangmanWidget::HangmanImages [private] |
Definition at line 38 of file HangmanWidget.h.
WImage* HangmanWidget::HurrayImage [private] |
Definition at line 39 of file HangmanWidget.h.
WTable* HangmanWidget::LetterButtonLayout [private] |
Definition at line 36 of file HangmanWidget.h.
std::vector<WPushButton *> HangmanWidget::LetterButtons [private] |
Definition at line 37 of file HangmanWidget.h.
const unsigned int HangmanWidget::MaxGuesses [private] |
Definition at line 45 of file HangmanWidget.h.
WPushButton* HangmanWidget::NewGameButton [private] |
Definition at line 43 of file HangmanWidget.h.
WText* HangmanWidget::StatusText [private] |
Definition at line 41 of file HangmanWidget.h.
WText* HangmanWidget::Title [private] |
Definition at line 35 of file HangmanWidget.h.
std::wstring HangmanWidget::User [private] |
Definition at line 49 of file HangmanWidget.h.
std::wstring HangmanWidget::Word [private] |
Definition at line 48 of file HangmanWidget.h.
WContainerWidget* HangmanWidget::WordContainer [private] |
Definition at line 40 of file HangmanWidget.h.
std::vector<WText *> HangmanWidget::WordLetters [private] |
Definition at line 42 of file HangmanWidget.h.