tags:

views:

237

answers:

3

“White" is the checking of correct number at wrong position. But I don't know how to count it correctly.

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int answer[4];
    int guess[4];
    int count = 0;

    srand(time(NULL)); 
    /*answer[0] = (rand() % 6)+1;
    answer[1] = (rand() % 6)+1;
    answer[2] = (rand() % 6)+1;
    answer[3] = (rand() % 6)+1;*/

    answer[0] = 3;
    answer[1] = 3;
    answer[2] = 5;
    answer[3] = 2;

    printf("%d %d %d %d\n", answer[0],  answer[1],  answer[2],  answer[3]);
    printf("          B W\n"); 

    do
    {
        int black = 0;
        int white = 0;
        count++;
        printf("Enter 4 numbers, this is your %d guess: ", count);
        scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);
        printf("\n%d %d %d %d\n", guess[0], guess[1], guess[2], guess[3]);

        int g2[2][6];

        for (int a = 0;a < 4;a++)
            g2[0][a]=answer[a];

        for (int i = 0;i < 4;i++)
            g2[1][i]=guess[i];

        if (answer[0]==guess[0])
            black++;
        if (answer[1]==guess[1])
            black++;
        if (answer[2]==guess[2])
            black++;
        if (answer[3]==guess[3])
            black++;

        if (answer[1]==guess[0] || answer[2]==guess[0] || answer[3]==guess[0])
            white++;
        if (answer[0]==guess[1] || answer[2]==guess[1] || answer[3]==guess[1])
            white++;
        if (answer[0]==guess[2] || answer[1]==guess[2] || answer[3]==guess[2])
            white++;
        if (answer[0]==guess[3] || answer[1]==guess[3] || answer[2]==guess[3])
            white++;

        if (black==4)
            white=0;

        g2[1][4]=black;
        g2[1][5]=white;

        for (int n = 0;n < 6;n++)
            printf(" %d",g2[1][n]); 

        printf("\n");
    }
    while (answer[0]!=guess[0] || answer[1]!=guess[1] ||
           answer[2]!=guess[2] || answer[3]!=guess[3]);

    printf("BINGO!!!\n");

    return 0;
}

Update 2:

for (int slot=0;slot<4;slot++)
{
    if (guess[slot] == answer[slot]) 
        black++;
    else 
        for (int s=0;s<4;s++)
            if (s != slot) 
            {
                if (guess[slot] == answer[s]) 
                    white++;
                break;
            }
}

Update 3:

for (int x=0;x<4;x++)
flag[x]=0;

for (int slot = 0;slot < 4;slot++) 
{ 
    if (guess[slot] == answer[slot])  
        if (flag[slot]==1)
            black++; 
    else  
        for (int s=0;s < 4;s++) 
            if (s != slot)  
            { 
                if (guess[slot] == answer[s])
                    if (flag[s]==1)
                    {
                        white++; 
                        break; 
                    }
            } 
} 

Update 4

for (int x=0;x<4;x++)
flag[x]=0;

for (int slot = 0;slot < 4;slot++) 
{ 
    if (guess[slot] == answer[slot])  {
        black++; 
        flag[slot]=1;
    }
else  
    for (int s=0;s < 4;s++)
        if (s != slot)
        { 
            if (guess[slot] == answer[s])  
            {
                white++; 
                flag[s]=1;
                break; 
            }
        } 
} 

Update 5:

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int answer[4];
    int guess[4];
    int flag[4];
    int count = 0;

    srand(time(NULL)); 
    /*answer[0] = (rand() % 6)+1;
    answer[1] = (rand() % 6)+1;
    answer[2] = (rand() % 6)+1;
    answer[3] = (rand() % 6)+1;*/

    answer[0] = 1;
    answer[1] = 2;
    answer[2] = 3;
    answer[3] = 4;

    printf("%d %d %d %d\n", answer[0],  answer[1],  answer[2],  answer[3]); 

    do
    {
        int black = 0;
        int white = 0;
        count++;
        printf("Enter 4 numbers, this is your %d guess: ", count);
        scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);

        int g2[2][6];

        for (int a = 0;a < 4;a++)
            g2[0][a]=answer[a];

        for (int i = 0;i < 4;i++)
            g2[1][i]=guess[i];

        for (int x=0;x<4;x++)
            flag[x]=0;

        for (int slot = 0;slot < 4;slot++) 
        { 
            if (guess[slot] == answer[slot])  
                black++; 
            else   
                for (int s=0;s < 4;s++)  
                    if (s != slot && guess[slot] == answer[s] && !flag[s]) 
                    {  
                        white++;  
                        flag[s]=1; 
                        break;  
                    }
        } 

        g2[1][4]=black;
        g2[1][5]=white;

        printf("Guess %d: ", count);

        for (int n = 0;n < 4;n++){
            printf(" %d",g2[1][n]);
        }

        printf(" Black: %d White: %d\n", g2[1][4], g2[1][5]);
        printf("\n");
    }
    while (answer[0]!=guess[0] || answer[1]!=guess[1] ||
           answer[2]!=guess[2] || answer[3]!=guess[3]);

    printf("BINGO!!!\n");

    return 0;
}
+1  A: 

