Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: trying to code mastermind in C

  1. #1

    trying to code mastermind in C

    hi guys,

    i'm trying to code mastermind in C since i'm learning C and i thought it would be a nice practice ...

    there are only two problems...

    one:

    if user enters let's say 12345, he has to press <enter> after every digit, i've tried doing this with getchar() but then you would have the problem of comparing a character array vs a integer array...

    second:

    trying to print a '0' for every digit that is not in the right place, but is present in the secret code....
    e.g.:

    input code: secret code:
    25468 51197 -->should print: 0
    22813 45672 -->should print: 0 (not two 0's since the 2 is present only once)
    21678 32245 -->should print: 0 (not two 0's since there is just one as input)

    hope this clears things up of what i mean...

    here's the code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>


    int main()
    {
    srand( (unsigned)time( NULL ) );
    int play_or_instruct;
    int get_random;
    int max_tries;
    int get_answer;
    int print_answer;
    int count_equal;
    int count;
    int correct;
    int secret_code[5];
    int answer[5];
    printf("\nWelcome to mastermind.\n");
    printf("press 1 to play, press 2 for instructions :");
    scanf("%d",&play_or_instruct);
    if (play_or_instruct==2) /*check for playing the game or instructions*/
    {
    printf("\n\nThe goal of the game is to guess the secret\n");
    printf("secret code. You have 10 tries for this.\n");
    printf("The code contains 5 random numbers from 0 - 9.\n");
    printf("Every \'*\' you see next to your input, \n");
    printf("means that you have one number correct and\n");
    printf("at the right place.\n");
    printf("Every \'0\' you see next to your input,\n");
    printf("means that you have one number correct, but\n");
    printf("not at the right place.\n");
    play_or_instruct=1;
    }
    if (play_or_instruct ==1)
    {
    printf("\nHere we go....\n\n");

    for (get_random = 0; get_random <5; get_random++) /*get the secret code with the*/
    secret_code[get_random] = rand()%10; /*random function*/
    for(max_tries=0;max_tries<10;max_tries++) /*count number of tries*/
    {
    printf("\nEnter your guess: ");
    for(get_answer=0;get_answer<=4;get_answer++)/*get the answer given*/
    {
    scanf("%d",&answer[get_answer]);
    }
    printf("\n");
    for(print_answer=0;print_answer<=4;print_answer++)/*print the answer*/
    printf("%d",answer[print_answer]);/*given to the screen*/
    printf("\t");
    for(count_equal=0;count_equal<=4;count_equal++) /*check if a number is*/
    { /*correct and at the right place*/
    if(answer[count_equal]==secret_code[count_equal])
    printf("*"); /*print * if correct*/

    }
    count=0;
    for(correct=0;correct<=4;correct++)/*check if the complete code is */
    { /*correct, for each digit*/
    if(answer[correct]==secret_code[correct])/*count + 1*/
    count++;
    }
    if(count==5)/*if count is equal to 5, let them know they've won, and*/
    { /*end the game.*/
    printf("\nCongratulations, you've won!\n");
    return 0;
    }

    }
    if(max_tries==10)/*if maximum number of tries have passed, let them know*/
    { /*they didn't made it and end the game*/
    printf("\nUnfortunately you did not make it.\n");
    return 0;
    }
    }
    }

    /*only got two problems,
    1st problem:
    getting to print a '0' for each character that is present, but not at the right place, and don't print it double if one character is compaired to two.
    2nd problem:
    as the code is now, the user has to press <enter> after each digit they enter, this has to change. e.g. when entering 12345, it now is 1<enter>2<enter>3<enter>4<enter>5<enter>.*/
    i've also attached the code, since this one is more readable with the spaces between them, and if someone wanted t o test it, this is much easier...

    thanks in advance for the replies....

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    For the first problem (I'll have a look at your second problem when I get home from work), you can use the following code to allow you to use two integer arrays:

    Code:
    int myint = atoi(getchar());
    atoi(char mychar) takes a character and converts it to an integer (note the difference between conversion and typecasting. I believe it support character arrays as well, so if you have a number that uses more than one character to display, it should also work.

    ac

  3. #3
    well, i can't seem to get it to work, but i'll get into it, perhaps i'm just making a mistake...

    i've tried changing answer[5] and secret_code[5] to char instead of array, but that doesn't work, i've tried removing the declaration at the top, and typed "int answer[get_answer]=atoi(getchar());" at the for loop, but this doesn't work...

    i've tried the same as above with char instead of int, i've tried it without the declaration, and kept the declaration at the top, also no success....

    but i'll keep looking at it....


    b.t.w. how did you like my comments and var names ?

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    i'm trying to code mastermind in C

    What does this means, if i understand what u want i can help u out.
    Cheers
    U get What U pay for.

  5. #5
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    lol, your comments and var names are amazingly good BTW -- I wasn't meaning that for absolutely everything, you had to think up a name for it; just try not to have big lists of variables which are just single letters.

    At the moment I'm trying to get getchar() to work. Bah...this aint going well. I thought it worked in relatively the same fashion as getch() - obviously not

    ac

  6. #6
    no, i've tried getchar() too, but then you get some strange output of about 10 different digits...

    i've found a function called getint(), don't know if this is specific for c++ or C, that's not clear yet, and i didn't got it to work yet....

    as for the vars, i didn't think i had to show it to someone at first, so that's why it were all single characters, but i thought, if i should rename them, then i might as well do it the best i can

    harbir: don't you know the game mastermind? it is a very known game usually played with colors, check this link here for an example of the game, only difference, i try to create one text based....

  7. #7
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I can get getchar() to work fine, but the problem is that it doesn't work the same as getch(), i.e. you still need to press return after typing in the number. You can get it to work and convert the return value to an integer by doing the following:

    Code:
    // ...all the usual crap before this
    
    char gcc;
    const char *pgcc = &gcc;
    int selection;
    
    gcc = (char)getchar();
    
    selection = atoi(pgcc);
    Hope that was the correct code But since it doesn't provide the functionality you require, I think the above code is fairly useless. I will keep searching for an answer for you, but at the moment all I can say is that (despite the fact that I compile all my c/c++ code on a linux box) it would be so much easier to compile this program using a windows compiler and the getch() function.

    [edit]missed out the typecast[/edit]

    ac

  8. #8
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    Hmm there is the possiblity of trying to use scanf() to get the variables in their right places. That way, you can read them straight as ints and do not have to worry about type cast.

    I will look more over the code and try to come up with some other stuff... btw, for atoi() did you include string.h? I think that's the library where the function is declared... if not, in what sense did it not work?
    /\\

  9. #9
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Ignore my last batch of code, it should work with:

    Code:
    char mychar;
    int myint;
    
    mychar = (char)getchar();
    myint = atoi(&mychar);
    Hypronix, the reason he can't use scanf is that he wants to implement the same functionality as getch() for windows - i.e. he doesn't want the user to have to press [return] after each number.

    One way to get round this that I though about would be to simply have the user type all four numbers and then "parse" them from the character array that you are storing them in. That should work fine.

    [edit]It is possible to use the ncurses library to use getch(), but I doubt you'll want to do that because it looks like a pain in the ass. It basically simulates a windowing interface in a text only environment and you have to add in a whole lot of code to get it to work as far as I can tell[/edit]

    ac

  10. #10
    Well, i've tried the getch() on both windows and linux (this was originally intended for linux) but i've got a copy of DEV-C++ too, and it compiles fine with it., as for the atoi(), i didn't include string.h, perhaps it will work...
    Ignore my last batch of code, it should work with:



    code:--------------------------------------------------------------------------------char mychar;
    int myint;

    mychar = (char)getchar();
    myint = atoi(&mychar);--------------------------------------------------------------------------------
    i'll give this a try as soon as i get home...
    one other question, how did you print the above section with the code: ? the only thing i can find to print code is with the quote option... is it or something like that?

    thanks for helping me solve this problem guys, i really appreciate it

    [edit]never mind the code question, as soon as i posted this post, i saw that [ code][ /code] worked to put the code in the post [/edit]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •