FancyShoes 0 Newbie Poster

I am having problems with my white pegs for my mastermind game. The white pegs mean that the user guessed the right color but it is not in the right spot. The problem is that it prints out the white too many times for 1 guess. The problem is in " struct Hint play(~~~~~)" at line 161.

#include<string>
#include<iostream>
#include <iomanip>
#include<cmath>
#include<ctime>
#include <stdlib.h>  
#include <cstdlib>
#include<windows.h>

using namespace std; 
int color; // made it global so I wouldn't have to pass by reference it everywhere.


struct Guess 
{
    char one;
    char two;
    char three;
    char four;
};

struct Hint
{
    int one;
    int two;
    int three;
    int four;
};

struct Answer
{
    int one;
    int two;
    int three;
    int four;
};

void setfg(unsigned int);    
void colors(char);
void swap(char&);
bool colourChar(char);
struct Hint play(struct Guess, struct Answer);
int getColorNum(char);
struct Answer random();
int main()
{     
            //row col
  char board[26][29] = { {201 , 205 , 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 
                          205, 205, 205, 205, 205, 205, 205, 205, 187},
                         {186 , 1, 1, 1, 1, 1, 1, 1, 2, 'M', 'A', 'S', 'T', 'E', 'R', 'M', 'I', 'N', 'D', '!', 2, 1, 1, 1, 1, 1, 1, 1, 186}, //force the input to lowercase
                         {204 , 205 , 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 203, 205, 205, 205, 205, 205,
                          205, 205, 205, 205, 205, 205, 205, 205, 185}, 
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186},
                         {186 , 178 , 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 177, 177, 178, 186},
                         {186 , 178 , 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 186}, 
                         {200 , 205 , 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 202, 205, 205, 205, 205, 205,
                          205, 205, 205, 205, 205, 205, 205, 205, 188} };
//make sure when user types in t
Answer a = random();
cout << a.one << a.two << a.three << a.four; //just so I can see the answer to test
for (int l = 4; l < 25; l+=2) //input loop
{
cout << " Choose 4 colors delimited by space ";
Guess g;
cin >> g.one >> g.two >> g.three >> g.four; //gets user guesses
Hint h = play(g, a);
board[l][2] = g.one;
board[l][5] = g.two;
board[l][8] = g.three;
board[l][11] = g.four;
board[l][16] = h.one;
board[l][19] = h.two;
board[l][22] = h.three;
board[l][25] = h.four;

  for (int q = 0; q < 25 ; q++ ) //q is row
        { 
          cout << setw(2);
          cout << endl;
          for(int e = 0 ; e < 29 ; e++ ) //e is col 
          {
            colors(board[q][e]);
            
            if (colourChar(board[q][e])) //prints out guess colors to game board
            {
            cout << (char) 219;
            e++;
            cout << (char) 219;
            }
            else
            {
            cout << board[q][e]; //prints out game board
            }
          }
        }
      }
  system("PAUSE");
  return 0;
}

void setfg(unsigned int fgcolor) //changes color
{                                    
        color = (color &240) + fgcolor;
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hcon,color);
}

void colors(char color) //sets user guess to a color
{
  
  switch(color)
  {
    case 'r': setfg(4); break;
    case 'p': setfg(12); break;
    case 'g': setfg(2); break;
    case 'm': setfg(5); break;
    case 'c': setfg(3); break;
    case 'y': setfg(14); break;
    case 'w': setfg(15); break;
    case 'b': setfg(0); break;
    default: setfg(7); break;
  }
}

bool colourChar(char colour) //returns true if input is in list
{
  switch(colour)
  {
    case 'r': return true;
    case 'p': return true;
    case 'g': return true;
    case 'm': return true;
    case 'c': return true;
    case 'y': return true;
    case 'b': return true;
    case 'w': return true;
    default: return false;
  }
}

//input guess (and answer)
// output Hint (B, B, W, W)
struct Hint play(struct Guess g, struct Answer a) // this part i am having trouble with. black and white pegs
{ 
  int white = 0;
  char choice[4];
 
