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 java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
@ -11,8 +12,10 @@ import java.util.Set;
/**
* The overall game settings
*/
public class GameSettings {
private List<PlayerSettings> players = new ArrayList<PlayerSettings>();
public class GameSettings implements Serializable {
private static final long serialVersionUID = -7221346125938175643L;
private ArrayList<PlayerSettings> players = new ArrayList<PlayerSettings>();
private int initialMeldThreshold;
private int jokerPoints;
@ -21,7 +24,7 @@ public class GameSettings {
private int stoneSetNumber;
private int numberOfStonesDealt;
private boolean noLimits;
private Set<StoneColor> stoneColors;
private HashSet<StoneColor> stoneColors;
/**
* Creates new GameSettings with default values
@ -34,8 +37,8 @@ public class GameSettings {
stoneSetNumber = 2;
numberOfStonesDealt = 14;
noLimits = false;
stoneColors = new HashSet<StoneColor>(Arrays.asList(BLACK, BLUE,
ORANGE, RED));
stoneColors = new HashSet<StoneColor>(Arrays.asList(BLACK, BLUE, ORANGE,
RED));
}
/**
@ -177,7 +180,7 @@ public class GameSettings {
* used stone colors
*/
public void setStoneColors(Set<StoneColor> stoneColors) {
this.stoneColors = stoneColors;
this.stoneColors = new HashSet<StoneColor>(stoneColors);
}
/**

View file

@ -1,5 +1,6 @@
package jrummikub.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -7,9 +8,11 @@ import java.util.List;
/**
* 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 List<Score> scores = new ArrayList<Score>();
private ArrayList<Score> scores = new ArrayList<Score>();
/**
* Gets the number of the first player of the first round

View file

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

View file

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

View file

@ -1,9 +1,11 @@
package jrummikub.model;
import java.io.Serializable;
/**
* Interface for {@link RoundState} model
*/
public interface IRoundState {
public interface IRoundState extends Serializable {
/**
* Get the current {@link GameSettings}
@ -70,16 +72,16 @@ public interface IRoundState {
public IPlayer getNthPlayer(int i);
/**
* Sets the player that will make the last turn before the round ends when
* the heap is empty
* Sets the player that will make the last turn before the round ends when the
* heap is empty
*
* @return the last player
*/
public abstract IPlayer getLastPlayer();
/**
* Gets the player that will make the last turn before the round ends when
* the heap is empty
* Gets the player that will make the last turn before the round ends when the
* heap is empty
*
* @param lastPlayer
* the last player
@ -95,8 +97,8 @@ public interface IRoundState {
public void setActivePlayerNumber(int i);
/**
* Gets the number of the current turn. Numbers smaller than one indicate
* hand inspection turns
* Gets the number of the current turn. Numbers smaller than one indicate hand
* inspection turns
*
* @return current turn number
*/

View file

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

View file

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

View file

@ -1,18 +1,20 @@
package jrummikub.model;
import java.awt.Color;
import java.io.Serializable;
import jrummikub.control.turn.TurnControlFactory;
/**
* The settings of a player
*/
public class PlayerSettings {
public class PlayerSettings implements Serializable {
private static final long serialVersionUID = 1963640115089275992L;
private String name;
private Color color;
private TurnControlFactory.Type turnControlType;
/**
* Create a new human player
*

View file

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

View file

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

View file

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

View file

@ -1,15 +1,18 @@
package jrummikub.model;
import java.io.Serializable;
/** Basic Rummikub Stone */
public class Stone implements Sizeable {
public class Stone implements Sizeable, Serializable {
private static final long serialVersionUID = 7032593080727812277L;
private int value;
private StoneColor color;
private final boolean joker;
/**
* Creates a joker of the given color. The color is only used for
* displaying.
* Creates a joker of the given color. The color is only used for displaying.
*
* @param color
* joker color

View file

@ -1,5 +1,6 @@
package jrummikub.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -11,8 +12,10 @@ import java.util.Random;
* players to draw one or more random Stones.
*/
public class StoneHeap {
List<Stone> heap;
public class StoneHeap implements Serializable {
private static final long serialVersionUID = -5247740086907775125L;
ArrayList<Stone> heap;
private Random generator = new Random();
/**

View file

@ -13,6 +13,7 @@ import jrummikub.util.Pair;
* @param <E>
* Type of positioned objects (must implement Sizeable)
*/
@SuppressWarnings("serial")
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
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 */
public class Table extends StoneTray<StoneSet> implements ITable {
private static final long serialVersionUID = 2433091681355019937L;
private GameSettings gameSettings;
private static class StoneInfo {
@ -84,8 +86,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
pickUp(set);
Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond()
.splitAt(1);
Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
StoneSet leftSet = firstSplit.getFirst();
StoneSet rightSet = secondSplit.getSecond();
@ -93,8 +94,8 @@ public class Table extends StoneTray<StoneSet> implements ITable {
if (set.classify(gameSettings).getFirst() == StoneSet.Type.RUN) {
Position leftPosition, rightPosition;
leftPosition = setPosition;
rightPosition = new Position(
setPosition.getX() + stonePosition + 1, setPosition.getY());
rightPosition = new Position(setPosition.getX() + stonePosition + 1,
setPosition.getY());
drop(leftSet, leftPosition);
drop(rightSet, rightPosition);

View file

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