# mastermind problem..

• 10-27-2005
skelectron
mastermind problem..
Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> int three(); main() { srand((unsigned)time(NULL)); four(); } four() { static initdone,f,s,t,r,fo,p; int first,second,third,fourth; if(initdone==0) { initdone=1; f=(rand()%10); s=(rand()%10); t=(rand()%10); fo=(rand()%10); } r=0; p=0; printf("\nI Already Set 4 Code For You..\n"); printf("%d%d%d%d\n",f,s,t,fo); printf("Please Insert Your Guess:\n"); printf("First:"); scanf("%d",&first); printf("\nSecond:"); scanf("%d",&second); printf("\nThird:"); scanf("%d",&third); printf("\nFourth:"); scanf("%d",&fourth); if(first==f) r=r+1; else if(first==s||first==t||first==fo) {p=p+1; } if(second==s) r=r+1; else if(second==f||second==t||second==fo) {p=p+1; } if(third==t) r=r+1; else if(third==f||third==s||third==fo) {p=p+1; } if(fourth==fo) r=r+1; else if(fourth==f||fourth==s||fourth==t) {p=p+1; } printf("\nYou Have place %d exact place and %d wrong place",r,p); if(r==4) {printf("\nCongratulations\n"); return 0; } else if(!(r==4)); {four(); } return 0; }```
this is my code,but when the secret number is 1851 and i put 1111 for my guess,it will say that i have placing 2 exact place and 2 wrong place...anybody can give me a solution?thx before.. :)
• 10-27-2005
cwr
The logic is wrong.

For the two conditionals (second and third) where it's not the correct digit, you are checking if the digit entered matches any of the other digits, and incrementing p, so it says there are two in the wrong place:

if (second==f||second==t||second==fo)
in this case, second==f and second==fo are true, because 1==1, so it gets incremented.

The solution is to remove the test once you have found a match. For example when you see that the user has got the first digit (1) correct, don't check it again in subsequent tests.

I would do it in this order:

Find any digits that are a match in the correct position, as you find them, eliminate them from the future tests.
From what's left over, check if there have been any correct digits, but in the wrong place.

Also, you should really look into using an array, and a loop, rather than hard coding it with four comparisons and separate variable names for each digit.

Also, implicit int's went out of fashion in about 1988. Declare main as int main, declare four as int four(), and put a prototype above main. Also use static int foo, rather than just static foo;
• 10-27-2005
dwks
And indent your code, too.
• 10-27-2005
skelectron
hahaha,thanks for all the critics,actually now i'm trying to create the mastermind with array and for loop,but i still dont know how to start..anybody can give me the logical hints?:)
• 10-27-2005
cwr
I thought I gave you the logic above? :)

This seems like roughly the way to do it:

Create two array of N chars (in the case of mastermind, 4, but define this is a constant, say NDIGITS), call them correct and guess.

Iterate through each digit of guess, check if the digit in the same position in correct is the same, ie a correct guess in the right place. When that's the case, change the digit in guess and correct in that position to an arbitrary invalid value, say -1, and increment your "exact match" counter.

Then, iterate through each digit of the guess array again, but ignore the ones that are -1. For each of the digits in guess, iterate through the whole of correct that are not -1, and compare for equality. If you find one equal, then again mark the digit in the correct array -1, so you don't check again and increment your "correct value but wrong position" counter.
• 10-30-2005
skelectron
Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> int guess(int); int main() { int digits; srand((unsigned)time(NULL)); printf("\nWelcome To Mastermind!\n"); printf("How Much Digits Do You Want To Play:\n"); scanf("%d",&digits); guess(digits); return 0; } int guess(int code) { int get_random,get_guess,checking,checking2,right,place; int guess_code[code-1]; int secret_code[code-1]; right = 0; place = 0; for(get_random=0;get_random<code;get_random++) {      secret_code[get_random] = rand()%10;         printf("%d",secret_code[get_random]); } printf("\n I Have Made %d Digit For You To Guess..\n",code); for(get_guess=0;get_guess<code;get_guess++) {      printf("Guess #%d:",get_guess+1);         scanf("%d",&guess_code[get_guess]); } for(checking=0;checking<code;checking++) {         if(secret_code[checking]==guess_code[checking])         {                 right = right + 1;                 secret_code[checking] = -1;         }         for(checking2=0;checking2<code;checking2++)         {                 if(secret_code[checking] == guess_code[checking2])                 {                         place = place + 1;                         secret_code[checking] = -1;                 }         } } printf("\nYou have placing %d code at a right place and %d code at a wrong place..\n",right,place); }```
now i'm trying to program it with for loop,but i still have a problem with the wrong place,anyone can help?:)