Created interface and abstract base class for turn control
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@307 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
f62b953c9e
commit
73f6fb9c1b
5 changed files with 76 additions and 40 deletions
|
@ -5,6 +5,8 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import jrummikub.control.turn.HumanTurnControl;
|
||||||
|
import jrummikub.control.turn.ITurnControl;
|
||||||
import jrummikub.model.Hand;
|
import jrummikub.model.Hand;
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
import jrummikub.model.IPlayer;
|
import jrummikub.model.IPlayer;
|
||||||
|
@ -93,7 +95,7 @@ public class RoundControl {
|
||||||
|
|
||||||
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
|
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
|
||||||
|
|
||||||
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
|
ITurnControl turnControl = new HumanTurnControl(roundState.getActivePlayer()
|
||||||
.getHand(), clonedTable, view, inspectOnly);
|
.getHand(), clonedTable, view, inspectOnly);
|
||||||
turnControl.getEndOfTurnEvent().add(new IListener() {
|
turnControl.getEndOfTurnEvent().add(new IListener() {
|
||||||
|
|
||||||
|
|
27
src/jrummikub/control/turn/AbstractTurnControl.java
Normal file
27
src/jrummikub/control/turn/AbstractTurnControl.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
|
import jrummikub.util.Event;
|
||||||
|
import jrummikub.util.IEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract base class for TurnControls
|
||||||
|
*/
|
||||||
|
public abstract class AbstractTurnControl implements ITurnControl {
|
||||||
|
|
||||||
|
protected Event endOfTurnEvent = new Event();
|
||||||
|
protected Event redealEvent = new Event();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract void startTurn();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IEvent getEndOfTurnEvent() {
|
||||||
|
return endOfTurnEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Event getRedealEvent() {
|
||||||
|
return redealEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package jrummikub.control;
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -6,6 +6,8 @@ import java.util.Comparator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import jrummikub.control.ITurnTimer;
|
||||||
|
import jrummikub.control.TurnTimer;
|
||||||
import jrummikub.model.Hand;
|
import jrummikub.model.Hand;
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
import jrummikub.model.ITable;
|
import jrummikub.model.ITable;
|
||||||
|
@ -14,8 +16,6 @@ import jrummikub.model.Stone;
|
||||||
import jrummikub.model.StoneColor;
|
import jrummikub.model.StoneColor;
|
||||||
import jrummikub.model.StoneSet;
|
import jrummikub.model.StoneSet;
|
||||||
import jrummikub.util.Connection;
|
import jrummikub.util.Connection;
|
||||||
import jrummikub.util.Event;
|
|
||||||
import jrummikub.util.IEvent;
|
|
||||||
import jrummikub.util.IListener;
|
import jrummikub.util.IListener;
|
||||||
import jrummikub.util.IListener1;
|
import jrummikub.util.IListener1;
|
||||||
import jrummikub.util.IListener2;
|
import jrummikub.util.IListener2;
|
||||||
|
@ -25,7 +25,7 @@ import jrummikub.view.IView;
|
||||||
/**
|
/**
|
||||||
* Controller for a single turn made by a human player
|
* Controller for a single turn made by a human player
|
||||||
*/
|
*/
|
||||||
public class TurnControl {
|
public class HumanTurnControl extends AbstractTurnControl {
|
||||||
private IHand hand;
|
private IHand hand;
|
||||||
private ITable table;
|
private ITable table;
|
||||||
private ITurnTimer timer;
|
private ITurnTimer timer;
|
||||||
|
@ -33,8 +33,6 @@ public class TurnControl {
|
||||||
|
|
||||||
private List<Stone> selectedStones = new ArrayList<Stone>();
|
private List<Stone> selectedStones = new ArrayList<Stone>();
|
||||||
|
|
||||||
private Event endOfTurnEvent = new Event();
|
|
||||||
private Event redealEvent = new Event();
|
|
||||||
private List<Connection> connections = new ArrayList<Connection>();
|
private List<Connection> connections = new ArrayList<Connection>();
|
||||||
|
|
||||||
private boolean inspectOnly;
|
private boolean inspectOnly;
|
||||||
|
@ -52,7 +50,7 @@ public class TurnControl {
|
||||||
* @param inspectOnly
|
* @param inspectOnly
|
||||||
* the current turn doesn't allow any table manipulation
|
* the current turn doesn't allow any table manipulation
|
||||||
*/
|
*/
|
||||||
public TurnControl(IHand hand, ITable table, IView view, boolean inspectOnly) {
|
public HumanTurnControl(IHand hand, ITable table, IView view, boolean inspectOnly) {
|
||||||
this.hand = hand;
|
this.hand = hand;
|
||||||
this.table = table;
|
this.table = table;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
@ -61,7 +59,7 @@ public class TurnControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test only constructor **/
|
/** Test only constructor **/
|
||||||
TurnControl(IHand hand, ITable table, IView view, ITurnTimer testTimer) {
|
HumanTurnControl(IHand hand, ITable table, IView view, ITurnTimer testTimer) {
|
||||||
this.hand = hand;
|
this.hand = hand;
|
||||||
this.table = table;
|
this.table = table;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
@ -69,9 +67,7 @@ public class TurnControl {
|
||||||
this.inspectOnly = false;
|
this.inspectOnly = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Start the turn
|
|
||||||
*/
|
|
||||||
public void startTurn() {
|
public void startTurn() {
|
||||||
|
|
||||||
IListener endOfTurnListener = new IListener() {
|
IListener endOfTurnListener = new IListener() {
|
||||||
|
@ -462,15 +458,6 @@ public class TurnControl {
|
||||||
view.setSelectedStones(new ArrayList<Stone>());
|
view.setSelectedStones(new ArrayList<Stone>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the event that is emitted when the turn is over
|
|
||||||
*
|
|
||||||
* @return end of turn event
|
|
||||||
*/
|
|
||||||
public IEvent getEndOfTurnEvent() {
|
|
||||||
return endOfTurnEvent;
|
|
||||||
}
|
|
||||||
|
|
||||||
static private int compareJokers(Stone s1, Stone s2) {
|
static private int compareJokers(Stone s1, Stone s2) {
|
||||||
if (!s1.isJoker() && s2.isJoker()) {
|
if (!s1.isJoker() && s2.isJoker()) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -562,13 +549,4 @@ public class TurnControl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Emitted when the round is aborted and needs to be restarted
|
|
||||||
*
|
|
||||||
* @return the event
|
|
||||||
*/
|
|
||||||
public Event getRedealEvent() {
|
|
||||||
return redealEvent;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
27
src/jrummikub/control/turn/ITurnControl.java
Normal file
27
src/jrummikub/control/turn/ITurnControl.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
|
import jrummikub.util.Event;
|
||||||
|
import jrummikub.util.IEvent;
|
||||||
|
|
||||||
|
public interface ITurnControl {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start the turn
|
||||||
|
*/
|
||||||
|
public abstract void startTurn();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the event that is emitted when the turn is over
|
||||||
|
*
|
||||||
|
* @return end of turn event
|
||||||
|
*/
|
||||||
|
public abstract IEvent getEndOfTurnEvent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emitted when the round is aborted and needs to be restarted
|
||||||
|
*
|
||||||
|
* @return the event
|
||||||
|
*/
|
||||||
|
public abstract Event getRedealEvent();
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package jrummikub.control;
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
import static jrummikub.model.StoneColor.BLACK;
|
import static jrummikub.model.StoneColor.BLACK;
|
||||||
import static jrummikub.model.StoneColor.BLUE;
|
import static jrummikub.model.StoneColor.BLUE;
|
||||||
|
@ -16,6 +16,8 @@ import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import jrummikub.control.ITurnTimer;
|
||||||
|
import jrummikub.control.turn.HumanTurnControl;
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
import jrummikub.model.ITable;
|
import jrummikub.model.ITable;
|
||||||
import jrummikub.model.MockHand;
|
import jrummikub.model.MockHand;
|
||||||
|
@ -35,7 +37,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link TurnControl}
|
* Tests for {@link HumanTurnControl}
|
||||||
*/
|
*/
|
||||||
public class TurnControlTest {
|
public class TurnControlTest {
|
||||||
static class AccessibleTable extends Table {
|
static class AccessibleTable extends Table {
|
||||||
|
@ -65,7 +67,7 @@ public class TurnControlTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TurnControl testControl;
|
HumanTurnControl testControl;
|
||||||
MockView mockView;
|
MockView mockView;
|
||||||
MockTimer mockTimer;
|
MockTimer mockTimer;
|
||||||
MockTable mockTable;
|
MockTable mockTable;
|
||||||
|
@ -103,7 +105,7 @@ public class TurnControlTest {
|
||||||
mockTimer = new MockTimer();
|
mockTimer = new MockTimer();
|
||||||
mockTable = new MockTable();
|
mockTable = new MockTable();
|
||||||
mockHand = new MockHand();
|
mockHand = new MockHand();
|
||||||
testControl = new TurnControl(mockHand, mockTable, mockView, mockTimer);
|
testControl = new HumanTurnControl(mockHand, mockTable, mockView, mockTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
|
@ -126,7 +128,7 @@ public class TurnControlTest {
|
||||||
|
|
||||||
mockHand.iterable = stones;
|
mockHand.iterable = stones;
|
||||||
|
|
||||||
testControl = new TurnControl(mockHand, mockTable, mockView, mockTimer);
|
testControl = new HumanTurnControl(mockHand, mockTable, mockView, mockTimer);
|
||||||
|
|
||||||
testControl.startTurn();
|
testControl.startTurn();
|
||||||
|
|
||||||
|
@ -619,7 +621,7 @@ public class TurnControlTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAddLeft() {
|
public void testAddLeft() {
|
||||||
AccessibleTable table = new AccessibleTable();
|
AccessibleTable table = new AccessibleTable();
|
||||||
TurnControl turnControl = new TurnControl(mockHand, table, mockView,
|
HumanTurnControl turnControl = new HumanTurnControl(mockHand, table, mockView,
|
||||||
mockTimer);
|
mockTimer);
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
Stone blueOne = new Stone(1, BLUE);
|
Stone blueOne = new Stone(1, BLUE);
|
||||||
|
@ -736,7 +738,7 @@ public class TurnControlTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAddRight() {
|
public void testAddRight() {
|
||||||
AccessibleTable table = new AccessibleTable();
|
AccessibleTable table = new AccessibleTable();
|
||||||
TurnControl turnControl = new TurnControl(mockHand, table, mockView,
|
HumanTurnControl turnControl = new HumanTurnControl(mockHand, table, mockView,
|
||||||
mockTimer);
|
mockTimer);
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
Stone blueOne = new Stone(1, BLUE);
|
Stone blueOne = new Stone(1, BLUE);
|
||||||
|
@ -853,7 +855,7 @@ public class TurnControlTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAddNewSet() {
|
public void testAddNewSet() {
|
||||||
AccessibleTable table = new AccessibleTable();
|
AccessibleTable table = new AccessibleTable();
|
||||||
TurnControl turnControl = new TurnControl(mockHand, table, mockView,
|
HumanTurnControl turnControl = new HumanTurnControl(mockHand, table, mockView,
|
||||||
mockTimer);
|
mockTimer);
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
Stone blueOne = new Stone(1, BLUE);
|
Stone blueOne = new Stone(1, BLUE);
|
||||||
|
@ -976,7 +978,7 @@ public class TurnControlTest {
|
||||||
|
|
||||||
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(
|
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(
|
||||||
mockHand.stones);
|
mockHand.stones);
|
||||||
Collections.sort(stones, new TurnControl.HandStonePositionComparator());
|
Collections.sort(stones, new HumanTurnControl.HandStonePositionComparator());
|
||||||
|
|
||||||
assertEquals(stones.size(), 13);
|
assertEquals(stones.size(), 13);
|
||||||
|
|
||||||
|
@ -1037,7 +1039,7 @@ public class TurnControlTest {
|
||||||
|
|
||||||
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(
|
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(
|
||||||
mockHand.stones);
|
mockHand.stones);
|
||||||
Collections.sort(stones, new TurnControl.HandStonePositionComparator());
|
Collections.sort(stones, new HumanTurnControl.HandStonePositionComparator());
|
||||||
|
|
||||||
assertEquals(stones.size(), 13);
|
assertEquals(stones.size(), 13);
|
||||||
|
|
Reference in a new issue