Source code of the Hangman example

Browse below the source code for Wt's Hangman example.

  • hangman
    • class Dictionary
      • Dictionary.C
        • Dictionary.h
        • class HangmanGame
          • class HangmanWidget
            • class HighScoresWidget
              • class ImagesWidget
                • class LettersWidget
                  • class Session
                    • class User
                      • class WordWidget
                        • hangman.C
                          • strings.xml
                            • strings_ru.xml
                              • templates.xml
                                • templates_ru.xml
                                  • wt_config.xml
                                    • icons
                                      • css
                                      /* 
                                       * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
                                       *
                                       * See the LICENSE file for terms of use.
                                       */
                                      
                                      #include <Wt/WApplication>
                                      #include <Wt/WStringUtil>
                                      
                                      #include "Dictionary.h"
                                      #include <fstream>
                                      #include <iostream>
                                      #include <time.h>
                                      #include <stdlib.h>
                                      
                                      std::wstring RandomWord(Dictionary dictionary)
                                      {
                                         std::ifstream dict;
                                         if (dictionary == DICT_NL) {
                                           dict.open((Wt::WApplication::appRoot() + "dict-nl.txt").c_str());
                                         } else { // english is default
                                           dict.open((Wt::WApplication::appRoot() + "dict.txt").c_str());
                                         }
                                            
                                         std::string retval;
                                         int numwords = 0;
                                         while(dict) {
                                            getline(dict, retval);
                                            numwords++;
                                         }
                                         dict.clear();
                                         dict.seekg(0);
                                      
                                         srand(time(0));
                                         int selection = rand() % numwords; // not entirely uniform, but who cares?
                                      
                                         while(selection--) {
                                            getline(dict, retval);
                                         }
                                         getline(dict, retval);
                                         for(unsigned int i = 0; i < retval.size(); ++i)
                                            if(retval[i] < 'A' || retval[i] > 'Z')
                                      	 std::cout << "word " << retval 
                                      		   << " contains illegal data at pos " << i << std::endl;
                                      
                                         return Wt::widen(retval);
                                      }