Thread: Finishing up Mastermind game program

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Finishing up Mastermind game program

    The last problem Im having is finding the right code to fix the almost function when there are more than one of a number

    ie: Code is 7880
    if I say 7899, it will say 1 right, almost 2
    How do I fix this?
    everything else will work if the hidden code doesnt have duplicate numbers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    /*Prototypes*/
    void intcodearray(int codearr[]);
    void getGuess(int guessarr[]);
    int comparearray(int codearr[], int guessarr[], int flagarr[]);
    void showresults(int flagarr[]);
    void outputarray(int outarr[]);
    
    /*main program input*/
    int main(int argc, char *argv[])
    {
      int codearr[4], guessarr[4], flagarr[4]={0,0,0,0};
      int max_tries=0;
      int win = 0;
      
      /*Introduction*/
      printf("===========================================================\n");
      printf("\t\t\tCODEBREAKER\n");
      printf("===========================================================\n");
      printf("Welcome to Codebreaker, please input 4 numbers between 0-9.\n");
      printf("You have up to TEN tries to figure out the code. Good Luck!\n\n");
      /*End Introduction*/ 
      
      
        intcodearray(codearr);
      
      outputarray(codearr);
      
     
      for(max_tries = 0 ; max_tries <= 10 ; max_tries++)
      { 
       printf("Code Guess: ");
      getGuess(guessarr);
      
      outputarray(guessarr);
      
    
     win = comparearray(codearr, guessarr, flagarr);
      
    //  outputarray(flagarr);
     
      if(win==4)/*max guesses reached, end game*/
      {
                      printf("You Win.\n");
                      break;
      }
      if(max_tries==10 && win != 4)
      {
                      printf("\nThat was TEN guesses, and you were unable to solve the puzzle.\n");
                      printf("You have failed.\n");
      }
          
    }
      
      system("PAUSE");	
      return 0;
    }
    
    /*Void Codes*/
    
    void intcodearray(int codearr[])
    {
        int i=0;
        srand(time(NULL));
        for(i=0 ; i <4 ; i++)
        {
                codearr[i]= rand()%9;
        } 
    }
    void getGuess(int guessarr[])
    {
         int guess=0;
         scanf("%d", &guess);
         
         guessarr[0] = guess / 1000;
         guess = guess % 1000;
         guessarr[1] = guess / 100;
         guess = guess % 100;
         guessarr[2] = guess / 10;
         guess = guess % 10;
         guessarr[3] = guess;
         
         
    }
    int comparearray(int codearr[], int guessarr[], int flagarr[])
    {
         int code, guess, flag;
         int i=0, outer=0;
         int right = 0, almost = 0;
         for(i=0 ; i <4 ; i++) //rights
         
                 {
                 if(codearr[i] == guessarr[i])
                  // printf("Yes\n", guessarr[i]);
                  {
                   flagarr[i] = 1;
                   right++;
                  }
    }
       for(outer=0 ; outer < 4 ; outer++)
         for(i=0 ; i <4 ; i++) //almost
         
                 {
                 if(codearr[outer] == guessarr[i]&&flagarr[outer]==0)
                  // printf("Yes\n", guessarr[i]);
                  
                  
                  
                  {
                   flagarr[i] = 1;
                   almost++;
                  }
                   
                   }
                 printf("Right %d , Almost %d\n", right, almost);
                return(right);
         
    }
    void showresults(int flagarr[])
    {
         
    }
    void outputarray(int outarr[])
    {
       int i=0;
       for(i=0 ; i < 4 ; i++)
       {
               printf("%d", outarr[i]);
       }  
       printf("\n\n");
    }
    Code:
    Another example
    
    Code is 4742
    
    if I guess 2474
    Right 0, Almost 2
    
    it should be almost 4, since 4 numbers match but all in the wrong position

    The code should go in here
    Code:
    int comparearray(int codearr[], int guessarr[], int flagarr[])
    {
         int code, guess, flag;
         int i=0, outer=0;
         int right = 0, almost = 0;
         for(i=0 ; i <4 ; i++) //rights
         
                 {
                 if(codearr[i] == guessarr[i])
                  // printf("Yes\n", guessarr[i]);
                  {
                   flagarr[i] = 1;
                   right++;
                  }
    }
       for(outer=0 ; outer < 4 ; outer++)
         for(i=0 ; i <4 ; i++) //almost
         
                 {
                 if(codearr[outer] == guessarr[i]&&flagarr[outer]==0)
                  // printf("Yes\n", guessarr[i]);
                  
                  
                  
                  {
                   flagarr[i] = 1;
                   almost++;
                  }
                   
                   }
                 printf("Right %d , Almost %d\n", right, almost);
                return(right);
         
    }
    Last edited by Charak; 02-17-2011 at 12:48 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Should your flagarr be all zeros each time you call your checking function (perhaps)?
    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.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Quote Originally Posted by Salem View Post
    Should your flagarr be all zeros each time you call your checking function (perhaps)?
    I changed the following code:
    Code:
    {
                   flagarr[i] = 0;
                   almost++;
                  }
    Now when the doubles come up

    right 0, almost 6

    2 to many


    // Well that doesnt work, as it just counts each time it checks each interger


    0032

    3020 = 1 right, 5 almost
    Last edited by Charak; 02-17-2011 at 01:05 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A nested pair of loops will do this.

    The char arrays must be 1 char longer than the number of digits, for this to work as I describe:

    Code:
    #include <stdio.h>
    #define SIZE 6
    
    int main() {
      int i,j, right=0, almost=0;
      char g[SIZE]="12348";
      char n[SIZE]="12345";
      printf("\n\nGuess: %s     Number: %s", g, n);
    
    Now the nested loops logic:
      begin outer for loop:
      for each char in the string, and incrementing once each time it loops, and i=0 to start
        if g[i] == n[i] then increment right
        
        begin inner for loop scanning the string, with j=0 at the start, and incrementing once per loop.
          if(g[i] == n[j]) then increment almost
        end inner for loop
      end outer for loop
    
      decrement right and almost, one time, due to the end of string char each array has
      almost equals almost -  right;
    
      printf("\nRight: %2d        Almost: %d \n", right, almost);
    
      return 0;
    }
    Just tested on a few numbers, but it worked. Give it a shot.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Two things.

    1) memset() flagarr to all zeros before each call to comparearray():
    Code:
    #include <string.h>
    
    ...
    
    memset(flagarr, 0x00, sizeof(*flagarr) * 4);
    win = comparearray(codearr, guessarr, flagarr);
    
    ...
    2) Rewrite the "almost" part of comparearray() to (1) skip those positions that were already guessed correctly and (2) stop after first match is found, otherwise the count will be incorrect in certain cases:
    Code:
    for(t = 0 ; t < 4 ; t++)
    {
      if (flagarr[t] == 0)
      {
        for(i = 0 ; i < 4 ; i++)
        {
          if(codearr[t] == guessarr[i])
          {
            almost++;
            break;
          }
        }
      }
    }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    And for your own sake, read an article on code indentation. Yours is a mess.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Replies: 9
    Last Post: 11-11-2007, 02:31 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM