Guessing game

Enhance guessing game

Page 1 of 1

1 Replies - 3296 Views - Last Post: 07 January 2009 - 07:23 AM Rate Topic: -----

#1 stephanos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 05-January 09

Guessing game

Post icon  Posted 07 January 2009 - 06:35 AM

#include <iostream>
#include <stdlib.h>
#include <time.h>

void player1();
void player2();
char p1 = 'n';
char p2 = 'n';
int ip1 = 0;
int ip2 = 0;

using namespace std;

void main()
{
char option;

do{
cout << "It is Player 1's turn!" << endl;
player1();
system("cls");
player2();
cout << "It is Player 2's turn!" << endl;
system("cls");
if(ip1>ip2)
cout << "Player 2 is the winner!!" << endl;
else if(ip2>ip1)
cout << "Player 1 is the winner!!" << endl;
else
{}
cout << "Do you want to play again? (y/n): ";
cin >> option;
system("cls");
}while(option == 'y');
}
void player1()
{
long secretnumber = 0, user_number = 0;
const long MAX = 1000, MIN = 1;

srand(time(NULL));
secretnumber = (rand() % (MAX - MIN + 1)) + MIN;

cout << "Try to guess the number I have from 1 to 1000" << endl << endl;

while((ip1<10) || (p1 == 'y'))
{
cout<<"PLease enter a number : ";
cin >> user_number;

if (secretnumber > user_number)
{
cout << "The number is too small,try again" << endl << endl;
}
else if (secretnumber < user_number)
{
cout << "The number is too big,try again" << endl << endl;
}
else if (secretnumber = user_number)
{
cout << "CONGRATULATIONS!You guessed the number"<< secretnumber <<" in " << ip1 << " attempts!" << endl << endl;
p1 = 'y';
}
else if (ip1 == 10)
{
cout << "YOU LOST. Did not guess the number "<< secretnumber <<" in 10 attempts!" << endl;
}
ip1++;
}
cout << "Next up is player 2" << endl;
system("pause");

}
void player2()
{
long secretnumber = 0, user_number = 0;
const long MAX = 1000, MIN = 1;

srand(time(NULL));
secretnumber = (rand() % (MAX - MIN + 1)) + MIN;

cout << "Try to guess the number I have from 1 to 1000" << endl << endl;

while((ip2<10) || (p2 == 'y'))
{
cout<<"PLease enter a number : ";
cin >> user_number;

if (secretnumber > user_number)
{
cout << "The number is too small,try again" << endl << endl;
}
else if (secretnumber < user_number)
{
cout << "The number is too big,try again" << endl << endl;
}
else if (secretnumber = user_number)
{
cout << "CONGRATULATIONS!You guessed the number"<< secretnumber <<" in " << ip2 << " attempts!" << endl << endl;
p2 = 'y';
}
else if (ip2 == 10)
{
cout << "YOU LOST. Did not guess the number "<< secretnumber <<" in 10 attempts!" << endl;
}
ip2++;
}
system("pause");

}





HOW DO I MAKE THIS GAME TO HAVE TWO PLAYERS?I.E. THE PLAYER 1 MUST DO UP 10 ATTEMPS OR LESS IF CORRECT,AND THEN THE PLAYER 2 MUST DO THE SAME.AFTER THAT,THE GAME MUST SHOW ON SCREEN ALL ATTEMPS PLAYER 1 DID AND ALL ATTEMPS PLAYER 2 DID AND SAY THAT THE PLAYER WITH THE LEAST ATTEMPS WINS.ALSO THE PROGRAM MUST ASK FOR THE NAMES OF THE TWO PLAYERS IN THE BEGINING.I MANAGED TO DO THIS PROGRAM BUT I HAVE THIS DIFFICULTY.PLEASE HELP!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Guessing game

#2 Linkowiezi   User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: Guessing game

Posted 07 January 2009 - 07:23 AM

First off: Please post your code in between [ code ]code goes here[ /code ].

And as a sulution to your problem(without having actually read trough the code that much since I'm at work and don't have much time and because it wasn't in code tags) you could use 'player' as an object array.
Ex:
class PlayerClass{
  //  Put player variables and functions here
  //  example:
  public:
    void doSomething();
};

and in your main function do something like this:
const int NUMBEROFPLAYERS = 2;
PlayerClass player[NUMBEROFPLAYERS];
int x = 0;
while( notDone ){
  //  Have the player do stuff within the loop
  player[x].doSomething();
  x++;
  if( x == NUMBEROFPLAYERS )
    x = 0;
}


You might have to read up on classes wich you can do here http://www.cplusplus...al/classes.html

Hope this was of any help to you :)

This post has been edited by Linkowiezi: 07 January 2009 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1