Solved

Mater Mind

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 207
Problem with MFCApp 78 421
Want to delete all my personal data 13 146
why "." vs "->" 23 120
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

828 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