Solved

Mater Mind

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now