C++ class, homework help

Pages: 12
The game logic in main may need some tweaking as well. Can you post your current code?
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

void instructions();
int beginGame(int[]);
void displayBoard(int board[][3]);
bool testWinner(int, int, int[]);

int main()
{
    int board_1[4][3] = { 38, 11, 83,
                         15, 6, 33,
                         10, 2, 20,
                         86, 0, 95};

	int board_2[4][3] = { 28, 10, 55,
                         89, 17, 98,
                         22, 4, 31,
                         69, 0, 78};

	int board_3[4][3] = { 90, 9, 45,
                         66, 12, 48,
                         34, 7, 70,
                         44, 0, 26};

	int ansBoard[3] = { 14,15,8 };
	int usedBoards[3] = {0,0,0};
	int userInput, number_wins = 0, guesses = 0, boardNumber;
	int board[4][3];
    instructions();
    do
    {
        beginGame(usedBoards);
        boardNumber = beginGame(usedBoards);
        switch(boardNumber){
            case 0:
                   displayBoard(board_1);
                   break;
            case 1:
                   displayBoard(board_2);
                   break;
            case 2:
                   displayBoard(board_3);
                   break;
        	}

        for (int i = 1; i < 4; i++)
        {
            cout << endl << "Enter your guess or zero to exit";
        	cin >> userInput;
        	int ans = userInput;

        	if (userInput == 0){
            cout << endl << "Exiting..";
            return 0;
        	}

        	else{
        		while (userInput < 0){
        				cout << endl << "Please enter a valid value";
        				cin >> userInput;
        				}
        		}

            testWinner(ans, boardNumber, ansBoard);
           if (testWinner(ans, boardNumber, ansBoard) == true)
            {
				switch (boardNumber)
				{
				case 0:
					number_wins++;
					usedBoards[0] =  1 ;
					break;
				case 1:
					number_wins++;
					usedBoards[1] =  1 ;
					break;
				case 2:
					number_wins++;
					usedBoards[2] =  1 ;
			   }

			   if ((usedBoards[0] == 0) || (usedBoards[1] == 0) || (usedBoards[2] == 0))
               {
                    cout << endl << "Do you want to play again or exit (0)";
                cin >> userInput;
                if (userInput == 0){
                    cout << endl << "Exiting..";
                    return 0;
                }
                else
                {
                    while (userInput < 0)
                {
                    cout << endl << "Please enter a valid value";
                    cin >> userInput;
                }

                if (userInput != 0)
                {
                    beginGame(usedBoards);
                }
                }
               }
               break;

            }

            else
            {
                    guesses++;
                    cout << endl << "Please try again, that was incorrect";
                    cin >> userInput;
            }
        }

                if (guesses > 3)
                {
                    cout << endl << "Do you want to play again?. 1 to play again, 0 to exit";
                    cin >> userInput;
                    if (userInput == 0){
                    cout << endl << "Exiting..";
                    return 0;
                    }

                    else{

                    while (userInput < 0)
                    {
                    cout << endl << "Please enter a valid value";
                    cin >> userInput;
                    }

                    }

                    if (userInput != 0)
                    {
                        main();
                    }
                }

    }while (number_wins < 3);
        cout << endl << "You are the number guessing Champion !!";
		return 0;
}



void instructions()
{
	cout << "**********************************" << endl;
	cout << endl << "MISSING NUMBERS GAME" << endl;
	cout << "A Fun Brain Game" << endl << endl << endl;
	cout << "Please enter a whole number to guess the missing number...";
	cout << endl << "Program Developed by: Rohan Jakhete" << endl;
	cout << "**********************************" << endl;
}

int beginGame(int usedBoards[])
{
    int randomNum;
    srand(time(0));
    do{
        randomNum = rand() % 3;
    }while (usedBoards[0] != 0 || usedBoards[1] != 0 || usedBoards[2] != 0);
    return randomNum;
}


void displayBoard(int board[4][3])
{
    for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board[x][y] << " ";
        }
        cout << endl;
    }
}


bool testWinner(int ans, int boardNum, int ansBoard[]){
    if (ans == ansBoard[boardNum]) {
        cout << endl << "Nice Job!!";
        return true;
    	}
    else
    {
        cout << endl << "Sorry, that is incorrect";
        return false;
    }
}

Last edited on
The only issue I am having right now is the do-while loop not repeating. Most other issues are solved.
If they don't win all three boards, number_wins may always be less than 3. The do while should run while there are still game boards available, if I understand correctly. (Or can they replay boards that they lost?)

The logic you have in your begin game function isn't checking whether the board indicated by the current random number has been used. It's looping while at least one of the game boards has been played.

1
2
3
4
    do{
        randomNum = rand() % 3;
    // while board 0 has been played OR board 1 has been played OR board 2 has been played
    }while (usedBoards[0] != 0 || usedBoards[1] != 0 || usedBoards[2] != 0)
;

Also, srand should only be called once at the beginning of the program, not inside a function where it is called each time the function runs.

1
2
3
4
5
6
7
8
9
10
11
int beginGame(int playedBoards[])
{
    int randomNum;
    while (true){
        randomNum = rand() % 3;
        if (playedBoards[randomNum] != 1)// if not already played
            break; // stop generating random numbers
    }
    playedBoards[randomNum] = 1; // mark board used
    return randomNum; // return board number to be played
}



The main can be simplified a bit. main() shouldn't be calling itself like it does at line 143.

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
	int userInput, number_wins = 0, guesses, boardNumber;
    bool winner;
    srand(time(0)); // seed random function once at beginning of main
 
    instructions();

    do{
        // initialize game board 
        guesses = 0; // reset guesses with new board
        winner = false; // reset winner
        boardNumber = beginGame(usedBoards);// get random board number
        //cout << "\nPlaying board number " << boardNumber << "\n";
        switch(boardNumber){ // display the current game board
            case 0:
                   displayBoard(board_1);
                   break;
            case 1:
                   displayBoard(board_2);
                   break;
            case 2:
                   displayBoard(board_3);
                   break;
        }
        // let them play (or exit)
        while (guesses < 3 && !winner){ // while guesses left and not winner
            //cout << "\nYou have " << 3 - guesses << " guesses left.";
            cout << endl << "Enter your guess or zero to exit: ";
            cin >> userInput;
            if (userInput == 0){
                cout << endl << "Exiting..";
                return 0;
            }
            else{
                 while (userInput < 0){
                    cout << endl << "Please enter a valid value: ";
                    cin >> userInput;
                }
            }

            guesses++;
            winner = testWinner(userInput, boardNumber, ansBoard);
        }

        if (winner){
			number_wins++;
        }
        else{
            cout << endl << "Sorry, you ran out of guesses for this board.\n";
        }
    // continue loop while boards not all played yet
    }while (!(usedBoards[0] == 1 && usedBoards[1] == 1 && usedBoards[2] == 1));
        
    // if (number_wins == 3)?
    cout << endl << "You are the number guessing Champion !!";
Last edited on
Wow, thank you so much. I understand your logic behind each statement, keeping it simple is better :) I have one question: If they want to play again, how would I execute that?
Would I just execute main() ?
main() shouldn't be called recursively. Meaning inside main, there shouldn't be a main();

When you say if they want to play again, what do you mean? That they could start with a fresh set of the same 3 boards? You already let them quit at each quess prompt, so they can exit at any time during a game.
Topic archived. No new replies allowed.
Pages: 12