home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / asm / NumGuess.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.3 KB  |  137 lines

  1. /*=====================================================================
  2.   File:      NumGuess.cpp
  3.  
  4.   Summary:   Brief summary of the file contents and purpose.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft NGWS SDK Code Samples.
  8.  
  9.   Copyright (C) 1999 Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20.  
  21. #using <mscorlib.dll>
  22. using namespace System;
  23. using namespace System::Globalization;
  24.  
  25. __gc class NumGuess
  26. {
  27. private:
  28.     int            MagicNumber;    // A field
  29.     static int     NumGuesses;    // A static field.
  30.  
  31. public:
  32.     NumGuess()    // A constructor
  33.     {
  34.         NumGuesses = 0;
  35.         MagicNumber = -1;
  36.         Console::WriteLine(L"\nGame Initialized!\n");
  37.         Console::WriteLine(L"The goal is to guess the Magic Number.");
  38.         Console::WriteLine(L"This will be a number between 1 and 100");
  39.         Console::WriteLine(L"I will tell you either higher or lower.");
  40.         Console::WriteLine(L"Have Fun!\n\n");
  41.     }
  42.     
  43.     bool Guess()    // A non-virtual function.
  44.     {
  45.         Int32 guess;
  46.         String *input;
  47.         bool retval = false;
  48.         int num;
  49.  
  50.         for (;;) {
  51.             Console::Write(L"Whats your guess? --> ");
  52.         
  53.             //get user input
  54.             input = Console::ReadLine();
  55.  
  56.             try {
  57.                 num = Int32::Parse(input, NumberStyles::Integer);
  58.             }
  59.             catch (Exception * e) {
  60.                 Console::WriteLine(L"Please enter a number.\n");
  61.                 continue;
  62.             }
  63.             break;
  64.         }
  65.  
  66.         //compare result and print message
  67.         if(num == MagicNumber)
  68.         {
  69.             Console::WriteLine(L"You Guessed er Chester!\n");
  70.             retval = true;
  71.         }
  72.         else if(num > MagicNumber)
  73.         {
  74.             Console::WriteLine(L"\tToo High\n");
  75.             retval = false;
  76.         }
  77.         else if(num < MagicNumber)
  78.         {
  79.             Console::WriteLine(L"\tToo Low\n");
  80.             retval = false;
  81.         }
  82.         NumGuesses ++;
  83.         return retval;
  84.     }
  85.  
  86.     bool PlayAgain()    // A non-virtual function.
  87.     {
  88.         String *input;
  89.  
  90.         Console::WriteLine(L"\nPlay Again (Y/N)?");
  91.         input = Console::In->ReadLine();
  92.         if(input->Equals(L"Y") || input->Equals(L"y"))
  93.         {
  94.             return true;
  95.         }
  96.         else
  97.         {
  98.             return false;
  99.         }
  100.     }
  101.     
  102.     virtual void GenNewNum()    // A virtual function.
  103.     {
  104.         //generate a random number
  105.         Random    *r = new Random();
  106.         MagicNumber = r->Next(1, 100);
  107.         //reset guesses
  108.         NumGuesses = 0;
  109.     }
  110.     
  111.     static void PrintGuesses()    // Static method.
  112.     {
  113.         Console::Write(NumGuesses);
  114.     }
  115. };
  116.  
  117. void main(void)    //entrypoint
  118. {
  119.     NumGuess    *ng;
  120.     //Create a new NumGuess object
  121.     ng = new NumGuess();
  122.     //Run game until NumGuess::PlayAgain() returns false
  123.     do{
  124.         //Generate a new random number
  125.         ng->GenNewNum();
  126.         //Run loop until guess is correct
  127.         while(!ng->Guess());
  128.         //Print guesses
  129.         Console::Write(L"It took you ");
  130.         ng->PrintGuesses();
  131.         Console::WriteLine(L" guesses.");
  132.     }
  133.     //do you want to play again?
  134.     while(ng->PlayAgain());
  135. }
  136.  
  137.