Make model fully serializable

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@381 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-07 16:59:06 +02:00
parent af3661fea0
commit 244abb7e73
16 changed files with 119 additions and 86 deletions

View file

@ -2,6 +2,7 @@ package jrummikub.model;
import static jrummikub.model.StoneColor.*; import static jrummikub.model.StoneColor.*;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -11,8 +12,10 @@ import java.util.Set;
/** /**
* The overall game settings * The overall game settings
*/ */
public class GameSettings { public class GameSettings implements Serializable {
private List<PlayerSettings> players = new ArrayList<PlayerSettings>(); private static final long serialVersionUID = -7221346125938175643L;
private ArrayList<PlayerSettings> players = new ArrayList<PlayerSettings>();
private int initialMeldThreshold; private int initialMeldThreshold;
private int jokerPoints; private int jokerPoints;
@ -21,7 +24,7 @@ public class GameSettings {
private int stoneSetNumber; private int stoneSetNumber;
private int numberOfStonesDealt; private int numberOfStonesDealt;
private boolean noLimits; private boolean noLimits;
private Set<StoneColor> stoneColors; private HashSet<StoneColor> stoneColors;
/** /**
* Creates new GameSettings with default values * Creates new GameSettings with default values
@ -34,8 +37,8 @@ public class GameSettings {
stoneSetNumber = 2; stoneSetNumber = 2;
numberOfStonesDealt = 14; numberOfStonesDealt = 14;
noLimits = false; noLimits = false;
stoneColors = new HashSet<StoneColor>(Arrays.asList(BLACK, BLUE, stoneColors = new HashSet<StoneColor>(Arrays.asList(BLACK, BLUE, ORANGE,
ORANGE, RED)); RED));
} }
/** /**
@ -51,7 +54,7 @@ public class GameSettings {
* Sets the initial meld threshold * Sets the initial meld threshold
* *
* @param value * @param value
* the value to set * the value to set
*/ */
public void setInitialMeldThreshold(int value) { public void setInitialMeldThreshold(int value) {
initialMeldThreshold = value; initialMeldThreshold = value;
@ -70,7 +73,7 @@ public class GameSettings {
* Sets the points counted for a joker * Sets the points counted for a joker
* *
* @param value * @param value
* the value to set * the value to set
*/ */
public void setJokerPoints(int value) { public void setJokerPoints(int value) {
jokerPoints = value; jokerPoints = value;
@ -89,7 +92,7 @@ public class GameSettings {
* Sets the number of jokers in game * Sets the number of jokers in game
* *
* @param value * @param value
* how many jokers will be used * how many jokers will be used
*/ */
public void setJokerNumber(int value) { public void setJokerNumber(int value) {
jokerNumber = value; jokerNumber = value;
@ -117,7 +120,7 @@ public class GameSettings {
* Set the highest stone value in use * Set the highest stone value in use
* *
* @param highestValue * @param highestValue
* highest stone value * highest stone value
*/ */
public void setHighestValue(int highestValue) { public void setHighestValue(int highestValue) {
this.highestValue = highestValue; this.highestValue = highestValue;
@ -136,7 +139,7 @@ public class GameSettings {
* Set the number of sets of stones in use * Set the number of sets of stones in use
* *
* @param stoneSets * @param stoneSets
* sets of stones in use * sets of stones in use
*/ */
public void setStoneSetNumber(int stoneSets) { public void setStoneSetNumber(int stoneSets) {
this.stoneSetNumber = stoneSets; this.stoneSetNumber = stoneSets;
@ -155,7 +158,7 @@ public class GameSettings {
* Set whether "No-Limits" rules are used * Set whether "No-Limits" rules are used
* *
* @param noLimits * @param noLimits
* use no limit rules * use no limit rules
*/ */
public void setNoLimits(boolean noLimits) { public void setNoLimits(boolean noLimits) {
this.noLimits = noLimits; this.noLimits = noLimits;
@ -174,10 +177,10 @@ public class GameSettings {
* Set stone colors used * Set stone colors used
* *
* @param stoneColors * @param stoneColors
* used stone colors * used stone colors
*/ */
public void setStoneColors(Set<StoneColor> stoneColors) { public void setStoneColors(Set<StoneColor> stoneColors) {
this.stoneColors = stoneColors; this.stoneColors = new HashSet<StoneColor>(stoneColors);
} }
/** /**
@ -193,7 +196,7 @@ public class GameSettings {
* Set number of stones dealt at game start * Set number of stones dealt at game start
* *
* @param number * @param number
* how many Stones will be dealt initially * how many Stones will be dealt initially
*/ */
public void setNumberOfStonesDealt(int number) { public void setNumberOfStonesDealt(int number) {
numberOfStonesDealt = number; numberOfStonesDealt = number;

View file

@ -1,5 +1,6 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -7,9 +8,11 @@ import java.util.List;
/** /**
* Class that stores information for a game of multiple rounds * Class that stores information for a game of multiple rounds
*/ */
public class GameState { public class GameState implements Serializable {
private static final long serialVersionUID = -5787975403310108391L;
private int firstRoundFirstPlayer; private int firstRoundFirstPlayer;
private List<Score> scores = new ArrayList<Score>(); private ArrayList<Score> scores = new ArrayList<Score>();
/** /**
* Gets the number of the first player of the first round * Gets the number of the first player of the first round

View file

@ -1,7 +1,6 @@
package jrummikub.model; package jrummikub.model;
import static jrummikub.model.StoneTray.Direction.LEFT; import static jrummikub.model.StoneTray.Direction.*;
import static jrummikub.model.StoneTray.Direction.RIGHT;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -13,6 +12,8 @@ import jrummikub.util.Pair;
/** Class managing a {@link Player}'s {@link Stone}s */ /** Class managing a {@link Player}'s {@link Stone}s */
public class Hand extends StoneTray<Stone> implements IHand { public class Hand extends StoneTray<Stone> implements IHand {
private static final long serialVersionUID = 192210056255744909L;
/** /**
* The width of the hand * The width of the hand
*/ */
@ -43,8 +44,8 @@ public class Hand extends StoneTray<Stone> implements IHand {
} }
@Override @Override
protected Pair<Position, Direction> fixInvalidDrop(Stone stone, protected Pair<Position, Direction> fixInvalidDrop(Stone stone, Position pos,
Position pos, Direction dir) { Direction dir) {
double x = pos.getX(); double x = pos.getX();
double y = pos.getY(); double y = pos.getY();
@ -55,11 +56,9 @@ public class Hand extends StoneTray<Stone> implements IHand {
return new Pair<Position, Direction>(new Position(0, y), RIGHT); return new Pair<Position, Direction>(new Position(0, y), RIGHT);
} else { } else {
if (getFreeRowSpace((int) y) == 0) { if (getFreeRowSpace((int) y) == 0) {
return new Pair<Position, Direction>(new Position(0, y + 1), return new Pair<Position, Direction>(new Position(0, y + 1), RIGHT);
RIGHT);
} else { } else {
return new Pair<Position, Direction>( return new Pair<Position, Direction>(new Position(WIDTH - 1, y), LEFT);
new Position(WIDTH - 1, y), LEFT);
} }
} }
} }
@ -85,8 +84,7 @@ public class Hand extends StoneTray<Stone> implements IHand {
List<Stone> stones = new ArrayList<Stone>(); List<Stone> stones = new ArrayList<Stone>();
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
.hasNext();) {
stones.add(iter.next().getFirst()); stones.add(iter.next().getFirst());
} }
@ -104,8 +102,7 @@ public class Hand extends StoneTray<Stone> implements IHand {
public int getIdenticalStoneCount() { public int getIdenticalStoneCount() {
List<Stone> stones = new ArrayList<Stone>(); List<Stone> stones = new ArrayList<Stone>();
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
.hasNext();) {
stones.add(iter.next().getFirst()); stones.add(iter.next().getFirst());
} }

View file

@ -1,9 +1,11 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
/** /**
* Interface for {@link Player} model * Interface for {@link Player} model
*/ */
public interface IPlayer { public interface IPlayer extends Serializable {
/** /**
* Get the current hand of the player * Get the current hand of the player

View file

@ -1,9 +1,11 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
/** /**
* Interface for {@link RoundState} model * Interface for {@link RoundState} model
*/ */
public interface IRoundState { public interface IRoundState extends Serializable {
/** /**
* Get the current {@link GameSettings} * Get the current {@link GameSettings}
@ -23,7 +25,7 @@ public interface IRoundState {
* Sets the current {@link Table} * Sets the current {@link Table}
* *
* @param table * @param table
* The new Table * The new Table
*/ */
public void setTable(ITable table); public void setTable(ITable table);
@ -55,7 +57,7 @@ public interface IRoundState {
* Returns the player that would be the active player after i turns * Returns the player that would be the active player after i turns
* *
* @param i * @param i
* number of turns * number of turns
* @return player active after i turns * @return player active after i turns
*/ */
public IPlayer getNthNextPlayer(int i); public IPlayer getNthNextPlayer(int i);
@ -64,25 +66,25 @@ public interface IRoundState {
* Returns the nth player * Returns the nth player
* *
* @param i * @param i
* player number * player number
* @return nth player * @return nth player
*/ */
public IPlayer getNthPlayer(int i); public IPlayer getNthPlayer(int i);
/** /**
* Sets the player that will make the last turn before the round ends when * Sets the player that will make the last turn before the round ends when the
* the heap is empty * heap is empty
* *
* @return the last player * @return the last player
*/ */
public abstract IPlayer getLastPlayer(); public abstract IPlayer getLastPlayer();
/** /**
* Gets the player that will make the last turn before the round ends when * Gets the player that will make the last turn before the round ends when the
* the heap is empty * heap is empty
* *
* @param lastPlayer * @param lastPlayer
* the last player * the last player
*/ */
public abstract void setLastPlayer(IPlayer lastPlayer); public abstract void setLastPlayer(IPlayer lastPlayer);
@ -90,13 +92,13 @@ public interface IRoundState {
* Makes the player with number i the active player * Makes the player with number i the active player
* *
* @param i * @param i
* number of the player to make active * number of the player to make active
*/ */
public void setActivePlayerNumber(int i); public void setActivePlayerNumber(int i);
/** /**
* Gets the number of the current turn. Numbers smaller than one indicate * Gets the number of the current turn. Numbers smaller than one indicate hand
* hand inspection turns * inspection turns
* *
* @return current turn number * @return current turn number
*/ */

View file

@ -1,5 +1,7 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
import jrummikub.util.Pair; import jrummikub.util.Pair;
/** /**
@ -9,7 +11,7 @@ import jrummikub.util.Pair;
* Objects held by the IStoneTray * Objects held by the IStoneTray
*/ */
public interface IStoneTray<E extends Sizeable> extends public interface IStoneTray<E extends Sizeable> extends
Iterable<Pair<E, Position>>, Cloneable { Iterable<Pair<E, Position>>, Cloneable, Serializable {
/** /**
* Adds object to the tray * Adds object to the tray

View file

@ -2,6 +2,8 @@ package jrummikub.model;
/** Class managing player data. No methods in release 1 */ /** Class managing player data. No methods in release 1 */
public class Player implements IPlayer { public class Player implements IPlayer {
private static final long serialVersionUID = 2588861964190952815L;
private PlayerSettings settings; private PlayerSettings settings;
private IHand hand; private IHand hand;
private boolean laidOut; private boolean laidOut;

View file

@ -1,25 +1,27 @@
package jrummikub.model; package jrummikub.model;
import java.awt.Color; import java.awt.Color;
import java.io.Serializable;
import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnControlFactory;
/** /**
* The settings of a player * The settings of a player
*/ */
public class PlayerSettings { public class PlayerSettings implements Serializable {
private static final long serialVersionUID = 1963640115089275992L;
private String name; private String name;
private Color color; private Color color;
private TurnControlFactory.Type turnControlType; private TurnControlFactory.Type turnControlType;
/** /**
* Create a new human player * Create a new human player
* *
* @param name * @param name
* the player's name * the player's name
* @param color * @param color
* the player's color * the player's color
*/ */
public PlayerSettings(String name, Color color) { public PlayerSettings(String name, Color color) {
this.name = name; this.name = name;
@ -49,7 +51,7 @@ public class PlayerSettings {
* Sets the player's color * Sets the player's color
* *
* @param color * @param color
* the new color * the new color
*/ */
public void setColor(Color color) { public void setColor(Color color) {
this.color = color; this.color = color;
@ -59,7 +61,7 @@ public class PlayerSettings {
* Sets the player's name * Sets the player's name
* *
* @param name * @param name
* the new name * the new name
*/ */
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
@ -69,7 +71,7 @@ public class PlayerSettings {
* Set the player's TurnControlFactory type * Set the player's TurnControlFactory type
* *
* @param turnControlType * @param turnControlType
* player's TurnControlFactory type * player's TurnControlFactory type
*/ */
public void setTurnControlType(TurnControlFactory.Type turnControlType) { public void setTurnControlType(TurnControlFactory.Type turnControlType) {
this.turnControlType = turnControlType; this.turnControlType = turnControlType;

View file

@ -1,11 +1,14 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
/** /**
* {@link Stone} Position class to determine positions on {@link Table} or * {@link Stone} Position class to determine positions on {@link Table} or
* {@link Hand} * {@link Hand}
*/ */
public class Position { public class Position implements Serializable {
private static final long serialVersionUID = -582497930480638380L;
private double x; private double x;
private double y; private double y;

View file

@ -1,14 +1,15 @@
package jrummikub.model; package jrummikub.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** Class managing the overall and momentary RoundState */ /** Class managing the overall and momentary RoundState */
public class RoundState implements IRoundState { public class RoundState implements IRoundState {
private static final long serialVersionUID = 8678490099871939059L;
private GameSettings gameSettings; private GameSettings gameSettings;
private ITable table; private ITable table;
private List<Player> players; private ArrayList<Player> players;
private int activePlayer; private int activePlayer;
private StoneHeap gameHeap; private StoneHeap gameHeap;
private IPlayer lastPlayer; private IPlayer lastPlayer;

View file

@ -1,25 +1,29 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Score of a single round * Score of a single round
*/ */
public class Score { public class Score implements Serializable {
private List<Boolean> winners; private static final long serialVersionUID = 2200041688506962025L;
private List<Integer> points;
private ArrayList<Boolean> winners;
private ArrayList<Integer> points;
/** /**
* Create a new round score * Create a new round score
* *
* @param winners * @param winners
* set for each player among the winners * set for each player among the winners
* @param points * @param points
* points of each player * points of each player
*/ */
public Score(List<Boolean> winners, List<Integer> points) { public Score(List<Boolean> winners, List<Integer> points) {
this.winners = winners; this.winners = new ArrayList<Boolean>(winners);
this.points = points; this.points = new ArrayList<Integer>(points);
} }
/** /**

View file

@ -1,18 +1,21 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
/** Basic Rummikub Stone */ /** Basic Rummikub Stone */
public class Stone implements Sizeable { public class Stone implements Sizeable, Serializable {
private static final long serialVersionUID = 7032593080727812277L;
private int value; private int value;
private StoneColor color; private StoneColor color;
private final boolean joker; private final boolean joker;
/** /**
* Creates a joker of the given color. The color is only used for * Creates a joker of the given color. The color is only used for displaying.
* displaying.
* *
* @param color * @param color
* joker color * joker color
*/ */
public Stone(StoneColor color) { public Stone(StoneColor color) {
this.value = 0; this.value = 0;
@ -24,9 +27,9 @@ public class Stone implements Sizeable {
* Creates a normal stone of a given color and value * Creates a normal stone of a given color and value
* *
* @param value * @param value
* stone value * stone value
* @param color * @param color
* stone color * stone color
*/ */
public Stone(int value, StoneColor color) { public Stone(int value, StoneColor color) {
this.value = value; this.value = value;
@ -73,10 +76,10 @@ public class Stone implements Sizeable {
@Override @Override
public String toString() { public String toString() {
if (joker) { if (joker) {
return "Stone[joker,color=" + color + "]"; return "Stone[joker,color=" + color + "]";
} else { } else {
return "Stone[value=" + value + ",color=" + color + "]"; return "Stone[value=" + value + ",color=" + color + "]";
} }
} }
} }

View file

@ -1,5 +1,6 @@
package jrummikub.model; package jrummikub.model;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -11,15 +12,17 @@ import java.util.Random;
* players to draw one or more random Stones. * players to draw one or more random Stones.
*/ */
public class StoneHeap { public class StoneHeap implements Serializable {
List<Stone> heap; private static final long serialVersionUID = -5247740086907775125L;
ArrayList<Stone> heap;
private Random generator = new Random(); private Random generator = new Random();
/** /**
* Creates 106 Stones according to standard rules * Creates 106 Stones according to standard rules
* *
* @param gameSettings * @param gameSettings
* (for number of sets/jokers, colors etc.) * (for number of sets/jokers, colors etc.)
* */ * */
public StoneHeap(GameSettings gameSettings) { public StoneHeap(GameSettings gameSettings) {
heap = new ArrayList<Stone>(); heap = new ArrayList<Stone>();
@ -69,7 +72,7 @@ public class StoneHeap {
* Removes several {@link Stone}s from the heap and returns them * Removes several {@link Stone}s from the heap and returns them
* *
* @param number * @param number
* number of requested Stones * number of requested Stones
* @return list of drawn stones * @return list of drawn stones
*/ */
public List<Stone> drawStones(int number) { public List<Stone> drawStones(int number) {
@ -94,7 +97,7 @@ public class StoneHeap {
* Put stones back on the heap * Put stones back on the heap
* *
* @param stones * @param stones
* collection of stones to put back * collection of stones to put back
*/ */
public void putBack(Collection<Stone> stones) { public void putBack(Collection<Stone> stones) {
heap.addAll(stones); heap.addAll(stones);

View file

@ -13,6 +13,7 @@ import jrummikub.util.Pair;
* @param <E> * @param <E>
* Type of positioned objects (must implement Sizeable) * Type of positioned objects (must implement Sizeable)
*/ */
@SuppressWarnings("serial")
public class StoneTray<E extends Sizeable> implements IStoneTray<E> { public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>(); protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();

View file

@ -5,6 +5,8 @@ import jrummikub.util.Pair;
/** Class administering the {@link Stone}s on the game-Table */ /** Class administering the {@link Stone}s on the game-Table */
public class Table extends StoneTray<StoneSet> implements ITable { public class Table extends StoneTray<StoneSet> implements ITable {
private static final long serialVersionUID = 2433091681355019937L;
private GameSettings gameSettings; private GameSettings gameSettings;
private static class StoneInfo { private static class StoneInfo {
@ -23,7 +25,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
* Constructor for a table * Constructor for a table
* *
* @param settings * @param settings
* GameSettings * GameSettings
*/ */
public Table(GameSettings settings) { public Table(GameSettings settings) {
gameSettings = settings; gameSettings = settings;
@ -33,7 +35,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
* Removes {@link Stone} from the Table * Removes {@link Stone} from the Table
* *
* @param stone * @param stone
* stone to pick up * stone to pick up
*/ */
@Override @Override
public void pickUpStone(Stone stone) { public void pickUpStone(Stone stone) {
@ -84,8 +86,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
pickUp(set); pickUp(set);
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition); Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond() Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
.splitAt(1);
StoneSet leftSet = firstSplit.getFirst(); StoneSet leftSet = firstSplit.getFirst();
StoneSet rightSet = secondSplit.getSecond(); StoneSet rightSet = secondSplit.getSecond();
@ -93,8 +94,8 @@ public class Table extends StoneTray<StoneSet> implements ITable {
if (set.classify(gameSettings).getFirst() == StoneSet.Type.RUN) { if (set.classify(gameSettings).getFirst() == StoneSet.Type.RUN) {
Position leftPosition, rightPosition; Position leftPosition, rightPosition;
leftPosition = setPosition; leftPosition = setPosition;
rightPosition = new Position( rightPosition = new Position(setPosition.getX() + stonePosition + 1,
setPosition.getX() + stonePosition + 1, setPosition.getY()); setPosition.getY());
drop(leftSet, leftPosition); drop(leftSet, leftPosition);
drop(rightSet, rightPosition); drop(rightSet, rightPosition);

View file

@ -1,14 +1,18 @@
package jrummikub.util; package jrummikub.util;
import java.io.Serializable;
/** /**
* A pair of objects * A pair of objects
* *
* @param <T1> * @param <T1>
* Type of first component * Type of first component
* @param <T2> * @param <T2>
* Type of second component * Type of second component
*/ */
public class Pair<T1, T2> { public class Pair<T1, T2> implements Serializable {
private static final long serialVersionUID = 9197464436906172698L;
private final T1 first; private final T1 first;
private final T2 second; private final T2 second;
@ -16,9 +20,9 @@ public class Pair<T1, T2> {
* Create a new pair from two values * Create a new pair from two values
* *
* @param first * @param first
* the first pair component * the first pair component
* @param second * @param second
* the second pair component * the second pair component
*/ */
public Pair(T1 first, T2 second) { public Pair(T1 first, T2 second) {
this.first = first; this.first = first;