Hi,
Any help would be appreciated.
In the pasted program the top for loop does not work for some reason. It is supposed to limit the number of turns of the game.
Could someone tell me if this is me doing something silly or if it's my compiler (it does have bugs).
Thanks.
Code:
/*******************************************************/
/* */
/* THE MASTERMIND GAME */
/* version 2 */
/* By David Clemente */
/*******************************************************/
//STATUS of program - works except 1. when you get repeated integers because 2 dimensional array check double counts.
// 2. when you don't enter integer
// 3. program has a bug where the first for loop does not work.
//
//okay to play if no repeated integers used.
//header files
#include <stdio.h>
#include <stdlib.h>
#include <clib.h>
//define constants
#define NUM_INTS
#define CORR_SEQ 1234
#define TRIES 2
#define SIZE 4
//main file
int main()
{
//Display name of game
printf("WELCOME TO MASTERMIND!!\n");
//for loop1 to limit number if tries
int i,j,k,m,n,p,q;
char r;
for(i=0;i<2;i++)
{
//Ask user to input sequence
printf("Please enter a sequence of 4 numbers\nDo not use double integers yet please\n\n");
//Enter data as integer
int guess; //guess int
int corr_int = CORR_SEQ; //stored correct int
char guess_array[SIZE]; //guess array
char corr_array[SIZE]; //stored correct int
scanf("%d", &guess);
if(!guess)
{
printf("Error");
exit(0);
}
//is guess correct
if(guess==CORR_SEQ)
{
printf("You guessed it using %d guesses !", i);//i does not seem to work
exit(0);
}
//change int guess into array of integers
sprintf( guess_array,"%d",guess); //convert int to char array
sprintf( corr_array,"%d",corr_int); // ----"----
//work out number of correct and number of correct & right place
k=0;
for(j=0;j<4;j++)
{
if(corr_array[j] == guess_array[j])
k++;
}
//work out number of correct and wrong place
n=0;
p=0;
for(j=0;j<4;j++)
{
for(m=0;m<4;m++)
{
if((corr_array[j] == guess_array[m])&&!(corr_array[j] == guess_array[j]))
n++;
}
}
//print number of correct numbers in correct locations
printf("\t\t\t\t %d right number right place\n\n",k);
printf("\t\t\t\t %d right number wrong place\n\n",n );
//end of for loop
}
return 0;
} //last main brace
[code][/code]tagged by Salem