This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
JRummikub/src/jrummikub/model/StoneHeap.java
Jannis Harder f6a3409ed5 Use correct joker colors
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@310 72836036-5685-4462-b002-a69064685172
2011-05-29 20:22:45 +02:00

94 lines
2.1 KiB
Java

package jrummikub.model;
import java.util.ArrayList;
import java.util.Arrays;
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;
private Random generator = new Random();
/** Creates 106 Stones according to standard rules */
public StoneHeap(GameSettings gameSettings) {
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
ArrayList<StoneColor> jokerColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor.values()));
StoneColor temp = jokerColors.get(1);
jokerColors.set(1, jokerColors.get(3));
jokerColors.set(3, temp);
int jokersLeft = gameSettings.getJokerNumber();
done : while(true) {
for (StoneColor c : jokerColors) {
if (jokersLeft == 0)
break done;
heap.add(new Stone(c));
jokersLeft--;
}
}
}
/**
* Removes random {@link Stone} from the heap and returns it
*
* @return the drawn stone
*/
public Stone drawStone() {
if (heap.isEmpty()) {
return null;
}
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>();
number = Math.min(number, heap.size());
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);
}
}