The program i made below is a c++ program. All I ask for is some feedback of my program. This program has a menu of option, but I have only implemented option 1,2,3,7thus far. So here is the code.

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<string>
using namespace std;
void MemoryMatchingGame();//The matching game;
const int numberOfCards = 16;//Needed for MemoryMatching game;
void shuffle(int [], int);//Needed for MemoryMatching game;
void displayBoard(const int [], int [], int, int, int);//Needed for MemoryMatching game;
void clearScreen();//Needed for MemoryMatching game;
bool checkGameStatus(const int [], int);//Needed for MemoryMatching game;
 void menu();//The main menu;
void NUMBERgame();//The number guessing game;
void Rock_Paper_Scissors();//The rock-paper-scissors game;
void Choices();//Includes menu function with user choice needed;
int main(){

//Declare/ Initialize variables;
	int pick;

//Dunction displays menu, and inputs;
Choices();













}
void Choices()
{	int pick;
	menu();
	cout<<"Which option would you like to choose: ";
	cin>>pick;
	cout<<endl;
	while((pick<1) || (pick>7))
			{
				cout<<"Invalid input. Please input a correct response"<<endl;
				cin>>pick;
			}
	switch(pick)
	{	menu();
		cout<<"Which option would you like to choose: ";
				cin>>pick;
			while((pick<1) || (pick>7))
			{
				cout<<"Invalid input. Please input a correct response"<<endl;
				cin>>pick;
			}
		case 1: MemoryMatchingGame();
				break;
		case 2: NUMBERgame();
			    break;
		case 3: Rock_Paper_Scissors();
				break;
	    case 7: exit(1);
			    break;
	}
}
void menu()
{
	cout<<"IN THIS MENU YOU HAVE MULTIPLE CHOICES OF GAMES AND APPLICATIONS"<<endl;
	cout<<"CHOOSE THE TYPE OF GAME/APPLICATION YOU WANT TO PLAY/PICK."<<endl;
	cout<<"AFTER YOUR CHOICE, A DESCRIPTION OF THE GAME WILL POP UP"<<endl;
	cout<<"IF YOU WANT TO CONTINUE TO PLAY THE GAME AFTER READING THE DESCRIPTION, THEN FOLLOW DIRECTION FROM THERE"<<endl;
	cout<<"!!HAVE FUN!!"<<endl<<endl;
	cout<<"1) THE MATCHING GAME "<<endl;
	cout<<"2) THE NUMBER GUESSING GAME "<<endl;
	cout<<"3) ROCK-PAPER-SICOSSORS GAME"<<endl;
	cout<<"4) BLACKJACK"<<endl;
	cout<<"5) POKER "<<endl;
	cout<<"6) CALCULATOR "<<endl;
	cout<<"7) Exit program :( "<<endl;

	return;
}
void NUMBERgame()
{
	//Declare variable/initialize
	char play;
    int choice1,count(0),choice2;
	//Seed random generator;
	srand(time(NULL));
	//Goal number
	int goal=rand()%250+1;
	//Displays the description of the game;
	cout<<"In this game a number is randomly generated from the range of 1-250. "<<endl;
	cout<<"The object of this game is for you to guess the random number. "<<endl;
	cout<<"There will be a guide : saying your guess is too high or low. "<<endl;
	cout<<"So is this your style of choice?(y/n) "<<endl;
	//Input choice;
	cin>>play;
	cout<<endl;
	if(play=='n'||'N')
	{	cout<<"1) Main Menu "<<endl<<"2) Exit program"<<endl;
		cin>>choice1;
		cout<<endl;
		if(choice1==1)
			Choices();
		else if(choice1==2)
				exit(1);
		else while(choice1!=1 && choice1!=2)
			{	cout<<"Invalid response. Please input a correct response"<<endl;
				cin>>choice1;
			}
	}

	
    while(play!='y' && play!='n' && play!='Y' && play!='N')
	{		cout<<"Your input is invalid. Please try again with valid input "<<endl;
			cin>>play;
	}
	if(play=='N'||play=='n')
		exit(1);
	while(play=='Y' ||play=='y')
	{	//Input your first guess
		cout<<"What is your guess number? : ";
		cin>>choice1,choice2;
		cout<<endl;
		//Keeps track of the number of guesses;
		count++;
		//check if the guess number is correct;
		if(choice1==goal)
		{	cout<<"Very nice, You have won! "<<endl<<endl;
			cout<<"It took you "<<count<<" times to guess the correct number"<<endl<<endl;
			cout<<"1) Go to main menu "<<endl;
			cout<<"2) Exit Program "<<endl;
			cin>>choice2;
			if(choice2==2)
				exit(1);
			if(choice2==1)
			{	cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
				menu();
			}
			else if(choice2!=1 || choice2!=2)
				{	cout<<endl<<endl<<"Invalid input. Please input correct number"<<endl;
					cin>>choice2;
				}

		}
			else if(choice1>goal)
			cout<<" Your guess number is too high "<<endl;
		else if(choice1<goal)
			cout<<" Your guess number is too low "<<endl;
			
	}
}
void MemoryMatchingGame()
{
//number of cards in the deck is set to 16



    //cards is the array that holds the 16 cards of the deck. 
    //Shown below is some arbitrary arrangement of cards on the table
    int cards[numberOfCards] = {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};

    //cardStatus is the array that holds the information on which cards are face up/face down.
    int cardStatus[numberOfCards];

    //keeps track of number of guesses taken by the player to complete the game.
    int guess = 0;

    //initialize the cardStatus array to all cards face down on the table(integer 0).
    for(int i = 0; i < numberOfCards-1; i++)
            cardStatus[i]=0;

    //shuffle the deck before the game begins.
    shuffle(cards,numberOfCards);

    //Ask user if he wants to play the game and if so which cards to turn face up.
    //integer coordinates to hold user's choices of cards to flip face up.
    int x1, y1, x2, y2,option;
    char response;
	cout<<"THIS IS A GAME OF MATCHING NUMBER 1-8. THIS GAME TESTS YOUR MEMORY."<<endl;
	cout<<"YOU WILL BE USING CORDINATE TO INPUT YOUR GUESSES "<<endl;
	cout<<"SO THE QUESTION IS : DO YOU WANT TO PLAY THIS GAME? (y/n)"<<endl;
    cin >> response;
	if(response=='N' || response=='n')
	{
		cout<<"1) Main menu "<<endl<<"2) Exit program "<<endl;
		cin>>guess;
		if(guess==1)
			Choices();
		else if(guess==2)
				exit(1);
		else while(guess<1||guess>2)
					{	cout<<"Invalid input. Please input a correct response "<<endl;
						cin>>guess;
					}
	}
    while(response == 'y' || response == 'Y')
    {
       //display the current board on the screen.
       displayBoard(cards, cardStatus, numberOfCards, -1, -1);
       guess++;
       do
       {
          //Which cards to turn face up
          cout << "\nEnter your guess (2 integers between 1 and 4 separated by a space).\n\nCoordinate One (x y): ";
          cin >>  x1 >> y1;
          cout <<"Coordinate Two (x y): ";
          cin >> x2 >> y2;          
       }while( !((x1 >= 1 && x1 <=4) && (y1 >= 1 && y1 <=4) && (x2 >= 1 && x2 <=4) && (y2 >= 1 && y2 <=4)) );
       //display the currrent board with the chosen cards flipped face up.
       displayBoard(cards, cardStatus, numberOfCards, (x1-1)*4+y1-1, (x2-1)*4+y2-1);
       //check if game is over! (If all cards have been flipped face up)
       //print out congratulatory note to the player and tell him/her how many guesses were used.
       if(checkGameStatus(cardStatus, numberOfCards))
       {
         cout << "Congrats! You took " << guess << " guesses!\n";
         cout<<" 1) Play this game"<<endl<<"2) Go to main menu"<<endl<<"3) Exit program "<<endl;
		 cin>>option;
		 while(option!=1 || option!=2 || option!=3)
		 {	cout<<"Invalid input. Please input a correct response"<<endl;
			cin>>option;
			cout<<endl;
		 }
		 if(option==3)
			 exit(1);
		 else if(option==2)
				 {		menu();
						break;
					 }
		 else if(option==1)
				{		MemoryMatchingGame();
						break;
				}
       }
       //If game is not over ask if the player wants to continue the game
       do
       {
         cout << "\nContinue Game (y/n): ";
         cin >> response;
		 if(response=='n')
		 {	 cout<<endl<<"1) Main Menu"<<endl<<"2) Exit Program"<<endl;
			cin>>option;
			cout<<endl;
			if(option==1)
				Choices();
			else if(option==2)
					exit(1);
			else while(option!=1 || option!=2)
			{	cout<<"Invalid response. Please input a correct response"<<endl;
				cin>>option;
				cout<<endl;
			}
		 }
       } while((response != 'y') && (response != 'Y') && (response != 'n') && (response != 'N'));
       //clear the old screen off the board
       clearScreen();                    
    }
    return;
}
/*
shuffle is a function which accepts an int array of some size n and shuffles them by repeatedly selecting two cards at random
and swapping them for some number of times (say 50 times).
*/
void shuffle(int arr[], int n)
{
     srand(time(0));
     int numberOfShuffles = 50;
     for(int i = 1; i <= numberOfShuffles; i++)
     {
       int a = rand()%n;
       int b = rand()%n;
       int temp = arr[a];
       arr[a] = arr[b];
       arr[b] = temp; 
     }
     return;
}

