Hi, I have an assignment that requires me to write out the code for a game.
It involves 3 columns and random numbers between 0-100, so you have to place a random number into each column and as soon as the number below gets bigger, the game finishes. The main aim of the game is to go as many rounds as you can.
Could i get some help please. thanks.
Running program

``````  Round: 1 column 1:0     column 2:0       column 3:0
In which column to put number 42?1

Round: 2 column 1:42     column 2:0       column 3:0
In which column to put number 68?3

Round: 3 column 1:42     column 2:0       column 3:68
In which column to put number 35?2

Round: 4 column 1:42     column 2:35      column 3:68
In which column to put number 1?1

Game Over. You'ver reached Round 4.
``````
6
Contributors
9
Replies
55
Views
3 Years
Discussion Span
Last Post by kal_crazy

We dont have any code. Just the output of the program. Could u give the code??

You can't just ask us to solve it for you...

No, that is not what this website is for. You should try it for yourself and then ask for help with a specific problem.

Okay, so I have written down the code. I am stuck with declaring the rounds and columns and what loops to use. Could I get any recommendations for that?

// This is what we have so far. We need to limit the number of columns to "3" and each round number should change as the game progresses.
- Currently, when the program asks the user to input a column, it will accept any number and continue when there are only 3 columns.
- Also when "Round: 1" is over, it still says "Round: 1" on round 2.

Will you be able to help me out with this please?

``````# include <iostream>
# include <stdlib.h>
# include <time.h>

using namespace std;

int main()
{
int col1max = 0, col2max = 0, col3max = 0;
int temp;
int colnum;
int numrounds = 0;
char cont = 'y';
bool greaterthan = true;
srand(time(0));

cout << "\t***********************************\n";
cout << "\t*     CS111 Assignment 1 s2       *\n";
cout << "\t***********************************\n";

cout << "\nINSTRUCTIONS:\n";
cout << "\nThe game ends if you put the number into a column\n";
cout << "that has a higher or equal value.\n";
cout << "The aim is to play as many rounds as possible\n";
while ( cont == 'y'){
col1max = 0; col2max = 0; col3max = 0;

while(greaterthan){

temp = (rand()%100)+1;

cout << "\nRound: 1" << endl;

cout << "\tcolumn 1: " << col1max << "\tcolumn 2: " << col2max << "\tcolumn 3: " << col3max << endl;
cout << "\tIn which column to put number " << temp << "?" << endl;
cin >> colnum;
if(colnum == 1){
if(temp < col1max){
greaterthan = false;
}
else
{
col1max = temp;
}
}
else if(colnum == 2){
if (temp < col2max){
greaterthan = false;
}
else{
col2max = temp;
}
}
else if( colnum ==3){
if (temp <col3max){
greaterthan = false;
}
else {
col3max = temp;
}

}

}

}

system ("PAUSE");
return 0;
}
``````
• Currently, when the program asks the user to input a column, it will accept any number and continue when there are only 3 columns.

Put a validation after taking the user input.
If the validation fails,ask the user to enter again.

• Also when "Round: 1" is over, it still says "Round: 1" on round 2.

When you have hard coded "Round :1" in your code,

``````    cout << "\nRound: 1" << endl;
``````

how do you expect it to change automatically with each iteration?

Have a variable to keep track of the rounds and use the same while printing this statement.

Edited by Tess James: Grammatical errors

``````# include <iostream>
# include <stdlib.h>
# include <time.h>
using namespace std;
int main()
{
int col1max = 0, col2max = 0, col3max = 0;
int random_number;
int colnum;
int round = 1;// your rounds is solved, just add round++ afterbdo loop. it will count by each loop
srand(time(0));
random_number =(rand()%100)+1;

cout << "\t***********************************\n";
cout << "\t*     CS111 Assignment 1 s2       *\n";
cout << "\t***********************************\n";
cout << "\nINSTRUCTIONS:\n";
cout << "\nThe game ends if you put the number into a column\n";
cout << "that has a higher or equal value.\n";
cout << "The aim is to play as many rounds as possible\n";

do
{
cout << "\nRound: " << endl;
cout << "\tcolumn 1: " << col1max << "\tcolumn 2: " << col2max << "\tcolumn 3: " << col3max << endl;
cout << "\tIn which column to put number " << random_number << "?" << endl;
cin >> colnum;
if(colnum == 1){
if(random_number <= col1max)
{
cout << "Game over, youve reached Round\n" <<round << endl;
}
else if ( col1max > random_number)
{
col1max= random_number;
}

else if(colnum == 2)
{
if (random_number <= col2max)
cout <<" Game over, youve reached Round\n" << round << endl;
}

else if (col2max > random_number)
{     col2max = random_number;

}
else if( colnum ==3){
if (random_number <= col3max)
cout <<"Game over, youve reached Round;\n" <<round << endl;
}
else if (col3max > random_number)
{
col3max = random_number;
}
}
round++;

}while (colnum >=1 && colnum <=3);
system ("PAUSE");
return 0;
}
``````

jst did hastily...make some alterations it will wrk.didnt have muj tym..i hope m given u sme idea to keep things smple also add some other functions along with the do while loop so it repeats with the round++ functn...kk

``````# include <iostream>
# include <stdlib.h>
# include <time.h>
using namespace std;
int main()
{
int col1max = 0, col2max = 0, col3max = 0;
int random_number=0;
int colnum;
int round = 0;
srand(time(NULL));

cout << "\t***********************************\n";
cout << "\t*     CS111 Assignment 1 s2       *\n";
cout << "\t***********************************\n";
cout << "\nINSTRUCTIONS:\n";
cout << "\nThe game ends if you put the number into a column\n";
cout << "that has a higher or equal value.\n";
cout << "The aim is to play as many rounds as possible\n";
do
{
round++;//increment round until random num is less than numbers in all columns
random_number =(rand()%100)+1;//generates a NEW random number every loop
cout << "\nRound: "<< round << endl;

cout << "\tcolumn 1: " << col1max << "\tcolumn 2: " << col2max << "\tcolumn 3: " << col3max << endl;
cout << "\tIn which column to put number " << random_number << "?" << endl;
cin >> colnum;

if(colnum == 1){
col1max= random_number;
}
else if(colnum == 2){
col2max = random_number;

}
else if( colnum ==3){
col3max = random_number;
}

}while ((random_number > col1max) || (random_number > col2max) || (random_number > col3max)); //loop continues when random num is greater than all col num

cout <<"Game over, youve reached Round: " <<round<< endl;

system ("PAUSE");
return 0;
}
``````

This is what I think would work as according to your question.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.