// mastermind "engine" class
// by dennis g. 2007

import java.util.Random;

public class Mengine {

int [] mind = {0,0,0,0};
Random gen = new Random();

    public void newgame() {
	// generate 4 random numbers...

        mind[0] = gen.nextInt(9);
        mind[1] = gen.nextInt(9);
        mind[2] = gen.nextInt(9);
        mind[3] = gen.nextInt(9);
    }

    public int [] test(int [] atest) {
	// first value of results is for the number of "correct at position"
	// second value of results is for the number "correct somewhere"
	// third value is for "not correct"
	int [] results = {0,0,0};

	// these are blacklists for the array to be checked
	// and the numbers in the mind
	// so they dont get counted multiple times

	boolean [] mindlist = {false,false,false,false};
	boolean [] blacklist = {false,false,false,false};

        // test if the values are correct on their positions
        for(int i=0; i<4; i++)
        {
            if (mind[i] == atest[i]) {
		results[0]++;
		blacklist[i]=true;
		mindlist[i]=true;
		}
        }


        // test if the values are correct for any position
        for(int i=0; i<4; i++)
        {

		for (int k=0; k<4; k++)
                {
			
		    //  1. k should not be i, as we already checked those, and these should not be included here
		    //  2. of course check if the number in the mind is the number it is compared to 
		    //  3. the number in the numbers to be checked array shouldnt be already counted
		    //  4. the number in the mind shouldnt be already counted
			
		    if (k !=i && atest[k] == mind[i] && blacklist[k]==false && mindlist[i]==false) {
			results[1]++;
			blacklist[k]=true;
			mindlist[i]=true;
//			k=4;
			}
                }
        }

	// calculate the leftover numbers which couldnt be find in the mind
	results[2] = 4-results[0]-results[1];

	// return the array of results :-)
	return results;

    }
}