Suggested alternative (pseudocode):

white = 0;
black = 0;
for slot=0 to 3
  if guess[slot] == answer[slot]
    black++
  else
    for s=0 to 3
      if s != slot
        if guess[slot] == answer[s]
          white++
          break

Update (detail)

 else  
    for (int s=0;s < 4;s++) 
      if (s != slot && guess[slot] == answer[s] && !flag[s])
      { 
          white++; 
          flag[s]=1;
           break; 
      }

I think this should do it: We're checking for another slot that's not the current one, one that's not a perfect (black) match, and that hasn't been used before to flag a black or white.

Carl Smotricz
Thanks, but how to add it in my codes? :(
Programme Newbie
Updated above, but still wrong in some case such as "3115". :(
Programme Newbie
Please make that a bit more specific: What was answer, guess and result pls?
Carl Smotricz
The answer is "3352". If I guess "3115", result should be 1 Black and 1 White. If I guess "0033", result should be 0 Black and 2 White.
Programme Newbie
Your update 2 is missing curly braces for the inner-most if, so it'll only check one slot per peg.
Michael Madsen
Like this? still not work:( if (guess[slot] == answer[s]) white++; { break; }
Programme Newbie
No: if (guess[slot] == answer[s]) { white++; break; }
Carl Smotricz
No, like this: if (guess[slot] == answer[s]) { white++; break; } The indentation in Carl's post shows where the blocks are.
Michael Madsen
It seems works, thanks;)
Programme Newbie
Actually, I don't think this really won't work after all. If you guessed 1123 and the code was 1234, it would give you a black and 3 whites, but there should only be 2 whites because the 1 has already been used for the black. You'd have to flag each peg in the answer as you used it, so you'd know not to use it again (and then, reset the flags before evaluating the next guess).
Michael Madsen
You could be right; I've forgotten the rules. Grandpa has a very nice set of rules, but we don't know if those are the rules from the OP's assignment.
Carl Smotricz
Which parts should I correct? :(
Programme Newbie
Add an array called flag, with 4 places (int flag[4]). Before running your check, iterate through the array and set them all to 0 (false). In the places you currently have black++ and white++, replace these with an if statement that checks if flag[slot] (if you're checking for black) or flag[s] (if you're checking for white) is true. If this is the case, THEN you execute black++/white++, and at the same time, set the flag you just tested to 1 (true). Try to do it on your own first - you've already used everything you need to write this, and you'll learn more that way.
Michael Madsen
What is the meaning of "iterate through the array and set them all to 0 (false)." ?
Programme Newbie
Read "iterate" as "loop". Read "loop" as "construct a `for` loop that runs over the possible subscripts of the array (i.e. 0 to 3) and does something with the contents at that index."
Carl Smotricz
for (int x=0;x<4;x++) flag[x]=0;Like this?
Programme Newbie
looks good to me.
Carl Smotricz
I have updated above, but seems not work, is it any wrongs?
Programme Newbie
Yes, you didn't correctly implement what Michael wrote. Look again and try a bit harder!
Carl Smotricz
Sorry...I still have no idea about that, which parts should I modify? :(
Programme Newbie
Updated. Still wrong:(
Programme Newbie
My answer has an update now, hopefully that will fix it.
Carl Smotricz
Oh...still not work. I have updated my whole program, is it any wrong?
Programme Newbie
My mistake, sorry: There should be a "flag[x] = 1" underneath "black++".
Carl Smotricz
"flag[slot] = 1"? It's seems really work now, thanks for all of you.:)
Programme Newbie
+2  A: 

Knuth answers this in The Computer as Master Mind and acknowledges that it's hard to define it exactly. He suggests this:

  1. Make two arrays, ans and guess, with a slot for each color.
  2. For each color, populate ans with the number of pegs of that color. Similarly for guess.
  3. Add up min(ans[i], guess[i]) for each i. This is whites plus blacks.
  4. Add up max(ans[i] - guess[i], 0) for each i. This is the number of whites.
Grandpa
Thanks, it's very detail.
Programme Newbie
A: 

try this:

    for (int x=0;x<4;x++)
        flag[x]=guess[x]==answer[x]?1:0;

    for (int slot = 0;slot < 4;slot++) 
    { 
        if (guess[slot] == answer[slot])  
            black++; 
        else   
            for (int s=0;s < 4;s++)  
                if (!flag[s] && guess[slot] == answer[s]) 
                {  
                    white++;  
                    flag[s]=1; 
                    break;  
                }
    } 
David Lazell