public class FindMeAlgorithm
{
	private int [] master;
	private int [] guess;
	
	public static void main(String [] args)
	{		
		FindMeAlgorithm run = new FindMeAlgorithm();
		run.test();
	}
	
	public void test ( )
	{
		master = new int []{1,1,2,3};
		guess = new int []{2,2,1,1};
		printMaster();
		printGuess();
		findExactAndPartialMatches();
		guess = new int []{2,2,3,2};
		printMaster();
		printGuess();
		findExactAndPartialMatches();
		guess = new int []{1,2,1,1};
		printMaster();
		printGuess();
		findExactAndPartialMatches();
		master = new int []{1,3,3,3};
		guess = new int []{2,1,1,1};
		printMaster();
		printGuess();
		findExactAndPartialMatches();
		guess = new int []{3,3,3,3};
		printMaster();
		printGuess();
		findExactAndPartialMatches();
		guess = new int []{3,3,1,1};
		printMaster();
		printGuess();
		findExactAndPartialMatches();
	}
	
	public void printMaster ( )
	{
		System.out.print("\n  MASTER: ");
		for(int i = 0; i < master.length; i++)
		{
			System.out.print(master[i]);
		}
	}
	
	public void printGuess ( )
	{
		System.out.print("   GUESS: ");
		for(int i = 0; i < guess.length; i++)
		{
			System.out.print(guess[i]);
		}
	}
	
	public void findExactAndPartialMatches ( )
	{
		boolean [] used = new boolean[4];  //  the default values are false;
		
		int exact = calculateExacts(used);
		int partial = calculatePartials(used);
		
		System.out.print("    Exact: " + exact);
		System.out.println("    Partial: " + partial);
	}

	public int calculateExacts(boolean [] used)
	{
		int count = 0;
		for(int i = 0; i < master.length; i++)
		{
			if(master[i] == guess[i])
			{
				count++;
				used[i] = true;
				guess[i] = -1;
			}
		}
		return count;
	}
	
	public int calculatePartials(boolean [] used)
	{
		int count = 0;
		for(int i = 0; i < master.length; i++)
		{
			for(int j = 0; j < guess.length; j++)
			{
				if(i != j && master[i] == guess[j] && !used[i])
				{
					count++;
					used[i] = true;
					guess[j] = -1;
				}
			}
		}
		return count;
	}
}

Back to APCS Main Page