Thread: c program for the mastermind game

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    2

    c program for the mastermind game

    Program Specifications
    Your program will play the mastermind game as follows:

    1. Prompt for the 1/2/3/4/5/6/7/8 digit key we will play with. No error checking here.
    2. Prompt the user for a guess.
    1. we check to see if the guess if exactly length 4
    2. we check to see if the guess contains all numbers
    3. we check to see if the guess has no repeats

    any violation of these rules prints an error message and prompts for a guess again. Bad input does not count as a guess.

    1. End the game if:
    1. the guess is correct. Print out a winner message
    2. it was the last guess (the twelth) and the guess is incorrect. Print out a loser message and the value of the key

    2. If no end condition occurs, print out how many of the guess numbers are exactly right (correct number in the correct position) and how many of the guess numbers are correct but not in the correct position. Example. The key is 1234 and the guess is 4256. The feedback would be 1 number exactly correct (2) and 1 number in the wrong position (4). That is, each digit in the guess produces at most one result (exact or wrongPosition).
    3. You should keep a history of guesses so that the user can see how they are progressing in a file.
    4. Continue the game until an end condition occurs





    Above is the program and below is my coding I need to fix but couldnt so I heard that this website is really helpful can anybody help me "I am a real life nob in C programming" and THANKS


    Code:
    #include  <stdio.h>
    
    #include <string.h>
    
    
    int  main()
    
    {
    
        char  master[8]={0x00};
    
        char  guess[8]={0x00};
    
        int   guesses, broken, match, almost, i, n;
    
    
    
        do {
        printf("Please enter your master  key :");
        fgets(master,8,stdin);
        } while ( strlen(master)==0 );
        
        for( guesses=1; guesses<13;  guesses++ )
        {
        printf("Please enter your guess  :");
        fgets(guess,  8,stdin);    
        
    
        
    
        match = 0;
        almost = 0;
        for( n=0; n<strlen(guess); n++ ) 
        {
        for( i=0; i<strlen(master);  i++)
    {
        if( guess[n]==master[i] )
        {
        if( n==i )
        match++;
        else
        almost++;
        }
        }
        }
        
    
        if( match==strlen(master) ) {
        broken = 1;
        break;
        }
        else {
        printf(" Matches = %d, Almost =  %d\n", (match-1), almost );
        }
        }
    
    
        if( broken ) {    
        printf("End of game .. you took  %d guesses \n",guesses);
    
        }
        else {
        puts(" Try again...\n");
        }
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ok, then what's wrong with your code? I could compile it and find out myself, but I want to see if you understand the program requirements.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It's unreadable as written.

    Indent style - Wikipedia, the free encyclopedia
    Apply a decent (and more importantly, consistent) indent style to your code.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you do re-post with improved formatting, be sure to also tell us what specifically you're having trouble with. Saying "below is my coding I need to fix" tells us nothing about the problem(s).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Again, Mastermind Game Assistant Question
    By Jesterira in forum C Programming
    Replies: 8
    Last Post: 12-07-2012, 06:45 PM
  2. Problem with simple Mastermind game(beginner)
    By newbie2012 in forum Game Programming
    Replies: 1
    Last Post: 11-12-2012, 03:51 AM
  3. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 02:49 AM
  4. Algorithmic Mastermind Program
    By pr0n in forum C Programming
    Replies: 1
    Last Post: 05-12-2008, 11:02 PM
  5. Replies: 37
    Last Post: 12-13-2007, 03:40 PM

Tags for this Thread