summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/RoundControl.java
blob: efa6f621d916ee3ab02687fbe3ccd2fe38c360c6 (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
package jrummikub.control;

import java.util.HashSet;
import java.util.Set;

import jrummikub.model.IGameState;
import jrummikub.model.IHand;
import jrummikub.model.ITable;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.util.IListener;
import jrummikub.util.Pair;
import jrummikub.view.IView;

public class RoundControl {
	private IGameState gameState;
	private IView view;
	private ITable clonedTable;

	public RoundControl(IGameState gameState, IView view) {
		this.gameState = gameState;
		this.view = view;
	}

	public void startRound() {
		deal();

		view.getStartTurnEvent().add(new IListener() {

			@Override
			public void handle() {
				startTurn();
			}
		});

		prepareTurn();
	}

	private void prepareTurn() {
		clonedTable = (ITable) gameState.getTable().clone();
		view.enableStartTurnPanel(true);
		view.getTablePanel().setStoneSets(clonedTable);
		view.setCurrentPlayerName(gameState.getActivePlayer().getName());
		view.getTablePanel().setRightPlayerName(
				gameState.getNthNextPlayer(1).getName());
		view.getTablePanel().setTopPlayerName(
				gameState.getNthNextPlayer(2).getName());
		view.getTablePanel().setLeftPlayerName(
				gameState.getNthNextPlayer(3).getName());
	}

	private void startTurn() {
		TurnControl turnControl = new TurnControl(gameState.getActivePlayer()
				.getHand(), clonedTable, view);
		turnControl.getEndOfTurnEvent().add(new IListener() {

			@Override
			public void handle() {
				endOfTurn();
			}
		});

		turnControl.startTurn();
	}

	void deal() {
		for (int i = 0; i < gameState.getPlayerCount(); i++) {
			IHand hand = gameState.getNthNextPlayer(i).getHand();
			for (int j = 0; j < 7; j++) {
				hand.drop(gameState.getGameHeap().drawStone(), new Position(j, 0));
				hand.drop(gameState.getGameHeap().drawStone(), new Position(j, 1));
			}
		}
	}

	private void endOfTurn() {
		Set<Stone> tableDiff = tableDifference(gameState.getTable(), clonedTable);

		if (!tableDiff.isEmpty()) { // Player has made a move
			if (clonedTable.isValid()) {
				gameState.setTable(clonedTable);

				if (gameState.getActivePlayer().getHand().getSize() == 0) {
					win();
					return;
				}
			} else {
				gameState.getGameHeap().putBack(tableDiff);
				dealPenalty(tableDiff.size());
			}
		} else { // Player hasn't made a move
			if (clonedTable.isValid()) {
				gameState.setTable(clonedTable);
			}

			dealStone();
		}

		gameState.nextPlayer();
		prepareTurn();
	}

	static Set<Stone> tableDifference(ITable oldTable, ITable newTable) {
		Set<Stone> ret = new HashSet<Stone>();

		for (Pair<StoneSet, Position> entry : newTable) {
			for (Stone stone : entry.getFirst()) {
				ret.add(stone);
			}
		}
		for (Pair<StoneSet, Position> entry : oldTable) {
			for (Stone stone : entry.getFirst()) {
				ret.remove(stone);
			}
		}

		return ret;
	}

	private void dealStone() {
		gameState
				.getActivePlayer()
				.getHand()
				.drop(gameState.getGameHeap().drawStone(),
						new Position(7 + (int) (Math.random() * 6), 0.5f));
	}

	private void dealPenalty(int count) {
		for (int i = 0; i < count + 3; ++i)
			dealStone();
	}

	private void win() {
		view.enableWinPanel(true);
	}
}