hi all,
i'm trying to code mastermind (textbased) in C, but i stumble into two problems..
here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand( (unsigned)time( NULL ) );
int play_or_instruct;
int get_random;
int max_tries;
int get_answer;
int print_answer;
int count_equal;
int count;
int correct;
int secret_code[5];
int answer[5];
printf("\nWelcome to mastermind.\n");
printf("press 1 to play, press 2 for instructions :");
scanf("%d",&play_or_instruct);
if (play_or_instruct==2) /*check for playing the game or instructions*/
{
printf("\n\nThe goal of the game is to guess the secret\n");
printf("secret code. You have 10 tries for this.\n");
printf("The code contains 5 random numbers from 0 - 9.\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 \'0\' you see next to your input,\n");
printf("means that you have one number correct, but\n");
printf("not at the right place.\n");
play_or_instruct=1;
}
if (play_or_instruct ==1)
{
printf("\nHere we go....\n\n");
for (get_random = 0; get_random <5; get_random++) /*get the secret code with the*/
secret_code[get_random] = rand()%10; /*random function*/
for(max_tries=0;max_tries<10;max_tries++) /*count number of tries*/
{
printf("\nEnter your guess: ");
for(get_answer=0;get_answer<=4;get_answer++)/*get the answer given*/
{
scanf("%d",&answer[get_answer]);
printf("\n");
}
for(print_answer=0;print_answer<=4;print_answer++)/*print the answer*/
printf("%d",answer[print_answer]);/*given to the screen*/
printf("\t");
for(count_equal=0;count_equal<=4;count_equal++) /*check if a number is*/
{ /*correct and at the right place*/
if(answer[count_equal]==secret_code[count_equal])
printf("*"); /*print * if correct*/
}
count=0;
for(correct=0;correct<=4;correct++)/*check if the complete code is */
{ /*correct, for each digit*/
if(answer[correct]==secret_code[correct])/*count + 1*/
count++;
}
if(count==5)/*if count is equal to 5, let them know they've won, and*/
{ /*end the game.*/
printf("\nCongratulations, you've won!\n");
return 0;
}
}
if(max_tries==10)/*if maximum number of tries have passed, let them know*/
{ /*they didn't made it and end the game*/
printf("\nUnfortunately you did not make it.\n");
return 0;
}
}
}
/*only got two problems,
1st problem:
getting to print a '0' for each character that is present, but not at the right place, and don't print it double if one character is compaired to two.
2nd problem:
as the code is now, the user has to press <enter> after each digit they enter, this has to change. e.g. when entering 12345, it now is 1<enter>2<enter>3<enter>4<enter>5<enter>.*/
the problems i mean are:
1st problem:
getting to print a '0' for each character that is present, but not at the right place, and don't print it double if one character is compaired to two.
2nd problem:
as the code is now, the user has to press <enter> after each digit they enter, this has to change. e.g. when entering 12345, it now is 1<enter>2<enter>3<enter>4<enter>5<enter>.
if anyone knows how i can solve these problems, and tell it to me of course , that would be highly appreciated..