2011-05-29 19:46:50 +02:00
|
|
|
package jrummikub.control.turn;
|
2011-05-04 15:20:52 +02:00
|
|
|
|
2011-05-05 19:56:47 +02:00
|
|
|
import java.util.ArrayList;
|
2011-05-09 18:53:37 +02:00
|
|
|
import java.util.Collections;
|
2011-05-09 21:55:18 +02:00
|
|
|
import java.util.Comparator;
|
2011-05-09 18:53:37 +02:00
|
|
|
import java.util.LinkedList;
|
2011-05-05 19:56:47 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2011-05-29 19:46:50 +02:00
|
|
|
import jrummikub.control.ITurnTimer;
|
|
|
|
import jrummikub.control.TurnTimer;
|
2011-05-10 05:53:30 +02:00
|
|
|
import jrummikub.model.Hand;
|
2011-05-08 22:46:31 +02:00
|
|
|
import jrummikub.model.Position;
|
2011-05-05 20:22:14 +02:00
|
|
|
import jrummikub.model.Stone;
|
2011-05-10 01:22:56 +02:00
|
|
|
import jrummikub.model.StoneColor;
|
2011-05-08 22:46:31 +02:00
|
|
|
import jrummikub.model.StoneSet;
|
2011-05-05 19:56:47 +02:00
|
|
|
import jrummikub.util.Connection;
|
2011-05-04 18:47:12 +02:00
|
|
|
import jrummikub.util.IListener;
|
2011-05-09 00:33:34 +02:00
|
|
|
import jrummikub.util.IListener1;
|
2011-05-05 20:22:14 +02:00
|
|
|
import jrummikub.util.IListener2;
|
2011-05-09 21:55:18 +02:00
|
|
|
import jrummikub.util.Pair;
|
2011-05-04 15:20:52 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
|
|
|
* Controller for a single turn made by a human player
|
|
|
|
*/
|
2011-05-29 19:46:50 +02:00
|
|
|
public class HumanTurnControl extends AbstractTurnControl {
|
2011-05-04 16:54:30 +02:00
|
|
|
private ITurnTimer timer;
|
2011-05-05 20:22:14 +02:00
|
|
|
|
|
|
|
private List<Stone> selectedStones = new ArrayList<Stone>();
|
|
|
|
|
2011-05-10 03:13:11 +02:00
|
|
|
private List<Connection> connections = new ArrayList<Connection>();
|
2011-05-29 19:46:53 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
2011-05-29 20:22:47 +02:00
|
|
|
* Create a new human player's turn control
|
2011-05-10 03:54:48 +02:00
|
|
|
*/
|
2011-05-29 20:22:47 +02:00
|
|
|
public HumanTurnControl() {
|
2011-05-04 17:33:44 +02:00
|
|
|
}
|
2011-05-05 00:19:02 +02:00
|
|
|
|
2011-05-29 19:46:53 +02:00
|
|
|
/**
|
|
|
|
* Get a factory for this turn control
|
|
|
|
*
|
|
|
|
* @return factory
|
|
|
|
*/
|
|
|
|
public static TurnControlFactory getFactory() {
|
|
|
|
return new TurnControlFactory() {
|
|
|
|
@Override
|
2011-05-29 20:22:47 +02:00
|
|
|
public ITurnControl create() {
|
|
|
|
return new HumanTurnControl();
|
2011-05-29 19:46:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-05-04 17:33:44 +02:00
|
|
|
/** Test only constructor **/
|
2011-05-29 20:22:47 +02:00
|
|
|
HumanTurnControl(ITurnTimer testTimer) {
|
2011-05-04 17:33:44 +02:00
|
|
|
this.timer = testTimer;
|
2011-05-04 18:47:12 +02:00
|
|
|
}
|
2011-05-05 00:19:02 +02:00
|
|
|
|
2011-05-29 19:46:50 +02:00
|
|
|
@Override
|
2011-05-05 19:49:57 +02:00
|
|
|
public void startTurn() {
|
2011-05-29 20:22:47 +02:00
|
|
|
if (this.timer == null) {
|
|
|
|
this.timer = new TurnTimer(view);
|
|
|
|
}
|
2011-05-05 19:56:47 +02:00
|
|
|
IListener endOfTurnListener = new IListener() {
|
2011-05-05 00:19:02 +02:00
|
|
|
|
2011-05-04 18:47:12 +02:00
|
|
|
@Override
|
|
|
|
public void handle() {
|
2011-05-27 17:54:46 +02:00
|
|
|
endOfTurn(false);
|
2011-05-04 18:47:12 +02:00
|
|
|
}
|
|
|
|
};
|
2011-05-05 19:56:47 +02:00
|
|
|
connections.add(timer.getTimeRunOutEvent().add(endOfTurnListener));
|
2011-05-05 20:22:14 +02:00
|
|
|
connections.add(view.getPlayerPanel().getEndTurnEvent()
|
|
|
|
.add(endOfTurnListener));
|
2011-05-29 19:46:53 +02:00
|
|
|
|
|
|
|
connections.add(view.getPlayerPanel().getRedealEvent()
|
|
|
|
.add(new IListener() {
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
endOfTurn(true);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-05 20:22:14 +02:00
|
|
|
|
2011-05-10 03:55:58 +02:00
|
|
|
addHandPanelHandlers();
|
|
|
|
addStoneCollectionHandlers();
|
2011-05-25 17:10:43 +02:00
|
|
|
if (!inspectOnly)
|
|
|
|
addTablePanelHandlers();
|
2011-05-10 03:55:58 +02:00
|
|
|
|
2011-05-31 03:04:22 +02:00
|
|
|
addListeners();
|
|
|
|
|
|
|
|
view.getHandPanel().setStones(hand.clone());
|
|
|
|
view.getHandPanel().resetCurrentRow();
|
|
|
|
view.enableStartTurnPanel(false);
|
|
|
|
|
|
|
|
timer.startTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addListeners() {
|
2011-05-10 03:55:58 +02:00
|
|
|
connections.add(view.getPlayerPanel().getSortByGroupsEvent()
|
|
|
|
.add(new IListener() {
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
sortByGroups();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
connections.add(view.getPlayerPanel().getSortByRunsEvent()
|
|
|
|
.add(new IListener() {
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
sortByRuns();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addHandPanelHandlers() {
|
2011-05-16 22:09:56 +02:00
|
|
|
connections.add(view.getHandPanel().getClickEvent()
|
2011-05-10 01:39:35 +02:00
|
|
|
.add(new IListener1<Position>() {
|
|
|
|
@Override
|
|
|
|
public void handle(Position pos) {
|
|
|
|
handClick(pos);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2011-05-16 22:09:56 +02:00
|
|
|
connections.add(view.getHandPanel().getStoneClickEvent()
|
2011-05-10 03:55:58 +02:00
|
|
|
.add(new IListener2<Stone, Boolean>() {
|
2011-05-05 20:22:14 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
2011-05-09 00:33:34 +02:00
|
|
|
stoneClick(stone, collect);
|
2011-05-05 20:22:14 +02:00
|
|
|
}
|
|
|
|
}));
|
2011-05-05 00:19:02 +02:00
|
|
|
|
2011-05-16 22:09:56 +02:00
|
|
|
connections.add(view.getHandPanel().getRangeClickEvent()
|
2011-05-10 03:55:58 +02:00
|
|
|
.add(new IListener2<Stone, Boolean>() {
|
2011-05-09 21:55:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
|
|
|
handRangeClick(stone, collect);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
}
|
2011-05-09 21:55:18 +02:00
|
|
|
|
2011-05-10 03:55:58 +02:00
|
|
|
private void addStoneCollectionHandlers() {
|
2011-05-05 21:46:58 +02:00
|
|
|
connections.add(view.getTablePanel().getStoneCollectionPanel()
|
|
|
|
.getStoneClickEvent().add(new IListener2<Stone, Boolean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
|
|
|
collectionStoneClick(stone, collect);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
|
2011-05-09 18:36:02 +02:00
|
|
|
connections.add(view.getTablePanel().getStoneCollectionPanel()
|
|
|
|
.getSetClickEvent().add(new IListener2<Stone, Boolean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
|
|
|
collectionSetClick(stone, collect);
|
|
|
|
}
|
2011-05-09 18:36:03 +02:00
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addTablePanelHandlers() {
|
2011-05-09 00:33:34 +02:00
|
|
|
connections.add(view.getTablePanel().getStoneClickEvent()
|
|
|
|
.add(new IListener2<Stone, Boolean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
|
|
|
stoneClick(stone, collect);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
|
2011-05-09 18:36:03 +02:00
|
|
|
connections.add(view.getTablePanel().getSetClickEvent()
|
|
|
|
.add(new IListener2<Stone, Boolean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
|
|
|
tableSetClick(stone, collect);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
|
2011-05-09 20:15:19 +02:00
|
|
|
connections.add(view.getTablePanel().getRangeClickEvent()
|
|
|
|
.add(new IListener2<Stone, Boolean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(Stone stone, Boolean collect) {
|
|
|
|
tableRangeClick(stone, collect);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-09 00:33:34 +02:00
|
|
|
|
|
|
|
connections.add(view.getTablePanel().getClickEvent()
|
|
|
|
.add(new IListener1<Position>() {
|
|
|
|
@Override
|
|
|
|
public void handle(Position pos) {
|
|
|
|
tableClick(pos);
|
|
|
|
}
|
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
|
|
|
|
addTablePanelConnectorClickHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addTablePanelConnectorClickHandlers() {
|
2011-05-09 00:33:34 +02:00
|
|
|
connections.add(view.getTablePanel().getLeftConnectorClickEvent()
|
|
|
|
.add(new IListener1<StoneSet>() {
|
|
|
|
@Override
|
|
|
|
public void handle(StoneSet set) {
|
2011-05-09 18:53:37 +02:00
|
|
|
connectorClick(set, false);
|
2011-05-09 00:33:34 +02:00
|
|
|
}
|
|
|
|
}));
|
2011-05-10 03:55:58 +02:00
|
|
|
|
2011-05-09 00:33:34 +02:00
|
|
|
connections.add(view.getTablePanel().getRightConnectorClickEvent()
|
|
|
|
.add(new IListener1<StoneSet>() {
|
|
|
|
@Override
|
|
|
|
public void handle(StoneSet set) {
|
2011-05-09 18:53:37 +02:00
|
|
|
connectorClick(set, true);
|
2011-05-09 00:33:34 +02:00
|
|
|
}
|
|
|
|
}));
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 01:39:35 +02:00
|
|
|
private void handClick(Position pos) {
|
|
|
|
List<Stone> handStones = new ArrayList<Stone>();
|
|
|
|
for (Stone s : selectedStones) {
|
|
|
|
if (hand.pickUp(s)) {
|
|
|
|
handStones.add(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (Stone s : handStones) {
|
|
|
|
float x = Math.max(0,
|
|
|
|
Math.min(13, pos.getX() - handStones.size() / 2.0f + i));
|
|
|
|
hand.drop(s, new Position(x, (float) Math.floor(pos.getY())));
|
|
|
|
selectedStones.remove(s);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
view.setSelectedStones(selectedStones);
|
2011-05-16 22:09:56 +02:00
|
|
|
view.getHandPanel().setStones(hand);
|
2011-05-10 01:39:35 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 01:22:56 +02:00
|
|
|
private void sortStones(Comparator<Stone> comparator) {
|
|
|
|
List<Stone> stones = new ArrayList<Stone>();
|
|
|
|
for (Pair<Stone, Position> entry : hand) {
|
|
|
|
stones.add(entry.getFirst());
|
|
|
|
}
|
|
|
|
for (Stone stone : stones) {
|
|
|
|
hand.pickUp(stone);
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(stones, comparator);
|
|
|
|
int x = 0, y = 0;
|
|
|
|
for (Stone stone : stones) {
|
|
|
|
hand.drop(stone, new Position(x, y));
|
|
|
|
x++;
|
2011-05-10 05:53:30 +02:00
|
|
|
if (x >= Hand.WIDTH) {
|
2011-05-10 01:22:56 +02:00
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
|
2011-05-16 22:09:56 +02:00
|
|
|
view.getHandPanel().setStones(hand);
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 01:22:56 +02:00
|
|
|
private void sortByRuns() {
|
|
|
|
sortStones(new RunComparator());
|
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
|
2011-05-10 01:22:56 +02:00
|
|
|
private void sortByGroups() {
|
|
|
|
sortStones(new GroupComparator());
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
2011-05-05 00:19:02 +02:00
|
|
|
|
2011-05-09 00:33:34 +02:00
|
|
|
private void stoneClick(Stone stone, boolean collect) {
|
2011-05-05 20:22:14 +02:00
|
|
|
if (collect) {
|
|
|
|
if (!selectedStones.remove(stone)) {
|
|
|
|
selectedStones.add(stone);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectedStones.clear();
|
|
|
|
selectedStones.add(stone);
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setSelectedStones(selectedStones);
|
|
|
|
}
|
|
|
|
|
2011-05-05 21:46:58 +02:00
|
|
|
private void collectionStoneClick(Stone stone, boolean collect) {
|
|
|
|
selectedStones.remove(stone);
|
|
|
|
|
|
|
|
if (collect) {
|
|
|
|
selectedStones.add(stone);
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setSelectedStones(selectedStones);
|
|
|
|
}
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-09 18:36:02 +02:00
|
|
|
private void collectionSetClick(Stone stone, Boolean collect) {
|
|
|
|
selectedStones.clear();
|
|
|
|
view.setSelectedStones(selectedStones);
|
|
|
|
}
|
|
|
|
|
2011-05-09 00:33:34 +02:00
|
|
|
private void pickUpSelectedStones() {
|
|
|
|
for (Stone stone : selectedStones) {
|
|
|
|
hand.pickUp(stone);
|
|
|
|
table.pickUpStone(stone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void tableClick(Position position) {
|
|
|
|
if (selectedStones.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pickUpSelectedStones();
|
2011-05-09 04:41:59 +02:00
|
|
|
table.drop(new StoneSet(selectedStones), new Position(position.getX()
|
|
|
|
- selectedStones.size() * 0.5f, position.getY() - 0.5f));
|
2011-05-09 00:33:34 +02:00
|
|
|
selectedStones.clear();
|
|
|
|
|
|
|
|
view.getTablePanel().setStoneSets(table);
|
2011-05-16 22:09:56 +02:00
|
|
|
view.getHandPanel().setStones(hand);
|
2011-05-09 00:33:34 +02:00
|
|
|
view.setSelectedStones(selectedStones);
|
2011-05-08 22:46:31 +02:00
|
|
|
}
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-09 18:36:03 +02:00
|
|
|
private void tableSetClick(Stone stone, Boolean collect) {
|
|
|
|
if (!collect) {
|
|
|
|
selectedStones.clear();
|
|
|
|
}
|
|
|
|
StoneSet selectedSet = table.findStoneSet(stone);
|
|
|
|
for (Stone setStone : selectedSet) {
|
|
|
|
selectedStones.remove(setStone);
|
|
|
|
selectedStones.add(setStone);
|
|
|
|
}
|
|
|
|
view.setSelectedStones(selectedStones);
|
|
|
|
}
|
|
|
|
|
2011-05-09 20:15:19 +02:00
|
|
|
private void tableRangeClick(Stone stone, Boolean collect) {
|
|
|
|
if (selectedStones.isEmpty()) {
|
2011-05-10 05:21:59 +02:00
|
|
|
stoneClick(stone, true);
|
2011-05-09 20:15:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-05-09 21:55:18 +02:00
|
|
|
Stone lastStone = selectedStones.get(selectedStones.size() - 1);
|
2011-05-09 20:15:19 +02:00
|
|
|
StoneSet lastSet = table.findStoneSet(lastStone);
|
|
|
|
StoneSet selectedSet = table.findStoneSet(stone);
|
|
|
|
if (lastSet != selectedSet) {
|
2011-05-10 05:21:59 +02:00
|
|
|
stoneClick(stone, true);
|
2011-05-09 20:15:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Stone> setStones = new ArrayList<Stone>();
|
|
|
|
for (Stone s : selectedSet) {
|
|
|
|
setStones.add(s);
|
|
|
|
}
|
|
|
|
int firstIndex = setStones.indexOf(lastStone);
|
|
|
|
int lastIndex = setStones.indexOf(stone);
|
|
|
|
if (firstIndex > lastIndex) {
|
|
|
|
int temp = firstIndex;
|
|
|
|
firstIndex = lastIndex;
|
|
|
|
lastIndex = temp;
|
|
|
|
}
|
|
|
|
for (int i = firstIndex; i <= lastIndex; i++) {
|
|
|
|
Stone s = setStones.get(i);
|
|
|
|
selectedStones.remove(s);
|
|
|
|
selectedStones.add(s);
|
|
|
|
}
|
2011-05-09 21:55:18 +02:00
|
|
|
|
|
|
|
view.setSelectedStones(selectedStones);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handRangeClick(Stone stone, Boolean collect) {
|
|
|
|
if (selectedStones.isEmpty()) {
|
2011-05-10 05:21:59 +02:00
|
|
|
stoneClick(stone, true);
|
2011-05-09 21:55:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Stone lastStone = selectedStones.get(selectedStones.size() - 1);
|
|
|
|
StoneSet lastSet = table.findStoneSet(lastStone);
|
|
|
|
if (lastSet != null) {
|
2011-05-10 05:21:59 +02:00
|
|
|
stoneClick(stone, true);
|
2011-05-09 21:55:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Pair<Stone, Position>> handPairs = new ArrayList<Pair<Stone, Position>>();
|
|
|
|
for (Pair<Stone, Position> entry : hand) {
|
|
|
|
handPairs.add(entry);
|
|
|
|
}
|
|
|
|
|
2011-05-10 00:28:06 +02:00
|
|
|
Collections.sort(handPairs, new HandStonePositionComparator());
|
2011-05-09 21:55:18 +02:00
|
|
|
|
|
|
|
List<Stone> handStones = new ArrayList<Stone>();
|
|
|
|
for (Pair<Stone, Position> entry : handPairs) {
|
|
|
|
handStones.add(entry.getFirst());
|
|
|
|
}
|
|
|
|
|
|
|
|
int firstIndex = handStones.indexOf(lastStone);
|
|
|
|
int lastIndex = handStones.indexOf(stone);
|
|
|
|
if (firstIndex > lastIndex) {
|
|
|
|
int temp = firstIndex;
|
|
|
|
firstIndex = lastIndex;
|
|
|
|
lastIndex = temp;
|
|
|
|
}
|
|
|
|
for (int i = firstIndex; i <= lastIndex; i++) {
|
|
|
|
Stone s = handStones.get(i);
|
|
|
|
selectedStones.remove(s);
|
|
|
|
selectedStones.add(s);
|
|
|
|
}
|
2011-05-09 20:15:19 +02:00
|
|
|
view.setSelectedStones(selectedStones);
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:53:37 +02:00
|
|
|
private void connectorClick(StoneSet set, boolean right) {
|
|
|
|
List<Stone> stones = new LinkedList<Stone>();
|
|
|
|
Position pos = table.getPosition(set);
|
|
|
|
for (Stone stone : set) {
|
|
|
|
stones.add(stone);
|
|
|
|
}
|
|
|
|
stones.removeAll(selectedStones);
|
|
|
|
if (right) {
|
|
|
|
Collections.reverse(stones);
|
|
|
|
}
|
|
|
|
pickUpSelectedStones();
|
|
|
|
StoneSet newSet = null;
|
|
|
|
for (Stone stone : stones) {
|
|
|
|
newSet = table.findStoneSet(stone);
|
|
|
|
if (newSet != null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newSet != null) {
|
|
|
|
Position newPos = table.getPosition(newSet);
|
|
|
|
table.pickUp(newSet);
|
|
|
|
if (right) {
|
|
|
|
StoneSet joinedSet = newSet.join(new StoneSet(selectedStones));
|
|
|
|
table.drop(joinedSet, newPos);
|
|
|
|
} else {
|
|
|
|
StoneSet joinedSet = new StoneSet(selectedStones).join(newSet);
|
2011-05-25 17:10:43 +02:00
|
|
|
table.drop(joinedSet, new Position(newPos.getX()
|
|
|
|
- selectedStones.size(), newPos.getY()));
|
2011-05-09 18:53:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2011-05-25 17:10:43 +02:00
|
|
|
table.drop(new StoneSet(selectedStones), new Position(pos.getX()
|
|
|
|
+ (set.size() - selectedStones.size()) * 0.5f, pos.getY()));
|
2011-05-09 18:53:37 +02:00
|
|
|
}
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-09 18:53:37 +02:00
|
|
|
selectedStones.clear();
|
2011-05-09 00:33:34 +02:00
|
|
|
|
2011-05-09 18:53:37 +02:00
|
|
|
view.getTablePanel().setStoneSets(table);
|
2011-05-16 22:09:56 +02:00
|
|
|
view.getHandPanel().setStones(hand);
|
2011-05-09 18:53:37 +02:00
|
|
|
view.setSelectedStones(selectedStones);
|
2011-05-08 22:46:31 +02:00
|
|
|
}
|
2011-05-05 21:46:58 +02:00
|
|
|
|
2011-05-27 17:54:46 +02:00
|
|
|
private void endOfTurn(boolean redeal) {
|
2011-05-04 18:47:12 +02:00
|
|
|
timer.stopTimer();
|
2011-05-27 17:54:46 +02:00
|
|
|
if (redeal) {
|
|
|
|
redealEvent.emit();
|
|
|
|
} else {
|
|
|
|
endOfTurnEvent.emit();
|
|
|
|
}
|
2011-05-05 19:56:47 +02:00
|
|
|
for (Connection c : connections) {
|
|
|
|
c.remove();
|
|
|
|
}
|
2011-05-05 21:35:41 +02:00
|
|
|
view.setSelectedStones(new ArrayList<Stone>());
|
2011-05-04 18:47:12 +02:00
|
|
|
}
|
2011-05-05 00:19:02 +02:00
|
|
|
|
2011-05-10 01:22:56 +02:00
|
|
|
static private int compareJokers(Stone s1, Stone s2) {
|
|
|
|
if (!s1.isJoker() && s2.isJoker()) {
|
|
|
|
return -1;
|
|
|
|
} else if (s1.isJoker() && !s2.isJoker()) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static private int compareColors(Stone s1, Stone s2) {
|
|
|
|
if (s1.getColor() == s2.getColor())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (StoneColor color : StoneColor.values()) {
|
|
|
|
if (s1.getColor() == color) {
|
|
|
|
return -1;
|
|
|
|
} else if (s2.getColor() == color) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static private int compareValues(Stone s1, Stone s2) {
|
|
|
|
if (s1.getValue() < s2.getValue()) {
|
|
|
|
return -1;
|
|
|
|
} else if (s1.getValue() > s2.getValue()) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class RunComparator implements Comparator<Stone> {
|
|
|
|
@Override
|
|
|
|
public int compare(Stone s1, Stone s2) {
|
|
|
|
int jokerCompare = compareJokers(s1, s2);
|
|
|
|
if (jokerCompare != 0) {
|
|
|
|
return jokerCompare;
|
|
|
|
}
|
|
|
|
|
|
|
|
int colorCompare = compareColors(s1, s2);
|
|
|
|
if (colorCompare != 0) {
|
|
|
|
return colorCompare;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compareValues(s1, s2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class GroupComparator implements Comparator<Stone> {
|
|
|
|
@Override
|
|
|
|
public int compare(Stone s1, Stone s2) {
|
|
|
|
int jokerCompare = compareJokers(s1, s2);
|
|
|
|
if (jokerCompare != 0) {
|
|
|
|
return jokerCompare;
|
|
|
|
}
|
|
|
|
|
|
|
|
int valueCompare = compareValues(s1, s2);
|
|
|
|
if (valueCompare != 0) {
|
|
|
|
return valueCompare;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compareColors(s1, s2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class HandStonePositionComparator implements
|
|
|
|
Comparator<Pair<Stone, Position>> {
|
|
|
|
@Override
|
2011-05-25 17:10:43 +02:00
|
|
|
public int compare(Pair<Stone, Position> pair1,
|
|
|
|
Pair<Stone, Position> pair2) {
|
2011-05-10 01:22:56 +02:00
|
|
|
Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond();
|
|
|
|
if (pos1.getY() < pos2.getY()) {
|
|
|
|
return -1;
|
|
|
|
} else if (pos1.getY() > pos2.getY()) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (pos1.getX() < pos2.getX()) {
|
|
|
|
return -1;
|
|
|
|
} else if (pos1.getX() > pos2.getX()) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|