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

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way, in my case, you could move the "g != 0" part to become a "if (g !=0)" outside the inner for-loop, so the whole loop is not run when the guess has already been used up.

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

  2. #32
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    I've written another version, this one should contain no bug and is easier for users to understand, it's just for fun

    Code:
    #include<stdio.h>
    
    
    main()
    {
    	srand(time(NULL));
    	int c,g,i,j,b;
    	
    
    	for (i = 1; i <= 4; i++)
    	{
    		c = rand();
    		scanf("&#37;d",&g);
                                    if (g == c)
    		{
    			b = true;
    		}	
    	}
    	
    		
    
    }
    Last edited by wtxwt; 10-24-2007 at 08:51 AM.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've written another version, this one should contain no bug and is easier for users to understand, it's just for fun
    Have you tested your program yet? You have a couple of array index out of bounds errors, at the very least.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Someone has written another version in another forum, I think it's better than mine, simple and efficient

    Code:
    #include <stdio.h>
    
    int main()
    {
      int answer = 0;
      int guess;
      int black = 0;
      int white = 0;
      int round = 1;
      int a1, a2, a3, a4;  
      int g1, g2, g3, g4;  
      int i;
    
      srand(time(NULL));
      for (i = 0; i < DIGIT_COUNT; ++i) {
        answer *= 10;
        answer += rand() &#37; DIGIT_LENGTH + MIN_DIGIT;
      }
      printf("The answer is %d\n", answer);
      for (round = 1; black != DIGIT_COUNT && round <= MAX_ROUND; ++round) {
        int workAnswer;
        int workGuess;
        int validInput;
    
        do {
          printf("Round %2d: Your guess (-1 to quit): ", round);
          validInput = scanf("%d", &guess);
          if (validInput == 0) {
            fflush(stdin);
            puts("Invalid input. Please enter again.");
          }
        } while (!validInput);
        if (guess == -1)
          break;
    
    
        a1 = a2 = a3 = a4 = 0;
        g1 = g2 = g3 = g4 = 0;
        black = 0;
        workAnswer = answer;
        workGuess = guess;
        if (workAnswer % 10 == workGuess % 10) {
          ++black;
          a1 = g1 = 1;
        }
        workAnswer /= 10;
        workGuess /= 10;
        if (workAnswer % 10 == workGuess % 10) {
          ++black;
          a2 = g2 = 1;
        }
        workAnswer /= 10;
        workGuess /= 10;
        if (workAnswer % 10 == workGuess % 10) {
          ++black;
          a3 = g3 = 1;
        }
        workAnswer /= 10;
        workGuess /= 10;
        if (workAnswer % 10 == workGuess % 10) {
          ++black;
          a4 = g4 = 1;
        }
    
        white = 0;
        workGuess = guess;
    
        
        if (g1 == 0) {
          int done = 0;
          workAnswer = answer;
          
          if (!done && workAnswer % 10 == workGuess % 10 && a2 == 0) {
            ++white;
            a2 = 1;
            done = 1;
          }
          workAnswer /= 10;
          if (!done && workAnswer % 10 == workGuess % 10 && a3 == 0) {
            ++white;
            a3 = 1;
            done = 1;
          }
          workAnswer /= 10;
          if (!done && workAnswer % 10 == workGuess % 10 && a4 == 0) {
            ++white;
            a4 = 1;
            done = 1;
          }
        }
    
       
        workGuess /= 10;
        if (g2 == 0) {
          int done = 0;
          workAnswer = answer;
          if (!done && workAnswer % 10 == workGuess % 10 && a1 == 0) {
            ++white;
            a1 = 1;
            done = 1;
          }
          workAnswer /= 10;
          workAnswer /= 10; /* skip digit 2 */
          if (!done && workAnswer % 10 == workGuess % 10 && a3 == 0) {
            ++white;
            a3 = 1;
            done = 1;
          }
          workAnswer /= 10;
          if (!done && workAnswer % 10 == workGuess % 10 && a4 == 0) {
            ++white;
            a4 = 1;
            done = 1;
          }
        }
    
       
        workGuess /= 10;
        if (g3 == 0) {
          int done = 0;
          workAnswer = answer;
          if (!done && workAnswer % 10 == workGuess % 10 && a1 == 0) {
            ++white;
            a1 = 1;
            done = 1;
          }
          workAnswer /= 10;
          if (!done && workAnswer % 10 == workGuess % 10 && a2 == 0) {
            ++white;
            a2 = 1;
            done = 1;
          }
          workAnswer /= 10;
          workAnswer /= 10; /* skip digit 3 */
          if (!done && workAnswer % 10 == workGuess % 10 && a4 == 0) {
            ++white;
            a4 = 1;
            done = 1;
          }
        }
    
        
        workGuess /= 10;
        if (g4 == 0) {
          int done = 0;
          workAnswer = answer;
          if (!done && workAnswer % 10 == workGuess % 10 && a1 == 0) {
            ++white;
            a1 = 1;
            done = 1;
          }
          workAnswer /= 10;
          if (!done && workAnswer % 10 == workGuess % 10 && a2 == 0) {
            ++white;
            a2 = 1;
            done = 1;
          }
          workAnswer /= 10;
          if (!done && workAnswer % 10 == workGuess % 10 && a3 == 0) {
            ++white;
            a3 = 1;
            done = 1;
          }
          no need to check digit
        }
        printf("Black: %d  White: %d\n", black, white);
      }
      printf("The answer is %d\n", answer);
    
      return 0;
    }
    Last edited by wtxwt; 10-24-2007 at 09:01 AM.

  5. #35
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well that is until they used fflush(stdin), then it all went horrible again.

    > no need to check digit
    What is this half a dozen lines from the end?
    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.

  6. #36
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't use arrays or pointers, but can you use functions? Because it would make the code a lot simpler if you can and do. (At the risk of repeating matsp's words.)
    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.

  7. #37
    Registered User
    Join Date
    Dec 2007
    Posts
    1
    Quote Originally Posted by wtxwt View Post
    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);}
    	}	
    }
    How if wanna do this by using array? Currently my assignment same urs but need do it with array. Google through the web and still duno wat to do ...

  8. #38
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Next time, you should probably start a new topic and just link to the old one instead of bumping an older topic.

    If you understand how Mastermind works, and you have a decent idea of how the I/O of the game should work, then this should be non-trivial.

    Show us what you have thus far and where you're stuck. If you just want to know where to start, start here:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	/* Put your code here */
    	return 0;
    }

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, 03: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, 09: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