summaryrefslogtreecommitdiff
path: root/Mastermind/MiguelEngine.java
blob: 536419eebd9239fe75be0dc9b64968c16d21c47b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package Mastermind;

import Mastermind.EngineResponse;

public class MiguelEngine{

	public static EngineResponse run (int c1, int c2, int c3, int c4, int g1, int g2, int g3, int g4)
	{
		int S[]={c1,c2,c3,c4};
		int T[]={g1,g2,g3,g4};
		return genericRun(S,T);
	}

	public static EngineResponse genericRun(int S[], int T[])
	{
		int N=S.length, perfect=0, goodcolor=0, i, j;
		boolean used[] = new boolean[N];

		// scan and mark `perfects`
		for (i=0;i<N;i++) 
			if(T[i]==S[i])
			{
				perfect++; 
				used[i] = true;
			}

		// scan and mark `good color`
		for (i=0;i<N;i++)
			for(j=0;j<N;j++)
				if (T[i]!=S[i] && T[i]==S[j] && !used[j])
				{
					goodcolor++; 
					used[j] = true;
					break;
				}

		// construct result
		return new EngineResponse (perfect, goodcolor, N-perfect-goodcolor);
	}

}