  getColorNum(g.one);
  getColorNum(g.two);
  getColorNum(g.three);
  getColorNum(g.four);
  
  if(a.one == (a.two || a.three || a.four))
  {
    white++;
  }
  if(a.two == (a.one || a.three || a.four))
  {
    white++;
  }
  if(a.three == (a.one || a.two || a.four))
  {
    white++;
  }
  if(a.four == (a.one || a.two || a.three ))
  {
    white++;
  }
  if(getColorNum(g.one) != (a.one || a.two || a.three || a.four))
  {
    choice[1] = (char) 177;
  }
  if(getColorNum(g.one) == (a.two || a.three || a.four))
  {
    if(white > 0);
    {
    choice[2] = (char) 177;
    }
    choice[1] = 'w';
  }
  if(getColorNum(g.two) != (a.one || a.two || a.three || a.four))
  {
    choice[2] = (char) 177;
  }
  if(getColorNum(g.two) == (a.one || a.three || a.four))
  {
    if(white > 0);
    {
    choice[2] = (char) 177;
    }
    
    choice[2] = 'w';
  }
  if(getColorNum(g.three) != (a.one || a.two || a.three || a.four))
  {
    choice[2] = (char) 177;
  }
  if(getColorNum(g.three) == (a.one || a.two || a.four))
  {
    if(white > 0);
    {
    choice[2] = (char) 177;
    }
    choice[3] = 'w';
  }
  if(getColorNum(g.four) != (a.one || a.two || a.three || a.four))
  {
    choice[4] = (char) 177;
  }
  if(getColorNum(g.four) == (a.one || a.two || a.three))
  {
    if(white > 0);
    {
    choice[2] = (char) 177;
    }
    choice[4] = 'w';
  }
  
  if(getColorNum(g.one) == a.one)
  {
    choice[1] = 'b';
  }
  
  if(getColorNum(g.two) == a.two)
  {
    choice[2] = 'b';
  }
  
  if(getColorNum(g.three) == a.three)
  {
    choice[3] = 'b';
  }
  
  if(getColorNum(g.four) == a.four)
  {
    choice[4] = 'b';
  }
  //else if(getColorNum(g.one) != (a.one || a.two || a.three || a.four))
//  {
//    choice[1] = (char) 177;
//  }
//  else if(getColorNum(g.two) != (a.one || a.two || a.three || a.four))
//  {
//    choice[2] = (char) 177;
//  }
//    else if(getColorNum(g.three) != (a.one || a.two || a.three || a.four))
//  {
//    choice[3] = (char) 177;
//  }
//    else if(getColorNum(g.four) != (a.one || a.two || a.three || a.four))
//  {
//    choice[4] = (char) 177;
//  }
   //first count how many of each color they have
   //case 1:
    // answer= GREEN GREEN CYAN RED
  // Guess = GREEN CYAN MAGENTA PINK 
  // should get = B W
  
  //go through each peg, tally colors used
  //go through answer tally colors used
  // answer = {g: 2, c: 1, r: 1}
  // guess  = {g: 1, c: 1, m: 1, p: 1}
  
  //foreach color in answer: (green)
  //  get guess count (amount of times appearing in guess) of that color (1)
  // white++;
   Hint h;
   h.one = choice[1];
   h.two = choice[2];
   h.three = choice[3];
   h.four = choice[4];
   return h;
}

int getColorNum(char ch) //changes char input guess to an int so input can work with random
{
  int red = 1;
  int pink = 2;
  int green = 3;
  int magenta = 4;
  int cyan = 5;
  int yellow = 6;
  
  switch(ch)
    {
      case 'r': return red;
      case 'p': return pink;
      case 'g': return green;
      case 'm': return magenta;
      case 'c': return cyan;
      case 'y': return yellow;
    }
}
struct Answer random() // generates a random set of 4 int 1-6
{
  Answer a;
  srand( time(NULL));
  a.one = rand()%6 + 1;
  a.two = rand()%6 + 1;
  a.three = rand()%6 + 1;
  a.four = rand()%6 + 1;
  return a;
}
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.