Skip to content

Commit

Permalink
code review WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
farin committed Oct 13, 2022
1 parent dfa2d9b commit b29fe15
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 226 deletions.
19 changes: 0 additions & 19 deletions src/main/java/com/jcloisterzone/action/MoveBlackDragonAction.java

This file was deleted.

38 changes: 3 additions & 35 deletions src/main/java/com/jcloisterzone/engine/StateGsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public Gson create() {
builder.registerTypeAdapter(RemoveMageOrWitchAction.class, new ActionSerializer("RemoveMageOrWitch"));
builder.registerTypeAdapter(LittleBuildingAction.class, new LittleBuildingActionSerializer());
builder.registerTypeAdapter(ScoreAcrobatsAction.class, new SelectFeatureActionSerializer());
builder.registerTypeAdapter(MoveBlackDragonAction.class, new MoveBlackDragonActionSerializer());
return builder.create();
}

Expand Down Expand Up @@ -294,14 +293,14 @@ public JsonElement serializeNeutralFigures(GameState root, JsonSerializationCont
}
pos = state.getBlackDragonDeployment();
if (pos != null) {
Tuple3<Vector<Position>,Integer,Array<Integer>> blackdragonmoves = root.getCapabilityModel(BlackDragonCapability.class);
BlackDragonCapabilityModel model = root.getCapabilityModel(BlackDragonCapability.class);
JsonObject data = new JsonObject();
data.add("position", context.serialize(pos));
if (root.getPhase() instanceof BlackDragonMovePhase) {
JsonArray visitedData = new JsonArray();
blackdragonmoves._1.forEach(p -> visitedData.add(context.serialize(p)));
model.getVisited().forEach(p -> visitedData.add(context.serialize(p)));
data.add("visited", visitedData);
data.addProperty("remaining", blackdragonmoves._2 - blackdragonmoves._1.length());
data.addProperty("remaining", model.getMoves() - model.getVisited().length());
}
neutral.add("blackdragon", data);
}
Expand Down Expand Up @@ -571,21 +570,6 @@ public JsonArray serializePlayEvents(GameState root, JsonSerializationContext co
}
continue;
}
if (ev instanceof NeutralFigureReturned) {
NeutralFigureReturned nfr = (NeutralFigureReturned) ev;
if (nfr.isForced()) {
JsonObject data = new JsonObject();
data.addProperty("type", "neutralfigure-returned");
data.addProperty("neutralfigure", nfr.getNeutralFigure().getClass().getSimpleName().toLowerCase());
Player nfrp = nfr.getPlayer();
if (nfrp != null) {
data.addProperty("player", nfr.getPlayer().getIndex());
}
data.add("from", context.serialize(nfr.getFrom()));
turnEvents.add(data);
}
continue;
}
if (ev instanceof RansomPaidEvent) {
RansomPaidEvent rev = (RansomPaidEvent) ev;
JsonObject data = new JsonObject();
Expand Down Expand Up @@ -944,20 +928,4 @@ public JsonElement serialize(LittleBuildingAction action, Type type, JsonSeriali
return json;
}
}

private class MoveBlackDragonActionSerializer implements JsonSerializer<MoveBlackDragonAction> {
@Override
public JsonElement serialize(MoveBlackDragonAction action, Type type, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("type", "MoveDragon");
json.addProperty("figureId", action.getFigureId());
JsonArray options = new JsonArray();
action.getOptions().forEach(pos -> {
options.add(context.serialize(pos));
});
json.add("options", options);
return json;
}
}

}
16 changes: 1 addition & 15 deletions src/main/java/com/jcloisterzone/event/NeutralFigureReturned.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

import com.jcloisterzone.board.pointer.BoardPointer;
import com.jcloisterzone.figure.neutral.NeutralFigure;
import com.jcloisterzone.Player;

