Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 16th, 2004, 4:28 AM   #1
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
Written A Text Version Of "mastermind"

hi all,

i've written another program in C, this time it is a text version of the game "mastermind", i think most of you will know this game from the early days...

just as with the password generator, this is also published under the GPL license and i would also appreciate response

and also you can keep a copy of it for yourself if you like it, that's the beauty of opensource

here's the source:

Mastermind.c

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*                                         *
* File: Mastermind.c                               *
*                                         *
* Purpose: a nice little game which challenge you to use your mind and find the  *
*      secret number...                            *
*                                         *
* Copyright (C) 2004 Scorpius, scorpius_unknown@yahoo.com, all rights reserved  *
*                                         *
* Mastermind is free software; you can redistribute it and/or           *
* modify it under the terms of the GNU General Public License           *
* as published by the Free Software Foundation; either version 2         *
* of the License, or (at your option) any later version.             *
*                                         *
* Mastermind is distributed in the hope that it will be useful,          *
* but WITHOUT ANY WARRANTY; without even the implied warranty of         *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
* GNU General Public License for more details.                  *
*                                         *
* You should have received a copy of the GNU General Public License        *
* along with this program; if not, write to the Free Software           *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   *
*                                         *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <time.h>

void manual(void);

int main(void)
{
  const char *set="123456";
  char secret[6];
  char guess[6];
  char again;
  int guess_check[5];
  int secret_check[5];
  char play='0';
  int run;
  int i,j;
  
  printf("Welcome to mastermind.\n");
  printf("press 1 to play, press 2 for help: ");
  play=getch();
  putchar(play);
  
  if (play=='2')
  {
    manual();
  }
  if((play!='1')&&(play!='2'))
  {
    printf("\nInvalid action!\n");
    return EXIT_FAILURE;
  }  
  again='y';
  while((again=='y')||(again=='Y'))
  {  
    int get_random;
    srand((unsigned)time(NULL));
    for(get_random=0;get_random<5;get_random++)
    {
        secret[get_random]=set[rand()%6];
    }
    secret[5]='\0';/*did this to make safe input*/
    for(run=1;run<9;run++)
    {    
        printf("\nEnter guess %d: ",run);
        for(i=0;i<5;i++)/*get the input*/
        {
            guess[i]=getch();
            putchar(guess[i]);
           
        }
        guess[5]='\0';/*did this to make safe input*/
    
        printf("\n%s\t",guess);
        int test=strcmp(guess,secret);/*check if complete string is correct*/
        if(test==0)
        {
            printf("*****");
            printf("\nCongratulations!!"); 
            printf("\nYou needed %d guesses to win, the secret code was: %s",run,guess);
            break;
        }
    
        for(i=0;i<5;i++)/*set check back to 0*/
        {
        secret_check[i]=0;
        guess_check[i]=0;
        }
        for(i=0;i<5;i++)
        {
            if(guess[i]==secret[i])/*if number is correct and at the right place*/
            {
                printf("*");
                guess_check[i]=1;
                secret_check[i]=1;
            }
                 
        }  
        for(i=0;i<5;i++)/*here's the loop to check if a digit is correct but not at the right place,*/
        {        /*then print 'o', and make sure the same digit is not used twice*/
            for(j=0;j<5;j++)
            {
                if((guess[i]==secret[j])&&(i!=j)&&(guess_check[i]!=1)&&(secret_check[j]!=1))
                {
                   printf("o");
                   guess_check[i]=1;
                   secret_check[j]=1;
                }
            }
        }
    }    
    if(run>8)/*only print the next line if user did not guess the number*/
    {
        printf("\n\nUnfortunately you did not guess it, the secret code was: %s\n",secret);
    }    
    printf("\nPlay again (y/n)?: ");
    again=getch();
    
  }
  return EXIT_SUCCESS; /*exit success, since the program runs like it should*/  
}  

  
void manual(void)/*the help function*/
{  
   printf("\n\nMastermind, Copyright (C) 2004, Scorpius, scorpius_unknown@yahoo.com\n"); 
   printf("\nThe goal of the game is to guess the secret code.\n");
   printf("The code contains 5 random numbers from 1 - 6.\n");
   printf("You have 8 tries for this.\n");   
   printf("Every \'*\' you see next to your input, \n");
   printf("means that you have one number correct and\n");
   printf("at the right place.\n");
   printf("Every \'o\' you see next to your input,\n");
   printf("means that you have one number correct, but\n");
   printf("not at the right place.\n");
   printf("\nLet's get started!\n\n");
   
}

this is written for windows at first, but if you change the function "getch()" to "getchar()" it should be able to work under linux as well

let me know what you think of it,

grtz


Scorpius
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 16th, 2004, 3:45 PM   #2
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Compiles nicely enough under dev-c++. I'll put the win32 compiled version up for those who don't have access to compiler

http://www.bigtonyc.com/mastermind.exe

and since it's gpl I guess I have to have the sources with it, so.

http://www.bigtonyc.com/mastermind.c

Very well done lepricaun
thechristelegacy is offline   Reply With Quote
Old Aug 16th, 2004, 3:59 PM   #3
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
i'm glad you like it
and i'm glad you posted it like this, since i haven't got my own site/server yet, so i can't get it on the net like that

do you mind if i refer people to your site to get the program???
i doubt you will, but i'll ask it anyway
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 16th, 2004, 4:54 PM   #4
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Lol, of course I don't mind. I have plenty of webspace to go around. If you want to write a small little site to host things (<5 megs) I'd be more than happy to host it for ya. Just write it, zip it, and I'll upload it if you'd like.
thechristelegacy is offline   Reply With Quote
Old Aug 17th, 2004, 12:31 AM   #5
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
thanks!!!! that would be great! i'll see with what i can come up with, i'll let you know

thanks again!
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 17th, 2004, 5:47 PM   #6
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Yeah. PM me or e-mail me when you have the stuff.
thechristelegacy is offline   Reply With Quote
Old Aug 18th, 2004, 12:30 AM   #7
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
that might take a little while, since i'm not a webdesigner, and i don't really know the language, so i must find out how it works first...

but many thanks for the offer in advance
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC