Skip to content

Commit

Permalink
#82 Show skat on ISS games
Browse files Browse the repository at this point in the history
  • Loading branch information
b0n541 committed Nov 17, 2020
1 parent 595fe59 commit 778d08e
Show file tree
Hide file tree
Showing 8 changed files with 2,976 additions and 3,066 deletions.
2,220 changes: 1,090 additions & 1,130 deletions jskat-base/src/main/java/org/jskat/control/SkatGame.java

Large diffs are not rendered by default.

195 changes: 94 additions & 101 deletions jskat-base/src/main/java/org/jskat/control/SkatGameReplayer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (C) 2020 Jan Schäfer ([email protected])
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,16 +15,8 @@
*/
package org.jskat.control;

import java.util.ArrayList;
import java.util.List;

import org.jskat.control.command.table.ShowCardsCommand;
import org.jskat.control.event.skatgame.BidEvent;
import org.jskat.control.event.skatgame.GameAnnouncementEvent;
import org.jskat.control.event.skatgame.GameFinishEvent;
import org.jskat.control.event.skatgame.GameStartEvent;
import org.jskat.control.event.skatgame.SkatGameEvent;
import org.jskat.control.event.skatgame.TrickCardPlayedEvent;
import org.jskat.control.event.skatgame.*;
import org.jskat.control.event.table.TableGameMoveEvent;
import org.jskat.control.event.table.TrickCompletedEvent;
import org.jskat.data.SkatGameData;
Expand All @@ -33,97 +25,98 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
* This class is used for replaying skat games
*/
public class SkatGameReplayer {

private final static Logger LOG = LoggerFactory.getLogger(SkatGameReplayer.class);

private final JSkatView view;
private final String tableName;
private SkatGameData data;
private final List<SkatGameEvent> gameMoves = new ArrayList<>();
private int currentMove = 0;

public SkatGameReplayer(final JSkatView view, final String tableName,
final List<SkatGameEvent> gameMoves) throws InterruptedException {
this.view = view;
this.tableName = tableName;
this.gameMoves.addAll(gameMoves);
resetReplay();
}

public void toStart() {
resetReplay();
}

public void oneMoveBackward() {
if (currentMove > 1) {
currentMove--;
gameMoves.get(currentMove).processBackward(data);
}
}

public void oneMoveForward() {
if (hasMoreMoves()) {
oneStepForward();
}
}

public void toEnd() {
while (hasMoreMoves()) {
oneStepForward();
}
}

private void resetReplay() {
currentMove = 0;
data = new SkatGameData();
// game start
oneStepForward();
// dealing
oneStepForward();
}

private boolean hasMoreMoves() {
return currentMove < gameMoves.size();
}

private void setGameState(final SkatGameEvent event) {
if (event instanceof GameStartEvent) {
view.setGameState(tableName, GameState.BIDDING);
} else if (event instanceof BidEvent) {
view.setGameState(tableName, GameState.BIDDING);
} else if (event instanceof GameAnnouncementEvent) {
view.setGameState(tableName, GameState.DECLARING);
} else if (event instanceof TrickCardPlayedEvent) {
view.setGameState(tableName, GameState.TRICK_PLAYING);
} else if (event instanceof GameFinishEvent) {
view.setGameState(tableName, GameState.GAME_OVER);
}
}

private void oneStepForward() {
final SkatGameEvent event = gameMoves.get(currentMove);

setGameState(event);

// TODO: code duplication with SkatGame.playCard()
if (event instanceof TrickCardPlayedEvent
&& data.getCurrentTrick() != null
&& data.getCurrentTrick().getFirstCard() == null) {
JSkatEventBus.TABLE_EVENT_BUSSES.get(tableName).post(
new TrickCompletedEvent(data.getLastCompletedTrick()));
} else if (event instanceof GameFinishEvent) {
JSkatEventBus.INSTANCE.post(new ShowCardsCommand(tableName, data
.getCardsAfterDiscard()));
}

event.processForward(data);

JSkatEventBus.INSTANCE.post(new TableGameMoveEvent(tableName, event));

currentMove++;
}
private final static Logger LOG = LoggerFactory.getLogger(SkatGameReplayer.class);

private final JSkatView view;
private final String tableName;
private SkatGameData data;
private final List<SkatGameEvent> gameMoves = new ArrayList<>();
private int currentMove = 0;

public SkatGameReplayer(JSkatView view, String tableName,
List<SkatGameEvent> gameMoves) throws InterruptedException {
this.view = view;
this.tableName = tableName;
this.gameMoves.addAll(gameMoves);
resetReplay();
}

public void toStart() {
resetReplay();
}

public void oneMoveBackward() {
if (currentMove > 1) {
currentMove--;
gameMoves.get(currentMove).processBackward(data);
}
}

public void oneMoveForward() {
if (hasMoreMoves()) {
oneStepForward();
}
}

public void toEnd() {
while (hasMoreMoves()) {
oneStepForward();
}
}

private void resetReplay() {
currentMove = 0;
data = new SkatGameData();
// game start
oneStepForward();
// dealing
oneStepForward();
}

private boolean hasMoreMoves() {
return currentMove < gameMoves.size();
}

private void setGameState(SkatGameEvent event) {
if (event instanceof GameStartEvent) {
view.setGameState(tableName, GameState.BIDDING);
} else if (event instanceof BidEvent) {
view.setGameState(tableName, GameState.BIDDING);
} else if (event instanceof GameAnnouncementEvent) {
view.setGameState(tableName, GameState.DECLARING);
} else if (event instanceof TrickCardPlayedEvent) {
view.setGameState(tableName, GameState.TRICK_PLAYING);
} else if (event instanceof GameFinishEvent) {
view.setGameState(tableName, GameState.GAME_OVER);
}
}

private void oneStepForward() {
SkatGameEvent event = gameMoves.get(currentMove);

setGameState(event);

// TODO: code duplication with SkatGame.playCard()
if (event instanceof TrickCardPlayedEvent
&& data.getCurrentTrick() != null
&& data.getCurrentTrick().getFirstCard() == null) {
JSkatEventBus.TABLE_EVENT_BUSSES.get(tableName).post(new TrickCompletedEvent(data.getLastCompletedTrick()));
} else if (event instanceof GameFinishEvent) {
JSkatEventBus.INSTANCE.post(new ShowCardsCommand(tableName, data.getCardsAfterDiscard(), data.getDealtSkat()));
}

event.processForward(data);

JSkatEventBus.INSTANCE.post(new TableGameMoveEvent(tableName, event));

currentMove++;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (C) 2020 Jan Schäfer ([email protected])
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,17 +15,26 @@
*/
package org.jskat.control.command.table;

import java.util.Map;

import org.jskat.util.CardList;
import org.jskat.util.Player;

import java.util.HashMap;
import java.util.Map;

public class ShowCardsCommand extends AbstractTableCommand {

public final Map<Player, CardList> cards;
public final Map<Player, CardList> cards = new HashMap<>();
public final CardList skat = new CardList();

public ShowCardsCommand(String tableName, Player player, CardList cards) {
this(tableName, Map.of(player, cards), null);
}

public ShowCardsCommand(String tableName, Map<Player, CardList> cards) {
super(tableName);
this.cards = cards;
}
public ShowCardsCommand(String tableName, Map<Player, CardList> cards, CardList skat) {
super(tableName);
this.cards.putAll(cards);
if (skat != null) {
this.skat.addAll(skat);
}
}
}
Loading

0 comments on commit 778d08e

Please sign in to comment.