/*
displayBoard is a function which accepts 5 parameters:
Parameter 1: Array 1 is the actual board (const modified)
Parameter 2: Array 2 is the array which holds information about whether the cards are faceup/facedown
Parameter 3: int - the size of both these arrays 
Parameter 4: int - the first index choice of the user of the array Array1 which should be flipped faceup
Parameter 5: int - the index of the array Array1 which should be flipped faceup
selection 
*/
void displayBoard(const int arr[], int status[], int n, int loc1, int loc2)
{
    cout << "\n    1   2   3   4\n" << endl;
    for(int i = 0; i <= n-1; i++)
    {
      if(i%4 == 0) 
      {
        cout << (i+1)/4 + 1 << "   ";
      }
      if (i == loc1) cout << arr[loc1] << "   ";
      else if (status[i] == 1) cout << arr[i] << "   ";
      else if (i == loc2) cout << arr[loc2] << "   ";
      else cout << "*" << "   ";
      if((i+1)%4 == 0) cout << "\n\n";
    }
    if((loc1 != loc2) && (arr[loc1] == arr[loc2])) 
    {
       status[loc1] = 1;
       status[loc2] = 1;
    }
    return;
}

/*
clearScreen is a function which prints some number of new lines (say, 100 new lines) on the screen. This function hides the cards
that have been temporarily placed faceup by forcing the old board off the screen.
*/
void clearScreen()
{
     for(int i=1; i <=100; i++)
          cout << "\n";
     return;   
}

