How to write a Mastermind game without using array and pointer?

This is a discussion on How to write a Mastermind game without using array and pointer? within the C Programming forums, part of the General Programming Boards category; I am writing a mastermind game using c but array and pointer are not allowed. The game requires the user ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    How to write a Mastermind game without using array and pointer?

    I am writing a mastermind game using c but array and pointer are not allowed.
    The game requires the user to guess the numbers only, guessing color is not required.

    If array can not be used, I have to write a long code in a for loop, I don't want to write so many "if then else" statement to code all the possibilities.

    What algorithm should I use?

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    if your only guessing numbers why not just say:
    Code:
    do
    {
     scanf("%d",&guess);
     if(guess != number);
      printf("try again");
    }while(guess != number);

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Are you expected to get say
    1234 (the hidden secret)

    and the user types in
    1247

    Results in the following output
    2 digits are correct and in the right place
    1 digit is correct and in the wrong place
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by Salem View Post
    Are you expected to get say
    1234 (the hidden secret)

    and the user types in
    1247

    Results in the following output
    2 digits are correct and in the right place
    1 digit is correct and in the wrong place
    Yes, this is what I want.
    Here is the source of my program, how to improve it?
    I don't want to use so many "if else" statement

    Another problem is that the program calculate the near match wrongly when repeated numbers appear, how to solve it?

    Code:
    #include<stdio.h>
    #include<time.h>
    
    main()
    {
    	srand(time(NULL));
    	int c1 = rand() %9 + 1,
    		c2 = rand() %9 + 1,
    		c3 = rand() %9 + 1,
    		c4 = rand() %9 + 1,
    		g1, g2, g3, g4,turn,exact,near;
    	//printf("Answer is: %d %d %d %d\n",c1,c2,c3,c4);
    	exact = 0;
    	near = 0;
    	for (turn = 1; turn <= 10; turn++)
    	{		
    		printf("Turn %d\n",turn);
    		printf("Please enter your guess (e.g. 1 3 3 4):\n");
    		scanf("%d %d %d %d",&g1,&g2,&g3,&g4);
    		
    		if (g1==c1)
    		exact++;
    		else if (g1==c2||g1==c3||g1==c4)
    		near++;
    		
    		if (g2==c2)
    		exact++;
    		else if (g2==c1||g2==c3||g2==c4)
    		near++;
    		
    		if (g3==c3)
    		exact++;
    		else if (g3==c1||g3==c2||g3==c4)
    		near++;
    		
    		if (g4==c4)
    		exact++;
    		else if (g4==c1||g4==c2||g4==c3)
    		near++;
    		
    		printf("Number of Exact Match: %d\n",exact);
    		printf("Number of Near Match: %d\n\n",near);
    		
    		if (exact==4)
    		{printf("You break the code by trying %d time(s)\n",turn);
    		printf("Game Over!");
    		turn = 10;}
    		else
    		{exact = 0;
    		near = 0;}
    		
    		if (turn==10 && exact!=4)
    		{printf("You fail to break the code!\n");
    		printf("Game Over!");
    		printf("\nAnswer is: %d %d %d %d\n",c1,c2,c3,c4);}
    	}	
    }
    Last edited by wtxwt; 10-19-2007 at 02:48 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Well you could 'fake' your array by doing say
    target = c1 * 1000 + c2 * 100 + c3 * 10 + c4;
    Then using lots of /10 and &#37;10 in a loop to extract digits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by Salem View Post
    Well you could 'fake' your array by doing say
    target = c1 * 1000 + c2 * 100 + c3 * 10 + c4;
    Then using lots of /10 and %10 in a loop to extract digits.
    it then generates another problem, the input format required is "1 2 3 4", if I combine all the number into a 4-digit number, how is this input format possible?

    How to solve the problem of calculating the near match wrongly?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by wtxwt View Post
    it then generates another problem, the input format required is "1 2 3 4", if I combine all the number into a 4-digit number, how is this input format possible?

    How to solve the problem of calculating the near match wrongly?
    Salem meant to put it together as one number, and then work on it like that AFTER you've read the individual numbers.

    To avoid your "near" mismatch when same numebr is present multiple times, you have to "remove the fund number". You may want to do this as a function, and first do the exact numbers, then find the numbers that are "near" after you've removed the exact ones.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by matsp View Post
    Salem meant to put it together as one number, and then work on it like that AFTER you've read the individual numbers.

    To avoid your "near" mismatch when same numebr is present multiple times, you have to "remove the fund number". You may want to do this as a function, and first do the exact numbers, then find the numbers that are "near" after you've removed the exact ones.

    --
    Mats
    What is the meaning of "remove the fund number"? I don't understand.
    What to do with it when I convert the 4 numbers into a single 4-digit number?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just set that digit to zero. So you pass in a 4-digit number, then you extract the individual digits, and if the digit is a match [exact or otherwise], you set the digit to zero. Then it will not compare with any of the numbers any longer.

    You can of course do the same thing in your code right now - set the guess to 0 when you've "used it" for a match - exact or near.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by matsp View Post
    Just set that digit to zero. So you pass in a 4-digit number, then you extract the individual digits, and if the digit is a match [exact or otherwise], you set the digit to zero. Then it will not compare with any of the numbers any longer.

    You can of course do the same thing in your code right now - set the guess to 0 when you've "used it" for a match - exact or near.

    --
    Mats
    I want to simplify my code further, those "if else" statement is too long, if I convert it into a 4-digit number, does that mean I can pretend to use a array but not using a array actually?

    What's the use of converting it to a 4-digit number and extract it again? Is it repetitious?
    ?
    Last edited by wtxwt; 10-19-2007 at 08:52 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you can use an int to represent a 4-digit number, call a function with that int, and extract each digit by using a loop that does "x = y %10; y /= 10" - x becomes the least significant digit, y is then divided to hold only the digits you have left.
    E.g.
    y = 1234
    x = 1234 % 10 -> x = 4
    y /= 10 -> y = 123
    ...
    x = 123 % 10 -> x = 3
    ...

    Since you know it's going to be 4 digits, it's pretty easy to do the loop for this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by matsp View Post
    Yes, you can use an int to represent a 4-digit number, call a function with that int, and extract each digit by using a loop that does "x = y %10; y /= 10" - x becomes the least significant digit, y is then divided to hold only the digits you have left.
    E.g.
    y = 1234
    x = 1234 % 10 -> x = 4
    y /= 10 -> y = 123
    ...
    x = 123 % 10 -> x = 3
    ...

    Since you know it's going to be 4 digits, it's pretty easy to do the loop for this.

    --
    Mats
    I think I need some time to figure out what you are saying, it's difficult to me because I am a beginner in C and I've just learned it for 2 days

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    I've encounterd a problem when comparing the near match, in a single "for loop" I can only compare the numbers of the same position in the 4-digit number, this makes the comparison impossible.

    The good news is that I finally finish the exact match part, that's really a piece of cake with the help of the fake array.

    Do I need to use "looping the loop" or 2 "for loop" to write it or anything else?

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    I am still not able to calculate the number of near match correctly, when repeated numbers exist in the code or the guess, the number of near match may become wrong.
    Can anyone help me?
    Last edited by wtxwt; 10-22-2007 at 05:05 AM.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    When you've matched a near value, you need to mark it in some way as being matched already.

    Eg. code = 1223 guess = 1322

    If for example zero is never used, then when the 3 is matched as a near guess, the guess becomes say 1022 to prevent the 3 from being matched again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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