Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- mastermind_game_v1.c
- mastermind game
- https://en.wikipedia.org/wiki/Mastermind_(board_game)
- In this console game MasterMind
- Computer will be the codemaker and player will be the codebreaker.
- The computer picks a sequence of 4 numbers from 1 to 5.
- The object of the game is to guess the exact positions of the colors
- in the computer's sequence in as few guesses as possible.
- After each guess, the computer gives you a score of exact and partial matches.
- Here colors will be replaced by numbers 1 to 5.
- Colors (numbers) may be repeated in the code.
- In the score (results of guess)
- Correct color & position X
- Correct color O
- None -
- Rules:
- The sequence can contain colors (numbers) 1,2,3,4,5.
- A color (number) can be used any number of times in the sequence.
- All four colors of the secret sequence will contain a color,
- no blanks/empties are allowed.
- Each guess must consist of 4 colors, no blanks.
- The player has 6 guesses to find the secret sequence.
- In settings we can: ( simple change default values in main() )
- Choose the color code length (4 - 9), default 4.
- Choose the number of used colors in the game (1 - 9), default 5.
- Choose the maximum number of guesses the player has (4 - 9), default 6.
- Another mastermind game that is played using a mouse,
- exe file size 56 KB (language Serbian or Croatian or Bosnian)
- download from:
- https://mega.nz/file/64ZnXAKA#Sg8lML0S8RiwsY6n1GLYwLIx-2ScjRiFjfO1XibOwT8
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h> // for strcpy()
- #include <stdlib.h> // for function rand()
- #include <time.h> // for time_t
- // the computer imagines a secret code that the user will guess
- void makeSecretCode( char secretCode[10], int color_code_length, int number_of_used_colors )
- {
- int i;
- for(i=0; i<color_code_length; i++) // 48 is ASSCII code for '0'
- secretCode[i] = rand() % number_of_used_colors + 1 + 48;
- secretCode[i] = '\0'; // finish the string
- }
- // returns 1 if all colors are in their position, otherwise returns 0
- int getScore( char guess[10], char secretCode[10],int color_code_length, int number_of_used_colors )
- {
- int i, j, position=0, color=0; // iterators and counters
- char tempGuess[10], tempSecretCode[10];
- strcpy(tempGuess, guess); // temporary, because we will change it
- strcpy(tempSecretCode, secretCode);
- for(i=0; i<color_code_length; i++) // count score correct color & position (X)
- if( tempGuess[i] == tempSecretCode[i] )
- {
- position++; // count correct positon
- tempGuess[i] = tempSecretCode[i] = 'X'; // mark these positions with X
- }
- for(i=0; i<color_code_length; i++) // count score correct color (O)
- for(j=0; j<color_code_length; j++)
- if( tempGuess[i] == tempSecretCode[j] && tempGuess[i] != 'X' )
- {
- color++; // count score color
- tempGuess[i] = tempSecretCode[j] = 'X'; // mark these positions with X
- }
- printf("\t\t\t "); // spacing
- for(i=0; i<position; i++) // print score:
- printf("%c", 'X'); // correct color & position
- for(i=0; i<color; i++)
- printf("%c", 'O'); // correct color
- for(i=0; i<color_code_length-(position+color); i++)
- printf("%c", '-'); // none
- printf("\n");
- if( position == color_code_length )
- return 1; // return 1 if all colors are in their position
- return 0; // otherwise return 0
- } // getScore()
- int main(void)
- { // counter and default values
- int attempt=0, color_code_length=4, number_of_used_colors=5, maximum_number_of_guesses=6;
- char secretCode[10], guess[10]; // strings to store variables
- time_t t; // for random number generator, don't forget to #include <time.h>
- int i; // iterator
- srand((unsigned) time(&t)); // Intializes random number generator, should only be called once!
- // Title
- printf("\n MasterMind game \t\t author Dragan Milicev \n\n Guess %d numbers of ", color_code_length );
- for(i=0; i<number_of_used_colors; i++)
- printf(" %d", i+1 );
- printf(" in maximum %d attempts \n\n", maximum_number_of_guesses);
- // the computer imagines a secret code that the user will guess
- makeSecretCode( secretCode, color_code_length, number_of_used_colors );
- // printf(" SecretCode %s \n\n", secretCode); // print the secret code imagined by computer
- while( attempt < maximum_number_of_guesses )
- {
- printf(" Enter your %d. guess: ", attempt+1 );
- scanf("%s", guess ); // get guess from user
- attempt++; // count the attempts
- // getScore() returns 1 if all colors are in their position, otherwise returns 0
- if ( getScore( guess, secretCode, color_code_length, number_of_used_colors ) )
- {
- printf("\n You are the WINNER after %d attempts ! \n", attempt );
- break; // exit while() loop
- }
- else if( attempt == maximum_number_of_guesses ) // all attempts were used, print secret code
- printf("\nSorry, nothing hit from maximum %d attempts \n%s was secretCode \n", attempt, secretCode);
- }
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment