Test für Austeilen geschrieben, eine Menge getSize hinzugefügt
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@116 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
8e32bc9e69
commit
6e9c1b539b
7 changed files with 84 additions and 27 deletions
|
@ -3,15 +3,23 @@ package jrummikub.control;
|
||||||
import jrummikub.model.GameState;
|
import jrummikub.model.GameState;
|
||||||
import jrummikub.view.IView;
|
import jrummikub.view.IView;
|
||||||
|
|
||||||
public class GameControl {
|
public class RoundControl {
|
||||||
private GameState gameState;
|
private GameState gameState;
|
||||||
private IView view;
|
private IView view;
|
||||||
|
|
||||||
public GameControl(GameState gameState, IView view) {
|
public RoundControl(GameState gameState, IView view) {
|
||||||
this.gameState = gameState;
|
this.gameState = gameState;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void startRound() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void deal() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void endOfTurn() {
|
private void endOfTurn() {
|
||||||
|
|
||||||
}
|
}
|
|
@ -6,9 +6,9 @@ import java.util.List;
|
||||||
|
|
||||||
/** Class managing the overall and momentary GameState */
|
/** Class managing the overall and momentary GameState */
|
||||||
public class GameState {
|
public class GameState {
|
||||||
ITable table;
|
private ITable table;
|
||||||
List<Player> players;
|
private List<Player> players;
|
||||||
int activePlayer;
|
private int activePlayer;
|
||||||
private StoneHeap gameHeap;
|
private StoneHeap gameHeap;
|
||||||
|
|
||||||
public GameState() {
|
public GameState() {
|
||||||
|
@ -19,18 +19,27 @@ public class GameState {
|
||||||
players.add(new Player(Color.green));
|
players.add(new Player(Color.green));
|
||||||
players.add(new Player(Color.black));
|
players.add(new Player(Color.black));
|
||||||
activePlayer = 0;
|
activePlayer = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITable getTable() {
|
public ITable getTable() {
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Changes the activePlayer to the next {@link Player} in the list */
|
public int getPlayerCount() {
|
||||||
public void nextPlayer() {
|
return players.size();
|
||||||
activePlayer = (activePlayer + 1) % 4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player activePlayer() {
|
public Player getPlayer(int i) {
|
||||||
|
return players.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Changes the activePlayer to the next {@link Player} in the list */
|
||||||
|
public void nextPlayer() {
|
||||||
|
activePlayer = (activePlayer + 1) % players.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player getActivePlayer() {
|
||||||
return players.get(activePlayer);
|
return players.get(activePlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,4 +37,6 @@ public interface IStoneTray<E extends Sizeable> extends
|
||||||
|
|
||||||
public IStoneTray<E> clone();
|
public IStoneTray<E> clone();
|
||||||
|
|
||||||
|
public int getSize();
|
||||||
|
|
||||||
}
|
}
|
|
@ -52,4 +52,8 @@ public class StoneHeap {
|
||||||
}
|
}
|
||||||
return drawnStones;
|
return drawnStones;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return heap.size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import jrummikub.util.Pair;
|
||||||
* or {@link StoneSet}s.
|
* or {@link StoneSet}s.
|
||||||
*
|
*
|
||||||
* @param <E>
|
* @param <E>
|
||||||
* Type of positioned objects (must implement Sizeable)
|
* Type of positioned objects (must implement Sizeable)
|
||||||
*/
|
*/
|
||||||
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
protected HashMap<E, Position> objects = new HashMap<E, Position>();
|
protected HashMap<E, Position> objects = new HashMap<E, Position>();
|
||||||
|
@ -39,10 +39,12 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
if (position.getY() < currentPosition.getY()) {
|
if (position.getY() < currentPosition.getY()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (position.getX() > currentPosition.getX() + currentObject.getWidth()) {
|
if (position.getX() > currentPosition.getX()
|
||||||
|
+ currentObject.getWidth()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (position.getY() > currentPosition.getY() + currentObject.getHeight()) {
|
if (position.getY() > currentPosition.getY()
|
||||||
|
+ currentObject.getHeight()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Position is inside the current object
|
// Position is inside the current object
|
||||||
|
@ -70,7 +72,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
.entrySet()) {
|
.entrySet()) {
|
||||||
Position currentPosition = i.getValue();
|
Position currentPosition = i.getValue();
|
||||||
E currentObject = i.getKey();
|
E currentObject = i.getKey();
|
||||||
if (!objectsOverlap(object, position, currentObject, currentPosition)) {
|
if (!objectsOverlap(object, position, currentObject,
|
||||||
|
currentPosition)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Object would be placed inside the current object
|
// Object would be placed inside the current object
|
||||||
|
@ -81,16 +84,16 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
// Move object to avoid overlap
|
// Move object to avoid overlap
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case TOP:
|
case TOP:
|
||||||
newPosition = new Position(currentPosition.getX(), position.getY()
|
newPosition = new Position(currentPosition.getX(),
|
||||||
- currentObject.getHeight());
|
position.getY() - currentObject.getHeight());
|
||||||
break;
|
break;
|
||||||
case BOTTOM:
|
case BOTTOM:
|
||||||
newPosition = new Position(currentPosition.getX(), position.getY()
|
newPosition = new Position(currentPosition.getX(),
|
||||||
+ object.getHeight());
|
position.getY() + object.getHeight());
|
||||||
break;
|
break;
|
||||||
case LEFT:
|
case LEFT:
|
||||||
newPosition = new Position(position.getX() - currentObject.getWidth(),
|
newPosition = new Position(position.getX()
|
||||||
currentPosition.getY());
|
- currentObject.getWidth(), currentPosition.getY());
|
||||||
break;
|
break;
|
||||||
case RIGHT:
|
case RIGHT:
|
||||||
newPosition = new Position(position.getX() + object.getWidth(),
|
newPosition = new Position(position.getX() + object.getWidth(),
|
||||||
|
@ -153,13 +156,15 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
float blockingRight = blocking.getValue().getX()
|
float blockingRight = blocking.getValue().getX()
|
||||||
+ blocking.getKey().getWidth();
|
+ blocking.getKey().getWidth();
|
||||||
float overlapRight = Math.min(objectRight, blockingRight);
|
float overlapRight = Math.min(objectRight, blockingRight);
|
||||||
float overlapLeft = Math.max(position.getX(), blocking.getValue().getX());
|
float overlapLeft = Math.max(position.getX(), blocking.getValue()
|
||||||
|
.getX());
|
||||||
float overlapX = overlapRight - overlapLeft;
|
float overlapX = overlapRight - overlapLeft;
|
||||||
float objectBottom = position.getY() + object.getHeight();
|
float objectBottom = position.getY() + object.getHeight();
|
||||||
float blockingBottom = blocking.getValue().getY()
|
float blockingBottom = blocking.getValue().getY()
|
||||||
+ blocking.getKey().getHeight();
|
+ blocking.getKey().getHeight();
|
||||||
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||||
float overlapTop = Math.max(position.getY(), blocking.getValue().getY());
|
float overlapTop = Math
|
||||||
|
.max(position.getY(), blocking.getValue().getY());
|
||||||
float overlapY = overlapBottom - overlapTop;
|
float overlapY = overlapBottom - overlapTop;
|
||||||
// vertical or horizontal Shift
|
// vertical or horizontal Shift
|
||||||
// TODO magic factor
|
// TODO magic factor
|
||||||
|
@ -178,8 +183,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Pair<E, Position>> iterator() {
|
public Iterator<Pair<E, Position>> iterator() {
|
||||||
final Iterator<Map.Entry<E, Position>> entryIterator = objects.entrySet()
|
final Iterator<Map.Entry<E, Position>> entryIterator = objects
|
||||||
.iterator();
|
.entrySet().iterator();
|
||||||
return new Iterator<Pair<E, Position>>() {
|
return new Iterator<Pair<E, Position>>() {
|
||||||
Iterator<Map.Entry<E, Position>> iterator = entryIterator;
|
Iterator<Map.Entry<E, Position>> iterator = entryIterator;
|
||||||
|
|
||||||
|
@ -225,4 +230,9 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSize() {
|
||||||
|
return objects.size();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
24
test/jrummikub/control/RoundControlTest.java
Normal file
24
test/jrummikub/control/RoundControlTest.java
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package jrummikub.control;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import jrummikub.model.GameState;
|
||||||
|
import jrummikub.view.MockView;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class RoundControlTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeal() {
|
||||||
|
MockView view = new MockView();
|
||||||
|
GameState testGameState = new GameState();
|
||||||
|
RoundControl testRound = new RoundControl(testGameState, view);
|
||||||
|
testRound.deal();
|
||||||
|
assertEquals(106 - testGameState.getPlayerCount() * 14, testGameState
|
||||||
|
.getGameHeap().getSize());
|
||||||
|
for (int i = 0; i < testGameState.getPlayerCount(); i++) {
|
||||||
|
assertEquals(14, testGameState.getPlayer(i).getHand().getSize());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -18,13 +18,13 @@ public class GameStateTest {
|
||||||
@Test
|
@Test
|
||||||
public void nextActiveTest() {
|
public void nextActiveTest() {
|
||||||
// All there?
|
// All there?
|
||||||
assertEquals(4, testGame.players.size());
|
assertEquals(4, testGame.getPlayerCount());
|
||||||
assertSame(Color.red, testGame.activePlayer().getColor());
|
assertSame(Color.red, testGame.getActivePlayer().getColor());
|
||||||
testGame.nextPlayer();
|
testGame.nextPlayer();
|
||||||
assertSame(Color.yellow, testGame.activePlayer().getColor());
|
assertSame(Color.yellow, testGame.getActivePlayer().getColor());
|
||||||
testGame.nextPlayer();
|
testGame.nextPlayer();
|
||||||
testGame.nextPlayer();
|
testGame.nextPlayer();
|
||||||
testGame.nextPlayer();
|
testGame.nextPlayer();
|
||||||
assertSame(Color.red, testGame.activePlayer().getColor());
|
assertSame(Color.red, testGame.getActivePlayer().getColor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue