home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09963.iso / strategy / hangman.zip / Hangman.cpp < prev    next >
C/C++ Source or Header  |  1996-01-15  |  6KB  |  234 lines

  1. //  CHangman.cpp
  2. //
  3. //  Jasen N. Tenney
  4. //    21 December 1995
  5. //  
  6. //  Functions for the CHangman class.
  7. //
  8. #include "stdafx.h"
  9. #include "Hangman.h"
  10.  
  11. CHangman::CHangman()
  12. {
  13.     // Do initialization here
  14.     m_CurrentDictionary.RemoveAll();
  15.     m_CurrentDictionary.SetSize( 7000, 2000);    // Set initial size and grow by size
  16.     srand( (unsigned)time( NULL ) );            // Seed random number generator
  17.     m_NumValidGuesses = 10;                        // Set number of valid guesses
  18.     Reset();                                    
  19.  
  20. CHangman::~CHangman()
  21. {
  22. }
  23.  
  24. void CHangman::LoadNewWord()
  25. {
  26.     UINT Index;
  27.  
  28.     Index                = rand() % m_WordCount;    // Get index of new word
  29.     m_CurrentWord        = m_CurrentDictionary.GetAt(Index);    // Load new word
  30.     m_CurrentGuess        = InitCurrentGuess();    // Initialize current guess "_ _..."
  31.     m_GuessesRemaining    = m_NumValidGuesses;    // Reset guesses remaining
  32.     m_TotalGuesses        = 0;                    // Total guesses so far
  33. }
  34.  
  35. BOOL CHangman::CheckLetter(char Letter)
  36. {
  37.     BOOL LetterAdded   = FALSE;
  38.     int Size  =0;
  39.     int Index =0;
  40.  
  41.     Size = m_CurrentWord.GetLength();    // Get length of current word
  42.     for(Index=0; Index<Size; Index++)    // Step through word to check        
  43.     {
  44.         if( m_CurrentWord[Index] == Letter )    // If we hit a letter then
  45.         {
  46.             m_CurrentGuess.SetAt( (Index*2), Letter);    // Set current guess to
  47.             LetterAdded = TRUE;                            // letter and change bool
  48.         }
  49.     }
  50.     if( LetterAdded == FALSE )
  51.         DecrementGuessRemain();        // If no letter added decrement guesses remaining
  52.  
  53.     IncrementTotalGuesses();        // Increment total guesses so far
  54.     return( LetterAdded );            // Return TRUE/FALSE if letter added or not
  55. }
  56.  
  57. UINT CHangman::GetGamesPlayed()
  58. {
  59.     return( m_GamesPlayed );        // Return number of games played so far
  60. }
  61.  
  62. UINT CHangman::GetGamesWon()
  63. {
  64.     return( m_GamesWon );            // Return number of games won so far
  65. }
  66.  
  67. UINT CHangman::GetPercentage()
  68. {
  69.     if( m_GamesPlayed != 0 )        // Only do divide if games played not zero
  70.         m_Percentage = (UINT)(((float)m_GamesWon / (float)m_GamesPlayed) * 100);
  71.     return( m_Percentage );            // Return the percentage
  72. }
  73.  
  74. CString CHangman::InitCurrentGuess()
  75. {
  76.     int Index = 0;
  77.     CString Temp;                    // Use a temporary string
  78.  
  79.     for(Index=0; Index < m_CurrentWord.GetLength(); Index++)
  80.         if( Index == ( m_CurrentWord.GetLength() - 1 ) )
  81.             Temp += "_";    // If last the just put "_" and not "_ "
  82.         else
  83.             Temp += "_ ";    // Make string look like "_ _ _ _"
  84.     
  85.     return( Temp );            // Return it to calling function
  86. }
  87.  
  88. CString CHangman::GetCurrentGuess()
  89. {
  90.     return( m_CurrentGuess );    // Return the current # of guesses so far
  91. }
  92.  
  93. void CHangman::Reset()
  94. {
  95.     m_GamesPlayed        = 0;    // Reset all variables to reflect new game
  96.     m_GamesWon            = 0; 
  97.     m_Percentage        = 0;
  98. }
  99.  
  100. UINT CHangman::GetGuessRemain()
  101. {
  102.     return( m_GuessesRemaining );    // Return the # of guesses remaining
  103. }
  104.  
  105. BOOL CHangman::CheckWordCompleted()
  106. {
  107.     BOOL WordCompleted = TRUE;
  108.     int Size  =0;
  109.     int Index =0;
  110.  
  111.     Size = m_CurrentWord.GetLength();    // Get length of current word
  112.     for(Index=0; Index<Size; Index++)
  113.     {
  114.         if( m_CurrentWord[Index] != m_CurrentGuess.GetAt( Index*2 ) )
  115.             WordCompleted = FALSE;    // If all letter do not match then not completed
  116.     }
  117.     return( WordCompleted );    // Return TRUE/FALSE if guess is completed or not
  118. }
  119.  
  120. void CHangman::IncrementGamesWon()
  121. {
  122.     m_GamesWon++;                // Increment # of games won
  123. }
  124.  
  125. void CHangman::IncrementGamesPlayed()
  126. {
  127.     m_GamesPlayed++;            // Increment # of games played
  128. }
  129.  
  130. CString CHangman::GetHintLetter()
  131. {
  132.     CString Letter;
  133.     int Size  =0;
  134.     int Index =0;
  135.  
  136.     Size = m_CurrentWord.GetLength();
  137.     for(Index=0; Index<Size; Index++)
  138.          if( m_CurrentGuess.GetAt( Index*2 ) == '_' )
  139.         {
  140.             Letter = m_CurrentWord.GetAt( Index );    // On first '_' return the
  141.             break;                                    // letter that should be there
  142.         }
  143.     return( Letter );
  144. }
  145.  
  146. void CHangman::DecrementGuessRemain()
  147. {    
  148.     if( m_GuessesRemaining > 0 )    // As long as guesses remaining is greater than
  149.         m_GuessesRemaining--;        // zero you can subtract from it
  150. }
  151.  
  152. CString CHangman::GetCurrentWord()
  153. {
  154.     return( m_CurrentWord );        // Return current word
  155. }
  156.  
  157. void CHangman::IncrementTotalGuesses()
  158. {
  159.     m_TotalGuesses++;                // Increment # of total guesses so far
  160. }
  161.  
  162. UINT CHangman::GetTotalGuesses()
  163. {
  164.     return( m_TotalGuesses );        // Return total guesses so far
  165. }
  166.  
  167. BOOL CHangman::LoadDictionary()
  168. {
  169.     CStdioFile f;
  170.     CFileException exception;
  171.     BOOL openStatus = FALSE;
  172.     BOOL fileStatus = TRUE;
  173.     char* pFileName = "hangman.dic";
  174.     UINT Index = 0;
  175.  
  176.     // open file for reading
  177.     openStatus = f.Open(pFileName,
  178.             CFile::modeRead, &exception);
  179.     if (!openStatus)
  180.     {
  181.         CString strMsg;
  182.         char tmpStr[30];
  183.  
  184.         _itoa( exception.m_cause, tmpStr, 10 );
  185.         strMsg =  "Error opening data file. Code: ";
  186.         strMsg += tmpStr;
  187.         strMsg += ".";
  188.         MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
  189.         return( FALSE );
  190.     } 
  191.     else
  192.     {
  193.         CString tmpStr;
  194.         do
  195.         {
  196.             try
  197.             {
  198.                 fileStatus = f.ReadString(tmpStr);
  199.                 if(fileStatus == TRUE)
  200.                 {
  201.                     m_CurrentDictionary.SetAt(Index,tmpStr);
  202.                     Index++;
  203.                 }
  204.                 
  205.             }
  206.             catch (CFileException exception)
  207.             {
  208.                 CString strMsg;
  209.                 char tmpStr[30];
  210.  
  211.                 _itoa( exception.m_cause, tmpStr, 10 );
  212.                 strMsg =  "Error reading data file. Code: ";
  213.                 strMsg += tmpStr;
  214.                 strMsg += ".";
  215.                 MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
  216.                 return( FALSE );
  217.             }
  218.         }
  219.         while (fileStatus == TRUE); 
  220.         f.Close();
  221.     }
  222.     m_WordCount = Index;
  223.     return( TRUE );
  224. }
  225.  
  226. void CHangman::LoadErrorDictionary()
  227. {
  228.     UINT Index;
  229.     for(Index= 0; Index<100; Index++)    // Upon error load dictionary to this
  230.        m_CurrentDictionary.SetAt(Index,"JASENNTENNEY");
  231.      m_WordCount=Index;
  232. }
  233.