Just a little help needed with my game

My first post.

I'll cut straight to it:

How do I make it so that if the user inputs a negative number, the program will display "Too bad, the solution was [the solution]" and then terminate the game?

I think I need to add some kinda if/else statement for "guess < 0" somewhere between lines 97 and 102... I've tried different things, but they don't work properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**********************************************************************
 *
 * Project 3: Bulls and Cows
 *
 * Author: Dreyth
 * Date: 22 February 2012
 *
 * This is my fourth project so far. Its purpose is to construct a 
 * version of a Bulls & Cows game. A correct guess in the correct 
 * place will show a 1. An incorrect guess will show a 0.
 *
 **********************************************************************/
#include <bjarne/std_lib_facilities.h>

// Function protoypes below.
void offer_help();
bool play_one_game(vector<int> solution);
bool play_again();

int main()
{
    // Seed the random number generator (RNG).
    srand(time(0));

    // Call the offer_help function.
    offer_help();

    // Use a boolean variable to set up a loop later.
    bool trial = true;

    // Set up the outside loop with the previously mentioned boolean variable.
    while(trial) {

    // Set up a vector containing the solution.
    vector<int> solution(4);
    for (int i = 0; i < solution.size(); i++)
	solution[i] = randint(10);

    // This will be used to replay a game.
    bool won_game = play_one_game(solution);

    // If bool won_game is true, then the user will be prompted to
    // play again or not.
    if (won_game)
	cout << "Congratulations! You won the game\n ";
    else {
	cout << "You gave up! The solution was: \n";
	for (int i = 0; i < solution.size(); i++)
	    cout << " " << solution[i];
	    cout << endl;
	}

        // If user decides to not play again, then trial is false and
        // we will not re-enter the outside loop.
	trial = play_again();
    }
}


void offer_help()
{

    // Prompt the user for help.
    cout << "Need help (0/1)? ";
    int help;
    cin >> help;

    // Display help message if user input a 1.
    cout << "I will generate a pattern of 4 numbers, each in the range 0 through 9." << endl
	 << "You will give a series of guesses of this pattern." << endl
         << "Each guess that you enter will be a line containing 4 integers," << endl
         << "separated by spaces, such as" << endl
         << "    2 4 7 1" << endl
         << "For each guess, I will echo back a list consisting of" << endl
         << "0's and 1's, with a 1 in a given positihon meaning that" << endl
         << "you guessed the number, and a zero meaning that you didn't." << endl
         << "For example, if the actual solution was 2 6 3 1, I'll respond" << endl
         << "   1 0 0 1" << endl
         << "See how many guesses it takes you to get to the solution!" << endl << endl
         << "If you want to give up, type a negative number for one of" << endl
         << "your guesses, and we'll tell you what the pattern was." << endl;

}


bool play_one_game(vector<int> solution)
{

    // Set up the integer showing what guess the user is on.
    int trialnum = 1;

    // Set up a vector containing the user's guesses.
    vector<int> guess(4);
    for (int i = 0; i < solution.size(); i++)

    // Set up inside loop for guesses.
    while(true){
	cout << "Guess #" << trialnum++ << "? ";
	for (int i = 0; i < solution.size(); i++)
	    cin >> guess[i];


    // Take care of correct or incorrect guesses.
        for (int i = 0; i < solution.size(); i++)
	if (guess[i] == solution[i])
	    cout << "1 ";
	else 
	    cout << "0 ";

	cout << endl;

        // If all is correct, play_one_game will be true.
	if(guess == solution)
	    return true;
    }
}


bool play_again() {
    
    int playagain;
    cout << "Would you like to play again (0/1)?";
    cin >> playagain;
    if (playagain == 1)
	return true;
    else 
	return false;
}
Last edited on
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
// Take care of correct or incorrect guesses.
        for (int i = 0; i < solution.size(); i++)
if(guess[i] < 0){return false;}
	if (guess[i] == solution[i])
	    cout << "1 ";
	else 
	    cout << "0 ";

	cout << endl;

        // If all is correct, play_one_game will be true.
	if(guess == solution)
	    return true;
Topic archived. No new replies allowed.