-
August 15th, 2004, 12:42 PM
#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]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|