summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/RoundState.java
blob: 65bc120674b69c6dcb1b006bebe50c6ec420fdef (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
package jrummikub.model;

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

/** Class managing the overall and momentary RoundState */
public class RoundState implements IRoundState {
	private GameSettings gameSettings;

	private ITable table;
	private List<Player> players;
	private int activePlayer;
	private StoneHeap gameHeap;
	private IPlayer lastPlayer;
	private int turnNumber; 

	/**
	 * Create a new RoundState with an empty table
	 * 
	 * @param gameSettings
	 *            the game settings
	 */
	public RoundState(GameSettings gameSettings) {
		this.gameSettings = gameSettings;

		table = new Table();
		players = new ArrayList<Player>();

		for (PlayerSettings playerSettings : gameSettings.getPlayerList()) {
			players.add(new Player(playerSettings, gameSettings));
		}

		turnNumber = 1-gameSettings.getPlayerList().size();
		
		activePlayer = 0;
		gameHeap = new StoneHeap();
	}

	@Override
	public ITable getTable() {
		return table;
	}

	@Override
	public void setTable(ITable table) {
		this.table = table;
	}

	@Override
	public int getPlayerCount() {
		return players.size();
	}

	@Override
	public void nextPlayer() {
		activePlayer = (activePlayer + 1) % players.size();
	}

	@Override
	public void setActivePlayerNumber(int i) {
		int j = i % players.size();
		if (j < 0) {
			j += players.size();
		}
		activePlayer = j;
	}

	@Override
	public IPlayer getNthNextPlayer(int i) {
		int j = (activePlayer + i) % players.size();
		if (j < 0) {
			j += players.size();
		}
		return players.get(j);
	}
	
	@Override
	public IPlayer getNthPlayer(int i) {
		int j = i % players.size();
		if (j < 0) {
			j += players.size();
		}
		return players.get(j);
	}

	@Override
	public IPlayer getActivePlayer() {
		return players.get(activePlayer);
	}

	@Override
	public StoneHeap getGameHeap() {
		return gameHeap;
	}

	@Override
	public GameSettings getGameSettings() {
		return gameSettings;
	}

	@Override
	public void setLastPlayer(IPlayer lastPlayer) {
		this.lastPlayer = lastPlayer;
	}

	@Override
	public IPlayer getLastPlayer() {
		return lastPlayer;
	}
	
	@Override
	public int getTurnNumber() {
		return turnNumber;
	}

	@Override
	public void nextTurn() {
		turnNumber++;
	}
}