dimanche 23 octobre 2016

Why does the computer assign 2 different random numbers when you select 2 player game mode?

Would someone please help me figure this out? I'm writing a program called The Guessing Game which tasks the player(s) with guessing the random number generated by the computer between 1-100. My problem I'm finding is that when you select two players and each player begins guessing, it turns out the computer has selected a random number for both players. I'm confused why this happens because I assign the random number to a single variable "compSelect" and test it against each player's guess. I have tried searching this question on this site, but could only find why the computer was prompting for the same player twice. However, this is not what I'm looking for.

In my output display, when player 1 and player 2 guess each other's answer each time, for example, player 1 guesses: 10, player 2 guesses: 10; player 1 guesses: 15, player 2 guesses: 15; the program eventually will say "guess lower" for player 1 and "guess higher" for player 2. Does anyone know why this is? I want both players to be guessing for the same number.

This is my code for the two player selection. I am using Code::Blocks 16.01.

#include <iostream>
#include <iomanip>
#include <cmath> //needed for pow() and ceil() ///also rand()
#include <conio.h> //needed for kbhit()
#include <windows.h> //needed for Sleep()
#include <cstdlib> //needed for srand() and rand()
#include <time.h> //needed for time()
using namespace std;

int main()
{
    int waitTime = pow(100,2); //amount of time computer will pause for
    int A, B, Y; //switch-case arguments //if-else
    int i = 0;
    int play1Guess, play2Guess, compSelect; //player guesses & computer's   selection ///while, if-else
    int numGuess1, numGuess2, numGuessSum1 = 0, numGuessSum2 = 0; //guess  counter
    int gamesPlay = 0; //games played counter
    double guessWinAvg1, guessWinAvg2; //average number of guesses it took before guessing the correct number
    char playerCount, replayGuessGame; //switch-case expression //if-else

    while (true) { //an infinite amount of runs until user selects N when prompted
    cout << "\n                          WELCOME TO THE GEUSSING GAME\n"
         << "          ------------------------------------------------------------\n\n"
         << setw(67) << "The computer will select a random number from 1-100.\n"
         << setw(53) << "Try to guess the number.\n"
         << setw(71) << "The player who guesses correctly with the fewest tries wins.\n"
         << setw(56) << "Exit the game by guessing 0.\n\n"
         << setw(47) << "Good luck!\n\n";
    system("pause"); //prompts user to continue

    cout << "\n\n  A - 1 Player"
         << "\n  B - 2 Players"
         << "\n\nHow Many Players?: ";
    cin >> playerCount; //allows user to play with friends

    switch (playerCount) { //game-play start
    case 'B':
    numGuess2 = 0; //resets guess count each instance
    gamesPlay++; //increments games played counter for player 1
    cout << "\nPlease wait while the computer selects a number . . .";
    srand(time(NULL)); //randomizes first seed int value each game
    compSelect = 1 + rand() % 100; //selects a random number between 1-100
    Sleep(waitTime); //pauses program for set amount of time
    cout << "\n\nThe computer has selected a number. You may begin guessing.\n\n\n";

    cout << "Player 1 Start: ";
    cin >> play1Guess; //stores player's guess
    numGuess1++; //counts number of guesses player takes
    while ((play1Guess != 0) || (play2Guess != 0)) { //if player guesses 0 - exits game
        if ((play1Guess > compSelect) || (play2Guess > compSelect)) {
            cout << "\nGuess Lower.\n\n\n";
        }
        else if ((play1Guess < compSelect) || (play2Guess < compSelect)) {
            cout << "\nGuess Higher.\n\n\n";
        }
        else if ((play1Guess == compSelect) || (play2Guess == compSelect)) {
            cout << "\nCorrect!";
            break; //prevents program from asking player to guess again
        }//end if-else chain

        int playType = 2; //size of array based on number of players ///array, nested if-else
        string switchTurns[playType] = {"Player 2 Guess: ", "Player 1 Guess: "}; //{switchTurns[0], switchTurns[1]}
        for (; i >= 0;) { //allows player to guess multiple times
            if (i == 0) { //if i is an odd integer
                cout << switchTurns[i]; //Player 2
                cin >> play2Guess;
                numGuess2++; //counts player 2 guesses
                i++; //increments i so else statement is true next time through
            }
            else { //if i is an even integer
                cout << switchTurns[i]; //Player 1
                cin >> play1Guess;
                numGuess1++; //counts player 1 guesses
                i--; //decrements i so if statement is true next time through
            }
            break; //forces program to reenter while loop
        }//end for loop
    }//end inner while loop

    cout << "Good Game!";
    cout << "Would you like to play again? <Y/N>: ";
    cin >> replayGuessGame;
    if (replayGuessGame == 'Y') //Replay
        ; //null
    else { //No replay
        guessWinAvg1 = numGuess1/double (gamesPlay); //avg guesses before winning for player 1
        guessWinAvg2 = numGuess2/double (gamesPlay); //" player 2
        cout << "You played " << gamesPlay << " time(s)." << "\n\n";

        if (numGuess1 < numGuess2) {
            cout << "Congratulations Player 1!!\nYou won The Guessing Game!"
                 << "\n\nYour average number of guesses before\nguessing the correct number was "
                 << guessWinAvg1 << "." << endl;
            cout << "Player 2 lost The Guessing Game!"
                 << "\n\nYour average number of guesses before\nguessing the correct number was "
                 << guessWinAvg2 << "." << endl;
        }//if
        else if (numGuess2 < numGuess1) {
            cout << "Congratulations Player 2!!\nYou won The Guessing Game!"
                 << "\n\nYour average number of guesses before\nguessing the correct number was "
                 << guessWinAvg2 << "." << endl;
            cout << "Player 1 lost The Guessing Game!"
                 << "\n\nYour average number of guesses before\nguessing the correct number was "
                 << guessWinAvg1 << "." << endl;
        }//else-if

        return 0; //terminates program //placed here to accommodate for continue (replay)
    }//end outer if-else

        break; //exits switch-case
    default: //included as a precaution
        cout << "\nInvalid Option.\n\n";
    }//switch-case

    cout << "\n\n\n"; //separates first game display from second game display
    continue; //allows player to replay Guessing Game
    }//end outer while loop
}//end main

Thanks to anyone who helps out. I really appreciate it!




Aucun commentaire:

Enregistrer un commentaire