• C

Mater Mind

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.

micpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

micpAuthor Commented:
Edited text of question
0
Mika_SaariCommented:
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);
}    

0
micpAuthor 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

0
alexoCommented:

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rigansenCommented:
At first glance, to compile without errors Mika Saari's code you have to:

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

rigansen.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.