#include <stdio.h>
#include <time.h>
#include <locale.h>
#define ARR_SIZE 4
#define MAX 10
typedef int Secret[ARR_SIZE];
typedef struct
{
int points;
char name[20];
}Player;
void addToScoreboard(int numberOfGuesses);
int showMenu()
{
int choice;
printf("\n\n-*-*-*-* MasterMind *-*-*-*-\n\n");
printf("1. Play\n\n2. Show scoreboard\n\n3. Exit\n\n");
printf("Make your choice choice(1-3):\n\n");
scanf("%d", &choice);
return choice;
}
void fillArray(Secret secretNumber)
{
int i;
int j;
int hasDuplicate;
for(i=0;i<ARR_SIZE;i++)
{
secretNumber[i] = rand() % MAX;
printf("array[%d] = %d\n\n", i, secretNumber[i]);
hasDuplicate = 0;
for(j=0; j<=(i-1); j++)
{
if (secretNumber[i] == secretNumber[j])
{
hasDuplicate = 1;
i--;
}
}
}
}
void giveGuess(Secret Guess)
{
int temp;
printf("\n\nGuess 4 numbers: ");
scanf("%d", &temp);
Guess[0] = (temp%10000) / 1000;
Guess[1] = (temp%1000) / 100 ;
Guess[2] = (temp%100) / 10 ;
Guess[3] = (temp%10) / 1 ;
}
void control(Secret secretNumber, Secret Guess, char help[])
{
int i,j;
int numRight=0;
int flag=0;
int numberOfGuesses=0;
printf("\n\n*-*-*-*-*-*-\n\nYour Guess was:\n\n");
while(numRight < ARR_SIZE)
{
giveGuess(Guess);
numberOfGuesses++;
for(i=0; i<ARR_SIZE; i++)
{
printf("%d ", Guess[i]);
}
printf("\n\n\n");
numRight=0;
for(i=0; i<ARR_SIZE; i++)
{
if( Guess[i] == secretNumber[i] )
{
help[i] = 'R';
numRight++;
flag++;
}
for (j=0; j < ARR_SIZE ; j++)
{
if (Guess[i] == secretNumber[j] && j != i && flag != 1)
{
help[i] = 'S';
break;
}
}
flag = 0;
if (help[i] != 'R' && help[i] != 'S')
{
help[i] = '_';
}
}
printf("help: ", help[i]);
for(i=0; i<ARR_SIZE; i++)
{
printf("%c ", help[i]);
}
printf("\n\nnumber of guesses done: %d", numberOfGuesses);
for(i=0; i < ARR_SIZE; i++)
{
help[i] ='_';
}
}
addToScoreboard(numberOfGuesses);
numRight=0;
for(i=0; i < ARR_SIZE; i++)
{
help[i] ='_';
}
}
void addToScoreboard(int numberOfGuesses)
{
FILE *scoreboardFile = fopen("scoreboard.txt", "a");
char name[20];
printf("\n\ngive name: ");
scanf("%s", name);
fprintf(scoreboardFile, "name: %s \npoints: %d\n\n\n", name, numberOfGuesses);
fclose(scoreboardFile);
}
int main()
{
int menuChoice=0;
Secret secretNumber;
Secret Guess;
char help[ARR_SIZE] = {'_', '_', '_', '_'};
Player player[10];
srand(time(NULL));
setlocale (LC_ALL, "swedish");
do
{
menuChoice = showMenu();
switch (menuChoice)
{
case 1:
fillArray(secretNumber);
control(secretNumber, Guess, help);
break;
/*case 2:
scoreboard("scoreboard.txt", player);
break;*/
}
}while (menuChoice != 3);
return 0;
}