home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: NumGuess.cpp
-
- Summary: Brief summary of the file contents and purpose.
-
- ---------------------------------------------------------------------
- This file is part of the Microsoft NGWS SDK Code Samples.
-
- Copyright (C) 1999 Microsoft Corporation. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Development Tools and/or on-line documentation. See these other
- materials for detailed information regarding Microsoft code samples.
-
- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
- KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- PARTICULAR PURPOSE.
- =====================================================================*/
-
- #using <mscorlib.dll>
- using namespace System;
- using namespace System::Globalization;
-
- __gc class NumGuess
- {
- private:
- int MagicNumber; // A field
- static int NumGuesses; // A static field.
-
- public:
- NumGuess() // A constructor
- {
- NumGuesses = 0;
- MagicNumber = -1;
- Console::WriteLine(L"\nGame Initialized!\n");
- Console::WriteLine(L"The goal is to guess the Magic Number.");
- Console::WriteLine(L"This will be a number between 1 and 100");
- Console::WriteLine(L"I will tell you either higher or lower.");
- Console::WriteLine(L"Have Fun!\n\n");
- }
-
- bool Guess() // A non-virtual function.
- {
- Int32 guess;
- String *input;
- bool retval = false;
- int num;
-
- for (;;) {
- Console::Write(L"Whats your guess? --> ");
-
- //get user input
- input = Console::ReadLine();
-
- try {
- num = Int32::Parse(input, NumberStyles::Integer);
- }
- catch (Exception * e) {
- Console::WriteLine(L"Please enter a number.\n");
- continue;
- }
- break;
- }
-
- //compare result and print message
- if(num == MagicNumber)
- {
- Console::WriteLine(L"You Guessed er Chester!\n");
- retval = true;
- }
- else if(num > MagicNumber)
- {
- Console::WriteLine(L"\tToo High\n");
- retval = false;
- }
- else if(num < MagicNumber)
- {
- Console::WriteLine(L"\tToo Low\n");
- retval = false;
- }
- NumGuesses ++;
- return retval;
- }
-
- bool PlayAgain() // A non-virtual function.
- {
- String *input;
-
- Console::WriteLine(L"\nPlay Again (Y/N)?");
- input = Console::In->ReadLine();
- if(input->Equals(L"Y") || input->Equals(L"y"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- virtual void GenNewNum() // A virtual function.
- {
- //generate a random number
- Random *r = new Random();
- MagicNumber = r->Next(1, 100);
- //reset guesses
- NumGuesses = 0;
- }
-
- static void PrintGuesses() // Static method.
- {
- Console::Write(NumGuesses);
- }
- };
-
- void main(void) //entrypoint
- {
- NumGuess *ng;
- //Create a new NumGuess object
- ng = new NumGuess();
- //Run game until NumGuess::PlayAgain() returns false
- do{
- //Generate a new random number
- ng->GenNewNum();
- //Run loop until guess is correct
- while(!ng->Guess());
- //Print guesses
- Console::Write(L"It took you ");
- ng->PrintGuesses();
- Console::WriteLine(L" guesses.");
- }
- //do you want to play again?
- while(ng->PlayAgain());
- }
-
-