![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#6 |
|
Expert Programmer
|
Yeah. PM me or e-mail me when you have the stuff.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|