/**
 * @(#)lotto.java
 *
 *
 * @author 
 * @version 1.00 2009/3/4
 */

import java.util.*;

public class mastermindgame 
{
		static Scanner pcso= new Scanner(System.in);
		public static void main (String args[])
   		 {
    		int invalid,ctr=0,improper,right;
    			int guess1,guess2,guess3;
    			
    			int usedR1=0,usedR2=0,usedR3=0;
    			int usedG1=0,usedG2=0,usedG3=0;
    			
	//random
    		
    		int random1=(int)(Math.random()*5+1);//1st number 

    		int random2=(int)(Math.random()*5+1);//second number

    		int random3=(int) (Math.random()*5+1);//third number

	//user input
				System.out.printf("\nGenerated Numbers = %d,%d,%d",random1,random2,random3);
			
    
    do{
    	invalid=0;
    	improper=0;
    	right=0;
    	ctr++;
    	usedR1=0;usedR2=0;usedR3=0;
    	usedG1=0;usedG2=0;usedG3=0;
			
    		System.out.println("\nInput guess for first number:");
    		guess1=pcso.nextInt();
    		
    		System.out.println("\nInput guess for second number:");
    		guess2=pcso.nextInt();
    		 
    		System.out.println("\nInput guess for third number:");
    		guess3=pcso.nextInt();
    		
  		  	if(random1==guess1)
			{	right++; 
			usedR1=1;
			usedG1=1;

			}
  		  	if(random2==guess2)
			{	right++; 
			usedR2=1;
			usedG2=1;

			}
			if(random3==guess3)
			{	right++; 
			usedR3=1;
			usedG3=1;
			}  		
  		
	
				if(usedG2==0 && usedR1==0)
				{
					if(random1==guess2)
					{
						improper++;
						usedR1=1;   	
						usedG2=1;						
					}
				}
				if(usedG3==0 && usedR1==0)
				{
					if(random1==guess3)
					{
						improper++;
						usedR1=1;
						usedG3=1;						
					}
				}
		
				if(usedG1==0 && usedR2==0)
				{
					if(random2==guess1)
					{
						improper++;
						usedR2=1;
						usedG1=1;						
					}
				}
				if(usedG3==0 && usedR2==0)
				{
					if(random2==guess3)
					{
						improper++;
						usedR2=1;
						usedG3=1;						
					}
				}

    			if(usedG1==0 && usedR3==0)
				{
					if(random3==guess1)
					{
						improper++;
						usedR3=1;
						usedG1=1;						
					}
				}
				if(usedG2==0 && usedR3==0)
				{
					if(random3==guess2)
					{
						improper++;
						usedR3=1;
						usedG2=1;						
					}
				}

	    				
    				invalid=3-right-improper; 
    				  
    		System.out.printf("\nInvalid = %d",invalid);
    		System.out.printf("\nImproper = %d",improper);
    		System.out.printf("\nRIGHT = %d",right);
    			}
    			while((random1!=guess1 || random2!=guess2 || random3!=guess3) && ctr>0 && ctr<10 && ctr!=10);//if satisfied it will loop back
    	
    	if(random1==guess1&&random2==guess2&&random3==guess3)	
    	{System.out.println("\n\nYou win!");
    	System.out.printf("\nYou finished the game with only %d trial/s",ctr);
    	}	
    		else
    	System.out.println("Sorry you have exceeded the maximum no. of trials");
    	
    	
    	}	
    
}