summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/ai/TurnLogic.java
blob: dcb95fc090e1c00940e70803fa6e874da888bf52 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package jrummikub.ai;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jrummikub.ai.fdsolver.Constraint;
import jrummikub.ai.fdsolver.Solver;
import jrummikub.ai.fdsolver.Var;
import jrummikub.ai.fdsolver.constraint.IfConstraint;
import jrummikub.ai.fdsolver.constraint.IndexConstraint;
import jrummikub.ai.fdsolver.constraint.LessThan;
import jrummikub.model.GameSettings;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import static jrummikub.ai.fdsolver.Constraints.*;

/**
 * Provides Humanlike Advanced Logic (HAL) that models the options given to a
 * player in a turn to the Solver MCP.
 */
public class TurnLogic {
	private Solver solver = new Solver();
	private GameSettings settings;
	private int stoneCount;
	private int maxSetSize;

	private Var<Boolean> trueVar;

	private List<Var<Boolean>> onTable = new ArrayList<Var<Boolean>>();

	private List<Var<Integer>> stoneValue = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> leftStoneValue = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> rightStoneValue = new ArrayList<Var<Integer>>();

	private List<Var<StoneColor>> stoneColor = new ArrayList<Var<StoneColor>>();
	private List<Var<StoneColor>> leftStoneColor = new ArrayList<Var<StoneColor>>();
	private List<Var<StoneColor>> rightStoneColor = new ArrayList<Var<StoneColor>>();

	private List<Var<Integer>> lowCount = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> leftLowCount = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> rightLowCount = new ArrayList<Var<Integer>>();

	private List<Var<Integer>> highCount = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> leftHighCount = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> rightHighCount = new ArrayList<Var<Integer>>();

	private List<Var<Integer>> setSize = new ArrayList<Var<Integer>>();

	private List<Var<Boolean>> isRun = new ArrayList<Var<Boolean>>();

	private List<Var<Integer>> leftNeighbor = new ArrayList<Var<Integer>>();
	private List<Var<Integer>> rightNeighbor = new ArrayList<Var<Integer>>();

	private List<Var<Integer>> stoneID = new ArrayList<Var<Integer>>();

	private List<Var<Boolean>> hasLeftNeighbor = new ArrayList<Var<Boolean>>();
	private List<Var<Boolean>> hasRightNeighbor = new ArrayList<Var<Boolean>>();

	private List<Boolean> isJoker = new ArrayList<Boolean>();

	public TurnLogic(GameSettings settings, Collection<Stone> tableStones,
			Collection<Stone> handStones) {
		this.settings = settings;
		stoneCount = tableStones.size() + handStones.size();
		maxSetSize = Math.max(settings.getHighestValue(), settings
				.getStoneColors().size());

		trueVar = solver.makeVar(true);

		int i = 0;
		for (Stone stone : tableStones) {
			addStone(i, true, stone);
			i++;
		}
		for (Stone stone : handStones) {
			addStone(i, false, stone);
			i++;
		}

		for (i = 0; i < stoneCount; i++) {
			addConstraints(i);
		}
	}

	private void addStone(int i, boolean table, Stone stone) {
		if (table) {
			onTable.add(trueVar);
		} else {
			onTable.add(solver.makeBoolVar());
		}
		isJoker.add(stone.isJoker());
		if (stone.isJoker()) {
			stoneValue.add(solver.makeRangeVar(1, settings.getHighestValue()));
			stoneColor.add(solver.makeVar(settings.getStoneColors()));
		} else {
			stoneValue.add(solver.makeVar(stone.getValue()));
			stoneColor.add(solver.makeVar(stone.getColor()));
		}

		leftStoneValue.add(solver.makeRangeVar(1, settings.getHighestValue()));
		rightStoneValue.add(solver.makeRangeVar(1, settings.getHighestValue()));
		leftStoneColor.add(solver.makeVar(settings.getStoneColors()));
		rightStoneColor.add(solver.makeVar(settings.getStoneColors()));

		lowCount.add(solver.makeRangeVar(0, maxSetSize - 1));
		leftLowCount.add(solver.makeRangeVar(0, maxSetSize - 1));
		rightLowCount.add(solver.makeRangeVar(0, maxSetSize - 1));

		highCount.add(solver.makeRangeVar(0, maxSetSize - 1));
		leftHighCount.add(solver.makeRangeVar(0, maxSetSize - 1));
		rightHighCount.add(solver.makeRangeVar(0, maxSetSize - 1));

		setSize.add(solver.makeRangeVar(2, maxSetSize - 1));

		isRun.add(solver.makeBoolVar());

		leftNeighbor.add(solver.makeRangeVar(0, stoneCount - 1));
		rightNeighbor.add(solver.makeRangeVar(0, stoneCount - 1));

		stoneID.add(solver.makeVar(i));

		hasLeftNeighbor.add(solver.makeBoolVar());
		hasRightNeighbor.add(solver.makeBoolVar());
	}

