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

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;

/**
 * StoneHeap creates all {@link Stone}s for a game, manages them and allows
 * players to draw one or more random Stones.
 */

public class StoneHeap {
	List<Stone> heap;
	Random generator = new Random();

	/** Creates 106 Stones according to standard rules */
	public StoneHeap() {
		heap = new ArrayList<Stone>();
		for (int i = 1; i <= 13; i++) {
			for (int j = 0; j < 2; j++) {
				for (StoneColor c : EnumSet.allOf(StoneColor.class)) {
					heap.add(new Stone(i, c));
				}
			}
		}
		// Joker
		heap.add(new Stone(StoneColor.BLACK));
		heap.add(new Stone(StoneColor.RED));
	}

	/**
	 * Removes random {@link Stone} from the heap and returns it
	 * 
	 * @return the drawn stone
	 */
	public Stone drawStone() {
		return heap.remove(generator.nextInt(heap.size()));
	}

	/**
	 * Removes several {@link Stone}s from the heap and returns them
	 * 
	 * @param number
	 *            number of requested Stones
	 * @return list of drawn stones
	 */
	public List<Stone> drawStones(int number) {
		List<Stone> drawnStones = new ArrayList<Stone>();
		for (int i = 0; i < number; i++) {
			drawnStones.add(drawStone());
		}
		return drawnStones;
	}

	/**
	 * Get the number of stones left
	 * 
	 * @return number of stones on the heap
	 */
	public int getSize() {
		return heap.size();
	}

	/**
	 * Put stones back on the heap
	 * 
	 * @param stones
	 *            collection of stones to put back
	 */
	public void putBack(Collection<Stone> stones) {
		heap.addAll(stones);
	}
}