trying to code mastermind in C - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: trying to code mastermind in C

  1. #11
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    AFAIK, atoi() isn't in string.h. I've been compiling all my tests in gcc without string.h and it works fine. If you're not getting a message about undeclared identifiers, then it probably isn't string.h, it'll be the way it has been coded.

    [edit]atoi() is in stdlib.h[/edit]

    ac

  2. #12
    well, i've tried your code like this:
    Code:
    char mychar;
    			for(get_answer=0;get_answer<=4;get_answer++)/*get the answer given*/
    			{
    				mychar=(char)getchar();
    				answer[get_answer]=atoi(&mychar);
    					/*scanf("%d",&answer[get_answer]);*/
    
    			printf("\n");
    			}
    and it works but now i've got another problem:

    when i enter '12345', and it prints my input, it prints '01234', why is this? can i solve this by adding 1 to each number i put in, or is there another solution to this?

  3. #13
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    The answer is to not use getchar() I think we should resign ourselves to the fact that getchar isn't going to work in your problem. I think you either need to get the user to enter the whole number and scanf() it, then parse the individual numbers from the input, or you need to compile on a windows system and use getch() (or rather, compile using a non-standard compiler).

    ac

  4. #14
    i see what you mean, but if the user would enter 12345 as an integer, how can i make sure that i've got all the number seperated?

    would it be something like:
    Code:
    scanf("%d",&myint);
    answer[0]=myint/10000;
    answer[1]=(myint/11000)*2;
    answer[2]=(myint/11100)*3;
    etc...
    don't mind the alghoritm, cause it will not work if user inputs something else then 12345,
    but is it possible to solve it like this?

  5. #15
    i've altered my code and it seems to work, but it doesn't
    can someone please tell me why???

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define false 0
    #define true 1
    
    
    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 correctplace=0;
    	int run,a;
    	int correctdigit=0;
    	int secret_code[5];
    	int answer[5];
    	int check[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");
    			check[get_answer] = false;
    			for (get_answer = 0; get_answer < 5; get_answer++)
    			{
       				if (answer[get_answer] == secret_code[get_answer])
       				{
          					correctplace++;
          					check[get_answer] = true;
       				}
    			}
    				
    			for (get_answer = 0; get_answer < 5; get_answer++)
    			{
    			   for (run = 0; run < 5; run++)
    			   {
    			      if ((answer[get_answer] == secret_code[run]) && (check[run] == false))
    			      {
    			         correctdigit++;
    			         check[run] = true;
    			      }
    			   }
    			}
    			if(correctplace ==5)
    			{
    				printf("\nCongratulations, you've won!\n");
    				return 0;
    			}
    			for(;correctplace >=0;correctplace--)
    				printf("*");
    			for(;correctdigit >=0;correctdigit--)
    				printf("0");
    			printf("\n");
    			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;
    			}
    		}
    	}
    	return 0;
    }
    especially the part where the *'s and 0's should be printed..

  6. #16
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Hey man, can you do us a favour and send a worked example. As in just post an example of what it should look like (pretend you're playing the game and type up as a post what you would see on the screen) That'll make it more obvious what's going wrong.

    ac

  7. #17
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    You need to use special chars to display special chars with printf

    And I think what you need to do is %' instead of just ' to print it out correctly. Similarily, cout uses \ to display such characters. I recommend you review the syntax of some of the more basic functions and always have a good referrence book [or two] handy while coding, especially when bugs show up.
    /\\

  8. #18
    Hey man, can you do us a favour and send a worked example. As in just post an example of what it should look like (pretend you're playing the game and type up as a post what you would see on the screen) That'll make it more obvious what's going wrong.
    the above code is a working example, but if you want i can compile it for you and put it in as an attachment, tell me what you want, linux or windows?

    as for the things that go wrong, when you enter 12345, it could give an output like this:

    12345 *0

    if you then enter 11111

    it might give an output like:


    11111 0

    that's what i mean, how is it possible when i enter 11111 it says that there is one digit correct, but not at the right place??

    and more of these strange things happen...


    You need to use special chars to display special chars with printf

    And I think what you need to do is %' instead of just ' to print it out correctly. Similarily, cout uses \ to display such characters. I recommend you review the syntax of some of the more basic functions and always have a good referrence book [or two] handy while coding, especially when bugs show up.
    i don't think i do, because if you compile the above source, it does print *'s and 0's ,but not like they should in order to let the game work, the program is working though, just not like the game should....


    hope this clears things up...

  9. #19
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    Okay it wasn't clear for me, I guess Dev uses a different way than gcc maybe... or maybe I'm wrong.

    It seems as a bug, so I can't give you a quick answer, I'll need to take some time and review the code.
    /\\

  10. #20
    well, i've compiled it with DEV-C++ too, and the program works with it, (on windows), just the same as on linux, and with also the same problem, program works, but not like the game is intended...

Posting Permissions

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