	private void add(Constraint c) {
		solver.addConstraint(c);
	}

	private void addConstraints(int i) {
		// Linking of neighbors
		add(when(hasLeftNeighbor.get(i),
				index(stoneID.get(i), leftNeighbor.get(i), rightNeighbor)));
		add(unless(hasLeftNeighbor.get(i), constant(leftNeighbor.get(i), 0)));
		add(when(hasLeftNeighbor.get(i),
				index(trueVar, leftNeighbor.get(i), hasRightNeighbor)));
		add(when(hasRightNeighbor.get(i),
				index(stoneID.get(i), rightNeighbor.get(i), leftNeighbor)));
		add(unless(hasRightNeighbor.get(i), constant(rightNeighbor.get(i), 0)));
		add(when(hasRightNeighbor.get(i),
				index(trueVar, rightNeighbor.get(i), hasLeftNeighbor)));
		// hand stones have no neighbors
		add(unless(onTable.get(i), constant(hasLeftNeighbor.get(i), false)));
		add(unless(onTable.get(i), constant(hasRightNeighbor.get(i), false)));

		// low/high counts and size
		add(unless(hasLeftNeighbor.get(i), constant(lowCount.get(i), 0)));
		add(unless(hasRightNeighbor.get(i), constant(highCount.get(i), 0)));

		add(when(hasLeftNeighbor.get(i),
				index(leftLowCount.get(i), leftNeighbor.get(i), lowCount)));
		add(unless(hasLeftNeighbor.get(i), constant(leftLowCount.get(i), 0)));

		add(when(hasRightNeighbor.get(i),
				index(rightLowCount.get(i), rightNeighbor.get(i), lowCount)));
		add(unless(hasRightNeighbor.get(i), constant(rightLowCount.get(i), 0)));

		add(when(hasLeftNeighbor.get(i),
				index(leftHighCount.get(i), leftNeighbor.get(i), highCount)));
		add(unless(hasLeftNeighbor.get(i), constant(leftHighCount.get(i), 0)));

		add(when(hasRightNeighbor.get(i),
				index(rightHighCount.get(i), rightNeighbor.get(i), highCount)));
		add(unless(hasRightNeighbor.get(i), constant(rightHighCount.get(i), 0)));

		add(when(hasLeftNeighbor.get(i),
				index(setSize.get(i), leftNeighbor.get(i), setSize)));

		add(when(hasRightNeighbor.get(i),
				index(setSize.get(i), rightNeighbor.get(i), setSize)));

		add(when(hasLeftNeighbor.get(i),
				offset(1, leftLowCount.get(i), lowCount.get(i))));
		add(when(hasRightNeighbor.get(i),
				offset(1, lowCount.get(i), rightLowCount.get(i))));

		add(when(hasRightNeighbor.get(i),
				offset(1, rightHighCount.get(i), highCount.get(i))));
		add(when(hasLeftNeighbor.get(i),
				offset(1, highCount.get(i), leftHighCount.get(i))));

		add(when(onTable.get(i),
				sum(lowCount.get(i), highCount.get(i), setSize.get(i))));

		add(unless(onTable.get(i), constant(setSize.get(i), 2)));

		// set same rules
		add(when(hasLeftNeighbor.get(i),
				index(isRun.get(i), leftNeighbor.get(i), isRun)));
		add(when(hasRightNeighbor.get(i),
				index(isRun.get(i), rightNeighbor.get(i), isRun)));

		add(unless(onTable.get(i), constant(isRun.get(i), false)));

		// rule neighbors
		add(when(hasLeftNeighbor.get(i),
				index(leftStoneColor.get(i), leftNeighbor.get(i), stoneColor)));
		add(unless(hasLeftNeighbor.get(i),
				constant(leftStoneColor.get(i), StoneColor.RED)));
		add(when(hasRightNeighbor.get(i),
				index(rightStoneColor.get(i), rightNeighbor.get(i), stoneColor)));
		add(unless(hasRightNeighbor.get(i),
				constant(rightStoneColor.get(i), StoneColor.RED)));

		add(when(hasLeftNeighbor.get(i),
				index(leftStoneValue.get(i), leftNeighbor.get(i), stoneValue)));
		add(unless(hasLeftNeighbor.get(i), constant(leftStoneValue.get(i), 1)));
		add(when(hasRightNeighbor.get(i),
				index(rightStoneValue.get(i), rightNeighbor.get(i), stoneValue)));
		add(unless(hasRightNeighbor.get(i), constant(rightStoneValue.get(i), 1)));
		// general rules

		add(when(
				hasLeftNeighbor.get(i),
				when(isRun.get(i),
						lessThanEq(leftStoneValue.get(i), stoneValue.get(i)))));
		add(when(
				hasRightNeighbor.get(i),
				when(isRun.get(i),
						lessThanEq(stoneValue.get(i), rightStoneValue.get(i)))));

		add(when(
				hasLeftNeighbor.get(i),
				when(isRun.get(i),
						lessThanEq(leftStoneColor.get(i), stoneColor.get(i)))));
		add(when(
				hasRightNeighbor.get(i),
				when(isRun.get(i),
						lessThanEq(stoneColor.get(i), rightStoneColor.get(i)))));

		// run rules

		add(when(
				hasLeftNeighbor.get(i),
				when(isRun.get(i),
						offset(1, leftStoneValue.get(i), stoneValue.get(i)))));
		add(when(
				hasRightNeighbor.get(i),
				when(isRun.get(i),
						offset(1, stoneValue.get(i), rightStoneValue.get(i)))));

		add(when(
				hasLeftNeighbor.get(i),
				when(isRun.get(i),
						same(leftStoneColor.get(i), stoneColor.get(i)))));
		add(when(
				hasRightNeighbor.get(i),
				when(isRun.get(i),
						same(stoneColor.get(i), rightStoneColor.get(i)))));

		// group rules
		add(when(
				hasLeftNeighbor.get(i),
				unless(isRun.get(i),
						same(leftStoneValue.get(i), stoneValue.get(i)))));
		add(when(
				hasRightNeighbor.get(i),
				unless(isRun.get(i),
						same(stoneValue.get(i), rightStoneValue.get(i)))));

		add(when(
				hasLeftNeighbor.get(i),
				unless(isRun.get(i),
						lessThan(leftStoneColor.get(i), stoneColor.get(i)))));
		add(when(
				hasRightNeighbor.get(i),
				unless(isRun.get(i),
						lessThan(stoneColor.get(i), rightStoneColor.get(i)))));

		// joker defaulting
		if (isJoker.get(i)) {
			add(unless(onTable.get(i), constant(stoneValue.get(i), 1)));
			add(unless(onTable.get(i),
					constant(stoneColor.get(i), StoneColor.RED)));
		}
	}

	public boolean solve() {
		Set<Integer> tableIDs = new HashSet<Integer>();
		Set<Integer> leftIDs = new HashSet<Integer>();
		boolean res = solver.solve();
		if (!res)
			return false;
		System.out.println("== Hand ==");
		for (int i = 0; i < stoneCount; i++) {
			if (onTable.get(i).getValue()) {
				tableIDs.add(i);
				if (!hasLeftNeighbor.get(i).getValue()) {
					leftIDs.add(i);
				}
			} else {
				outputStone(i);
			}
		}
		System.out.println("");
		System.out.println("== Table ==");
		boolean newLine = false;
		boolean first = true;
		int nextID = -1; // leftIDs.iterator().next();
		while (!tableIDs.isEmpty()) {
			if (tableIDs.contains(nextID)) {
				tableIDs.remove(nextID);
				leftIDs.remove(nextID);
				if (newLine) {
					if (!first) {
						System.out.println("");
					}
					first = false;
					newLine = false;
				}
				outputStone(nextID);
				if (!hasRightNeighbor.get(nextID).getValue()) {
					newLine = true;
					nextID = -1;
				} else {
					nextID = rightNeighbor.get(nextID).getValue();
				}
			} else {
				if (leftIDs.isEmpty()) {
					nextID = tableIDs.iterator().next();
				} else {
					nextID = leftIDs.iterator().next();
				}
				newLine = true;
			}
		}
		System.out.println("");
		System.out.println("");
		return res;
	}

	private void outputStone(int i) {
		System.out.print("["
				+ stoneColor.get(i).getValue().toString().substring(0, 1)
				+ stoneValue.get(i).getValue() + "]");
		// + "," + leftNeighbor.get(i).getValue() +
		// hasLeftNeighbor.get(i).getValue() + lowCount.get(i).getValue() + ","
		// + rightNeighbor.get(i).getValue() +
		// hasRightNeighbor.get(i).getValue() + highCount.get(i).getValue() +
		// "]");
	}
}