/*
checkGameStatus is a function which accepts the array which holds the information about which cards are 
faceup/facedown and its size. It returns a boolean value which correponds to whether the game is over.
Clearly, the game is over when every card has been idenitified by the player and is faceup.
*/
bool checkGameStatus(const int status[], int n)
{
     int count = 0;
     for (int i=0; i <= n-1; i++)
     {
         count+=status[i];
     }
     return (count == n);
}

void Rock_Paper_Scissors()
{	// Declare/Initialize variables;
	char choice,pick;
	int compChoice,random,playerCount(0),compCount(0), option;
	//Seed random generator;
	srand(time(0));

	//This is the description of the game;
	cout<<"THIS IS THE TRADITIONAL GAME OF ROCK-PAPER-SCISSORS"<<endl;
	cout<<"THE RULES OF THIS GAME ARE THE FOLLOWING: "<<endl<<endl;
	cout<<"A) THIS GAME WILL BE THE BEST OUT 5."<<endl;
	cout<<"B) ROCK BEATS SCISSORS, BUT LOOSES AGAINST PAPER"<<endl;
	cout<<"C) SCISSORS BEATS PAPER, BUT LOOSES AGAINST ROCK"<<endl;
	cout<<"D) PAPER BEATS ROCK, BUT LOOSES AGAINST ROCK"<<endl<<endl;
	cout<<"WILL THIS GAME BE YOUR FANCY?(y-n)"<<endl;
	cin>>choice;
do{
	 while(choice!='y' && choice!='n' && choice!='Y' && choice!='N')
	{		cout<<"Your input is invalid. Please try again with valid input "<<endl;
			cin>>choice;
	}
	 if(choice=='n'||choice=='N')
		 Choices();

	 else if(choice=='y' || choice == 'Y')
	 {
		 cout<<"Which object would you like to pick: r-for rock, p-for paper,s-for scissors"<<endl;
		 cin>>pick;
		 cout<<endl;
		 
		 if(pick!='r'&& pick!='p'&& pick!='s')
		 {
			 cout<<"Invalid input. Please input a valid response. "<<endl;
			 cin>>pick;
			 cout<<endl;
		 }
		  if(pick=='r')
			 {	 cout<<"You picked |Rock|"<<endl;
					compChoice= rand()%3+1;

				if(compChoice==1)//1 is represented as rock
						cout<<"The computer choose |rock| "<<endl<<"So this round is a tie. Nobody wins "<<endl;

					else if(compChoice==2)//2 represents paper.
					{		cout<<"The computer choose |Paper| "<<endl;
							compCount++;
							if(compCount==3)//Checks if the computer wins
							{	cout<<"The computer won this round and the game!"<<endl<<endl;
								cout<<"1) Play this game"<<endl<<"2) Go to the main menu"<<endl<<"3) Exit program"<<endl;
								cin>>option;

								if(option==3)
									exit(1);
								else if(option==2)
									Choices();
								else if(option==1)
									Rock_Paper_Scissors();
								else while((option<1)||(option>3))
								{	cout<<"Invalid input. Please input a valid response"<<endl;
									cin>>option;
								}
							}
							cout<<"The computer wins this round"<<endl<<endl;
					}
					else if(compChoice==3)//3 represents scissors.
					{	playerCount++;
							cout<<"The computer choose |Scissors| "<<endl;
							cout<<"You win this round"<<endl<<endl;
							if(playerCount==3)
							{	cout<<"You won this game!"<<endl<<endl;
								cout<<"1) Play this game"<<endl<<"2) Go to the main menu"<<endl<<"3) Exit program"<<endl;
								cin>>option;

								if(option==3)
									exit(1);
								else if(option==2)
									Choices();
								else if(option==1)
									Rock_Paper_Scissors();
								else while((option<1)||(option>3))
								{	cout<<"Invalid input. Please input a valid response"<<endl;
									cin>>option;
								}
							}
					}
		 }
			 else if(pick=='p')
			 {		 cout<<"You picked 'Paper'"<<endl;
							compChoice= rand()%3+1;
					if(compChoice==1)//1 is represented as rock
					{	cout<<"The computer choose 'rock' "<<endl<<"This round goes to you "<<endl<<endl;
							playerCount++;
						if(playerCount==3)
							{	cout<<"You won this round and the game!"<<endl<<endl;
								cout<<"1) Play this game"<<endl<<"2) Go to the main menu"<<endl<<"3) Exit program"<<endl;
								cin>>option;
								if(option==3)
									exit(1);
								else if(option==2)
									Choices();
								else if(option==1)
									Rock_Paper_Scissors();
								else while((option<1)||(option>3))
								{	cout<<"Invalid input. Please input a valid response"<<endl;
									cin>>option;
								}
							}

					}
					else if(compChoice==2)//2 represents paper.
					{	cout<<"The computer choose 'Paper' "<<endl<<"This round is a tie. Nobody wins"<<endl;
					}
					else if(compChoice==3)//3 represents scissors.
					{	compCount++;
							cout<<"The computer choose 'Scissors'"<<endl;
							cout<<"The computer wins this round"<<endl<<endl;
							if(compCount==3)
							{	cout<<"The computer wins this round and the game!"<<endl<<endl;
								cout<<"1) Play this game"<<endl<<"2) Go to the main menu"<<endl<<"3) Exit program"<<endl;
								cin>>option;
								if(option==3)
									exit(1);
								else if(option==2)
									Choices();
								else if(option==1)
									Rock_Paper_Scissors();
								else while((option<1)||(option>3))
								{	cout<<"Invalid input. Please input a valid response"<<endl;
									cin>>option;
								}
							}
					}
					}
			 else if(pick == 's')
		{		 cout<<"You picked 'Scissors'"<<endl;
					compChoice=rand()%3+1;
				if(compChoice==1)//1 represents rock
					{	compCount++;
							cout<<"The computer choose 'Rock'"<<endl;
							cout<<"The computer wins this round"<<endl<<endl;
							if(compCount==3)
							{	cout<<"The computer won this round and the game!"<<endl<<endl;
								cout<<"1) Play this game"<<endl<<"2) Go to the main menu"<<endl<<"3) Exit program"<<endl;
								cin>>option;
								if(option==3)
									exit(1);
								else if(option==2)
										Choices();
								else if(option==1)
									Rock_Paper_Scissors();
								else while((option<1)||(option>3))
								{	cout<<"Invalid input. Please input a valid response"<<endl;
									cin>>option;
								}
							}
				}
				else if(compChoice==2)//2 represents paper
				{	playerCount++;
							cout<<"The computer choose 'paper'"<<endl;
							cout<<"You win this round"<<endl<<endl;
							if(playerCount==3)
							{	cout<<"You won this round and the game!"<<endl<<endl;
								cout<<"1) Play this game"<<endl<<"2) Go to the main menu"<<endl<<"3) Exit program"<<endl;
								cin>>option;
								cout<<endl;
								if(option==3)
									exit(1);
								else if(option==2)
								{	Choices();
									break;

								}
								else if(option==1)
									Rock_Paper_Scissors();
								else while((option<1)||(option>3))
								{	cout<<"Invalid input. Please input a valid response"<<endl;
									cin>>option;

								}
							}
					}

					
				else if(compChoice==3)//3 represents scissors
				{		cout<<"The computer choose 'scissor'"<<endl<<"This round is a tie"<<endl;
				}



	 }


	}
  }while(compCount!=3||playerCount!=3|| option==2);
}

Recommended Answers

All 6 Replies

First off, if you want some constructive feedback my immediate question is why, (a) you are learning C++/programming for fun and decided to write some code and want to have some feed back.
(b) you are taking a class and this is to be marked, and you are hoping to improve it. (c) other...

Actually both (a) and (b) and maybe (c) are good. But it causes a difference in what I say about the code.

First off:

Is there anything that is c++ in it other than the std::cout/cin ?
There are no classes / use of the stl, etc.
If you are starting with C and moving up not a problem, for a c++ class, not so good.

Layout.... Arrh!!!!! -- That is not good.

Beyond that shuffle stands out as awful. It will not result in particularly random cards.

There are a number of exit(1) lines. This is a bit ungraceful.

The conditional breaks are very very difficult to follow. I think that is were you need to code round your logic. [I mean the if (condition) break; that litter the code.]


For this kind of task, my next step would be to write a simple command parser, so that games could be played simultaniously.
(I don't mean across computers, but command could be simple letters to start a game eg.

play B       // play blackjack
B b 50       // bet 50
B show      // show cards (again)
B card        // get an exxtra card
B hold        // hold
play RPS     // play rock paper scissors
R 1             // select rock etc. 
B show       // back to blackjack again

you really don't have to take my advice on that, but I was trying to see a way to get you out of the loop in a loop in a loop structure you have, but at the same time not require you to write a full GUI.

Also after a while (say 1 turn) the user is bored with the messages telling him what to do. I would only do that if the user typed help.

Enjoy the coding.

First off, if you want some constructive feedback my immediate question is why, (a) you are learning C++/programming for fun and decided to write some code and want to have some feed back.
(b) you are taking a class and this is to be marked, and you are hoping to improve it. (c) other...

Actually both (a) and (b) and maybe (c) are good. But it causes a difference in what I say about the code.

First off:

Is there anything that is c++ in it other than the std::cout/cin ?
There are no classes / use of the stl, etc.
If you are starting with C and moving up not a problem, for a c++ class, not so good.

Layout.... Arrh!!!!! -- That is not good.

Beyond that shuffle stands out as awful. It will not result in particularly random cards.

There are a number of exit(1) lines. This is a bit ungraceful.

The conditional breaks are very very difficult to follow. I think that is were you need to code round your logic. [I mean the if (condition) break; that litter the code.]


For this kind of task, my next step would be to write a simple command parser, so that games could be played simultaniously.
(I don't mean across computers, but command could be simple letters to start a game eg.

play B       // play blackjack
B b 50       // bet 50
B show      // show cards (again)
B card        // get an exxtra card
B hold        // hold
play RPS     // play rock paper scissors
R 1             // select rock etc. 
B show       // back to blackjack again

you really don't have to take my advice on that, but I was trying to see a way to get you out of the loop in a loop in a loop structure you have, but at the same time not require you to write a full GUI.

Also after a while (say 1 turn) the user is bored with the messages telling him what to do. I would only do that if the user typed help.

Enjoy the coding.

Thanks StuXYZ for the constructive criticism.

I have not taken any programming class except this c++ ( if that answers one of your question)

Your advice helps, because a program should be efficient as possible.

And your idea of the game sampler sounds like a cool idea. I will try to implement that algorithm. And again thanks for your comment.

One more thing. this program supposed to be user friendly. what do you think?

No more opinions?

Have you learned about classes in your course yet? If so then you should be using them. If not, you should at least break your functions up into smaller, more manageable functions. Also you should learn how and when to use header files and multiple cpp files rather than having everything in one big file.

More oppions the marrier :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.