public class NeutralFigureReturned extends PlayEvent {

private final BoardPointer from;
private final NeutralFigure<?> neutralFigure;
private final boolean forced;
/** true if meeple is returned different way than scoring feature */
private final Player player;

public NeutralFigureReturned(PlayEventMeta metadata, NeutralFigure<?> neutralFigure, BoardPointer from, Boolean forced, Player player) {
public NeutralFigureReturned(PlayEventMeta metadata, NeutralFigure<?> neutralFigure, BoardPointer from) {
super(metadata);
this.neutralFigure = neutralFigure;
this.from = from;
this.forced = forced;
this.player = player;
}

public BoardPointer getFrom() {
Expand All @@ -27,12 +21,4 @@ public BoardPointer getFrom() {
public NeutralFigure<?> getNeutralFigure() {
return neutralFigure;
}

public boolean isForced() {
return forced;
}

public Player getPlayer() {
return player;
}
}
5 changes: 0 additions & 5 deletions src/main/java/com/jcloisterzone/figure/Barn.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public boolean canBeEatenByDragon(GameState state) {
return false;
}

@Override
public boolean canBeEatenByBlackDragon(GameState state) {
return false;
}

@Override
public DeploymentCheckResult isDeploymentAllowed(GameState state, FeaturePointer fp, Structure feature) {
if (!(feature instanceof Field)) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/jcloisterzone/figure/Figure.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public boolean isInSupply(GameState state) {
return !isDeployed(state);
}

public boolean canBeEatenByBlackDragon(GameState state) {
return true;
}

@Override
public int hashCode() {
return id.hashCode();
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/jcloisterzone/figure/Follower.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public boolean canBeEatenByDragon(GameState state) {
return !(getFeature(state) instanceof Castle);
}

@Override
public boolean canBeEatenByBlackDragon(GameState state) {
return !(getFeature(state) instanceof Castle);
}
public boolean isCaptured(GameState state) {
Array<List<Follower>> model = state.getCapabilityModel(TowerCapability.class);
return model != null && Stream.concat(model).find(f -> f == this).isDefined();
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/jcloisterzone/figure/neutral/BigTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public class BigTop extends NeutralFigure<Position> {
public BigTop(String id) {
super(id);
}

@Override
public boolean canBeEatenByBlackDragon(GameState state) {
return false;
}
}
5 changes: 0 additions & 5 deletions src/main/java/com/jcloisterzone/figure/neutral/Count.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public class Count extends NeutralFigure<FeaturePointer> {
public Count(String id) {
super(id);
}

@Override
public boolean canBeEatenByBlackDragon(GameState state) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public GameStatePhaseReducer(GameSetup setup, double initialRandom) {
if (setup.contains(CornCircleCapability.class)) next = new CornCirclePhase(randomGenerator, next);

if (setup.contains(BlackDragonCapability.class)) next = new BlackDragonPlacePhase(randomGenerator, next);

if (setup.contains(DragonCapability.class) && "after-scoring".equals(setup.getStringRule(Rule.DRAGON_MOVEMENT))) {
next = new DragonPhase(randomGenerator, next);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,21 @@

import com.jcloisterzone.Immutable;
import com.jcloisterzone.board.Position;
import com.jcloisterzone.board.pointer.BoardPointer;
import com.jcloisterzone.board.pointer.FeaturePointer;
import com.jcloisterzone.feature.Completable;
import com.jcloisterzone.figure.Meeple;
import com.jcloisterzone.figure.neutral.BlackDragon;
import com.jcloisterzone.figure.neutral.NeutralFigure;
import com.jcloisterzone.game.Capability;
import com.jcloisterzone.game.state.GameState;
import com.jcloisterzone.reducers.MoveNeutralFigure;
import com.jcloisterzone.reducers.UndeployMeeple;
import com.jcloisterzone.reducers.UndeployNeutralFigure;

import io.vavr.collection.Array;
import io.vavr.collection.Vector;
import io.vavr.Tuple2;
import io.vavr.Tuple3;

/**
* @model Tuple3<Vector<Position>,Integer,Array<Integer> : visited tiles by black dragon, count of finished features, score on start of turn
*/

@Immutable
public class BlackDragonCapability extends Capability<Tuple3<Vector<Position>,Integer,Array<Integer>>> {
public class BlackDragonCapability extends Capability<BlackDragonCapabilityModel> {

private static final long serialVersionUID = 1L;

Expand All @@ -32,7 +25,7 @@ public class BlackDragonCapability extends Capability<Tuple3<Vector<Position>,In
@Override
public GameState onStartGame(GameState state) {
state = state.mapNeutralFigures(nf -> nf.setBlackDragon(new BlackDragon("blackdragon.1")));
state = setInitialTurnState(state);
state = setModel(state, new BlackDragonCapabilityModel(EMPTY_VISITED, 0));
return state;
}

Expand All @@ -43,58 +36,30 @@ public boolean isMeepleDeploymentAllowed(GameState state, Position pos) {

@Override
public GameState beforeCompletableScore(GameState state, java.util.Set<Completable> features) {
Tuple3<Vector<Position>,Integer,Array<Integer>> model = getModel(state);
state = setModel(state, new Tuple3<>(EMPTY_VISITED,features.size(),model._3));
if (features.size() > 0) {
state = setModel(state, new BlackDragonCapabilityModel(EMPTY_VISITED, features.size()));
}
return state;
}

@Override
public GameState onTurnPartCleanUp(GameState state) {
return setInitialTurnState(state);
}

public GameState setInitialTurnState(GameState state) {
return setModel(state, new Tuple3<>(EMPTY_VISITED,0,state.getPlayers().getScore()));
}

public Vector<Position> getVisitedPositions(GameState state) {
Vector<Position> visitedpositions = getModel(state)._1;
return visitedpositions == null ? BlackDragonCapability.EMPTY_VISITED : visitedpositions;
}

public Integer getMoves(GameState state) {
Integer finishedfeatures = getModel(state)._2;
return finishedfeatures == null ? 0 : finishedfeatures;
}

public Array<Integer> getScore(GameState state) {
return getModel(state)._3;
}

public GameState moveBlackDragon(GameState state, Position pos) {
state = (
new MoveNeutralFigure<>(state.getNeutralFigures().getBlackDragon(), pos)
).apply(state);

state = blackDragonOnTile(state, pos);
state = clearTile(state, pos);
return state;
}

public GameState blackDragonOnTile(GameState state, Position pos) {
public GameState clearTile(GameState state, Position pos) {
for (Tuple2<Meeple, FeaturePointer> t: state.getDeployedMeeples()) {
Meeple m = t._1;
FeaturePointer fp = t._2;
if (pos.equals(fp.getPosition()) && m.canBeEatenByBlackDragon(state)) {
if (pos.equals(fp.getPosition()) && m.canBeEatenByDragon(state)) {
state = (new UndeployMeeple(m, true)).apply(state);
}
}
for (Tuple2<NeutralFigure<?>, BoardPointer> t: state.getNeutralFigures().getDeployedNeutralFigures().toSet()) {
NeutralFigure<?> nf = t._1;
BoardPointer bp = t._2;
if (pos.equals(bp.getPosition()) && state.getNeutralFigures().getBlackDragon() != nf && nf.canBeEatenByBlackDragon(state)) {
state = (new UndeployNeutralFigure(nf, true)).apply(state);
}
}

return state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jcloisterzone.game.capability;

import com.jcloisterzone.board.Position;
import io.vavr.collection.Vector;

public class BlackDragonCapabilityModel {

private final Vector<Position> visited;
private final int moves;

public BlackDragonCapabilityModel(Vector<Position> visited, int moves) {
this.visited = visited;
this.moves = moves;
}

public Vector<Position> getVisited() {
return visited;
}

public BlackDragonCapabilityModel setVisited(Vector<Position> visited) {
return new BlackDragonCapabilityModel(visited, moves);
}

public int getMoves() {
return moves;
}

public BlackDragonCapabilityModel setMoves(int moves) {
return new BlackDragonCapabilityModel(visited, moves);
}
}
Loading

0 comments on commit b29fe15

Please sign in to comment.