Permalink
Browse files

Game, Field, FieldManager implemented

  • Loading branch information...
1 parent 68a3b6b commit b1e2f6c15f0b9000237413af7ccbbb1ae1609ae9 @chrstphlbr chrstphlbr committed Dec 17, 2011
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,9 +1,23 @@
package at.yoerg.businesslogic.board;
+import java.util.Collection;
import java.util.List;
+import at.yoerg.util.CollectionUtil;
+
public class Board {
private List<Field> fields;
+
+ public Collection<Field> getAllFields() {
+ return CollectionUtil.copy(getFields());
+ }
+ protected List<Field> getFields() {
+ return fields;
+ }
+
+ protected void setFields(List<Field> fields) {
+ this.fields = fields;
+ }
}
@@ -12,12 +12,12 @@ public Board createRandomBoard() {
}
public Board createRandomBoard(int fieldCount) {
-
+ return createBoard(FieldManager.getInstance().getRandomFields(fieldCount));
}
public Board createBoard(List<Field> fields) {
Board board = new Board();
-
+ board.setFields(fields);
return board;
}
}
@@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
+import java.util.Random;
import java.util.Set;
import at.yoerg.util.CollectionUtil;
@@ -39,15 +40,26 @@ private void init() {
throw new IllegalArgumentException("fieldCount has to be greater than 0");
}
List<Field> randomFields = new ArrayList<Field>();
+ Field[] fieldArray = (Field[])registeredFields.toArray();
for(int i = 0; i < fieldCount; i++) {
- randomFields.add(getRandomField());
+ randomFields.add(getRandomField(fieldArray));
}
return randomFields;
}
- private Field getRandomField() {
-
+ private Field getRandomField(Field[] fieldArray) {
+ Random rand = new Random();
+ int fieldsSize = fieldArray.length;
+ int randomOffset = rand.nextInt(fieldsSize - 1);
+ return fieldArray[randomOffset];
+ }
+
+ public void registerField(Field field) {
+ if(field == null) {
+ throw new NullPointerException();
+ }
+ registeredFields.add(field);
}
}
@@ -3,6 +3,8 @@
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
+import java.util.Iterator;
+import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
@@ -76,10 +78,36 @@ public boolean removePlayer(Player player) {
return CollectionUtil.copy(getPlayers().keySet());
}
- public Turn nextTurn() {
+ public Turn nextTurn(int pips) {
}
+ // returns the next player in line
+ private Player getNextPlayer() {
+ if(getPlayers().size() == 0) {
+ throw new IllegalStateException("no players added to game");
+ }
+ // if no turns have been played return the first player in line
+ if(getTurns().size() == 0) {
+ return getPlayers().firstKey();
+ }
+ Player lastPlayer = getTurns().last().getPlayer();
+ Iterator<Player> it = getPlayers().keySet().iterator();
+ Player current = null;
+ while(it.hasNext()) {
+ current = it.next();
+ if(lastPlayer.equals(current)) {
+ if(it.hasNext()) {
+ current = it.next();
+ } else {
+ current = getPlayers().firstKey();
+ }
+ break;
+ }
+ }
+ return current;
+ }
+
// returns copy of the turn set
public Collection<Turn> getAllTurns() {
return CollectionUtil.copy(getTurns());

0 comments on commit b1e2f6c

Please sign in to comment.