mastermind problem..

This is a discussion on mastermind problem.. within the C Programming forums, part of the General Programming Boards category; 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; ...

  1. #1
    C Newbie
    Join Date
    Oct 2005
    Posts
    13

    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..

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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;

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    And indent your code, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C Newbie
    Join Date
    Oct 2005
    Posts
    13
    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?

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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.

  6. #6
    C Newbie
    Join Date
    Oct 2005
    Posts
    13
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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