0
I need help to finish this simple Guessing number game (The source code is on my profile and comments )
Hello, I'm learning c++, and I'm making a simple guessing game, its working for 1 player, but I would like to know how can I make it for 2 players, I want to make challenge between two players and the winner is who took less guesses,my question is How can I make that without repeating the whole code for the player1, and also how can I change the number being guessed once it will be the same as long as the program is running..
1 Odpowiedź
0
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
//A SIMPLE GUESSING GAME BY MASTER_NAOZI
using namespace std;
short int play1GuessedTime, play2GuessedTime = 0;
int main()
{
unsigned int play1Guessed, play2Guessed,
numberGuessed;
string player1, player2;
srand((unsigned int)time(0));//TO FEED A REAL RANDOM NUMBERS
numberGuessed = rand() % (100-1+1)+1;
cout << "THE PLAYERS HAVE TO PUT THE CODE TO OPEN THE DOOR!\n";
cout << "PLEASE INPUT THE NAME OF PLAYER 1\n";
getline(cin, player1);
cout << "PLEASE INPUT THE NAME OF PLAYER 2\n";
getline(cin, player2);
cout << "THE CODE IS A NUMBER BETWEEN 1 AND 100.\n";
cout <<player1<<" PLEASE INPUT THE CODE!\n";
cin >>play1Guessed;
while (play1Guessed!= numberGuessed)
{
if (numberGuessed > play1Guessed)
{
cout << "THE NUMBER IS TOO LOW\n";
}
else
{
cout << "THE NUMBER IS TOO HIGH\n";
}
cout << "WRONG CODE PLEASE INPUT AGAIN\n";
cin >> play1Guessed;
play1GuessedTime++;//I dont know why its only working when i declarate as global variable
}
cout << "CONGRATULATIONS "<<player1<<", YOU UNLOCKED THE DOOR, THE NUMBER IS: "<<numberGuessed;
/*I NEED NOW TO ASK THE PLAYER 2 TO PLAY AND AT THE END OF THE GAME
THE WINNER IS WHO TOOK LESS TIME GUESSING THE RIGHT NUMBER*/
return 0;
}