I made a simple color finding game. the problem is that my color input guess is not good so complex I want them in one line and also I want to see my tries at certain places. can you help me
thanks in advance.

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <conio.h>

using namespace std;

int main(int argc, char *argv[])
{
    int j;
    int trial=0;
    int counters=0;
    int color_correct=0;
    string guess[4];    
    vector <string> colors;
    colors.push_back("yellow");
    colors.push_back("blue");
    colors.push_back("black");
    colors.push_back("white");
     colors.push_back("red");
    colors.push_back("green");    
    srand(time(0));
     random_shuffle(colors.begin(),colors.end());
   cout << "\t \t \t Find the colors in order.\n\n ";
   cout << "\n there are six available colors which are yellow,red,green,black,blue,white \n";
   cout << "\n pls select 4 and find their places\n \n ";
 
     while (counters != 4 )
     {  counters=0;  
     color_correct=0; 
   for ( int j=0;j<4;j++)
   {
       cout << "\n"<<j+1<<". color=\n\n";
       cin >> guess[j];
       cout <<"\n";
       }           
 for (int j=0;j<4;j++)
 for (int i=0;i<4;i++)
 
     if (colors[j] == guess[i])
     color_correct=color_correct+1;

      for ( int j=0;j<4;j++)
   {
      if (colors[j] == guess[j])
      counters=counters+1;   
       }  
       trial++;
       cout << trial<<".try  ";
        for ( int j=0;j<4;j++)
   {
       cout << guess[j]<<"\t";       
       } 
         
       
     cout << " \n \n you know "<< color_correct<< "  colors \n";   
    cout << " you know "<< counters<< "  colors in order\n";
  
   }       
   cout <<" you guessed all correct.";           
 
   getch();
    
}

Recommended Answers

All 2 Replies

Can you explain what you are trying to do a bit more. I ran the program and didn't understand what I was trying to do. Looking at the code, you read 4 colors from the command line, then you try to compare the input colors to the ordered list of colors you hard coded? I don't get the point?

Also, you should use compare() to compare strings:
http://programmingexamples.net/index.php?title=CPP/Strings/Compare

David

So the easiest thing for you todo is make this into classes. I'm guess you don't know about classes that much. So the next best thing you can do is make this into functions.

Here is a brief skeleton.

//#include everything
int main(){
 const int MAX_COLORS_TO_GUESS = 4;
 string guessColors[MAX_COLORS_TO_GUESS] = { "yellow","green"...and so on };
 std::vector<string> correctColors(guessColors,guessColors + MAX_COLORS_TO_GUESS);
 std::random_shuffle(correctColors.begin(),correctColors.end());
 int correctColorsGuessed = 0;
 int correctPositionOfCorrectColorGuessed = 0;
 int numberOfTries = 0;
 while(correctColorsGuessed != MAX_COLORS_TO_GUESS && correctPositionOfCorrectColorGuessed != MAX_COLORS_TO_GUESS ){
   std::vector<string> userGuessedColors = getUserGuessedColors();
   correctColorsGuessed = getCorrectColorsGuessed(correctColors,userGuessedColors);
   correctPositionOfCorrectColorGuessed = getNumberOfCorrectPosition(correctColors,userGuessedColors);
 ++numberOfTries;
 }
 printInformation(numberOfTries,..and more stuff if you need it);
}

This seems like the mastermind game? Now all you have to do is implement the needed
functions. Make sure they are simple. If they get complicated, then break the function into sub-functions.

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.