Solved

Mater Mind

Posted on 1997-12-14
5
315 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.

0
Comment
Question by:micp
5 Comments
 

Author Comment

by:micp
ID: 1256641
Edited text of question
0
 

Expert Comment

by:Mika_Saari
ID: 1256642
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
 

Author Comment

by:micp
ID: 1256643
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
 
LVL 11

Accepted Solution

by:
alexo earned 200 total points
ID: 1256644

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
 
LVL 2

Expert Comment

by:rigansen
ID: 1256645
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 424
outside company writing a dll in c/c++, I want to call it from C# 2 72
How to align numbers in C using the %d 2 98
delete-remove 14 87
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question