summaryrefslogtreecommitdiff
path: root/projects/project3_kenken/stab2/kenkensolver.java
blob: dd36f5ef8776313625c8914c7a0990f1390f23f1 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
public class kenkensolver
{
	public static boolean done = false;
	public static void main(String args[])
	{
		Solver s = new Solver("3x3_1.txt");
		s.printPuzzle();
		KStack stack = new KStack();
		//int[][] implied = s.implyByRows();
		solve(s,stack);
		System.out.println();
		s.printPuzzle();
	}
	public static void solve(Solver s, KStack k)
	{
		System.out.println("Attempting to solve:");
		s.printArray(s.puzzle);
		System.out.println("Stack is:");
		s.printArray(k.toArray());
		if(s.isSolved())
		{
			System.out.println("Done!");
			done = true;
			return;
		}
		else 
		{
			int[][] rows = s.implyByRows();
			if(rows.length > 0)
			{
				//System.out.println("Implying by rows because imply returned ");
				//s.printArray(rows);
				for(int[] i : rows)
				{
					k.push(i);
				}
				solve(s,k);
				return;
			}
			else
			{
				int[][] cols = s.implyByCols();
				if(cols.length > 0)
				{
					for(int[] i : cols)
					{
						k.push(i);
					}
					solve(s,k);
					return;
				}
				else
				{
					int[][] cage = s.implyByCages();
					if(cage.length > 0)
					{
						for(int[] i : cage)
						{
							k.push(i);
						}
						solve(s,k);
						return;
					}
					else
					{//We have implyed by rows, cols, and cages, now we just have to guess.
						//Fist, find the first(from top left) spot in the puzzle that's still 0
						int lastx = -1;
						int lasty = -1;
						search:
						for(int x = 0; x < s.size; x++)
						{
							for(int y = 0; y < s.size;y++)
							{
								if(s.puzzle[x][y] == 0)
								{
									lastx = x;
									lasty = y;
									System.out.println("Attempting to guess number for (" + x + "," + y + ")");
									break search;
								}
							}
						}
						boolean[] possibilities = s.getPossibilities(lastx,lasty);
						int numpossible = 0;
						for(boolean b : possibilities)
						{
							if(b)
								numpossible++;
						}
						if(numpossible > 0)
						{
							int[] guess = new int[4];
							for(int i = 0; i < possibilities.length && !done; i++)
							{
								System.out.println("Unable to imply a solution for (" + lastx + "," + lasty + ") guessing:");
								s.printArray(possibilities);
								if(possibilities[i])
								{
									if(i == possibilities.length-1)
									{
										guess[0] = 0;
									}
									else
									{
										guess[0] = 1;
									}	
									guess[1] = lastx;
									guess[2] = lasty;
									guess[3] = i+1;
									k.push(guess);
									s.put(lastx,lasty,i+1);
									solve(s,k);
								}
							}
						}
						else
						{
							//We have exhusted all possibilities for this square
							System.out.println("\nExhausted possibilities.. backtracking\n");
							while(k.getLength() > 0 && k.peek()[0] == 0 && !done) //While implied stuff is still on top
							{
								int[] thisitem = k.pop();
								System.out.println("Removeing:");
								s.printArray(thisitem);
								s.remove(thisitem[1],thisitem[2]);
							}
							//And pop the last guess
							int[] lastguess = k.pop();
							System.out.println("Removeing:");
							s.printArray(lastguess);
							s.remove(lastguess[1],lastguess[2]);
							System.out.println("Done backtracking");
							return;
						}
					}
				}
			}
		}
		//System.out.println("Something has gone horribly wrong!");
	}
}