/*----------------------------------------------
  
  mastmind.c

  (P) 1993 Laurentiu Cristofor

  plain implementation of Mastermind game; 

  one of my first C programs 
  (has goto's so it must be the first!),
  it's been originally built for MS-DOS 
  using a Borland C compiler and
  non-ANSI I/O functions in 1993,
  and transfered to Unix in 1997

------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* high level clear screen procedure :-) */
void clrscr(void)
{
  int i;

  for(i=0;i<=80;i++) puts("");
}

void randomize(void)
{
  time_t tt;
  char buf[64], hundreds[3];

  /* Get current hundredth of second in variable hundreds */
  tt = time(NULL);
  strcpy(buf, ctime(&tt));
  hundreds[0]=tolower(buf[17]);
  hundreds[1]=tolower(buf[18]);
  hundreds[2]='\0';
  srandom(atoi(hundreds));
}

int main(void)
{
  char c;
  int m,n,i,j,p,t,hidden[4],guess[4];
  
  while (1)
    {
      clrscr();
      randomize();
      puts("***********************");
      puts("* M A S T E R M I N D *");
      puts("***********************");
      putchar('\n');
      putchar('\n');
      puts("You must guess 4 numbers out of 1 , 2 , 3 , 4 , 5 and 6 with possible repetitions.");
      putchar('\n');
      puts("The computer evaluates you try with:");
      puts("	       *  =  number guessed in right position");
      puts("	       +  =  number guessed in wrong position");
      puts("	       -  =  no number guessed");
      putchar('\n');
      puts("Wanna play (y/n) ?");
      
      c=getchar();
      
      if(c=='y')
	{
	again:	clrscr();
	printf("\n \t \t \t      M A S T E R M I N D \n \n");
	puts("To quit game press q.");
	puts("To start a new game press n.");
	putchar('\n');
	
	for(i=0;i<4;i++)
	  hidden[i] = random()%6+1;
	
	n=0;
	while(1)
	  {
	    n++;
	    
	    if(n < 20)
	      printf("Guess no. %d : ",n);
	    else
	      printf("You can always try another time:");
	    
	    for(i=0;i<4;i++)
	      {
		c=getchar();
		
		if(c=='q')
		  goto end;
		if(c=='n')
		  goto again;
		if(c=='1')
		  m=1;
		if(c=='2')
		  m=2;
		if(c=='3')
		  m=3;
		if(c=='4')
		  m=4;
		if(c=='5')
		  m=5;
		if(c=='6')
		  m=6;
		if(c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6')
		  if(c != 'q' && c != 'n')
		    {
		      i = i-1;
		      continue;
		    }
		guess[i] = m;
	      }
	    
	    t=0;
	    p=0;
	    for(i=0;i<4;i++)
	      if (guess[i] == hidden[i])
		{
		  t++;
		  hidden[i] = hidden[i] - 8;
		  guess[i] = guess[i] - 16;
		}
	    for(i=0;i<4;i++)
	      for(j=0;j<4;j++)
		if (guess[i] == hidden[j])
		  {
		    p++;
		    hidden[j] = hidden[j] - 8;
		    guess[i] = guess[i] - 16;
		  }
	    
	    putchar(' ');
	    
	    fflush(stdin);
	    
	    if(t != 0)
	      for(i=0;i<t;i++)
		{
		  putchar('*');
		  putchar(' ');
		}
	    if(p != 0)
	      for(i=0;i<p;i++)
		{
		  putchar('+');
		  putchar(' ');
		}
	    if(t+p == 0)
	      putchar('-');

	    putchar('\n');

	    for(i=0;i<4;i++)
	      if (hidden[i] < 0)
		hidden[i] = hidden[i] + 8;

	    if(t == 4)
	      {
		if(n <= 4)
		  printf("EXCELLENT!!! You needed only %d guesses\n",n);
		if(n > 4 && n <= 7)
		  printf("Congratulations! You needed %d guesses\n",n);
		if(n > 7 && n <= 10)
		  printf("Good! You needed %d guesses\n",n);
		if(n > 10 && n <= 15)
		  printf("Poor! You needed %d guesses; you need to practice more!\n",n);
		if(n > 15)
		  printf("CATASTROPHIC!!! You needed %d guesses...\n",n);
		
		putchar('\n');
		puts("Another game (y/n) ?");
		c=getchar();
		if(c=='y')
		  goto again;
		else
		  goto end;
	      }
	  }
	}
      if(c=='n')
	goto end;
      if(c!='y' && c!='n')
	{
	  continue;
	}
    }

end:	clrscr();
puts("Mastermind (P) 1993 Cristofor Laurentiu Bogdan");
}

