summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/control/turn/AIControlTest.java
blob: 6da4b0083dc06c123389395f6a7b7e70c9a90fe5 (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
package jrummikub.control.turn;

import static org.junit.Assert.*;

import java.awt.Color;

import jrummikub.model.GameSettings;
import jrummikub.model.IHand;
import jrummikub.model.IPlayer;
import jrummikub.model.ITable;
import jrummikub.model.Player;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.Table;
import jrummikub.util.IListener;
import jrummikub.view.MockView;

import org.junit.Before;
import org.junit.Test;

/** */
public class AIControlTest {
	ITurnControl aiControl;

	GameSettings gameSettings;
	PlayerSettings playerSettings;
	IPlayer player;
	ITable table;
	MockView view;

	boolean turnEnded;
	boolean redealt;

	/** */
	@Before
	public void setUp() {
		aiControl = TurnControlFactory.getFactory(Type.COMPUTER).create();
		((AIControl) aiControl).useBackgroundThread = false;
		gameSettings = new GameSettings();
		playerSettings = new PlayerSettings("ROBOT_01", Color.GRAY);
		player = new Player(playerSettings);
		table = new Table(gameSettings);
		view = new MockView();
		turnEnded = false;
		redealt = false;

		aiControl.getEndOfTurnEvent().add(new IListener() {

			@Override
			public void handle() {
				turnEnded = true;
			}

		});

		aiControl.getRedealEvent().add(new IListener() {

			@Override
			public void handle() {
				redealt = true;
			}

		});

		IHand hand = player.getHand();
		hand.drop(new Stone(11, StoneColor.RED), new Position(0, 0));
		hand.drop(new Stone(12, StoneColor.RED), new Position(0, 0));
		hand.drop(new Stone(13, StoneColor.RED), new Position(0, 0));
	}

	/**
	 * @throws InterruptedException
	 *             if timeout
	 */
	@Test(timeout = 10000)
	public void testTurnZeroNoRedealing() throws InterruptedException {
		aiControl.setup(new ITurnControl.TurnInfo(table, player.getHand(),
				player.getLaidOut(), TurnMode.MAY_REDEAL), gameSettings, view);
		aiControl.startTurn();
		assertTrue(turnEnded);
		assertFalse(redealt);
		assertEquals(table.getSize(), 0);
	}

	/**
	 * @throws InterruptedException
	 *             if timeout
	 */
	@Test(timeout = 10000)
	public void testTurnZeroNotMelding() throws InterruptedException {
		aiControl
				.setup(new ITurnControl.TurnInfo(table, player.getHand(),
						player.getLaidOut(), TurnMode.INSPECT_ONLY),
						gameSettings, view);
		aiControl.startTurn();
		assertTrue(turnEnded);
		assertFalse(redealt);
		assertEquals(table.getSize(), 0);
	}

	/**
	 * @throws InterruptedException
	 *             if timeout
	 */
	@Test(timeout = 10000)
	public void testNormalTurnMelding() throws InterruptedException {
		aiControl.setup(new ITurnControl.TurnInfo(table, player.getHand(),
				player.getLaidOut(), TurnMode.NORMAL_TURN), gameSettings, view);
		aiControl.startTurn();
		assertTrue(turnEnded);
		assertFalse(redealt);
		assertEquals(table.getSize(), 1);
		assertEquals(player.getHand().getSize(), 0);
	}
}