Barry King Barry King - 9 months ago 101
Objective-C Question

MasterMind scoring algorithm in Objective C

I'm looking for an elegant way to compute the score of a guess in the MasterMind game in Objective C, based on this article already on stackoverflow:

MasterMind scoring algorithm in C# using LINQ

Max Max
Answer Source

For now I've come up with this (well, it's actually C but you can easily rewrite it using NSArray and isEqual)

int secret[] = { 1, 2, 3, 1 };
int guess[]  = { 1, 1, 2, 2 };
int white = 0, black = 0;
for(int i=0; i<4; ++i) {
    if( secret[i] == guess[i] ) {
        secret[i] = 0;
        ++white;
        continue;
    }
    for(int j=0; j<4; ++j) {
        if( secret[j] == guess[i] ) {
            secret[j] = 0;
            ++black;
            break;
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download