Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to count the "white" correctly in mastermind guessing game in c?
    text
    copied!<p>“White" is the checking of correct number at wrong position. But I don't know how to count it correctly.</p> <pre><code>#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", &amp;guess[0], &amp;guess[1], &amp;guess[2], &amp;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 &lt; 4;a++) g2[0][a]=answer[a]; for (int i = 0;i &lt; 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 &lt; 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; } </code></pre> <hr> <p>Update 2:</p> <pre><code>for (int slot=0;slot&lt;4;slot++) { if (guess[slot] == answer[slot]) black++; else for (int s=0;s&lt;4;s++) if (s != slot) { if (guess[slot] == answer[s]) white++; break; } } </code></pre> <hr> <p>Update 3:</p> <pre><code>for (int x=0;x&lt;4;x++) flag[x]=0; for (int slot = 0;slot &lt; 4;slot++) { if (guess[slot] == answer[slot]) if (flag[slot]==1) black++; else for (int s=0;s &lt; 4;s++) if (s != slot) { if (guess[slot] == answer[s]) if (flag[s]==1) { white++; break; } } } </code></pre> <hr> <p>Update 4</p> <pre><code>for (int x=0;x&lt;4;x++) flag[x]=0; for (int slot = 0;slot &lt; 4;slot++) { if (guess[slot] == answer[slot]) { black++; flag[slot]=1; } else for (int s=0;s &lt; 4;s++) if (s != slot) { if (guess[slot] == answer[s]) { white++; flag[s]=1; break; } } } </code></pre> <hr> <p>Update 5:</p> <pre><code>#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", &amp;guess[0], &amp;guess[1], &amp;guess[2], &amp;guess[3]); int g2[2][6]; for (int a = 0;a &lt; 4;a++) g2[0][a]=answer[a]; for (int i = 0;i &lt; 4;i++) g2[1][i]=guess[i]; for (int x=0;x&lt;4;x++) flag[x]=0; for (int slot = 0;slot &lt; 4;slot++) { if (guess[slot] == answer[slot]) black++; else for (int s=0;s &lt; 4;s++) if (s != slot &amp;&amp; guess[slot] == answer[s] &amp;&amp; !flag[s]) { white++; flag[s]=1; break; } } g2[1][4]=black; g2[1][5]=white; printf("Guess %d: ", count); for (int n = 0;n &lt; 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload