学校 C++ 项目

C++ Project for School

=)

我是新来的,我正在做一个简单的学校期末项目,我很难与我的老师取得联系。代码没有显示任何错误,但没有按照我想要的方式工作,我将简单地 运行 说明我想要它做什么,我将 post 代码也!! =) 好的,对于我的项目,我希望两个玩家轮流猜测一定范围内的随机数,第一个猜对的人获胜。所以当玩家猜错时,它会在玩家之间交替,我的随机数生成器也有问题,它似乎给出了相同的数字,我不知道如何设置它和去哪里为了让它发挥作用,我认为我的设置很好,但我确实需要帮助 =(

#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;
}
void playerOneTurn()
{
    int guess;
    cout << "Player 1, take your guess!!\n";
    cin >> guess;

    if (guess == randomNumber)
    {
        cout << "YOU GOT IT!! CONGRATS!!\n";
    }
    else
    {
        if (guess < randomNumber)
            cout << "Close, try to guess higher!!\n";
        else if (guess > randomNumber)
            cout << "Close, try to guess lower!!\n";
        playerTwoTurn();
    }
}

void playerTwoTurn()
{
    int guess;
    cout << "Player 1, take your guess!!\n";
    cin >> guess;

    if (guess == randomNumber)
    {
        cout << "YOU GOT IT!! CONGRATS!!\n";
    }
    else
    {
        if (guess < randomNumber)
            cout << "Close, try to guess higher!!\n";
        else if (guess > randomNumber)
            cout << "Close, try to guess lower!!\n";
        playerOneTurn();
    }
}

试试下面的代码:

#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_name;
string player2_name;

void player1_getName();
void player2_getName();

int playerOneTurn();
int playerTwoTurn();

int randomNumber;

int main()
{
    srand((unsigned)time(0));
    randomNumber = rand() % 50 + 1;

    description();

    player1_getName();
    player2_getName();

    while(1)
    {
        if(playerOneTurn() || playerTwoTurn())
        {
            break;
        }
    }
}


//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
void player1_getName()
{
    cout << "Who will be Player 1?\n";
    cout << "Enter Name: ";
    cin >> player1_name;
    cout << "Welcome " << player1_name << ".\n\n";
}

//Set's name variable and asks for input from player to type name and then returns name back
void player2_getName()
{
    cout << "Who will be Player 2?\n";
    cout << "Enter Name: ";
    cin >> player2_name;
    cout << "Welcome " << player2_name << ".\n\n";
}

//Set's up Player1's Turn and if it is wrong, passes it to Player2
int playerOneTurn()
{
    int guess;
    cout << player1_name<<", take your guess!!\n";
    cin >> guess;

    if (guess < randomNumber)
    {
        cout << "Close, try to guess higher!!\n\n";
        return 0;
    }
    else if (guess > randomNumber)
    {
        cout << "Close, try to guess lower!!\n\n";
        return 0;
    }
    else if (guess == randomNumber)
    {
        cout <<"\t"<<player1_name<< ", YOU GOT IT!! CONGRATS!!\n\n";
        return 1;
    }
}


//Sets Player2's Turn and if it is wrong, loops it back to Player1
int playerTwoTurn()
{
    int guess;
    cout << player2_name<<", take your guess!!\n";
    cin >> guess;

    if (guess < randomNumber)
    {
        cout << "Close, try to guess higher!!\n\n";
        return 0;
    }
    else if (guess > randomNumber)
    {
        cout << "Close, try to guess lower!!\n\n";
        return 0;
    }
    else if (guess == randomNumber)
    {
        cout <<"\t"<< player2_name<< ", YOU GOT IT!! CONGRATS!!\n\n";
        return 1;
    }
}

下面的代码使用了一些我确定您还没有涉及的内容。

这是为了避免copy/paste/submit的诱惑。

它应该对您仍然有价值,因为它将演示如何让游戏在玩家之间来回切换,直到做出正确的猜测。它还消除了重复的代码。最后,它展示了如何使用 <random> 生成随机数的更惯用的 C++ 方法。

#include <iostream>
#include <random>
#include <string>

/*
 * Function to prompt the user and store the result; does no bad input checking
 *
 * Return: val is an output parameter, stores inputted value
 *
 * Parameters:
 *   prompt: the string to show the user so they know what to type
 *   val: the variable that will hold the user input
 */
template <typename T>
void prompt(std::string prompt, T& val) {
  std::cout << prompt << ": ";
  std::cin >> val;
}

/*
 * Function to take a player's turn and provide feedback on their guess
 * Performs no bad input checking
 *
 * Return: guessed value
 *
 * Parameters:
 *   player: indicates the player number
 */
int take_turn(int player) {
  int guess;
  prompt("\nReady Player " + std::to_string(player) + "! Your guess", guess);

  return guess;
}

int main() {
  // It's more lines, but it's the preferred C++ way of getting a random number
  // It separates the random number generation from the desired output
  // Meaning prng could also be used to fill a normal distribution or a poisson
  // or etc.
  std::mt19937 prng(std::random_device{}());
  std::uniform_int_distribution<int> range(1, 50);
  int secret = range(prng);
  bool playerOneTurn = true;

  // This is just "copy protection" so that this code doesn't get blindly
  // copy/pasted and turned in. It's a lambda that will tell the loop
  // whose turn it is
  auto active_player = [&playerOneTurn]() { return playerOneTurn ? 1 : 2; };

  // Cheating for testing purposes
  std::cout << "Psst! " << secret << '\n';

  // The loop that ensures the game goes on until someone guesses correctly
  while (1) {
    int guess = take_turn(active_player());

    if (guess == secret) {
      std::cout << "Player " << active_player() << " wins!\n";
      break;  // Debated directly returning here.
    } else if (guess < secret) {
      std::cout << "Guess higher\n";
    } else {  // Don't need to check if higher, it has to be if we got here
      std::cout << "Guess lower\n";
    }

    playerOneTurn = !playerOneTurn;
  }

  return 0;
}