+ Reply to Thread
Results 1 to 4 of 4
  1. #1

    written command line game "mastermind"

    hi all,

    i've written a command line based game called "mastermind". i think most of you know the game from our early days..

    well i've written my own and i would like to know what you guys think of it.

    here's the game:
    Mastermind.c
    Code:
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    *                                                                                  *
    *  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");
          
    }
    b.t.w., feel free to keep a copy of the game if you like it, it is under the GPL license

    grtz


    Scorpius

    [edit]
    changed the source to get rid of some bugs, changed the charset from 0-9 to 1-6 and max tries from 10 to 8, this way the game is more playable...
    [/edit]

    [edit2]changed the source again, there were some possible buffer overflows which i had to remove, thanks to capi from www.security-forums.com for helping me locate them
    [/edit2]

  2. #2
    Banned whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300 whizkid2300
    Join Date
    Jun 2003
    Posts
    1,302
    Nice little right up, if I have the time, I will port it to perl sometime later. I have actually never played mastermind.

    I don't feel like counting, but how many lines of code were there to that program?

  3. #3
    Blast From the Past hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute hexadecimal has a reputation beyond repute
    Join Date
    Jan 2003
    Posts
    729
    i get errors when i try to run this.......im working with it now trying to see if its programming or user error *most likely user error*....il post back in a few with what errors i get
    work it harder, make it better, do it faster, makes us stronger

  4. #4
    try the new source, perhaps it does work now, i've tried to make it as general as possible (compatible with windows, linux), but maybe i've still missed something...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides