View Full Version: My Guess The Number Game

C++ Learning Community > C++ Creations > My Guess The Number Game


Title: My Guess The Number Game


ookooa - August 10, 2004 08:20 PM (GMT)
This is the guess the number game I have made to day.
CODE

/*
Name: Guess The Number
Copyright:    

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  http://www.opensource.org/licenses/gpl-license.php
 
Author: (omited)
Compiler: MinGW 3.3.1
Date: 10/08/04 20:08
Description: I think the title explanes it. If you find any bugs
repot them to (omited)
*/

#include <cstdlib>
#include <ctime>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void start();
void result();

int Life; // Number of lifes
int RandNoR; // Random Number Range
int RandNo; // Random Number

void start()
{
  cout << "1. Hard" << endl;
  cout << "2. Easy" << endl;
  cout << "Any other key to Exit" << endl;
 
  int InputM; // Input Mode
  cin >> InputM;
  cout << endl;
 
  switch(InputM)
  {
      case 1 :
          Life = 5;
          break;
      case 2 :
          Life = 8;
          break;
      default :
          exit(0);
          break;
  }
 
  cout << "1. Guess between (1-15)" << endl;
  cout << "2. Guess between (1-30)" << endl;
  cout << "3. Guess between (1-40)" << endl;
  cout << "Any other key to Exit" << endl;
 
  int InputGR; // Input Guess Range
  cin >> InputGR;
  cout << endl;
 
  switch(InputGR)
  {
      case 1 :
          RandNoR = 16;
          break;
      case 2 :
          RandNoR = 31;
          break;
      case 3 :
          RandNoR = 41;
          break;
      default:
          exit(0);
          break;
  }
 
  srand(static_cast<unsigned>(time(0)));
  RandNo = (1 + rand() % RandNoR);
 
  result();
}

void result()
{
  int InputG; // Input Guess
 
  cout << Life << " life's left" << endl;
  cout << "Guess the Number: ";
  cin >> InputG;
  cout << endl;
 
  while((InputG != RandNo) && (Life != 0))
  {
      if(InputG < RandNo)
      {
          cout << "To Small" << endl;
      }
      else if(InputG > RandNo)
      {
          cout << "To Big" << endl;
      }
             
      Life--; // Life - 1
      cout << Life << " life's left" << endl;
      cout << "Guess the Number: ";
      cin >> InputG;
      cout << endl;
  }
  if(Life == 0)
  {
      cout << "Sorry you lost." << endl << endl;
      start();
  }
  else if(InputG == RandNo)
  {
      cout << "YOU WON!" << endl << endl;
      start();
  }
}

int main(int argc, char *argv[])
{
  cout << "Guess the number." << endl;
  cout << "You have a limmited numbe of lifes if you run" << endl;
  cout << "out of lifes you have lost" << endl << endl;
 
  start();
  return(0);
}    

Please comment (bad) and good about the code.
What improvment can I make?
Any errors you think you see?
etc. etc.

~ookooa

c++ - August 23, 2004 12:31 AM (GMT)
Nice little program, I found one spelling mistake...

You have a limmited numbe of lifes if you run

should be

You have a limmited number of lifes if you run

Dragon - August 23, 2004 02:06 AM (GMT)
:lol: Does that really matter?

c++ - August 25, 2004 10:47 PM (GMT)
QUOTE (Dragon @ Aug 23 2004, 02:06 AM)
:lol: Does that really matter?

no not really :D

ookooa - August 26, 2004 09:47 AM (GMT)
There was a little bug in that previous code, ave got a better version now, with a few added fetures.
CODE
/*
 Name: Guess The Number
 Copyright:    

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
 Author: omited
 Compiler: MinGW 3.3.1
 Date: 11/08/04 00:14
 Description: I think the title explanes it. If you find any bugs
 report them to omited
*/

// Header files
#include <cstdlib>
#include <ctime>
#include <iostream>

// Namespaces
using std::cout;
using std::cin;
using std::endl;

// Functions
void Start();
void Result();
void Help();
void HowToUse();
void Contact();

// Global variables
int Life; // Number of lifes
int RandNoR; // Random Number Range
int RandNo; // Random Number

// Stats class
class Stats
{
   private:
       int Win; // Keep record of wins
       int Lose; // Keep record of losses
   public:
       int Wins()
       {
           Win++; // Add 1 to int Win
           cout << "You have won " <<  Win << " games." << endl; // Disply wins
           cout << "And you have lost " << Lose << " games." << endl << endl; // Disply losses
       }
       int Lost()
       {
           Lose++; // Add 1 to int Lose
           cout << "You have won " <<  Win << " games." << endl; // Disply wins
           cout << "And you have lost " << Lose << " games." << endl << endl; // Disply losses
       }
};    

Stats Player;

// START FUNCTION
void Start()
{
   cout << "1. Hard" << endl;
   cout << "2. Easy" << endl;
   cout << "3. Help" << endl;
   cout << "Any other key to Exit" << endl;
   
   int InputM; // Input Mode
   cin >> InputM;
   cout << endl;
   
   switch(InputM)
   {
       case 1 :
           Life = 5;
           break;
       case 2 :
           Life = 8;
           break;
       case 3 :
           Help();
           break;
       default :
           exit(0);
           break;
   }
   
   cout << "1. Guess between (1-15)" << endl;
   cout << "2. Guess between (1-30)" << endl;
   cout << "3. Guess between (1-40)" << endl;
   cout << "Any other key to Exit" << endl;
   
   int InputGR; // Input Guess Range
   cin >> InputGR;
   cout << endl;
   
   switch(InputGR)
   {
       case 1 :
           RandNoR = 15;
           break;
       case 2 :
           RandNoR = 30;
           break;
       case 3 :
           RandNoR = 40;
           break;
       default:
           exit(0);
           break;
   }
   
   srand(static_cast<unsigned>(time(0)));
   RandNo = (1 + rand() % RandNoR);
   
   Result();
}

// RESULT FUNCTION
void Result()
{
   int InputG; // Input Guess
   
   cout << Life << " life's left" << endl;
   cout << "Guess the Number: ";
   cin >> InputG;
   cout << endl;
   
   while((InputG != RandNo) && (Life != 0))
   {
       if((InputG < 1) || (InputG > RandNoR))
       {
           cout << "That number is not between 1 and " << RandNoR << endl;
           cout << "Try again" << endl;
           Result();
       }
       else
       {    
           if(InputG < RandNo)
           {
               cout << "To Small" << endl;
           }
           else if(InputG > RandNo)
           {
               cout << "To Big" << endl;
           }
               
           Life--;
           cout << Life << " life's left" << endl;
           cout << "Guess the Number: ";
           cin >> InputG;
           cout << endl;
       }    
   }
   
   if(Life == 0)
   {
       cout << "Sorry you lost." << endl << endl;
       Player.Lost();
       Start();
   }
   else if(InputG == RandNo)
   {
       cout << "YOU WON!" << endl << endl;
       Player.Wins();
       Start();
   }
}

// HELP FUNCTIO
void Help()
{
   cout << "This game  is fairly simple. In the menus you pick what" << endl;
   cout << "you want and enter the corosponding number beside it" << endl;
   cout << "to chose it." << endl << endl;
   cout << "First you need to pick if you want it hard or easy," << endl;
   cout << "hard gives you 5 lives and easy gives you 8 lives." << endl << endl;
   cout << "You then pick the range of numbers you want. (1-15) " << endl;
   cout << "will let you guess a number from 1 to 15 and (1-30) will" << endl;
   cout << "let you guess a number from 1 to 30 etc." << endl << endl;
   
   Start();
}

// MAIN FUNCTION
int main(int argc, char *argv[])
{
   cout << "Guess the number." << endl << endl;
   
   Start();
   return(0);
}      

Kyro Shingami - October 15, 2004 11:26 PM (GMT)
I may be a little too late... but...

You have a limmited numbe of lifes if you run

should be

You have a limmited number of lifes if you run

Even the correct seems wrong O.o
You have a limited number of lives if you run




* Hosted for free by InvisionFree