Thread: Problem with simple Mastermind game(beginner)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Problem with simple Mastermind game(beginner)

    Hi guys this is my first post and also I should mention that I am a novice programmer so please try to be as patient with me as possible.
    So the game I am trying make is the Mastermind game. It is a 2 player game in which one player makes a code and the other player tries to guess it in a number of tries. After each try the program returns 'b' to show the there is a character in the right place, 'w' to show there is an occurring character in the wrong place, and '.' for every character that does not occur at all, My code runs but then crashes after the guess is inputted. Please help me figure out why this is. Here is the code:

    Code:
      
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    
    
    bool ifWin(char*result, int guesslength)/*checks if the user won*/
    {
        int i;
        bool win=true;
        for (i=0; i<=guesslength; i++)
        {
            if(result[i]!='b')
            {
                win=false;
            }
        }
        return win;
    }
    
    
    char**createPattern(int patternlength, int guesses)
    {
        int i;
        char**pattern;
        pattern = (char**)malloc(sizeof(char*)*guesses+1);
        
        for (i=0;i<guesses;i++)
        {
            pattern[i] = (char*)malloc(sizeof(char)*patternlength);
        }
        return pattern;
    }
    
    
    char*compare(char**pattern, int guessnumber, int guesses)
    {
     int c=0, i, n;         
     char*result=(char*)malloc(sizeof(char)*guesses);
     for (i=0;i<=guesses;i++)
     {
     if(pattern[0][i]==pattern[guessnumber][i])
     {
       result[c]='b';
       pattern[0][i]='z';
       c++;
     }
     for (n=0;n<=guesses; n++)
     {
         if(pattern[0][n]==pattern[guessnumber][i])
         {
             result[c]='w';
             c++;
         }
     }
    }
    int j;
     for (j=0;j<guesses;j++)
     {
         if (result[j]!='b'||'w')
         {
             result[j]='.';
         }
          
     }
    return result; 
        
    }
    
    
    int main(void)
    {
        char**pattern;
        char*result;
        bool win=false;
        int patternlength, guesses;
        printf("Enter the length of the pattern: ");
        scanf("%d",&patternlength);
        printf("Enter the number of guesses: ");
        scanf("%d",&guesses);
        pattern=createPattern(patternlength,guesses);
        printf("Enter the key pattern: ");
        scanf("%s",&pattern[0]);
        int c=1;
        do 
        {
            printf("Enter a guess: ");
            scanf("%s",&pattern[1]);
            result=compare(pattern, c, guesses);
            win=ifWin(result, patternlength);
            printf("%s\n",result);
            c++;
        }while(c<=guesses||win==true);
        if (win==true)
        {
            printf("You Won!!");
        }
        else
        {
            printf("You Lost!!");
        }
        return 0;
    }
    Thanks for any help.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    I don't really use C much but when you use scanf to get a variable into the char array pattern how come you only use one data position? like &pattern[1]... its a 2d array shouldn't you have two data positions? Like &pattern[0][0] and &pattern[1][0]? It seems there would be room for undefined behavior here.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 02:49 AM
  2. Mastermind program problem with iterator
    By Truckomobil in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2009, 01:16 PM
  3. Replies: 37
    Last Post: 12-13-2007, 03:40 PM
  4. mastermind problem..
    By skelectron in forum C Programming
    Replies: 5
    Last Post: 10-30-2005, 07:52 AM
  5. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM

Tags for this Thread