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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import jrummikub.util.Pair;

/** Class managing {@link Stone}s joined together to form sets */
public class StoneSet implements Iterable<Stone> {
	private List<Stone> stones;

	public StoneSet(Stone stone) {
		stones = Collections.singletonList(stone);
	}

	public StoneSet(List<Stone> stones) {
		this.stones = new ArrayList<Stone>(stones);
	}

	/** Test for rule conflict within the StoneSet */
	public boolean isValid() {
		if (stones.size() < 3) {
			return false;
		}
		int nonJoker1 = -1, nonJoker2 = -1;
		for (int i = 0; i < stones.size(); i++) {
			if (stones.get(i).isJoker()) {
				continue;
			}
			nonJoker2 = nonJoker1;
			nonJoker1 = i;
		}
		if (nonJoker2 == -1) {
			return true;
		}
		// is run
		if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2)
				.getColor()) {
			StoneColor runColor = stones.get(nonJoker1).getColor();
			int startValue = stones.get(nonJoker1).getValue() - nonJoker1;
			int endValue = startValue + stones.size() - 1;
			if (startValue < 1 || endValue > 13) {
				return false;
			}
			for (int i = 0; i < stones.size(); i++) {
				if (stones.get(i).isJoker()) {
					continue;
				}
				if (stones.get(i).getColor() != runColor) {
					// warum macht er das nicht?
					return false;
				}
				if (stones.get(i).getValue() != i + startValue) {
					return false;
				}
			}
			return true;

		}
		// is group
		else {
			if (stones.size() > 4) {
				return false;
			}
			Set<StoneColor> seenColors = new HashSet<StoneColor>();
			for (Stone i : stones) {
				if (i.isJoker()) {
					continue;
				}
				if (seenColors.contains(i.getColor())) {
					return false;
				} else {
					seenColors.add(i.getColor());
				}
			}
			return true;
		}
	}

	/**
	 * Splits the StoneSet at a specified {@link Position} and returns two new
	 * Stone Sets
	 * 
	 * @param position
	 *            Splitting {@link Position}
	 */
	public Pair<StoneSet, StoneSet> splitAt(int position) {
		// Exception falls falscher index
		if (position == 0 || position == stones.size()) {

		} else {

		}
		return null;

	}

	/**
	 * Joins StoneSet to another StoneSet and returns the resulting new StoneSet
	 * 
	 * @param other
	 *            StoneSet to be joined to active StoneSet
	 */
	public StoneSet join(StoneSet other) {
		return null;

	}

	public int size() {
		return stones.size();
	}

	public Stone get(int i) {
		return stones.get(i);
	}

	@Override
	public Iterator<Stone> iterator() {
		final Iterator<Stone> it = stones.iterator();

		return new Iterator<Stone>() {

			@Override
			public boolean hasNext() {
				return it.hasNext();
			}

			@Override
			public Stone next() {
				return it.next();
			}

			@Override
			public void remove() {
				// removing stones is impossible

				throw new NotImplementedException();
			}
		};
	}

}