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));
} }
/** /**
@ -177,7 +180,7 @@ public class GameSettings {
* 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);
} }
/** /**

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}
@ -70,16 +72,16 @@ public interface IRoundState {
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
@ -95,8 +97,8 @@ public interface IRoundState {
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,18 +1,20 @@
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
* *

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,13 +1,17 @@
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
@ -18,8 +22,8 @@ public class Score {
* 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,15 +1,18 @@
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

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,8 +12,10 @@ 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();
/** /**

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 {
@ -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,5 +1,7 @@
package jrummikub.util; package jrummikub.util;
import java.io.Serializable;
/** /**
* A pair of objects * A pair of objects
* *
@ -8,7 +10,9 @@ package jrummikub.util;
* @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;