Thread: Mastermind C project, need help!

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    6

    Cool Mastermind C project, need help!

    Hello guys, its my first thread on this forum.

    I got project from computer programming which is game mastermind. For those who dont know this game the point is to guess secret code which is set by one of the players. For example player set up secret code which is 4 integer numbers in range from 0 - 5, he can reapet number but can't leave empty spaces between them. The second player have to guess the sequence of the code.
    So, for example code is 0 1 2 1
    second player make random guess 0 0 1 1, of course it would be almost impossible to guess the sequence without any tips so, after each made guess player who create the code give guessing player answer, how many of the numbers is placed on correctly, or how many of the numbers is in the sequence but its place is wrong. So if the code was 0 3 2 1 and player made a guess 0 0 1 1 his answer will be 2 - are placed correctly and 1 - is in the code but on the wrong placed.


    Now moving to the point after basic ideas of the game, i have to write a programm which will guess the sequence basing only on the answers. So the user write down or imagine some code of 4 integere numbers in range from 0 - 5, and programm starts guessing, after each guess user gives the answers until computer will break the code.


    Generally i got problem with everything i dont have idea even how to start it, i am real beginner about programming and espacialy about the C language. Any ideas how to make algorithm some parts of the code in general would be helpful. Thanks for your time!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The very first thing you do is put your computer aside, get some paper and a pen, and begin planning how you want to approach this.

    Think about the logic you will need to solve this problem. Figure out the list of steps you must follow in order to solve this problem "on paper". When you have a list of steps, then you can start converting it into code.

    Programming is about problem-solving. Working through these steps to come up with an algorithm on your own is an important skill to learn.

    Start thinking of how you might go about solving this problem "by hand", and see what you come up with.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    So, i've been working on it for a while. I followed your suggestion and i made something and got some ideas, but i am writing here again cause i need your help about ideas how i can implement this.
    So i got an array with 1296 elements which contains all combinations with number numbers from 1 - 6.
    Now i am working on algorithm which will solve this dependly on answer, my idea is that it will delete combinations from an array which will give him the same answer as previous guesses. and delete all combinations with some number if it is sure that this number cannot be contained in the secret code.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I am surprised that you were asked to program the "code breaker", and not the "code maker".

    Anyway, I would first think about how to organize the data involved. I would handle each code digit individually rather than combined into a single 4 digit integer value.

    So you might use several four element arrays - one array for the code, one array for the guess, and one array to hold the status of each digit in the guess (correct in value and location, correct in value only, undetermined).

    Simply striking all combinations that would produce the same clues (answers) as in previous guesses will not work though. Since you (the program) do not know the code, you also do not know what the answer would be for any other combination.

    -

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The idea is to start with a set of all possible combinations.

    LOOP:
    Randomly chose a guess from the set of possible combinations.
    Remove guess from set.

    Note the score the guess gets.
    If it's a winning score, end loop

    Compare the guess against all remaining possibilities.
    If a possibility doesn't get the same score, remove it from the set.
    ENDLOOP

    This algorithm is not optimal, but will solve the problem.
    For optimality, see Knuth's algorithm (and refinements).

    As for maintaining the set, you only need a single bit per member.
    So for 1296 numbers, you need a 1296/8 = 162 byte bit array.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by algorism View Post
    ...
    Compare the guess against all remaining possibilities.
    If a possibility doesn't get the same score, remove it from the set.
    ...
    That makes more sense.

    -

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    Quote Originally Posted by algorism View Post
    The idea is to start with a set of all possible combinations.

    LOOP:
    Randomly chose a guess from the set of possible combinations.
    Remove guess from set.

    Note the score the guess gets.
    If it's a winning score, end loop

    Compare the guess against all remaining possibilities.
    If a possibility doesn't get the same score, remove it from the set.
    ENDLOOP

    This algorithm is not optimal, but will solve the problem.
    For optimality, see Knuth's algorithm (and refinements).

    As for maintaining the set, you only need a single bit per member.
    So for 1296 numbers, you need a 1296/8 = 162 byte bit array.

    Ok, so i got problems about implementing this to my programm, any ideas or suggestions how can i do it? I mean this algorithm which will compare the answers and remove possibilites from an array.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The codes are 4-digit base 6 numbers.

    Can you convert to/from base 6?
    * Write a function that takes a base-6 number as a string and returns an int.
    * Write a function that takes an int and prints a base-6 representation

    Do you know how to make a bitset? The idea is to use an array of unsigned ints and treat it as if all the bits were one giant array of bits.
    Code:
    unsigned bs[BS_SIZE] = {0}; // or perhaps unsigned long 
    
    bs_set(bs, 110);  // sets bit 110 of bs
    
    if (bs_isset(bs, 110)) // returns value of bs bit 110
      ;
    The bitset is implemented with integer division, modulus operator, bit shifting, bitwise-or and bitwise-and.

    Can you create a function to give the score of a given code and guess? The followng func assumes the returned score is encoded as (number of hits * 10) + (number of near misses).
    Code:
    int calc_score(int code, int guess) {
    }
    You also need a function to choose a guess out of the remaining possibilities in the bitset (the ones that are still 0), and a function to remove non-possibilities from the bitset.

    Show some code. Try some of the above.

    EDIT: Now that I think about it, since you only have 1296 possibilities, instead of a bitset you could just use an array of chars (or ints) where each element is either 0 or 1.
    Last edited by algorism; 02-02-2016 at 11:18 AM.

  9. #9
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    Hey, guys i've been working on for a while. And i completly got stuck in this moment, and the deadline is coming for me.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <cstdlib>
    #include <string.h>
    
    
    using namespace std;
    
    
    char allseq[1296][4];
    int z;
    void hit_counter(char *seq_1, char *seq_2, int *result_1, int *result_2);
    
    
    void sequence_deleter(char *seq);
    
    
    
    
    int main() {
    	srand((unsigned)time(NULL));
    	int round/* z */, x, y, c, p;
    	char code[5];
    	char str[5];
    	//char allseq [1926][4];
    	int  sl, sk, skl;											//sl-słowo łamacz
    	z = 1296;
    	//sk-słowo koder
    	//skl-słowo klucz
    	for (x = 0; x<1296; x++)
    	{
    
    
    		allseq[x][0] = (x % 6) + 1;
    		allseq[x][1] = ((x / 6) % 6) + 1;
    		allseq[x][2] = ((x / 36) % 6) + 1;
    		allseq[x][3] = ((x / 216) % 6) + 1;
    
    
    	};
    	x = rand() % z;
    	sl = allseq[x][3] * 1000 + allseq[x][2] * 100 + allseq[x][1] * 10 + allseq[x][0];
    	printf("First guess: ");
    	printf("%d", sl);
    	printf("\nAnswer: ");
    	scanf("%4s", code);
    	sprintf(str,"%d",sl);
    	int k=0;
    	int number_x = 0;
    	int number_o = 0;
    	while (code[k])
    	{
    		if (code[k] == 'x')
    		{
    			number_x++;
    			k++;
    			continue;
    		}
    		else if (code[k] == 'o')
    		{
    			number_o++;
    			k++;
    			continue;
    		}
    		k++;
    	}
    	int res1, res2;
    	for (int i = 0; i < 1296;i++)
    	{
    		hit_counter(allseq[i], str, &res1, &res2);
    		if (res1 != number_x || res2 != number_o)
    		{ sequence_deleter(allseq[i]); }
    	}
    
    
    	system("pause");
    	return 0;
    }
    
    
    
    
    void hit_counter(char *seq_1, char *seq_2, int *result_1, int *result_2)   {
    	char k1[5];  /* for sequence copy not to destroy the original one */
    	*result_1 = 0; /* for good placed pin's black one's) */
    	*result_2 = 0; /* for white pin's */
    
    
    	strcpy(k1, seq_1);
    	for (int i = 0; i < 4; i++)
    	{
    		if (k1[i] == seq_2[i])
    		{
    			*result_1++;
    			k1[i] = 'S';
    		}
    	}
    
    
    	for (int i = 0; i < 5; i++)
    	{
    		for (int j = 0; j < 4; j++)
    		{
    			if (k1[i] == seq_2[j])
    			{
    				*result_2++;
    				k1[i] = 'S';
    			}
    
    
    		}
    	}
    }
    
    
    
    
    void sequence_deleter(char *seq)
    {
    	int i=0;
    	while (seq[i])
    	{
    		seq[i] = 'S';
    	}
    }
    Here is my code, generally i have no idea how to move on. I dont know how it should cope my strings and delete them from the all_seq two dimensional array. Generally i don't know how to send all these sequence combination to these functions. Any idea how i can finish this or something useful? Looking forward for your quick answers, thanks for all help to this time!

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This is supposed to be C, right? Because "<cstdlib>" and "using namespace std" are C++.

  11. #11
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    Yes, yes. Only C, forget to remove this library from the beginning, my compilator just added it

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What compiler/IDE are you using? It's not good that it adds such things. Are you compiling as C?

  13. #13
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    Yes i'm compiling as C, and its working.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by gugi10 View Post
    Yes i'm compiling as C, and its working.
    There is no C compiler I am aware of that automatically adds C++ statements to the code by itself. But since you don't seem to care enough to provide more information, then I don't care either.

    Your code is very difficult to follow. Quite frankly, it's a mess.

    • There is a lot of clutter
    • Multiple unused variables
    • Poor variable naming (single-letter and unclear variable names)
    • Poor variable usage (global variables, redundant variables that serve the same purpose)
    • Use of magic numbers all over the place


    It appears you banged out code as you went, instead of planning the program (like I originally suggested) and testing along the way.

    When I tried to run the program (guessing what the input should be based on the code), it seg-faulted.

    I don't think I can personally help you salvage this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mastermind
    By roknroll3r in forum C Programming
    Replies: 5
    Last Post: 12-17-2013, 05:47 PM
  2. help with mastermind
    By jjexpress in forum C Programming
    Replies: 6
    Last Post: 08-16-2010, 09:24 AM
  3. mastermind
    By roeyman in forum C Programming
    Replies: 11
    Last Post: 01-01-2009, 08:21 AM
  4. Mastermind
    By grade10 in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2002, 03:52 PM
  5. MasterMind
    By ski6ski in forum Game Programming
    Replies: 0
    Last Post: 12-05-2001, 09:25 AM

Tags for this Thread