We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Mater Mind

micp
micp asked
on
Medium Priority
419 Views
Last Modified: 2010-04-02
Anybody has a straight source code for the game "Master Mind" without using routine functions or 'strcpy'. Please explain in detail with lots of comments.
This is for a beginner's class.

Comment
Watch Question

Author

Commented:
Edited text of question
Hello !

      I hope this is something you need.
      I am not sure if Master Mind is like this but
      I hope.

      Code doesn't include any error checkings or anything
      else only the idea how I thought MasterMind works.
      So when using the code please insert only
      CNT count of numbers to the insert line.



#include <stdio.h>

/* Set how long this you want the number string to be */
#define CNT 7

/* Main program */
int
main(int ac, char*av[])
{
   /* Variables */
   char szRnd[CNT]; /* Generate random number string here */
   char szStr[CNT]; /* Get user number string here */
   char szOut[CNT]; /* This is used to print the information to user */
   int  iCnt, iCnt1;/* Couple of for loop variables */
     
   /* Set random seed, mandatory ! time is used to get the seed*/
   srand(time(NULL));
   
   /* Generate random number */
   /* First loop from 0 to CNT */
   for (iCnt = 0; iCnt < CNT; iCnt++)    
   {      
      /* Generate number from 0-9 to the specified place (iCnt) of the number st
ring szRnd */
      sprintf(&szRnd[iCnt],"%d",(rand()%10));
   }

loop:
   /* Start asking CNT long strings */
   printf("Give %d characters long string\n", CNT);
   /* Get user number string to the szStr */
   scanf("%s", &szStr);
   
   /* Set the szOut string to be -, so the whole string will be CNT * '-' Defaul
t:(-------)*/
   memset(szOut,45,CNT);
   
   /* Check numbers in string */
   /* Loop whole system so compare user inserted string to random string */
   for(iCnt=0;iCnt<CNT;iCnt++)
   {
      for(iCnt1=0;iCnt1<CNT;iCnt1++)
      {        

         /* If number can be found from the random string but is in wrong place
then set '*' to
          * the output string (* = 42 in ascii) */
         if(iCnt != iCnt1 && szStr[iCnt] == szRnd[iCnt1] && szOut[iCnt] == 45)
           szOut[iCnt] = 42;
         
         /* If Number is right in the right place then put the number to the out
put string */          
         if(iCnt == iCnt1 && szStr[iCnt] == szRnd[iCnt1])
           szOut[iCnt] = szStr[iCnt];                    
      }
   }

   /* Print out the output string for example '--*--5-' it means that 5 is in th
e
    * right place and * means that number found in the random string but is in t
he wrong place */
   printf("%s\n",szOut);
   
   /* Check if number string is same */
   for(iCnt=0;iCnt<CNT;iCnt++)
   {
      /* If just one number differs from the original random string immediatelly
 
       * go to loop */
      if(szOut[iCnt] != szRnd[iCnt])
        goto loop;
   }  
   
end:
   printf("Right number was %s\n",szRnd);
}    

Author

Commented:
I get the error messages below, so the code does not compile.
Here is more information for the next expert, no offense Mika, I need the code in a rush.

The program has to evaluate if the user is a beginner or expert, if expert: the 4 colors (chosen out of 5 possible) could be generated with the same colors, if beginner, the colors generated (at random) have to be all different. The program has to show the number of games played, the number of games won and the average.

Warn :  noname00.cpp(8,22):Cannot create pre-compiled header: write failed
Error:  noname00.cpp(17,7):Call to undefined function 'srand'
Error:  noname00.cpp(17,12):Call to undefined function 'time'
Error:  noname00.cpp(25,33):Call to undefined function 'rand'
Error:  noname00.cpp(36,8):Call to undefined function 'memset'
Warn :  noname00.cpp(76,2):Parameter 'ac' is never used
Warn :  noname00.cpp(76,2):Parameter 'av' is never used

Commented:

Get the sources you seek from ftp.sunet.se file: /pub/os/Linux/xtra/games/strategy/mastermind.tar.gz

or better yet, give your gray cells a good workout.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
At first glance, to compile without errors Mika Saari's code you have to:

#include <string.h>
#include <stdlib.h>
#include <time.h>

rigansen.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.