summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/turn/BaseAIControl.java
blob: 3c4834109e7e28f1de059a0ed0752777be524964 (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
package jrummikub.control.turn;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.swing.SwingUtilities;

import jrummikub.control.AIUtil;
import jrummikub.control.ITurnTimer;
import jrummikub.control.TurnTimer;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.Connection;
import jrummikub.util.IListener;
import jrummikub.util.Pair;

/**
 * Base class for AI players
 * 
 */
public class BaseAIControl extends AbstractTurnControl {
	long startTime;
	private ITurnTimer turnTimer;
	private List<Connection> connections = new ArrayList<Connection>();
	private Thread computeThread;

	private volatile boolean stopRunning = false;

	@Override
	public void startTurn() {
		turnTimer = new TurnTimer(view);
		connections.add(turnTimer.getTimeRunOutEvent().add(new IListener() {

			@Override
			public void handle() {
				cleanUp();
				endOfTurnEvent.emit();
			}
		}));

		computeThread = new Thread(new Runnable() {
			@Override
			public void run() {
				compute();
			}
		});
		startTime = System.currentTimeMillis();

		turnTimer.startTimer();
		computeThread.start();
	}

	private void cleanUp() {
		turnTimer.stopTimer();

		for (Connection c : connections) {
			c.remove();
		}
		stopRunning = true;
	}

	private void compute() {
		switch (turnMode) {
		case MAY_REDEAL:
			emitRedeal();
			break;
		case INSPECT_ONLY:
			emitEndOfTurn();
			break;
		case NORMAL_TURN:
			turn();
			break;
		}
	}

	private Stone findMatchingStone(Stone target) {
		for (Pair<Stone, Position> entry : hand) {
			Stone stone = entry.getFirst();
			if (stone.getValue() == target.getValue()
					&& stone.getColor() == target.getColor()) {
				return stone;
			}
		}
		for (Pair<Stone, Position> entry : hand) {
			Stone stone = entry.getFirst();
			if (stone.isJoker()) {
				return stone;
			}
		}
		return null;
	}

	private Stone pickUpMatchingStone(Stone target) {
		Stone match = findMatchingStone(target);
		hand.pickUp(match);
		return match;
	}

	private void turn() {
		List<Stone> stones = new ArrayList<Stone>();

		for (Pair<Stone, Position> entry : hand) {
			stones.add(entry.getFirst());
		}

		Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> counts = AIUtil
				.countStones(stones);

		AIUtil aiUtil = new AIUtil(settings);

		Pair<List<StoneSet>, Integer> result = aiUtil.findSetsWithTotalPoints(
				Math.max(30, settings.getInitialMeldThreshold() * 2),
				counts.getFirst(), counts.getSecond());

		if (!player.getLaidOut()
				&& result.getSecond() < settings.getInitialMeldThreshold()) {
			emitEndOfTurn();
			return;
		}

		for (StoneSet set : result.getFirst()) {
			List<Stone> handStones = new ArrayList<Stone>();
			for (Stone stone : set) {
				handStones.add(pickUpMatchingStone(stone));
			}
			table.drop(new StoneSet(handStones), new Position(
					(float) Math.random() * 30 - 15, (float) Math.random() * 6 - 3));
		}

		emitEndOfTurn();
	}

	private void emitRedeal() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				if (!stopRunning) {
					cleanUp();
					redealEvent.emit();
				}
			}
		});
	}

	private void emitEndOfTurn() {
		long timeElapsed = System.currentTimeMillis() - startTime;
		long timeNeeded = Math.min((long) (1000 + Math.random() * hand.getSize()
				* 100), 50000);
		long waitTime = timeNeeded - timeElapsed;

		if (waitTime > 0) {
			try {
				Thread.sleep(waitTime);
			} catch (InterruptedException e) {
				// This shouldn't happen
			}
		}

		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				if (!stopRunning) {
					cleanUp();
					endOfTurnEvent.emit();
				}
			}
		});
	}

	/**
	 * Get the factory for the base AI control
	 * 
	 * @return the factory
	 */
	static public TurnControlFactory getFactory() {
		return new TurnControlFactory() {
			@Override
			public ITurnControl create() {
				return new BaseAIControl();
			}
		};
	}

}