=)
I am new here and I am working on a simple school final project and I am having a hard time getting in touch with my teacher. The code isn't showing any errors but isn't working the way I want it to, i'll give a simple run down as to what I want it to do and I will post the code too!! =) Ok, for my project, I want two players to take turns guessing a random number within a certain range and the first to get it right wins. So it will be alternating between the players when they get the guess wrong and I am having troubles with my random number generator as well, it seems like it is giving the same number, I am stuck with how to set it up and where to go to make it work, I thought I had a good set up going but I definitely need help =(
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
//Need players to take turns
//Guess numbers until one is correct
//Declare Functions
void description();
string Player1(string name1);
string Player2(string name2);
int playerOneTurn();
int playerTwoTurn();
int randomNumber = rand() % 50 + 1;
bool isPlayerOneTurn = true; //Global Variable
bool isPlayerTwoTurn = true; //Global Varibale
int main()
{
srand((unsigned)time(0));
int randomNumber = rand() % 50 + 1;
cout << randomNumber;
description();
Player1("name1");
Player2("name2");
playerOneTurn();
playerTwoTurn();
}
//Display instructions for players on how the game works and tips to win
void description()
{
cout << "\tWelcome to What is the Number!!\n";
cout << "In this game, you and a friend will take turns\n";
cout << "guessing a random number between 1 & 50.\n";
cout << "You will continue this until one of you gets it right.\n";
cout << "If you guess a number higher then the random, it will tell you to go lower...\n";
cout << "If you guess a number lower then the random, it will tell you to go higher....\n";
cout << "Tip: It's better to start at the midway point to get a sense of where in the range it
is.\n\n";
}
//Set's name variable and asks for input from player to type name and then returns name back
string Player1(string name1)
{
string name;
string& First = name;
cout << "Who will be Player 1?\n";
cout << "Enter Name: ";
cin >> First;
cout << "Welcome " << First << ".\n\n";
return name1;
}
//Set's name variable and asks for input from player to type name and then returns name back
string Player2(string name2)
{
string name;
string* Second;
Second = &name;
cout << "Who will be Player 2?\n";
cout << "Enter Name: ";
cin >> *Second;
cout << "Welcome " << *Second << ".\n\n";
return name2;
}
//Set's up Player1's Turn and if it is wrong, passes it to Player2
int playerOneTurn()
{
int guess;
cout << "Player 1, take your guess!!\n";
cin >> guess;
if (isPlayerOneTurn == true && isPlayerTwoTurn == false)
{
if (guess < randomNumber)
{
cout << "Close, try to guess higher!!\n";
isPlayerOneTurn == false && isPlayerTwoTurn == true;
}
else if (guess > randomNumber)
{
cout << "Close, try to guess lower!!\n";
isPlayerOneTurn == false && isPlayerTwoTurn == true;
}
else if (guess == randomNumber)
{
cout << "YOU GOT IT!! CONGRATS!!\n";
}
else
{
cout << "Incorrect Input....\n";
isPlayerOneTurn == false && isPlayerTwoTurn == true;
}
}
return guess;
}
//Sets Player2's Turn and if it is wrong, loops it back to Player1
int playerTwoTurn()
{
int guess;
cout << "Player 2, take your guess!!\n";
cin >> guess;
if (isPlayerTwoTurn == true && isPlayerOneTurn == false)
{
if (guess < randomNumber)
{
cout << "Close, try to guess higher!!\n";
isPlayerTwoTurn == false && isPlayerOneTurn == true;
}
else if (guess > randomNumber)
{
cout << "Close, try to guess lower\n";
isPlayerTwoTurn == false && isPlayerOneTurn == true;
}
else if (guess == randomNumber)
{
cout << "YOU GOT IT!! CONGRATS!!\n";
}
else
{
cout << "Incorrect Input..\n";
isPlayerTwoTurn == false && isPlayerOneTurn == true;
}
}
return guess;
}
Aucun commentaire:
Enregistrer un commentaire