/* 
   Program Name: Mastermind
   Author: Ben Jacobs <dooberwah@crosswinds.net>

   Description: A version of the classic game Mastermind. You can
   change how many places you want to guess at by changing the defined
   value PLACES, everything else should take care of itself. 

   Comments: Please don't use this to cheat on any of your classes, I
   know that some schools have assignments to make a mastermind
   game. Please don't steal the code and claim you own it.

   Bugs report: please report any bugs to dooberwah@crosswinds.net.
*/


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#define PLACES 4
#define POSSIBLE (PLACES + 2)

int comp(char guess[], char hidden[]);

int main(void)
{
  int i;
  char hidden[PLACES], guess[PLACES];
  
  srand((unsigned)time(NULL));

  printf("Mastermind - by Ben Jacobs 2001\n");

  for (i = 0; i < PLACES; i++) 
    hidden[i] = rand()%POSSIBLE + 65;

  while (comp(guess, hidden) != 1) {
    for (i = 0; i < PLACES; i++) {
      guess[i] = toupper(getchar());
      if (guess[i] < 65 || guess[i] > 90)
	i--;
    }
    printf("\n");
  }

  printf("Hurray, you won.\n");

  return 0;
}

int comp(char guess[], char hidden[])
{
  int exact=0, nexact=0, i, j;
  char newhidden[PLACES];

  for (i = 0; i < PLACES; i++)
    newhidden[i] = hidden[i];


  for (i = 0; i < PLACES; i++) 
    if (guess[i] == newhidden[i]) {
      exact++;
      guess[i] = 'q';
      newhidden[i] = 'z';
    }

  for (i = 0; i < PLACES; i++)
    for(j = 0; j < PLACES; j++) {
      if (guess[i] == newhidden[j]) {
	nexact++;
	guess[i] = 'q';
	newhidden[j] = 'z';
      }
    }
  printf("Exact %d\t!Exact %d\n", exact, nexact);
  if (exact == PLACES) 
    return 1;
  else
    return 0;
}
