Mastermind Assistant Program - Help needed

This is a discussion on Mastermind Assistant Program - Help needed within the C Programming forums, part of the General Programming Boards category; This program should print all the possible solution for a single mastermind game. The output should look like this: Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    Mastermind Assistant Program - Help needed

    This program should print all the possible solution for a single mastermind game.
    The output should look like this:
    Code:
    Enter the pattern length: 3
    Input the guess pattern: abc
    Enter the number of black pegs in the feedback: 2
    Enter the number of white pegs in the feedback: 0
    The possible key patterns are:
    aacaba
    abb
    abd
    abe
    abf
    acc
    adc
    aec
    afc
    bbc
    cbc
    dbc
    ebc
    fbc
    Here is my code and have no idea why it wouldn't work.....

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    
    
    
    
    
    
    void scanIn(char *pattern, int length)
    {
        char input[length + 1];
        scanf("%s", input);
    
    
        int i;
        for(i = 0; i < length; i++)
        {
            pattern[i] = input[i];
        }
    }
    
    
    
    
    void printresult(int length, int count, char *guess, int black, int white, char *test)
    {
        int i,j,k;
        bool gues........[length];
        bool testHit[length];
        int blackFeedBack;
        int whiteFeedBack;
        if(count == length)
        {
            int x;
            for(x = 0; x < length; x++)
                printf("%c", test[x]);
            printf("\n");
            return;
        }
        else
        {
            char t;
            for (t = 'a'; t <= 'f'; t++)
            {
                test[count] = t;
                bool rightGuess = true;
    
    
                for(i = 0; i < length; i++)
                {
                    gues........[i] = false;
                    testHit[i] = false;
                    blackFeedBack = 0;
                    whiteFeedBack = 0;
                }
    
    
                for(i = 0; i < length; i++)
                {
                    if(guess[i] == test[i])
                    {
                        blackFeedBack++;
                        gues........[i] = true;
                        testHit[i] = true;
                    }
                }
    
    
                for(i = 0; i < length; i++)
                {
                    for(j = 0; j < length && !testHit[i]; j++)
                    {
                        if(guess[i] == test[j] && !gues........[j])
                        {
                            whiteFeedBack++;
                            testHit[i] = true;
                            gues........[j] = true;
                        }
                    }
                }
    
    
    
    
                printf("b:%d,%d w:%d,%d\n", blackFeedBack, black, whiteFeedBack, white);
    
    
    //            int x;
    //            for(x = 0; x < length; x++)
    //                printf("%c", test[x]);
    //            printf("\n");
    
    
                if(blackFeedBack != black)
                    rightGuess = false;
                else if(whiteFeedBack != white)
                    rightGuess = false;
                if(rightGuess)
                    printf("This is a right guess!\n");
                if(rightGuess)
                {
                    printresult(length, count + 1, guess, black, white, test);
                }
            }
        }
    }
    
    
    int main(void)
    {
        int length;
        printf("Enter the pattern length: ");
        scanf("%d", &length);
    
    
        char guess[length];
        printf("Input the guess pattern: ");
        scanIn(guess, length);
    
    
        int black, white;
        printf("Enter the number of black pegs in the feedback: ");
        scanf("%d", &black);
    
    
        printf("Enter the number of white pegs in the feedback: ");
        scanf("%d", &white);
    
    
    
    
        char test[length+1];
        int i;
        for(i = 0; i < length; i++)
        {
            test[i]='a';
        }
        test[length] = '\0';
    
    
    
    
        printf("The possible key patterns are: \n");
        printresult(length, 0, guess, black, white, test);
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    This is the newest version of my code. It works but it take forever to due with larger guess pattern length.

    What can I do to my code to speed it up?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    
    
    
    
    
    
    void scanIn(char *pattern, int length)
    {
        char input[length + 1];
        scanf("%s", input);
    
    
        int i;
        for(i = 0; i < length; i++)
        {
            pattern[i] = input[i];
        }
    }
    
    
    bool pegsTester1(char *guess, char *test, int black, int white, int length)
    {
        int i,j,k;
        bool gues........[length];
        bool testHit[length];
        int blackFeedBack;
        int whiteFeedBack;
    
    
        for(i = 0; i < length; i++)
            {
                gues........[i] = false;
                testHit[i] = false;
                blackFeedBack = 0;
                whiteFeedBack = 0;
            }
    
    
            for(i = 0; i < length; i++)
            {
                if(guess[i] == test[i])
                {
                    blackFeedBack++;
                    gues........[i] = true;
                    testHit[i] = true;
                }
            }
    
    
            for(i = 0; i < length; i++)
            {
                for(j = 0; j < length && !testHit[i]; j++)
                {
                    if(guess[i] == test[j] && !gues........[j])
                    {
                        whiteFeedBack++;
                        testHit[i] = true;
                        gues........[j] = true;
                    }
                }
            }
    
    
            if(blackFeedBack != black || whiteFeedBack != white)
                return false;
            else
                return true;
    
    
    }
    
    
    
    
    void printresult(int length, int count, char *guess, int black, int white, char *test)
    {
        if(count == length)
        {
            bool rightGuess = pegsTester1(guess, test, black, white, length);
            if(rightGuess)
            {
                int x;
                for(x = 0; x < length; x++)
                    printf("%c", test[x]);
                printf("\n");
            }
            return;
        }
        else
        {
            char t;
            for (t = 'a'; t <= 'f'; t++)
            {
                test[count] = t;
                printresult(length, count + 1, guess, black, white, test);
            }
        }
    }
    
    
    int main(void)
    {
        int length;
        printf("Enter the pattern length: ");
        scanf("%d", &length);
    
    
        char guess[length];
        printf("Input the guess pattern: ");
        scanIn(guess, length);
    
    
        int black, white;
        printf("Enter the number of black pegs in the feedback: ");
        scanf("%d", &black);
    
    
        printf("Enter the number of white pegs in the feedback: ");
        scanf("%d", &white);
    
    
    
    
        char test[length+1];
        int i;
        for(i = 0; i < length; i++)
        {
            test[i]='a';
        }
        test[length] = '\0';
    
    
    
    
        printf("The possible key patterns are: \n");
        printresult(length, 0, guess, black, white, test);
    
    
        return 0;
    
    
    }

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,048
    What is this? It's definitely not right.

    Code:
    bool gues........[length];
    What input are you giving the program?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Sorry that variable got cencored by the system, it suppose to be guess_Hit without the underscore

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,048
    Ha! That's rich.
    So what input are you giving the program?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    So, the input should be the length of the pattern, the pattern itself and the number of black and white pegs.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,048
    I mean, what values are you actually entering?
    I'm trying to give it input, but it just prints "The possible key patterns are:" and stops. I'd rather have good data to enter so I can narrow down on the possible issue, rather than trying to understand the program in its entirety.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by Jesterira View Post
    Sorry that variable got cencored by the system, it suppose to be guess_Hit without the underscore
    Lol. It's like "assumption" being censored as "buttumption", or "assassination" as "buttbuttination".

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    There is an example of the output at the main post. If that what you mean. The length is 3, the pattern is abc and the black is 2 the white is 0.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,048
    Yes ... but you've since talked about the program being slow with larger inputs. Ah, heck, that's enough to go on.

    I used a length of 8 and it didn't "take forever." Can you explain how slow it is running for you? And how you're measuring it?

    [aside] I'm glad my editor is not as upset about perceived vulgarity as the forum software

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Actually. At the length of 8 the program still works fine. But for some reason, I need it to work for more than a length of 15 with a runtime less than 30 second. So, what should I do? Thanks for helping tho.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,048
    Well, at first I thought you might be running into delays with recursion. However, I'm kind of tired and having trouble deciphering your code, especially your "printresult()" function, which is the cause of the massive delays.

    So, I did a little analysis to get a better understanding of what was going on. I ran the program with the following inputs:

    Code:
    10            // not even the 15 you're looking for
    abcdefghij
    2
    0
    It took about 32.6 seconds, and found that the "printresult()" function was called 72,559,411 times. My computer doesn't even seem to be interested in trying to calculate it with a length of 15.

    I'd consider restructuring the function - but if you're limited by the rules of the game, there might be little you can do.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Is there another way to solve this problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Marking assistant
    By walay234 in forum C Programming
    Replies: 1
    Last Post: 10-17-2011, 04:00 AM
  2. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 01:49 AM
  3. A problem of if-else .Want the assistant
    By poolomlpz in forum C# Programming
    Replies: 4
    Last Post: 07-03-2009, 04:29 PM
  4. Mastermind program problem with iterator
    By Truckomobil in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2009, 01:16 PM
  5. Algorithmic Mastermind Program
    By pr0n in forum C Programming
    Replies: 1
    Last Post: 05-12-2008, 11:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21