repo
stringlengths 1
191
⌀ | file
stringlengths 23
351
| code
stringlengths 0
5.32M
| file_length
int64 0
5.32M
| avg_line_length
float64 0
2.9k
| max_line_length
int64 0
288k
| extension_type
stringclasses 1
value |
---|---|---|---|---|---|---|
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/Priority.java | package game.rules.play.moves.nonDecision.effect.requirement;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the first list of moves with a non-empty set of moves.
*
* @author Eric.Piette
* @remarks To prioritise a list of legal moves over another. For example in
* some draughts games, if you can capture, you must capture, if not
* you can move normally.
*/
public final class Priority extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** the list of Moves to eval. */
private final Moves[] list;
//-------------------------------------------------------------------------
/**
* For selecting the first set of moves with a legal move between many set of
* moves.
*
* @param list The list of moves.
* @param then The moves applied after that move is applied.
*
* @example (priority { (forEach Piece "Leopard" (step (to if:(is Enemy (who
* at:(to)))))) (forEach Piece "Leopard" (step (to if:(is In (to)
* (sites Empty))))) })
*/
public Priority
(
final Moves[] list,
@Opt final Then then
)
{
super(then);
this.list = list;
}
/**
* For selecting the first set of moves with a legal move between two moves.
*
* @param list1 The first set of moves.
* @param list2 The second set of moves.
* @param then The moves applied after that move is applied.
*
* @example (priority (forEach Piece "Leopard" (step (to if:(is Enemy (who
* at:(to)))))) (forEach Piece "Leopard" (step (to if:(is In (to)
* (sites Empty)))) ))
*/
public Priority
(
final Moves list1,
final Moves list2,
@Opt final Then then
)
{
super(then);
list = new Moves[] {list1, list2};
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
for (final Moves move : list)
{
final Moves l = move.eval(context);
if (!l.moves().isEmpty())
{
if (then() != null)
for (int j = 0; j < l.moves().size(); j++)
l.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < l.moves().size(); j++)
l.moves().get(j).setMovesLudeme(move);
return l;
}
}
return new BaseMoves(super.then());
}
//-------------------------------------------------------------------------
@Override
public boolean canMove(final Context context)
{
for (final Moves moves : list)
{
if (moves.canMove(context))
return true;
}
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
for (final Moves moves : list)
gameFlags |= moves.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.Priority.id(), true);
for (final Moves moves : list)
concepts.or(moves.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
for (final Moves moves : list)
writeEvalContext.or(moves.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
for (final Moves moves : list)
readEvalContext.or(moves.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
for (final Moves moves : list)
missingRequirement |= moves.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
for (final Moves moves : list)
willCrash |= moves.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
boolean isStatic = true;
for (final Moves moves : list)
isStatic = isStatic && moves.isStatic();
return isStatic;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
for (final Moves moves : list)
moves.preprocess(game);
}
//-------------------------------------------------------------------------
/**
* @return Array of Moves
*/
public Moves[] list()
{
return list;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String englishString = "moves with priority: ";
for(final Moves m : list)
englishString += m.toEnglish(game) + ", ";
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return englishString.substring(0, englishString.length()-2) + thenString;
}
//-------------------------------------------------------------------------
}
| 5,965 | 23.056452 | 78 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/While.java | package game.rules.play.moves.nonDecision.effect.requirement;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanConstant.TrueConstant;
import game.functions.booleans.BooleanFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.context.TempContext;
import other.move.Move;
/**
* Applies a move until the condition becomes false.
*
* @author Eric.Piette
*/
public final class While extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The moves to apply until the condition is false. */
final Moves moves;
/** The conditions to check. */
final BooleanFunction condition;
//-------------------------------------------------------------------------
/**
* @param condition Conditions to make false thanks to the move to apply.
* @param moves Moves to apply until the condition is false.
* @param then The moves applied after that move is applied.
*
* @example (while (!= 100 (score P1)) (addScore P1 1))
*/
public While
(
final BooleanFunction condition,
final Moves moves,
@Opt final Then then
)
{
super(then);
this.condition = condition;
this.moves = moves;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Return intersected list of moves
final Moves result = new BaseMoves(super.then());
final Context newContext = new TempContext(context);
int numIteration = 0;
while (condition.eval(newContext))
{
for (final Move m : moves.eval(newContext).moves())
{
m.apply(newContext, false);
result.moves().add(m);
}
numIteration++;
if (numIteration > Constants.MAX_NUM_ITERATION)
{
throw new IllegalArgumentException(
"Infinite While(), the condition can not be reached.");
}
}
// Add the consequences.
if (then() != null)
for (int j = 0; j < result.moves().size(); j++)
result.moves().get(j).then().add(then().moves());
return result;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = moves.gameFlags(game) | super.gameFlags(game) | condition.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(condition.concepts(game));
concepts.or(moves.concepts(game));
concepts.set(Concept.CopyContext.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(condition.writesEvalContextRecursive());
writeEvalContext.or(moves.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(condition.readsEvalContextRecursive());
readEvalContext.or(moves.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (condition instanceof TrueConstant)
{
game.addRequirementToReport("The ludeme (while ...) has an infinite condition which is \"true\".");
missingRequirement = true;
}
missingRequirement |= super.missingRequirement(game);
missingRequirement |= condition.missingRequirement(game);
missingRequirement |= moves.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= condition.willCrash(game);
willCrash |= moves.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
condition.preprocess(game);
moves.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "while " + condition.toEnglish(game) + " " + moves.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 5,310 | 24.907317 | 102 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/package-info.java | /**
* Move requirements define criteria that must be satisfied for a move to be legal.
* These are typically applied to lists of generated moves to filter out those that do not meet the
* specified criteria.
* Move requirements can be quite powerful when used correctly, but care must be taken as they can have a high performance overhead.
*/
package game.rules.play.moves.nonDecision.effect.requirement;
| 413 | 50.75 | 132 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/Max.java | package game.rules.play.moves.nonDecision.effect.requirement.max;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.effect.requirement.max.distance.MaxDistance;
import game.rules.play.moves.nonDecision.effect.requirement.max.moves.MaxCaptures;
import game.rules.play.moves.nonDecision.effect.requirement.max.moves.MaxMoves;
import game.types.play.RoleType;
import other.context.Context;
//-----------------------------------------------------------------------------
/**
* Filters a list of legal moves to keep only the moves allowing the maximum
* number of moves in a turn.
*
* @author Eric.Piette and cambolbro
*/
@SuppressWarnings("javadoc")
public final class Max extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For getting the moves with the max captures or the max number of legal moves
* in the turn.
*
* @param maxType The type of property to maximise.
* @param moves The moves to filter.
* @param withValue If true, the capture has to maximise the values of the capturing pieces too.
* @param then The moves applied after that move is applied.
*
* @example (max Moves (forEach Piece))
*
* @example (max Captures (forEach Piece))
*/
public static Moves construct
(
final MaxMovesType maxType,
@Opt @Name final BooleanFunction withValue,
final Moves moves,
@Opt final Then then
)
{
switch (maxType)
{
case Captures:
return new MaxCaptures(withValue, moves, then);
case Moves:
return new MaxMoves(withValue, moves, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Max(): A MaxMovesType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For getting the moves with the max distance.
*
* @param maxType The type of property to maximise.
* @param trackName The name of the track.
* @param owner The owner of the track.
* @param moves The moves to filter.
* @param then The moves applied after that move is applied.
*
* @example (max Distance (forEach Piece))
*/
public static Moves construct
(
final MaxDistanceType maxType,
@Opt final String trackName,
@Opt final RoleType owner,
final Moves moves,
@Opt final Then then
)
{
switch (maxType)
{
case Distance:
return new MaxDistance(trackName, owner, moves, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Max(): A MaxDistanceType is not implemented.");
}
//-------------------------------------------------------------------------
private Max()
{
super(null);
// Ensure that compiler does pick up default constructor
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Max.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return 0L;
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("Max.canMoveTo(): Should never be called directly.");
}
//-------------------------------------------------------------------------
}
| 4,116 | 26.630872 | 98 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/MaxDistanceType.java | package game.rules.play.moves.nonDecision.effect.requirement.max;
/**
* Defines the types of properties which can be used for the Max super ludeme
* according to a distance.
*
* @author Eric.Piette
*/
public enum MaxDistanceType
{
/**
* To filter the moves to keep only the moves allowing the maximum distance on a
* track in a turn.
*/
Distance,
}
| 364 | 20.470588 | 81 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/MaxMovesType.java | package game.rules.play.moves.nonDecision.effect.requirement.max;
/**
* Defines the types of properties which can be used for the Max super ludeme
* with only a move ludeme in entry.
*
* @author Eric.Piette
*/
public enum MaxMovesType
{
/**
* To ilter a list of legal moves to keep only the moves allowing the maximum
* number of moves in a turn.
*/
Moves,
/**
* To filter a list of moves to keep only the moves doing the maximum possible
* number of captures.
*/
Captures,
}
| 500 | 20.782609 | 79 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/package-info.java | /**
* The {\tt (max ...)} `super' ludeme filters a list of moves to maximise a
* property.
*/
package game.rules.play.moves.nonDecision.effect.requirement.max;
| 163 | 26.333333 | 75 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/distance/MaxDistance.java | package game.rules.play.moves.nonDecision.effect.requirement.max.distance;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.equipment.container.board.Track;
import game.functions.ints.board.Id;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.play.RoleType;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.context.TempContext;
import other.move.Move;
/**
* Filters the moves to keep only the moves allowing the maximum distance on a
* track in a turn.
*
* @author Eric.Piette
*/
@Hide
public final class MaxDistance extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The moves to maximise. */
private final Moves moves;
/** The name of the track. */
private final String trackName;
/** The owner of the track. */
private final RoleType owner;
/**
* @param trackName The name of the track.
* @param owner The owner of the track.
* @param moves The moves to filter.
* @param then The moves applied after that move is applied.
*/
public MaxDistance
(
@Opt final String trackName,
@Opt final RoleType owner,
final Moves moves,
@Opt final Then then
)
{
super(then);
this.moves = moves;
this.trackName = trackName;
this.owner = owner;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves returnMoves = new BaseMoves(super.then());
Track track = null;
final int who = (owner == null) ? Constants.UNDEFINED : new Id(null, owner).eval(context);
for (final Track t : context.tracks())
{
if (trackName == null || (who == Constants.OFF && t.name().equals(trackName)
|| (who != Constants.OFF && t.owner() == who && t.name().contains(trackName))))
{
track = t;
break;
}
}
// If the track does exist we return an empty list of moves.
if (track == null)
return moves;
// We compute the moves.
final Moves movesToEval = moves.eval(context);
final int[] distanceCount = new int[movesToEval.moves().size()];
for (int i = 0; i < movesToEval.moves().size(); i++)
{
final Move m = movesToEval.moves().get(i);
int indexFrom = Constants.UNDEFINED;
int indexTo = Constants.UNDEFINED;
for (int j = 0; j < track.elems().length; j++)
{
if (track.elems()[j].site == m.fromNonDecision())
indexFrom = j;
else if (track.elems()[j].site == m.toNonDecision())
indexTo = j;
if(indexFrom != Constants.UNDEFINED && indexTo != Constants.UNDEFINED)
break;
}
final int distance = Math.abs(indexFrom - indexTo);
distanceCount[i] = context.recursiveCalled() ? distance
: getDistanceCount(context, track, context.state().mover(), m, distance);
}
int max = 0;
// Get the max of the distanceCount.
for (final int sizeDistance : distanceCount)
if (sizeDistance > max)
max = sizeDistance;
// Keep only the large distance moves.
for (int i = 0; i < movesToEval.moves().size(); i++)
if (distanceCount[i] == max)
returnMoves.moves().add(movesToEval.moves().get(i));
final Moves toReturn = moves.eval(context);
// Store the Moves in the computed moves.
for (int j = 0; j < toReturn.moves().size(); j++)
toReturn.moves().get(j).setMovesLudeme(toReturn);
return toReturn;
}
//-------------------------------------------------------------------------
/**
*
* @param context
* @param move
* @return the count of the replay of this move.
*/
private int getDistanceCount(final Context context, final Track track, final int mover, final Move m,
final int distance)
{
if (m.isPass() || m.toNonDecision() == m.fromNonDecision())
return distance;
final Context newContext = new TempContext(context);
newContext.setRecursiveCalled(true);
newContext.game().apply(newContext, m);
// Not same player
if (mover != newContext.state().mover())
return distance;
final Moves legalMoves = newContext.game().moves(newContext);
final int[] distanceCount = new int[legalMoves.moves().size()];
for (int i = 0; i < legalMoves.moves().size(); i++)
{
final Move newMove = legalMoves.moves().get(i);
int indexFrom = Constants.UNDEFINED;
int indexTo = Constants.UNDEFINED;
for (int j = 0; j < track.elems().length; j++)
{
if (track.elems()[j].site == m.fromNonDecision())
indexFrom = j;
else if (track.elems()[j].site == m.toNonDecision())
indexTo = j;
if (indexFrom != Constants.UNDEFINED && indexTo != Constants.UNDEFINED)
break;
}
final int newDistance = Math.abs(indexFrom - indexTo);
distanceCount[i] = getDistanceCount(newContext, track, mover, newMove, distance + newDistance);
}
int max = 0;
// Get the max of the replayCount.
for (final int sizeDistance : distanceCount)
if (sizeDistance > max)
max = sizeDistance;
return max;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = moves.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(moves.concepts(game));
concepts.or(super.concepts(game));
concepts.set(Concept.MaxDistance.id(), true);
concepts.set(Concept.CopyContext.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(moves.writesEvalContextRecursive());
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(moves.readsEvalContextRecursive());
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean isStatic()
{
final boolean isStatic = moves.isStatic();
return isStatic;
}
@Override
public void preprocess(final Game game)
{
moves.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasTrack())
{
game.addRequirementToReport("The ludeme (max Distance ...) is used but the board has no tracks.");
missingRequirement = true;
}
missingRequirement |= moves.missingRequirement(game);
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= moves.willCrash(game);
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String trackNameString = "";
if (trackName != null)
trackNameString = "along track \"" + trackName + "\" ";
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "perform any of the following moves which travels the furthest distance " + trackNameString + moves.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 8,005 | 25.335526 | 138 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/moves/MaxCaptures.java | package game.rules.play.moves.nonDecision.effect.requirement.max.moves;
import java.util.BitSet;
import java.util.List;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import gnu.trove.list.array.TIntArrayList;
import other.action.Action;
import other.action.ActionType;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Filters a list of moves to keep only the moves doing the maximum possible number of captures.
*
* @author Eric.Piette
*
* @remarks For games allowing only the maximum possible number of captures in one move (e.g.
* Triad).
*/
@Hide
public final class MaxCaptures extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The moves to maximise. */
private final Moves moves;
/** To maximise the values of the capturing pieces. */
private final BooleanFunction withValueFn;
/**
* @param withValue If true, the capture has to maximise the values of the capturing pieces too.
* @param moves The moves to filter.
* @param then The moves applied after that move is applied.
*/
public MaxCaptures
(
@Opt @Name final BooleanFunction withValue,
final Moves moves,
@Opt final Then then
)
{
super(then);
this.moves = moves;
withValueFn = (withValue == null) ? new BooleanConstant(false) : withValue;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves returnMoves = new BaseMoves(super.then());
final Moves movesToEval = moves.eval(context);
final boolean withValue = withValueFn.eval(context);
// Compute the number of capture for each move.
final TIntArrayList numCaptureByMove = new TIntArrayList();
if(!withValue)
{
for (final Move m : movesToEval.moves())
{
int numCapture = 0;
final List<Action> actions = m.getActionsWithConsequences(context);
for (final Action action : actions)
if (action != null &&
action.actionType().equals(ActionType.Remove))
numCapture++;
numCaptureByMove.add(numCapture);
}
}
else
{
for (final Move m : movesToEval.moves())
{
int numCapture = 0;
final List<Action> actions = m.getActionsWithConsequences(context);
for (final Action action : actions)
if (action != null && action.actionType().equals(ActionType.Remove))
{
final int site = action.to();
final SiteType type = action.toType();
final ContainerState cs = context.containerState(0);
final int value = cs.value(site, type);
numCapture += value;
}
numCaptureByMove.add(numCapture);
}
}
// Compute the maximum of capture.
int maxCapture = 0;
for (int i = 0; i < numCaptureByMove.size(); i++)
if (numCaptureByMove.getQuick(i) > maxCapture)
maxCapture = numCaptureByMove.getQuick(i);
// Keep only the maximum one.
for (int i = 0; i < numCaptureByMove.size(); i++)
if (numCaptureByMove.getQuick(i) == maxCapture)
returnMoves.moves().add(movesToEval.get(i));
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < returnMoves.moves().size(); j++)
returnMoves.moves().get(j).setMovesLudeme(returnMoves);
return returnMoves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = moves.gameFlags(game) | withValueFn.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(moves.concepts(game));
concepts.or(withValueFn.concepts(game));
concepts.or(super.concepts(game));
concepts.set(Concept.MaxCapture.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(moves.writesEvalContextRecursive());
writeEvalContext.or(withValueFn.writesEvalContextRecursive());
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(moves.readsEvalContextRecursive());
readEvalContext.or(withValueFn.readsEvalContextRecursive());
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= moves.missingRequirement(game);
missingRequirement |= withValueFn.missingRequirement(game);
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= moves.willCrash(game);
willCrash |= withValueFn.willCrash(game);
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
if(!withValueFn.isStatic())
return false;
final boolean isStatic = moves.isStatic();
return isStatic;
}
@Override
public void preprocess(final Game game)
{
moves.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "if " + withValueFn.toEnglish(game) + " then perform any of the following moves which captures the most pieces " + moves.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 6,734 | 26.602459 | 159 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/requirement/max/moves/MaxMoves.java | package game.rules.play.moves.nonDecision.effect.requirement.max.moves;
import java.util.BitSet;
import java.util.List;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import other.action.Action;
import other.action.ActionType;
import other.concept.Concept;
import other.context.Context;
import other.context.TempContext;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Filters a list of legal moves to keep only the moves allowing the maximum number of
* moves in a turn.
*
* @author Eric.Piette
*
* @remarks For games like International Draughts.
*/
@Hide
public final class MaxMoves extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The moves to maximise. */
private final Moves moves;
/** To maximise the values of the capturing pieces. */
private final BooleanFunction withValueFn;
/**
* @param withValue If true, the capture has to maximise the values of the capturing pieces too.
* @param moves The moves to filter.
* @param then The moves applied after that move is applied.
*/
public MaxMoves
(
@Opt @Name final BooleanFunction withValue,
final Moves moves,
@Opt final Then then
)
{
super(then);
this.moves = moves;
withValueFn = (withValue == null) ? new BooleanConstant(false) : withValue;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves returnMoves = new BaseMoves(super.then());
final Moves movesToEval = moves.eval(context);
final boolean withValue = withValueFn.eval(context);
final int[] replayCount = new int[movesToEval.moves().size()];
// We store evalled moves because they'll include already-evalled consequents, more efficient to keep them
final Move[] evalledMoves = new Move[movesToEval.moves().size()];
for (int i = 0; i < movesToEval.moves().size(); i++)
{
final Move m = movesToEval.moves().get(i);
// System.out.println(m);
final Context newContext = new TempContext(context);
evalledMoves[i] = newContext.game().apply(newContext, m);
if (!withValue)
{
replayCount[i] = getReplayCount(newContext, 1, withValue);
}
else
{
int numCaptureWithValue = 0;
final List<Action> actions = m.getActionsWithConsequences(context);
for (final Action action : actions)
{
if (action != null && action.actionType().equals(ActionType.Remove))
{
final int site = action.to();
final int level = action.levelTo();
final SiteType type = action.toType();
final ContainerState cs = context.containerState(0);
final int value = cs.value(site, level, type);
numCaptureWithValue += value;
}
}
replayCount[i] = getReplayCount(newContext, numCaptureWithValue, withValue);
}
}
int max = 0;
// Get the max of the replayCount.
for (final int count : replayCount)
{
if (count > max)
max = count;
}
// Keep only the longest moves.
for (int i = 0; i < evalledMoves.length; i++)
if (replayCount[i] == max)
returnMoves.moves().add(evalledMoves[i]);
// Store the Moves in the computed moves.
for (int j = 0; j < returnMoves.moves().size(); j++)
returnMoves.moves().get(j).setMovesLudeme(returnMoves);
return returnMoves;
}
//-------------------------------------------------------------------------
/**
* @param context The context.
* @param withValue If true, the capture has to maximise the values of the capturing pieces too.
* @return the count of the replay of this move.
*/
private int getReplayCount(final Context contextCopy, final int count, final boolean withValue)
{
if (contextCopy.state().prev() != contextCopy.state().mover() || contextCopy.trial().over())
return count;
final Moves legalMoves = contextCopy.game().moves(contextCopy);
final int[] replayCount = new int[legalMoves.moves().size()];
for (int i = 0; i < legalMoves.moves().size(); i++)
{
final Move newMove = legalMoves.moves().get(i);
final Context newContext = new TempContext(contextCopy);
newContext.game().apply(newContext, newMove);
if (!withValue)
{
replayCount[i] = getReplayCount(newContext, count + 1, withValue);
}
else
{
int numCaptureWithValue = 0;
final List<Action> actions = newMove.getActionsWithConsequences(contextCopy);
for (final Action action : actions)
{
if (action != null && action.actionType().equals(ActionType.Remove))
{
final int site = action.to();
final SiteType type = action.toType();
final ContainerState cs = contextCopy.containerState(0);
final int value = cs.value(site, type);
numCaptureWithValue += value;
}
}
replayCount[i] = getReplayCount(newContext, count + numCaptureWithValue, withValue);
}
}
int max = 0;
// Get the max of the replayCount.
for (final int nbReplay : replayCount)
{
if (nbReplay > max)
max = nbReplay;
}
return max;
}
// private int getReplayCount(final Context context, final int count, final boolean withValue)
// {
// if (context.state().prev() != context.state().mover() || context.trial().over())
// return count;
//
// final Moves legalMoves = context.game().moves(context);
//
// final int[] replayCount = new int[legalMoves.moves().size()];
//
// for (int i = 0; i < legalMoves.moves().size(); i++)
// {
// final Move newMove = legalMoves.moves().get(i);
// context.game().apply(context, newMove);
// if (!withValue)
// {
// replayCount[i] = getReplayCount(context, count + 1, withValue);
// }
// else
// {
// int numCaptureWithValue = 0;
// final List<Action> actions = newMove.getActionsWithConsequences(context);
// for (final Action action : actions)
// {
// if (action != null && action.actionType().equals(ActionType.Remove))
// {
// final int site = action.to();
// final SiteType type = action.toType();
// final ContainerState cs = context.containerState(0);
// final int value = cs.value(site, type);
// numCaptureWithValue += value;
// }
// }
// replayCount[i] = getReplayCount(context, count + numCaptureWithValue, withValue);
// }
// context.game().undo(context);
// }
//
// int max = 0;
//
// // Get the max of the replayCount.
// for (final int nbReplay : replayCount)
// {
// if (nbReplay > max)
// max = nbReplay;
// }
//
// return max;
// }
//-------------------------------------------------------------------------
@Override
public boolean canMove(final Context context)
{
// Don't care about max moves here; as soon as we have at least 1 move,
// we know that we can move (even if that one may not be the max move!)
return moves.canMove(context);
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = moves.gameFlags(game) | withValueFn.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(moves.concepts(game));
concepts.or(withValueFn.concepts(game));
concepts.or(super.concepts(game));
concepts.set(Concept.MaxMovesInTurn.id(), true);
concepts.set(Concept.CopyContext.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(moves.writesEvalContextRecursive());
writeEvalContext.or(withValueFn.writesEvalContextRecursive());
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(moves.readsEvalContextRecursive());
readEvalContext.or(withValueFn.readsEvalContextRecursive());
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= moves.missingRequirement(game);
missingRequirement |= withValueFn.missingRequirement(game);
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= moves.willCrash(game);
willCrash |= withValueFn.willCrash(game);
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
if(!withValueFn.isStatic())
return false;
final boolean isStatic = moves.isStatic();
return isStatic;
}
@Override
public void preprocess(final Game game)
{
moves.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "if " + withValueFn.toEnglish(game) + " then perform any of the following moves which has the most sub-moves " + moves.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 10,137 | 27.397759 | 157 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/Set.java | package game.rules.play.moves.nonDecision.effect.set;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.intArray.IntArrayFunction;
import game.functions.intArray.math.Difference;
import game.functions.ints.IntFunction;
import game.functions.region.RegionFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.effect.set.direction.SetRotation;
import game.rules.play.moves.nonDecision.effect.set.hidden.SetHidden;
import game.rules.play.moves.nonDecision.effect.set.nextPlayer.SetNextPlayer;
import game.rules.play.moves.nonDecision.effect.set.pending.SetPending;
import game.rules.play.moves.nonDecision.effect.set.player.SetScore;
import game.rules.play.moves.nonDecision.effect.set.player.SetValuePlayer;
import game.rules.play.moves.nonDecision.effect.set.site.SetCount;
import game.rules.play.moves.nonDecision.effect.set.site.SetState;
import game.rules.play.moves.nonDecision.effect.set.site.SetValue;
import game.rules.play.moves.nonDecision.effect.set.suit.SetTrumpSuit;
import game.rules.play.moves.nonDecision.effect.set.team.SetTeam;
import game.rules.play.moves.nonDecision.effect.set.value.SetCounter;
import game.rules.play.moves.nonDecision.effect.set.value.SetPot;
import game.rules.play.moves.nonDecision.effect.set.var.SetVar;
import game.types.board.HiddenData;
import game.types.board.SiteType;
import game.types.play.RoleType;
import game.util.moves.Player;
import other.IntArrayFromRegion;
import other.context.Context;
/**
* Sets some aspect of the game state in response to a move.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class Set extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For setting a team.
*
* @param setType The type of property to set.
* @param team The index of the team.
* @param roles The roleType of each player on the team.
* @param then The moves applied after that move is applied.
*
* @example (set Team 1 {P1 P3})
*/
public static Moves construct
(
final SetTeamType setType,
final IntFunction team,
final RoleType[] roles,
@Opt final Then then
)
{
return new SetTeam(team, roles, then);
}
//-------------------------------------------------------------------------
/**
* For setting the hidden information.
*
* @param setType The type of property to set.
* @param dataType The type of hidden data [Invisible].
* @param dataTypes The types of hidden data [Invisible].
* @param type The graph element type [default of the board].
* @param at The site to set the hidden information.
* @param region The region to set the hidden information.
* @param level The level to set the hidden information [0].
* @param value The value to set [True].
* @param to The player with these hidden information.
* @param To The roleType with these hidden information.
* @param then The moves applied after that move is applied.
*
* @example (set Hidden What at:(last To) to:Mover)
* @example (set Hidden What at:(last To) to:P2)
* @example (set Hidden Count (sites Occupied by:Next) to:Mover)
*/
public static Moves construct
(
final SetHiddenType setType,
@Opt @Or final HiddenData dataType,
@Opt @Or final HiddenData[] dataTypes,
@Opt final SiteType type,
@Name @Or2 final IntFunction at,
@Or2 final RegionFunction region,
@Opt @Name final IntFunction level,
@Opt final BooleanFunction value,
@Name @Or final Player to,
@Name @Or final RoleType To,
@Opt final Then then
)
{
int numNonNull = 0;
if (dataType != null)
numNonNull++;
if (dataTypes != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException(
"Set(): With SetHiddenType only one dataType or dataTypes parameter must be non-null.");
int numNonNull2 = 0;
if (at != null)
numNonNull2++;
if (region != null)
numNonNull2++;
if (numNonNull2 != 1)
throw new IllegalArgumentException(
"Set(): With SetHiddenType one at or region parameter must be non-null.");
numNonNull = 0;
if (to != null)
numNonNull++;
if (To != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Set(): With SetHiddenType one to or To parameter must be non-null.");
switch (setType)
{
case Hidden:
return new SetHidden(dataTypes != null ? dataTypes : dataType != null ? new HiddenData[]
{ dataType } : null, type, new IntArrayFromRegion(at, region), level, value, to, To, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetHiddenType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the trump suit.
*
* @param setType The type of property to set.
* @param suit The suit to choose.
* @param suits The possible suits to choose.
* @param then The moves applied after that move is applied.
*
* @example (set TrumpSuit (card Suit at:(handSite Shared)))
*
*/
public static Moves construct
(
final SetTrumpType setType,
@Or final IntFunction suit,
@Or final Difference suits,
@Opt final Then then
)
{
int numNonNull = 0;
if (suit != null)
numNonNull++;
if (suits != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Set(): With SetSuitType only one suit or suits parameter must be non-null.");
switch (setType)
{
case TrumpSuit:
return new SetTrumpSuit(suit, suits, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetSuitType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the next player.
*
* @param setType The type of property to set.
* @param who The data of the next player.
* @param nextPlayers The indices of the next players.
* @param then The moves applied after that move is applied.
*
* @example (set NextPlayer (player (mover)))
*
*/
public static Moves construct
(
final SetNextPlayerType setType,
@Or final Player who,
@Or final IntArrayFunction nextPlayers,
@Opt final Then then
)
{
int numNonNull = 0;
if (who != null)
numNonNull++;
if (nextPlayers != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException("Set(): With SetPlayerType only one who or nextPlayers parameter can be non-null.");
switch (setType)
{
case NextPlayer:
return new SetNextPlayer(who, nextPlayers, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetPlayerType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the rotations.
*
* @param setType The type of property to set.
* @param to Description of the ``to'' location [(to (from))].
* @param directions The index of the possible new rotations.
* @param direction The index of the possible new rotation.
* @param previous True to allow movement to the left [True].
* @param next True to allow movement to the right [True].
* @param then The moves applied after that move is applied.
*
* @example (set Rotation)
*
* @example (set Rotation (to (last To)) next:False)
*/
public static Moves construct
(
final SetRotationType setType,
@Opt final game.util.moves.To to,
@Opt @Or final IntFunction[] directions,
@Opt @Or final IntFunction direction,
@Opt @Name final BooleanFunction previous,
@Opt @Name final BooleanFunction next,
@Opt final Then then
)
{
int numNonNull = 0;
if (directions != null)
numNonNull++;
if (direction != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException(
"Set(): With SetRotationType zero or one directions or direction parameter must be non-null.");
switch (setType)
{
case Rotation:
return new SetRotation(to, directions, direction, previous, next, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetRotationType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the value or the score of a player.
*
* @param setType The type of property to set.
* @param player The index of the player.
* @param role The role of the player.
* @param value The value of the player.
* @param then The moves applied after that move is applied.
*
* @example (set Value Mover 1)
*
* @example (set Score P1 50)
*/
public static Moves construct
(
final SetPlayerType setType,
@Or final game.util.moves.Player player,
@Or final RoleType role,
final IntFunction value,
@Opt final Then then
)
{
int numNonNull = 0;
if (player != null)
numNonNull++;
if (role != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Set(): With SetType Only one player or role parameter m be non-null.");
switch (setType)
{
case Value:
return new SetValuePlayer(player, role, value, then);
case Score:
return new SetScore(player, role, value, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the pending values.
*
* @param setType The type of property to set.
* @param value The value of the pending state [1].
* @param region The set of locations to put in pending.
* @param then The moves to apply afterwards.
*
* @example (set Pending)
* @example (set Pending (sites From (forEach Piece)))
*/
public static Moves construct
(
final SetPendingType setType,
@Opt @Or final IntFunction value,
@Opt @Or final RegionFunction region,
@Opt final Then then
)
{
switch (setType)
{
case Pending:
return new SetPending(value, region, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetPendingType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the counter or the variables.
*
* @param setType The type of property to set.
* @param name The name of the var.
* @param newValue The new counter value [-1].
* @param then The moves to apply afterwards.
*
* @example (set Var (value Piece at:(last To)))
*/
public static Moves construct
(
final SetVarType setType,
@Opt final String name,
@Opt final IntFunction newValue,
@Opt final Then then
)
{
switch (setType)
{
case Var:
return new SetVar(name, newValue, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetVarType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the counter or the variables.
*
* @param setType The type of property to set.
* @param newValue The new counter value [-1].
* @param then The moves to apply afterwards.
*
* @example (set Counter -1)
*/
public static Moves construct
(
final SetValueType setType,
@Opt final IntFunction newValue,
@Opt final Then then
)
{
switch (setType)
{
case Counter:
return new SetCounter(newValue,then);
case Pot:
return new SetPot(newValue, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetValueType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For setting the count or the state of a site.
*
* @param setType The type of property to set.
* @param type The graph element type [default SiteType of the board].
* @param at The site to set.
* @param level The level to set [0].
* @param value The new value.
* @param then The moves to apply afterwards.
*
* @example (set State at:(last To) (mover))
*
* @example (set Count at:(last To) 10)
*
* @example (set Value at:(last To) 10)
*
*/
public static Moves construct
(
final SetSiteType setType,
@Opt final SiteType type,
@Name final IntFunction at,
@Name @Opt final IntFunction level,
final IntFunction value,
@Opt final Then then
)
{
switch (setType)
{
case Count:
return new SetCount(type, at, value, then);
case State:
return new SetState(type, at, level, value, then);
case Value:
return new SetValue(type, at, level, value, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Set(): A SetSiteType is not implemented.");
}
//-------------------------------------------------------------------------
private Set()
{
super(null);
// Ensure that compiler does pick up default constructor
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Set.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return 0L;
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("Set.canMoveTo(): Should never be called directly.");
}
//-------------------------------------------------------------------------
}
| 15,129 | 28.096154 | 122 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetHiddenType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of hidden information that can be set in the game state.
*/
public enum SetHiddenType
{
/** Sets the hidden information of a location. */
Hidden,
} | 230 | 22.1 | 77 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetNextPlayerType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of player that can be set in the game state.
*/
public enum SetNextPlayerType
{
/** Sets the next player. */
NextPlayer,
}
| 206 | 17.818182 | 65 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetPendingType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of `pending' value that can be set in the game state.
*/
public enum SetPendingType
{
/** Sets specified sites to a certain pending value. */
Pending,
}
| 236 | 20.545455 | 74 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetPlayerType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines properties related to the players that can be set in the game state.
*/
public enum SetPlayerType
{
/** Sets the value associated with a player. */
Value,
/** Sets the score of a player. */
Score,
}
| 275 | 18.714286 | 79 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetRotationType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of rotation that can be set in the game state.
*/
public enum SetRotationType
{
/** Sets the rotation of a piece. */
Rotation,
}
| 212 | 18.363636 | 67 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetSiteType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines properties of sites that can be set in the game state.
*/
public enum SetSiteType
{
/** Set the count value for specified sites. */
Count,
/** Set the local state value for specified sites. */
State,
/** Set the piece value for specified sites. */
Value,
}
| 336 | 18.823529 | 65 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetTeamType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines teams that can be set.
*
* @author Eric.Piette
*/
public enum SetTeamType
{
/** Set a team. */
Team,
} | 178 | 13.916667 | 53 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetTrumpType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of suit that can be set in the game state.
*/
public enum SetTrumpType
{
/** Sets the trump suit. */
TrumpSuit,
}
| 197 | 17 | 63 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetValueType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of integer values that can be set in the game state.
*/
public enum SetValueType
{
/** Sets the counter of the game state. */
Counter,
/** Sets the pot of the game state. */
Pot,
}
| 267 | 18.142857 | 73 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/SetVarType.java | package game.rules.play.moves.nonDecision.effect.set;
/**
* Defines the types of integer values that can be set to var.
*/
public enum SetVarType
{
/** Sets the `var' variable of the game state. */
Var,
}
| 210 | 18.181818 | 62 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/package-info.java | /**
* The {\tt (set ...)} `super' ludeme sets some aspect of the game state in response to a move.
* This includes, for example, setting a counter value, or the next player, or the state of a site, etc.
*/
package game.rules.play.moves.nonDecision.effect.set;
| 264 | 43.166667 | 105 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/direction/SetRotation.java | package game.rules.play.moves.nonDecision.effect.set.direction;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.iterator.From;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.action.state.ActionSetRotation;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Changes the direction of a piece.
*
* @author Eric.Piette and cambolbro
*
* @remarks This ludeme applies to games with oriented pieces, e.g. Ploy.
*/
@Hide
public final class SetRotation extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which Direction. */
private final IntFunction siteFn;
/** Which Set of direction. */
private final IntFunction[] directionsFn;
/** Previous Direction. */
private final BooleanFunction previous;
/** Next Direction. */
private final BooleanFunction next;
/** Add on Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param to Description of the ``to'' location [(to (from))].
* @param directions The index of the possible new rotations.
* @param direction The index of the possible new rotation.
* @param previous True to allow movement to the left [True].
* @param next True to allow movement to the right [True].
* @param then The moves applied after that move is applied.
*/
public SetRotation
(
@Opt final game.util.moves.To to,
@Opt @Or final IntFunction[] directions,
@Opt @Or final IntFunction direction,
@Opt @Name final BooleanFunction previous,
@Opt @Name final BooleanFunction next,
@Opt final Then then
)
{
super(then);
int numNonNull = 0;
if (directions != null)
numNonNull++;
if (direction != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException("Zero or one Or parameter must be non-null.");
siteFn = (to == null) ? new From(null) : (to.loc() != null) ? to.loc() : new From(null);
if (directions != null)
directionsFn = directions;
else
directionsFn = (direction == null) ? null : new IntFunction[]
{ direction };
this.previous = (previous == null) ? new BooleanConstant(true) : previous;
this.next = (next == null) ? new BooleanConstant(true) : next;
type = (to == null) ? null : to.type();
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final BaseMoves moves = new BaseMoves(super.then());
final int site = siteFn.eval(context);
if (site == Constants.OFF)
return moves;
if (directionsFn != null)
{
for (final IntFunction directionFn : directionsFn)
{
final int direction = directionFn.eval(context);
final ActionSetRotation actionRotation = new ActionSetRotation(type, site, direction);
if (isDecision())
actionRotation.setDecision(true);
final Move action = new Move(actionRotation);
action.setFromNonDecision(site);
action.setToNonDecision(site);
action.setMover(context.state().mover());
moves.moves().add(action);
}
}
if (previous != null || next != null)
{
final int currentRotation = context.containerState(context.containerId()[site]).rotation(site, type);
final int maxRotation = context.game().maximalRotationStates() - 1;
if (previous != null && previous.eval(context))
{
final int newRotation = (currentRotation > 0) ? currentRotation - 1 : maxRotation;
final ActionSetRotation actionRotation = new ActionSetRotation(type, site, newRotation);
if (isDecision())
actionRotation.setDecision(true);
final Move action = new Move(actionRotation);
action.setFromNonDecision(site);
action.setToNonDecision(site);
action.setMover(context.state().mover());
moves.moves().add(action);
}
if (next != null && next.eval(context))
{
final int newRotation = (currentRotation < maxRotation) ? currentRotation + 1 : 0;
final ActionSetRotation actionRotation = new ActionSetRotation(type, site, newRotation);
if (isDecision())
actionRotation.setDecision(true);
final Move action = new Move(actionRotation);
action.setFromNonDecision(site);
action.setToNonDecision(site);
action.setMover(context.state().mover());
moves.moves().add(action);
}
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game) | siteFn.gameFlags(game) | previous.gameFlags(game)
| next.gameFlags(game)
| GameType.Rotation;
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
gameFlags |= direction.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(super.concepts(game));
concepts.or(siteFn.concepts(game));
concepts.or(previous.concepts(game));
concepts.or(next.concepts(game));
concepts.set(Concept.PieceRotation.id(), true);
if(isDecision())
concepts.set(Concept.RotationDecision.id(), true);
else
concepts.set(Concept.SetRotation.id(), true);
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
concepts.or(direction.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(siteFn.writesEvalContextRecursive());
writeEvalContext.or(previous.writesEvalContextRecursive());
writeEvalContext.or(next.writesEvalContextRecursive());
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
writeEvalContext.or(direction.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(siteFn.readsEvalContextRecursive());
readEvalContext.or(previous.readsEvalContextRecursive());
readEvalContext.or(next.readsEvalContextRecursive());
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
readEvalContext.or(direction.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= siteFn.missingRequirement(game);
missingRequirement |= previous.missingRequirement(game);
missingRequirement |= next.missingRequirement(game);
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
missingRequirement |= direction.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= siteFn.willCrash(game);
willCrash |= previous.willCrash(game);
willCrash |= next.willCrash(game);
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
willCrash |= direction.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
boolean isStatic = siteFn.isStatic() | previous.isStatic() | next.isStatic();
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
isStatic |= direction.isStatic();
return isStatic;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
siteFn.preprocess(game);
previous.preprocess(game);
next.preprocess(game);
if (directionsFn != null)
for (final IntFunction direction : directionsFn)
direction.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String directionsString = "all directions";
if (directionsFn != null)
{
directionsString = "[";
for (final IntFunction i : directionsFn)
directionsString += i.toEnglish(game) + ",";
directionsString = directionsString.substring(0,directionsString.length()-1) + "]";
}
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the rotation of " + type.name().toLowerCase() + " " + siteFn.toEnglish(game) + " to " + directionsString + thenString;
}
//-------------------------------------------------------------------------
}
| 10,182 | 28.688047 | 132 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/hidden/SetHidden.java | package game.rules.play.moves.nonDecision.effect.set.hidden;
import java.util.ArrayList;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.HiddenData;
import game.types.board.SiteType;
import game.types.play.RoleType;
import game.types.state.GameType;
import game.util.moves.Player;
import gnu.trove.list.array.TIntArrayList;
import main.StringRoutines;
import other.IntArrayFromRegion;
import other.PlayersIndices;
import other.action.Action;
import other.action.hidden.ActionSetHidden;
import other.action.hidden.ActionSetHiddenCount;
import other.action.hidden.ActionSetHiddenRotation;
import other.action.hidden.ActionSetHiddenState;
import other.action.hidden.ActionSetHiddenValue;
import other.action.hidden.ActionSetHiddenWhat;
import other.action.hidden.ActionSetHiddenWho;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Sets the hidden information of a region.
*
* @author Eric.Piette
*/
@Hide
public final class SetHidden extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Hidden data type. */
private final HiddenData[] dataTypes;
/** Which region. */
private final IntArrayFromRegion region;
/** Level. */
private final IntFunction levelFn;
/** Value to set. */
private final BooleanFunction valueFn;
/** The player to set the hidden information. */
private final IntFunction whoFn;
/** The RoleType if used */
private final RoleType roleType;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* To set hidden information.
*
* @param dataTypes The types of hidden data [Invisible].
* @param type The graph element type [default of the board].
* @param region The region to set the hidden information.
* @param level The level to set the hidden information [0].
* @param value The value to set [True].
* @param to The player with these hidden information.
* @param To The players with these hidden information.
* @param then The moves applied after that move is applied.
*/
public SetHidden
(
@Opt final HiddenData[] dataTypes,
@Opt final SiteType type,
final IntArrayFromRegion region,
@Name @Opt final IntFunction level,
@Opt final BooleanFunction value,
@Or final Player to,
@Or final RoleType To,
@Opt final Then then
)
{
super(then);
this.dataTypes = dataTypes;
this.region = region;
levelFn = (level == null) ? new IntConstant(0) : level;
valueFn = (value == null) ? new BooleanConstant(true) : value;
this.type = type;
whoFn = (to == null && To == null) ? null : To != null ? RoleType.toIntFunction(To) : to.originalIndex();
roleType = To;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Return list of legal "to" moves
final Moves moves = new BaseMoves(super.then());
final int[] sites = region.eval(context);
final int level = levelFn.eval(context);
final SiteType realType = (type != null) ? type : context.game().board().defaultSite();
final boolean value = valueFn.eval(context);
final int who = whoFn.eval(context);
final Move move = new Move(new ArrayList<Action>());
final int numPlayers = context.game().players().count();
if (roleType != null && RoleType.manyIds(roleType))
{
final TIntArrayList idPlayers = PlayersIndices.getIdRealPlayers(context, roleType);
if (dataTypes == null)
{
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHidden(pid, realType, site, level, value);
move.actions().add(action);
}
}
}
else
{
for (final HiddenData hiddenData : dataTypes)
{
switch (hiddenData)
{
case What:
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHiddenWhat(pid, realType, site, level, value);
move.actions().add(action);
}
}
break;
case Who:
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHiddenWho(pid, realType, site, level, value);
move.actions().add(action);
}
}
break;
case State:
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHiddenState(pid, realType, site, level, value);
move.actions().add(action);
}
}
break;
case Count:
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHiddenCount(pid, realType, site, level, value);
move.actions().add(action);
}
}
break;
case Rotation:
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHiddenRotation(pid, realType, site, level, value);
move.actions().add(action);
}
}
break;
case Value:
for (final int site : sites)
{
for (int i = 0; i < idPlayers.size(); i++)
{
final int pid = idPlayers.get(i);
final Action action = new ActionSetHiddenValue(pid, realType, site, level, value);
move.actions().add(action);
}
}
break;
default:
break;
}
}
}
}
else // The case to apply setHidden to a specific player.
{
if (who >= 1 && who <= numPlayers) // The player has to be a real player.
{
if (dataTypes == null)
{
for (final int site : sites)
{
final Action action = new ActionSetHidden(who, realType, site, level, value);
move.actions().add(action);
}
}
else
{
for (final HiddenData hiddenData : dataTypes)
{
switch (hiddenData)
{
case What:
for (final int site : sites)
{
final Action action = new ActionSetHiddenWhat(who, realType, site, level, value);
move.actions().add(action);
}
break;
case Who:
for (final int site : sites)
{
final Action action = new ActionSetHiddenWho(who, realType, site, level, value);
move.actions().add(action);
}
break;
case State:
for (final int site : sites)
{
final Action action = new ActionSetHiddenState(who, realType, site, level, value);
move.actions().add(action);
}
break;
case Count:
for (final int site : sites)
{
final Action action = new ActionSetHiddenCount(who, realType, site, level, value);
move.actions().add(action);
}
break;
case Rotation:
for (final int site : sites)
{
final Action action = new ActionSetHiddenRotation(who, realType, site, level, value);
move.actions().add(action);
}
break;
case Value:
for (final int site : sites)
{
final Action action = new ActionSetHiddenValue(who, realType, site, level, value);
move.actions().add(action);
}
break;
default:
break;
}
}
}
}
}
moves.moves().add(move);
if (then() != null)
move.then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game) | GameType.HiddenInfo;
gameFlags |= SiteType.gameFlags(type);
gameFlags |= region.gameFlags(game);
gameFlags |= levelFn.gameFlags(game);
gameFlags |= valueFn.gameFlags(game);
gameFlags |= whoFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(SiteType.concepts(type));
concepts.or(region.concepts(game));
concepts.or(levelFn.concepts(game));
concepts.or(valueFn.concepts(game));
concepts.or(whoFn.concepts(game));
concepts.set(Concept.HiddenInformation.id(),true);
if (dataTypes == null)
{
concepts.set(Concept.InvisiblePiece.id(), true);
concepts.set(Concept.SetInvisible.id(), true);
}
else
for (final HiddenData dataType : dataTypes)
{
switch (dataType)
{
case What:
concepts.set(Concept.HidePieceType.id(), true);
concepts.set(Concept.SetHiddenWhat.id(), true);
break;
case Who:
concepts.set(Concept.HidePieceOwner.id(), true);
concepts.set(Concept.SetHiddenWho.id(), true);
break;
case Count:
concepts.set(Concept.HidePieceCount.id(), true);
concepts.set(Concept.SetHiddenCount.id(), true);
break;
case Value:
concepts.set(Concept.HidePieceValue.id(), true);
concepts.set(Concept.SetHiddenValue.id(), true);
break;
case Rotation:
concepts.set(Concept.HidePieceRotation.id(), true);
concepts.set(Concept.SetHiddenRotation.id(), true);
break;
case State:
concepts.set(Concept.HidePieceState.id(), true);
concepts.set(Concept.SetHiddenState.id(), true);
break;
default:
break;
}
}
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(region.writesEvalContextRecursive());
writeEvalContext.or(levelFn.writesEvalContextRecursive());
writeEvalContext.or(valueFn.writesEvalContextRecursive());
writeEvalContext.or(whoFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(region.readsEvalContextRecursive());
readEvalContext.or(levelFn.readsEvalContextRecursive());
readEvalContext.or(valueFn.readsEvalContextRecursive());
readEvalContext.or(whoFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= region.missingRequirement(game);
missingRequirement |= levelFn.missingRequirement(game);
missingRequirement |= valueFn.missingRequirement(game);
missingRequirement |= whoFn.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
if (roleType != null)
{
if (RoleType.isTeam(roleType) && !game.requiresTeams())
{
game.addRequirementToReport(
"(set Hidden ...): A roletype corresponding to a team is used but the game has no team: "
+ roleType + ".");
missingRequirement = true;
}
final int indexRoleType = roleType.owner();
if (indexRoleType > game.players().count())
{
game.addRequirementToReport(
"The roletype used in the rule (set Hidden ...) is wrong: " + roleType + ".");
missingRequirement = true;
}
}
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= region.willCrash(game);
willCrash |= levelFn.willCrash(game);
willCrash |= valueFn.willCrash(game);
willCrash |= whoFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
region.preprocess(game);
levelFn.preprocess(game);
valueFn.preprocess(game);
whoFn.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String hiddenDataTypeString = "all properties";
if (dataTypes != null)
{
hiddenDataTypeString = "";
for (final HiddenData h : dataTypes)
hiddenDataTypeString += h.name().toLowerCase() + ", ";
hiddenDataTypeString = "properties " + hiddenDataTypeString.substring(0, hiddenDataTypeString.length()-2);
}
String regionString = "";
if (region != null)
regionString = " in region " + region.toEnglish(game);
String levelString = "";
if (levelFn != null)
levelString = " at level " + levelFn.toEnglish(game);
String valueString = "";
if (valueFn != null)
valueString = " to value " + valueFn.toEnglish(game);
String whoString = "";
if (whoFn != null)
whoString = " for Player " + whoFn.toEnglish(game);
else if (roleType != null)
whoString = " for " + roleType.name().toLowerCase();
String typeString = " sites";
if (type != null)
typeString = " " + type.name().toLowerCase() + StringRoutines.getPlural(type.name()) + " ";
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the hidden values for " + hiddenDataTypeString + valueString + whoString + " at all" + typeString + regionString + levelString + thenString;
}
//-------------------------------------------------------------------------
}
| 14,810 | 27.984344 | 154 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/nextPlayer/SetNextPlayer.java | package game.rules.play.moves.nonDecision.effect.set.nextPlayer;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.intArray.IntArrayConstant;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.util.moves.Player;
import main.Constants;
import other.action.state.ActionSetNextPlayer;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Is used to set the next player.
*
* @author Eric.Piette and cambolbro
*/
@Hide
public final class SetNextPlayer extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The indices of the possible next player. */
private final IntArrayFunction nextPlayerFn;
/**
* @param who The data of the next player.
* @param nextPlayers The indices of the next players.
* @param then The moves applied after that move is applied.
*/
public SetNextPlayer
(
@Or final Player who,
@Or final IntArrayFunction nextPlayers,
@Opt final Then then
)
{
super(then);
int numNonNull = 0;
if (who != null)
numNonNull++;
if (nextPlayers != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException("Only one Or parameter can be non-null.");
if (nextPlayers != null)
nextPlayerFn = nextPlayers;
else
nextPlayerFn = new IntArrayConstant(new IntFunction[]
{ who.index() });
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int[] nextPlayerIds = nextPlayerFn.eval(context);
for (final int nextPlayerId : nextPlayerIds)
{
if (nextPlayerId < 1 || nextPlayerId > context.game().players().count())
{
System.err.println("The Player " + nextPlayerId + " can not be set");
continue;
}
final ActionSetNextPlayer actionSetNextPlayer = new ActionSetNextPlayer(nextPlayerId);
if (isDecision())
actionSetNextPlayer.setDecision(true);
final Move move = new Move(actionSetNextPlayer);
move.setFromNonDecision(Constants.OFF);
move.setToNonDecision(Constants.OFF);
move.setMover(context.state().mover());
moves.moves().add(move);
}
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game) | nextPlayerFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (isDecision())
concepts.set(Concept.SetNextPlayer.id(), true);
concepts.or(nextPlayerFn.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(nextPlayerFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(nextPlayerFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= nextPlayerFn.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= nextPlayerFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return nextPlayerFn.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
nextPlayerFn.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the next mover to Player " + nextPlayerFn.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 5,464 | 24.300926 | 89 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/pending/SetPending.java | package game.rules.play.moves.nonDecision.effect.set.pending;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.ints.IntFunction;
import game.functions.region.RegionFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import main.Constants;
import other.action.state.ActionSetPending;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
//-----------------------------------------------------------------------------
/**
* Returns the set of moves that set the "pending" value in the state.
*
* @author Eric.Piette and cambolbro and Dennis Soemers
*
*/
@Hide
public final class SetPending extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Single value (typically site) to set as pending */
private final IntFunction value;
/** Region to set as pending */
private final RegionFunction region;
//-------------------------------------------------------------------------
/**
* @param value The value to refer to the pending state [1].
* @param region The set of locations to put in pending.
* @param then The moves applied after that move is applied.
*
*/
public SetPending
(
@Opt @Or final IntFunction value,
@Opt @Or final RegionFunction region,
@Opt final Then then
)
{
super(then);
this.value = value;
this.region = region;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
if (region == null)
{
final Move move;
final ActionSetPending actionPending = (value == null) ? new ActionSetPending(Constants.UNDEFINED)
: new ActionSetPending(value.eval(context));
move = new Move(actionPending);
moves.moves().add(move);
}
else
{
final int[] sites = region.eval(context).sites();
if (sites.length != 0)
{
final ActionSetPending actionPending = new ActionSetPending(sites[0]);
final Move move = new Move(actionPending);
for (int i = 1; i < sites.length; i++)
{
final ActionSetPending actionToadd = new ActionSetPending(sites[i]);
move.actions().add(actionToadd);
}
moves.moves().add(move);
}
}
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.PendingValues | super.gameFlags(game);
if (value != null)
gameFlags |= value.gameFlags(game);
if (region != null)
gameFlags |= region.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.SetPending.id(), true);
if (value != null)
concepts.or(value.concepts(game));
if (region != null)
concepts.or(region.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (value != null)
writeEvalContext.or(value.writesEvalContextRecursive());
if (region != null)
writeEvalContext.or(region.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (value != null)
readEvalContext.or(value.readsEvalContextRecursive());
if (region != null)
readEvalContext.or(region.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (value != null)
missingRequirement |= value.missingRequirement(game);
if (region != null)
missingRequirement |= region.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (value != null)
willCrash |= value.willCrash(game);
if (region != null)
willCrash |= region.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
if (value != null)
value.preprocess(game);
if (region != null)
region.preprocess(game);
}
@Override
public String toEnglish(final Game game)
{
String englishString = "";
if (value != null)
englishString = "set the site " + value.toEnglish(game) + " to pending";
else if (region != null)
englishString = "set the region " + region.toEnglish(game) + " to pending";
else
englishString = "set pending";
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return englishString + thenString;
}
}
| 6,079 | 24.123967 | 101 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/player/SetScore.java | package game.rules.play.moves.nonDecision.effect.set.player;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.play.RoleType;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import other.PlayersIndices;
import other.action.state.ActionSetScore;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
//-----------------------------------------------------------------------------
/**
* Sets the score of a player.
*
* @author Eric.Piette
*/
@Hide
public final class SetScore extends Effect
{
private static final long serialVersionUID = 1L;
/** The player. */
private final IntFunction playerFn;
/** The roleType. */
private final RoleType role;
/** The score. */
private final IntFunction scoreFn;
//-------------------------------------------------------------------------
/**
* @param player The index of the player.
* @param role The roleType of the player.
* @param score The new score.
* @param then The moves applied after that move is applied.
*/
public SetScore
(
@Or final game.util.moves.Player player,
@Or final RoleType role,
final IntFunction score,
@Opt final Then then
)
{
super(then);
playerFn = (player == null) ? RoleType.toIntFunction(role) : player.index();
this.role = role;
scoreFn = score;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int playerId = playerFn.eval(context);
final int score = scoreFn.eval(context);
if(role != null)
{
// Code to handle specific roleType.
final TIntArrayList idPlayers = PlayersIndices.getIdRealPlayers(context, role);
for(int i = 0; i < idPlayers.size();i++)
{
final int pid = idPlayers.get(i);
final ActionSetScore actionScore = new ActionSetScore(pid, score, Boolean.FALSE);
final Move move = new Move(actionScore);
moves.moves().add(move);
}
}
else
{
final ActionSetScore actionScore = new ActionSetScore(playerId, score, Boolean.FALSE);
final Move move = new Move(actionScore);
moves.moves().add(move);
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.Score | GameType.HashScores | super.gameFlags(game);
gameFlags |= playerFn.gameFlags(game);
gameFlags |= scoreFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.Scoring.id(), true);
concepts.or(playerFn.concepts(game));
concepts.or(scoreFn.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(playerFn.writesEvalContextRecursive());
writeEvalContext.or(scoreFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(playerFn.readsEvalContextRecursive());
readEvalContext.or(scoreFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= playerFn.missingRequirement(game);
missingRequirement |= scoreFn.missingRequirement(game);
if (role != null && !game.requiresTeams())
{
if (RoleType.isTeam(role) && !game.requiresTeams())
{
game.addRequirementToReport(
"(sites Occupied ...): A roletype corresponding to a team is used but the game has no team: "
+ role + ".");
missingRequirement = true;
}
}
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= playerFn.willCrash(game);
willCrash |= scoreFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
playerFn.preprocess(game);
scoreFn.preprocess(game);
super.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set score of " + playerFn.toEnglish(game) + " to " + scoreFn.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
/**
* @return The player to set the score.
*/
public IntFunction player()
{
return playerFn;
}
/**
* @return The score to set.
*/
public IntFunction score()
{
return scoreFn;
}
} | 6,367 | 24.370518 | 100 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/player/SetValuePlayer.java | package game.rules.play.moves.nonDecision.effect.set.player;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.play.RoleType;
import main.Constants;
import other.action.Action;
import other.action.others.ActionSetValueOfPlayer;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Is used to set the value associated with a player.
*
* @author Eric.Piette
*/
@Hide
public final class SetValuePlayer extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The player. */
private final IntFunction playerId;
/** The value. */
private final IntFunction valueFn;
//-------------------------------------------------------------------------
/**
* @param player The index of the player.
* @param role The role of the player.
* @param value The value of the player.
* @param then The moves applied after that move is applied.
*/
public SetValuePlayer
(
@Or final game.util.moves.Player player,
@Or final RoleType role,
final IntFunction value,
@Opt final Then then
)
{
super(then);
if (player != null)
playerId = player.index();
else
playerId = RoleType.toIntFunction(role);
valueFn = value;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final BaseMoves moves = new BaseMoves(super.then());
final int pid = playerId.eval(context);
final int value = valueFn.eval(context);
if(pid < 0 || pid > context.game().players().count())
return moves;
final Action action = new ActionSetValueOfPlayer(pid, value);
final Move move = new Move(action);
move.setFromNonDecision(Constants.OFF);
move.setToNonDecision(Constants.OFF);
move.setMover(context.state().mover());
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = playerId.gameFlags(game) | valueFn.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(playerId.concepts(game));
concepts.or(valueFn.concepts(game));
concepts.set(Concept.PlayerValue.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(playerId.writesEvalContextRecursive());
writeEvalContext.or(valueFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(playerId.readsEvalContextRecursive());
readEvalContext.or(valueFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= playerId.missingRequirement(game);
missingRequirement |= valueFn.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= playerId.willCrash(game);
willCrash |= valueFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
playerId.preprocess(game);
valueFn.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the value of Player " + playerId.toEnglish(game) + " to " + valueFn.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 5,319 | 24.825243 | 111 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/site/SetCount.java | package game.rules.play.moves.nonDecision.effect.set.site;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import other.action.state.ActionSetCount;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Sets the count of a site.
*
* @author Eric.Piette
*/
@Hide
public final class SetCount extends Effect
{
private static final long serialVersionUID = 1L;
/** Which site. */
private final IntFunction locationFunction;
/** New count. */
private final IntFunction newCount;
/** Add on Cell/Edge/Vertex. */
protected SiteType type;
//-------------------------------------------------------------------------
/**
* To set the count of a site.
*
* @param type The graph element type of the location [Default =
* Cell (or Vertex if the main board uses
* intersections)].
* @param locationFunction The site to modify the count.
* @param newCount The new count.
* @param then The moves applied after that move is applied.
*/
public SetCount
(
@Opt final SiteType type,
final IntFunction locationFunction,
final IntFunction newCount,
@Opt final Then then
)
{
super(then);
this.locationFunction = locationFunction;
this.newCount = newCount;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Return list of legal "to" moves
final Moves moves = new BaseMoves(super.then());
final int loc = locationFunction.eval(context);
final int count = newCount.eval(context);
final ContainerState cs = context.containerState(context.containerId()[loc]);
final int what = cs.what(loc, type);
final ActionSetCount action = new ActionSetCount(type, loc, what, count);
final Move move = new Move(action);
moves.moves().add(move);
if (then() != null)
move.then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
gameFlags |= locationFunction.gameFlags(game);
gameFlags |= newCount.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(SiteType.concepts(type));
concepts.or(locationFunction.concepts(game));
concepts.or(newCount.concepts(game));
concepts.set(Concept.PieceCount.id(), true);
concepts.set(Concept.SetCount.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(locationFunction.writesEvalContextRecursive());
writeEvalContext.or(newCount.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(locationFunction.readsEvalContextRecursive());
readEvalContext.or(newCount.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= locationFunction.missingRequirement(game);
missingRequirement |= newCount.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= locationFunction.willCrash(game);
willCrash |= newCount.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return locationFunction.isStatic() && newCount.isStatic();
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
locationFunction.preprocess(game);
newCount.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the count of " + type.name().toLowerCase() + " " + locationFunction.toEnglish(game) + " to " + newCount.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 5,607 | 25.578199 | 147 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/site/SetState.java | package game.rules.play.moves.nonDecision.effect.set.site;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.action.BaseAction;
import other.action.state.ActionSetState;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Sets the local state of a location.
*
* @author Eric.Piette
*/
@Hide
public final class SetState extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The sites. */
private final IntFunction siteFn;
/** The level. */
private final IntFunction levelFn;
/** The value. */
private final IntFunction state;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param site The site to modify the local state.
* @param level The level to modify the local state.
* @param state The new local state.
* @param then The moves applied after that move is applied.
*/
public SetState
(
@Opt final SiteType type,
@Name final IntFunction site,
@Opt final IntFunction level,
final IntFunction state,
@Opt final Then then
)
{
super(then);
siteFn = site;
this.state = state;
this.type = type;
levelFn = level;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int stateValue = state.eval(context);
final int level = levelFn == null ? Constants.UNDEFINED : levelFn.eval(context);
if (stateValue < 0 || level < Constants.UNDEFINED)
return moves;
final int site = siteFn.eval(context);
if(site < 0)
return moves;
final int cid = site >= context.containerId().length ? 0 : context.containerId()[site];
SiteType realType = type;
if (cid > 0)
realType = SiteType.Cell;
else if (realType == null)
realType = context.board().defaultSite();
if(cid == 0)
{
if(site >= context.containers()[0].topology().getGraphElements(realType).size())
return moves;
}
else
{
if((site - context.sitesFrom()[cid]) >= context.containers()[cid].topology().getGraphElements(SiteType.Cell).size())
return moves;
}
final BaseAction action = new ActionSetState(realType, site, level, stateValue);
final Move move = new Move(action);
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.SiteState | siteFn.gameFlags(game) | state.gameFlags(game) | super.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (levelFn != null)
gameFlags |= levelFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(siteFn.concepts(game));
if (levelFn != null)
concepts.or(levelFn.concepts(game));
concepts.or(SiteType.concepts(type));
concepts.or(state.concepts(game));
concepts.set(Concept.SiteState.id(), true);
concepts.set(Concept.SetSiteState.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (levelFn != null)
writeEvalContext.or(levelFn.writesEvalContextRecursive());
writeEvalContext.or(siteFn.writesEvalContextRecursive());
writeEvalContext.or(state.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (levelFn != null)
readEvalContext.or(levelFn.readsEvalContextRecursive());
readEvalContext.or(siteFn.readsEvalContextRecursive());
readEvalContext.or(state.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (levelFn != null)
missingRequirement |= levelFn.missingRequirement(game);
missingRequirement |= siteFn.missingRequirement(game);
missingRequirement |= state.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (levelFn != null)
willCrash |= levelFn.willCrash(game);
willCrash |= siteFn.willCrash(game);
willCrash |= state.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
if (levelFn != null && !levelFn.isStatic())
return false;
return siteFn.isStatic() && state.isStatic();
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
if (levelFn != null)
levelFn.preprocess(game);
siteFn.preprocess(game);
state.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "SetState [siteFn=" + siteFn + ", state=" + state + "then=" + then() + "]";
}
@Override
public String toEnglish(final Game game)
{
final SiteType realType = (type != null) ? type : game.board().defaultSite();
String siteString = "";
if (siteFn != null)
siteString = " " + siteFn.toEnglish(game);
String levelString = "";
if (levelFn != null)
levelString = " (level " + levelFn.toEnglish(game) + ")";
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the state of the " + realType.name() + siteString + levelString + " to " + state.toEnglish(game) + thenString;
}
}
| 7,096 | 25.781132 | 124 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/site/SetValue.java | package game.rules.play.moves.nonDecision.effect.set.site;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.action.BaseAction;
import other.action.state.ActionSetValue;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Sets the piece value of a location.
*
* @author Eric.Piette
*/
@Hide
public final class SetValue extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The site. */
protected final IntFunction siteFn;
/** The level. */
private final IntFunction levelFn;
/** The value. */
protected final IntFunction value;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param site The site to modify the local state.
* @param level The level to modify the local state.
* @param value The new piece value.
* @param then The moves applied after that move is applied.
*/
public SetValue
(
@Opt final SiteType type,
@Name final IntFunction site,
@Opt final IntFunction level,
final IntFunction value,
@Opt final Then then
)
{
super(then);
siteFn = site;
this.value = value;
this.type = type;
levelFn = level;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int level = levelFn == null ? Constants.UNDEFINED : levelFn.eval(context);
final int valueInt = value.eval(context);
if (valueInt < 0 || level < Constants.UNDEFINED)
return moves;
final BaseAction action = new ActionSetValue(type, siteFn.eval(context), level, valueInt);
final Move move = new Move(action);
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.Value | siteFn.gameFlags(game) | value.gameFlags(game) | super.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (levelFn != null)
gameFlags |= levelFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(siteFn.concepts(game));
if (levelFn != null)
concepts.or(levelFn.concepts(game));
concepts.set(Concept.PieceValue.id(), true);
concepts.set(Concept.SetValue.id(), true);
concepts.or(SiteType.concepts(type));
concepts.or(value.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(siteFn.writesEvalContextRecursive());
writeEvalContext.or(value.writesEvalContextRecursive());
if (levelFn != null)
writeEvalContext.or(levelFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(siteFn.readsEvalContextRecursive());
readEvalContext.or(value.readsEvalContextRecursive());
if (levelFn != null)
readEvalContext.or(levelFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (levelFn != null)
missingRequirement |= levelFn.missingRequirement(game);
missingRequirement |= super.missingRequirement(game);
missingRequirement |= siteFn.missingRequirement(game);
missingRequirement |= value.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (levelFn != null)
willCrash |= levelFn.willCrash(game);
willCrash |= super.willCrash(game);
willCrash |= siteFn.willCrash(game);
willCrash |= value.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
if (levelFn != null && !levelFn.isStatic())
return false;
return siteFn.isStatic() && value.isStatic();
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
if (levelFn != null)
levelFn.preprocess(game);
siteFn.preprocess(game);
value.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "SetValue [siteFn=" + siteFn + ", value=" + value + "then=" + then() + "]";
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String levelString = "";
if (levelFn != null)
levelString = " at " + levelFn.toString();
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the count of " + type.name().toLowerCase() + " " + siteFn.toEnglish(game) + levelString + " to " + value.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 6,502 | 25.542857 | 148 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/suit/SetTrumpSuit.java | package game.rules.play.moves.nonDecision.effect.set.suit;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.component.Component;
import game.functions.intArray.IntArrayConstant;
import game.functions.intArray.IntArrayFunction;
import game.functions.intArray.math.Difference;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import main.Constants;
import other.action.Action;
import other.action.cards.ActionSetTrumpSuit;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Chooses a trump suit from a set of suits.
*
* @author Eric.Piette and cambolbro
*
* @remarks This ludeme is used for card games that involve trumps.
*/
@Hide
public final class SetTrumpSuit extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** All the possible suits. */
private final IntArrayFunction suitsFn;
//-------------------------------------------------------------------------
/**
* @param suit The suit to choose.
* @param suits The possible suits to choose.
* @param then The moves applied after that move is applied.
*/
public SetTrumpSuit
(
@Or final IntFunction suit,
@Or final Difference suits,
@Opt final Then then
)
{
super(then);
int numNonNull = 0;
if (suit != null)
numNonNull++;
if (suits != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Only one Or parameter must be non-null.");
if (suits != null)
this.suitsFn = suits;
else
this.suitsFn = new IntArrayConstant(new IntFunction[]
{ suit });
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final BaseMoves moves = new BaseMoves(super.then());
final int[] suits = suitsFn.eval(context);
for (final int suit : suits)
{
final Action action = new ActionSetTrumpSuit(suit);
if (isDecision())
action.setDecision(true);
final Move move = new Move(action);
move.setFromNonDecision(Constants.OFF);
move.setToNonDecision(Constants.OFF);
move.setMover(context.state().mover());
moves.moves().add(move);
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game) | GameType.NotAllPass;
gameFlags |= suitsFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
boolean gameHasCard = false;
for (int i = 1; i < game.equipment().components().length; i++)
{
final Component component = game.equipment().components()[i];
if (component.isCard())
{
gameHasCard = true;
break;
}
}
if (!gameHasCard)
{
game.addRequirementToReport("The ludeme (set TrumpSuit ...) is used but the equipment has no cards.");
missingRequirement = true;
}
missingRequirement |= super.missingRequirement(game);
missingRequirement |= suitsFn.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= suitsFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (isDecision())
concepts.set(Concept.ChooseTrumpSuitDecision.id(), true);
else
concepts.set(Concept.SetTrumpSuit.id(), true);
concepts.set(Concept.Card.id(), true);
concepts.or(suitsFn.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(suitsFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(suitsFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean isStatic()
{
return suitsFn.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
}
}
| 5,577 | 23.464912 | 105 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/team/SetTeam.java | package game.rules.play.moves.nonDecision.effect.set.team;
import java.util.ArrayList;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.play.RoleType;
import game.types.state.GameType;
import main.Constants;
import other.action.Action;
import other.action.state.ActionAddPlayerToTeam;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.trial.Trial;
/**
* Sets a team.
*
* @author Eric.Piette
*/
@Hide
public final class SetTeam extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The index of the team. */
final IntFunction teamIdFn;
/** The index of the players on the team. */
final IntFunction[] players;
/** The roletypes used to check them in the required warning method. */
final RoleType[] roles;
//-------------------------------------------------------------------------
/**
* @param team The index of the team.
* @param roles The roleType of each player on the team.
* @param then The moves applied after that move is applied.
*/
public SetTeam
(
final IntFunction team,
final RoleType[] roles,
@Opt final Then then
)
{
super(then);
this.teamIdFn = team;
this.players = new IntFunction[roles.length];
for (int i = 0; i < roles.length; i++)
{
final RoleType role = roles[i];
this.players[i] = RoleType.toIntFunction(role);
}
this.roles = roles;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final BaseMoves moves = new BaseMoves(super.then());
final int teamId = teamIdFn.eval(context);
final Move move = new Move(new ArrayList<Action>());
for (final IntFunction player : players)
{
final int playerIndex = player.eval(context);
// We ignore all the player indices which are not real players.
if (playerIndex < 1 || playerIndex > context.game().players().count())
continue;
final ActionAddPlayerToTeam actionTeam = new ActionAddPlayerToTeam(teamId, playerIndex);
move.actions().add(actionTeam);
move.setFromNonDecision(Constants.OFF);
move.setToNonDecision(Constants.OFF);
move.setMover(context.state().mover());
}
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.Team;
for (final IntFunction player : players)
gameFlags |= player.gameFlags(game);
gameFlags |= teamIdFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
// We check if the roles are corrects.
if (roles != null)
{
for (final RoleType role : roles)
{
final int indexOwnerPhase = role.owner();
if (indexOwnerPhase < 1 || indexOwnerPhase > game.players().count())
{
game.addRequirementToReport(
"At least a roletype is wrong in a starting rules (set Team ...): " + role + ".");
missingRequirement = true;
break;
}
}
}
final int teamId = teamIdFn.eval(new Context(game, new Trial(game)));
if (teamId < 1 || teamId > game.players().count())
{
game.addRequirementToReport("In (set Team ...), the index of the team is wrong.");
missingRequirement = true;
}
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= teamIdFn.willCrash(game);
for (final IntFunction player : players)
willCrash |= player.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.set(Concept.Team.id(), true);
concepts.set(Concept.Coalition.id(), true);
concepts.or(super.concepts(game));
concepts.or(teamIdFn.concepts(game));
for (final IntFunction player : players)
concepts.or(player.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(teamIdFn.writesEvalContextRecursive());
for (final IntFunction player : players)
writeEvalContext.or(player.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(teamIdFn.readsEvalContextRecursive());
for (final IntFunction player : players)
readEvalContext.or(player.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
teamIdFn.preprocess(game);
for (final IntFunction player : players)
player.preprocess(game);
}
}
| 6,165 | 24.167347 | 91 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/value/SetCounter.java | package game.rules.play.moves.nonDecision.effect.set.value;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import other.action.BaseAction;
import other.action.state.ActionSetCounter;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Sets the current counter of the game.
*
* @author Eric.Piette
*
* @remarks The counter is incremented at each move, so to reinitialize it to 0
* at the next move, the counter has to be set at -1.
*/
@Hide
public final class SetCounter extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** New value for the counter. */
private final IntFunction newValue;
//-------------------------------------------------------------------------
/**
* @param newValue The new counter [-1].
* @param then The moves applied after that move is applied.
*/
public SetCounter
(
@Opt final IntFunction newValue,
@Opt final Then then
)
{
super(then);
this.newValue = (newValue == null) ? new IntConstant(-1) : newValue;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final BaseAction actionSetCounter = new ActionSetCounter(newValue.eval(context));
final Move move = new Move(actionSetCounter);
moves.moves().add(move);
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = newValue.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(newValue.concepts(game));
concepts.set(Concept.InternalCounter.id(), true);
concepts.set(Concept.SetInternalCounter.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(newValue.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(newValue.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= newValue.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= newValue.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return newValue.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
newValue.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "SetCounter(" + newValue + ")";
}
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the current counter of the game to " + newValue + thenString;
}
}
| 4,456 | 23.899441 | 83 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/value/SetPot.java | package game.rules.play.moves.nonDecision.effect.set.value;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import main.Constants;
import other.action.state.ActionSetPot;
import other.context.Context;
import other.move.Move;
/**
* Set the pot value of the state.
*
* @author Eric.Piette
*/
@Hide
public final class SetPot extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
private final IntFunction value;
/**
* @param value The value to set the pot [-1].
* @param then The moves applied after that move is applied.
*/
public SetPot
(
@Opt final IntFunction value,
@Opt final Then then
)
{
super(then);
this.value = (value == null) ? new IntConstant(Constants.UNDEFINED) : value;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final Move move;
final ActionSetPot actionSetPot = new ActionSetPot(value.eval(context));
move = new Move(actionSetPot);
moves.moves().add(move);
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
if (value != null)
gameFlags |= value.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (value != null)
concepts.or(value.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(value.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(value.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (value != null)
missingRequirement |= value.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (value != null)
willCrash |= value.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
if (value != null)
value.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the value of the pot to " + value.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 4,297 | 22.615385 | 78 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/set/var/SetVar.java | package game.rules.play.moves.nonDecision.effect.set.var;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import main.Constants;
import other.action.state.ActionSetTemp;
import other.action.state.ActionSetVar;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Stores an integer in the state in the variable "var".
*
* @author Eric.Piette
*/
@Hide
public final class SetVar extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The value to set. */
private final IntFunction value;
/** The name of the var */
private final String name;
/**
* @param name The name of the var.
* @param value The value to store in the context [-1].
* @param then The moves applied after that move is applied.
*/
public SetVar
(
@Opt final String name,
@Opt final IntFunction value,
@Opt final Then then
)
{
super(then);
this.value = (value == null) ? new IntConstant(Constants.UNDEFINED) : value;
this.name = name;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final Move move;
if (name == null)
{
final ActionSetTemp actionTemp = new ActionSetTemp(value.eval(context));
move = new Move(actionTemp);
moves.moves().add(move);
}
else
{
final ActionSetVar actionSetVar = new ActionSetVar(name, value.eval(context));
move = new Move(actionSetVar);
moves.moves().add(move);
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.MapValue | super.gameFlags(game);
if (value != null)
gameFlags |= value.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (value != null)
concepts.or(value.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.Variable.id(), true);
concepts.set(Concept.SetVar.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (value != null)
writeEvalContext.or(value.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (value != null)
readEvalContext.or(value.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (value != null)
missingRequirement |= value.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (value != null)
willCrash |= value.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
if (value != null)
value.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "set the variable " + name + " to " + value.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 5,080 | 22.632558 | 82 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/AddScore.java | package game.rules.play.moves.nonDecision.effect.state;
import java.util.BitSet;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.play.RoleType;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import other.PlayersIndices;
import other.action.state.ActionSetScore;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Adds a value to the score of a player.
*
* @author Eric.Piette
*/
public final class AddScore extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The players. */
private final IntFunction players[];
/** The score. */
private final IntFunction scores[];
/** The roleTypes used to check in the requiredLudeme */
private final RoleType[] roles;
//-------------------------------------------------------------------------
/**
* For adding a score to a player.
*
* @param player The index of the player.
* @param role The roleType of the player.
* @param score The score of the player.
* @param then The moves applied after that move is applied.
*
* @example (addScore Mover 50)
*/
public AddScore
(
@Or final game.util.moves.Player player,
@Or final RoleType role,
final IntFunction score,
@Opt final Then then
)
{
super(then);
int numNonNull = 0;
if (player != null)
numNonNull++;
if (role != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Exactly one Or parameter must be non-null.");
players = new IntFunction[1];
players[0] = (player == null) ? RoleType.toIntFunction(role) : player.index();
if (score != null)
{
scores = new IntFunction[1];
scores[0] = score;
}
else
{
scores = null;
}
roles = (role != null) ? new RoleType[] { role } : null;
}
/**
* For adding a score to many players.
*
* @param players The indices of the players.
* @param roles The roleType of the players.
* @param scores The scores to add.
* @param then The moves applied after that move is applied.
*
*
* @example (addScore {P1 P2} {50 10})
*/
public AddScore
(
@Or final IntFunction[] players,
@Or final RoleType[] roles,
final IntFunction[] scores,
@Opt final Then then
)
{
super(then);
int numNonNull = 0;
if (players != null)
numNonNull++;
if (roles != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Exactly one Or parameter must be non-null.");
if (players != null)
{
this.players = players;
}
else
{
this.players = new IntFunction[roles.length];
for (int i = 0; i < roles.length; i++)
{
final RoleType role = roles[i];
this.players[i] = RoleType.toIntFunction(role);
}
}
this.scores = scores;
this.roles = roles;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int length = Math.min(players.length, scores.length);
if(roles != null)
{
for (int i = 0; i < length; i++)
{
final RoleType role = roles[i];
final int score = scores[i].eval(context);
final TIntArrayList idPlayers = PlayersIndices.getIdRealPlayers(context, role);
for(int j = 0; j < idPlayers.size();j++)
{
final int pid = idPlayers.get(j);
final ActionSetScore actionScore = new ActionSetScore(pid, score, Boolean.TRUE);
final Move move = new Move(actionScore);
moves.moves().add(move);
}
}
}
else
{
for (int i = 0; i < length; i++)
{
final int playerId = players[i].eval(context);
final int score = scores[i].eval(context);
final ActionSetScore actionScore = new ActionSetScore(playerId, score, Boolean.TRUE);
final Move move = new Move(actionScore);
moves.moves().add(move);
}
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.Score | GameType.HashScores | super.gameFlags(game);
for (final IntFunction player : players)
gameFlags |= player.gameFlags(game);
for (final IntFunction score : scores)
gameFlags |= score.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.Scoring.id(), true);
for (final IntFunction player : players)
concepts.or(player.concepts(game));
for (final IntFunction score : scores)
concepts.or(score.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
for (final IntFunction player : players)
writeEvalContext.or(player.writesEvalContextRecursive());
for (final IntFunction score : scores)
writeEvalContext.or(score.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
for (final IntFunction player : players)
readEvalContext.or(player.readsEvalContextRecursive());
for (final IntFunction score : scores)
readEvalContext.or(score.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (roles != null)
{
for (final RoleType role : roles)
{
final int indexOwnerPhase = role.owner();
if (role.equals(RoleType.Mover) || role.equals(RoleType.Next) || role.equals(RoleType.Prev) || role.equals(RoleType.Player))
continue;
if (indexOwnerPhase < 1 || indexOwnerPhase > game.players().count())
{
game.addRequirementToReport(
"An incorrect roletype is used in the ludeme (addScore ...): " + role + ".");
missingRequirement = true;
}
}
}
for (final IntFunction player : players)
missingRequirement |= player.missingRequirement(game);
for (final IntFunction score : scores)
missingRequirement |= score.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
for (final IntFunction player : players)
willCrash |= player.willCrash(game);
for (final IntFunction score : scores)
willCrash |= score.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
for (final IntFunction player : players)
player.preprocess(game);
if (scores != null)
for (final IntFunction score : scores)
score.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String text = "";
for (int i = 0; i < players.length; i++)
text += "add score " + scores[i].toEnglish(game) + " to player " + players[i].toEnglish(game) + "\n";
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return text + thenString;
}
}
| 8,715 | 23.761364 | 128 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/MoveAgain.java | package game.rules.play.moves.nonDecision.effect.state;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import other.action.state.ActionSetNextPlayer;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.state.State;
/**
* Is used to move again.
*
* @author Eric.Piette
* @remarks For games with multiple moves in a turn.
*/
public final class MoveAgain extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param then The moves applied after that move is applied.
*
* @example (moveAgain)
*/
public MoveAgain
(
@Opt final Then then
)
{
super(then);
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final State state = context.state();
final Moves moves = new BaseMoves(super.then());
moves.moves().add(new Move(new ActionSetNextPlayer(state.mover())));
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.MoveAgain | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.MoveAgain.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "move again" + thenString;
}
}
| 3,735 | 21.642424 | 76 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/package-info.java | /**
* State move generators create moves based on certain properties of the game state.
*/
package game.rules.play.moves.nonDecision.effect.state;
| 149 | 29 | 84 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/forget/Forget.java | package game.rules.play.moves.nonDecision.effect.state.forget;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.effect.state.forget.value.ForgetValue;
import game.rules.play.moves.nonDecision.effect.state.forget.value.ForgetValueAll;
import other.context.Context;
/**
* Forget information about the state to be used in future state.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class Forget extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For forgetting all values.
*
* @param rememberType The type of property to forget.
* @param name The name of the remembering values.
* @param valueType To specify which values.
* @param then The moves applied after that move is applied.
*
* @example (forget Value All)
*/
public static Moves construct
(
final ForgetValueType rememberType,
@Opt final String name,
final ForgetValueAllType valueType,
@Opt final Then then
)
{
switch (rememberType)
{
case Value:
switch (valueType)
{
case All:
return new ForgetValueAll(name,then);
default:
break;
}
break;
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Forget(): A ForgetValueType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For forgetting a value.
*
* @param rememberType The type of property to forget.
* @param name The name of the remembering values.
* @param value The value to forget.
* @param then The moves applied after that move is applied.
*
* @example (forget Value (count Pips))
*/
public static Moves construct
(
final ForgetValueType rememberType,
@Opt final String name,
final IntFunction value,
@Opt final Then then
)
{
switch (rememberType)
{
case Value:
return new ForgetValue(name, value, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Forget(): A ForgetValueType is not implemented.");
}
//-------------------------------------------------------------------------
private Forget()
{
super(null);
// Ensure that compiler does pick up default constructor
}
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Forget.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return super.gameFlags(game);
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("Forget.canMoveTo(): Should never be called directly.");
}
//-------------------------------------------------------------------------
} | 3,553 | 24.568345 | 98 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/forget/ForgetValueAllType.java | package game.rules.play.moves.nonDecision.effect.state.forget;
/**
* Defines the types of the super ludeme Forget Value.
*
* @author Eric.Piette
*/
public enum ForgetValueAllType
{
/** To forget all the values.. */
All,
} | 229 | 18.166667 | 62 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/forget/ForgetValueType.java | package game.rules.play.moves.nonDecision.effect.state.forget;
/**
* Defines the types of the super ludeme Forget for values.
*
* @author Eric.Piette
*/
public enum ForgetValueType
{
/** To forget a value. */
Value,
} | 225 | 17.833333 | 62 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/forget/package-info.java | /**
* The {\tt (forget ...)} `super' ludeme forgets some information stored.
*/
package game.rules.play.moves.nonDecision.effect.state.forget;
| 145 | 28.2 | 73 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/forget/value/ForgetValue.java | package game.rules.play.moves.nonDecision.effect.state.forget.value;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import other.action.state.ActionForgetValue;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Forgets a value stored previously in the state.
*
* @author Eric.Piette
*/
@Hide
public final class ForgetValue extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The value to forget. */
private final IntFunction value;
/** The name of the remembering values. */
private final String name;
/**
* @param name The name of the remembering values.
* @param value The value to forget.
* @param then The moves applied after that move is applied.
*/
public ForgetValue
(
@Opt final String name,
final IntFunction value,
@Opt final Then then
)
{
super(then);
this.value = value;
this.name = name;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final ActionForgetValue action = new ActionForgetValue(name, value.eval(context));
final Move move = new Move(action);
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.RememberingValues | value.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(value.concepts(game));
concepts.or(super.concepts(game));
concepts.set(Concept.ForgetValues.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(value.writesEvalContextRecursive());
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(value.readsEvalContextRecursive());
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= value.missingRequirement(game);
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= value.willCrash(game);
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
value.preprocess(game);
super.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "forget the value " + value + thenString;
}
//-------------------------------------------------------------------------
} | 4,520 | 23.437838 | 94 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/forget/value/ForgetValueAll.java | package game.rules.play.moves.nonDecision.effect.state.forget.value;
import java.util.ArrayList;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import other.action.Action;
import other.action.state.ActionForgetValue;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Forgets all the values remembered before.
*
* @author Eric.Piette
*/
@Hide
public final class ForgetValueAll extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The name of the remembering values. */
private final String name;
/**
* @param name The name of the remembering values.
* @param then The moves applied after that move is applied.
*/
public ForgetValueAll
(
@Opt final String name,
@Opt final Then then
)
{
super(then);
this.name = name;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final Move move = new Move(new ArrayList<Action>());
if (name != null)
{
final TIntArrayList rememberingValue = context.state().mapRememberingValues().get(name);
if (rememberingValue != null)
{
for (int i = 0; i < rememberingValue.size(); i++)
{
final int value = rememberingValue.get(i);
final ActionForgetValue action = new ActionForgetValue(name, value);
move.actions().add(action);
}
}
}
else
{
final TIntArrayList rememberingValue = context.state().rememberingValues();
if (rememberingValue != null)
{
for (int i = 0; i < rememberingValue.size(); i++)
{
final int value = rememberingValue.get(i);
final ActionForgetValue action = new ActionForgetValue(name, value);
move.actions().add(action);
}
}
for (final String key : context.state().mapRememberingValues().keySet())
{
final TIntArrayList rememberingNameValue = context.state().mapRememberingValues().get(key);
if (rememberingNameValue != null)
{
for (int i = 0; i < rememberingNameValue.size(); i++)
{
final int value = rememberingNameValue.get(i);
final ActionForgetValue action = new ActionForgetValue(key, value);
move.actions().add(action);
}
}
}
}
if (!move.actions().isEmpty())
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.RememberingValues | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.ForgetValues.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "forget all previously remembered values" + thenString;
}
//-------------------------------------------------------------------------
} | 5,309 | 23.027149 | 95 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/remember/Remember.java | package game.rules.play.moves.nonDecision.effect.state.remember;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.effect.state.remember.state.RememberState;
import game.rules.play.moves.nonDecision.effect.state.remember.value.RememberValue;
import other.context.Context;
/**
* Remember information about the state to be used in future state.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class Remember extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For remembering a value.
*
* @param rememberType The type of property to remember.
* @param name The name of the remembering values.
* @param value The value to remember.
* @param unique True if each remembered value has to be unique [False].
* @param then The moves applied after that move is applied.
*
* @example (remember Value (count Pips))
*/
public static Moves construct
(
final RememberValueType rememberType,
@Opt final String name,
final IntFunction value,
@Opt @Name final BooleanFunction unique,
@Opt final Then then
)
{
switch (rememberType)
{
case Value:
return new RememberValue(name,value,unique,then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Remember(): A RememberValueType is not implemented.");
}
/**
* For remembering the current state.
*
* @param rememberType The type of property to remember.
* @param then The moves applied after that move is applied.
*
* @example (remember State)
*/
public static Moves construct
(
final RememberStateType rememberType,
@Opt final Then then
)
{
switch (rememberType)
{
case State:
return new RememberState(then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Remember(): A RememberStateType is not implemented.");
}
//-------------------------------------------------------------------------
private Remember()
{
super(null);
// Ensure that compiler does pick up default constructor
}
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Remember.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return super.gameFlags(game);
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("Remember.canMoveTo(): Should never be called directly.");
}
//-------------------------------------------------------------------------
} | 3,470 | 25.7 | 100 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/remember/RememberStateType.java | package game.rules.play.moves.nonDecision.effect.state.remember;
/**
* Defines the types of the super ludeme Remember for state.
*
* @author Eric.Piette
*/
public enum RememberStateType
{
/** To remember the state. */
State,
}
| 235 | 17.153846 | 64 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/remember/RememberValueType.java | package game.rules.play.moves.nonDecision.effect.state.remember;
/**
* Defines the types of the super ludeme Remember for values.
*
* @author Eric.Piette
*/
public enum RememberValueType
{
/** To remember a value. */
Value,
} | 233 | 18.5 | 64 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/remember/package-info.java | /**
* The {\tt (remember ...)} `super' ludeme remembers some information to use
* them in future states.
*/
package game.rules.play.moves.nonDecision.effect.state.remember;
| 176 | 28.5 | 76 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/remember/state/RememberState.java | package game.rules.play.moves.nonDecision.effect.state.remember.state;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import other.action.state.ActionStoreStateInContext;
import other.context.Context;
import other.move.Move;
/**
* Stores the current state.
*
* @author Eric.Piette
*/
@Hide
public final class RememberState extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param then The moves applied after that move is applied.
*/
public RememberState
(
@Opt final Then then
)
{
super(then);
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final ActionStoreStateInContext action = new ActionStoreStateInContext();
final Move move = new Move(action);
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return true;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "remember the current state" + thenString;
}
//-------------------------------------------------------------------------
} | 3,684 | 21.607362 | 76 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/remember/value/RememberValue.java | package game.rules.play.moves.nonDecision.effect.state.remember.value;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import other.action.state.ActionRememberValue;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Remembers a value (in storing it in the state).
*
* @author Eric.Piette
*/
@Hide
public final class RememberValue extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The value to remember. */
private final IntFunction value;
/** If True we remember it only if not only remembered. */
private final BooleanFunction uniqueFn;
/** The name of the remembering values. */
private final String name;
/**
* @param name The name of the remembering values.
* @param value The value to remember.
* @param unique If True we remember a value only if not already remembered
* [False].
* @param then The moves applied after that move is applied.
*/
public RememberValue
(
@Opt final String name,
final IntFunction value,
@Opt @Name final BooleanFunction unique,
@Opt final Then then
)
{
super(then);
this.value = value;
uniqueFn = (unique == null) ? new BooleanConstant(false) : unique;
this.name = name;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int valueToRemember = value.eval(context);
final boolean hasToBeUnique = uniqueFn.eval(context);
boolean isUnique = true;
if(hasToBeUnique)
{
final TIntArrayList valuesInMemory = (name == null) ? context.state().rememberingValues()
: context.state().mapRememberingValues().get(name);
if (valuesInMemory != null)
for (int i = 0; i < valuesInMemory.size(); i++)
{
final int valueInMemory = valuesInMemory.get(i);
if (valueInMemory == valueToRemember)
{
isUnique = false;
break;
}
}
}
if(!hasToBeUnique || (hasToBeUnique && isUnique))
{
final ActionRememberValue action = new ActionRememberValue(name, valueToRemember);
final Move move = new Move(action);
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
}
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.RememberingValues | value.gameFlags(game) | uniqueFn.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(value.concepts(game));
concepts.or(uniqueFn.concepts(game));
concepts.or(super.concepts(game));
concepts.set(Concept.Variable.id(), true);
concepts.set(Concept.RememberValues.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(value.writesEvalContextRecursive());
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(value.readsEvalContextRecursive());
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= value.missingRequirement(game);
missingRequirement |= uniqueFn.missingRequirement(game);
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= value.willCrash(game);
willCrash |= uniqueFn.willCrash(game);
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
value.preprocess(game);
uniqueFn.preprocess(game);
super.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "remember the value " + value.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
} | 5,933 | 25.491071 | 121 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/swap/Swap.java | package game.rules.play.moves.nonDecision.effect.state.swap;
import annotations.And;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.effect.state.swap.players.SwapPlayers;
import game.rules.play.moves.nonDecision.effect.state.swap.sites.SwapPieces;
import game.types.play.RoleType;
import other.context.Context;
/**
* Swaps two players or two pieces.
*
* @author Eric.Piette and cambolbro
*/
@SuppressWarnings("javadoc")
public final class Swap extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For swapping two pieces.
*
* @param swapType The type of property to take.
* @param locA The first location [(lastFrom)].
* @param locB The second location [(lastTo)].
* @param then The moves applied after that move is applied.
*
* @example (swap Pieces)
*/
public static Moves construct
(
final SwapSitesType swapType,
@And @Opt final IntFunction locA,
@And @Opt final IntFunction locB,
@Opt final Then then
)
{
switch (swapType)
{
case Pieces:
return new SwapPieces(locA, locB, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Swap(): A SwapSitesType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For swapping two players.
*
* @param takeType The type of property to take.
* @param player1 The index of the first player.
* @param role1 The role of the first player.
* @param player2 The index of the second player.
* @param role2 The role of the second player.
* @param then The moves applied after that move is applied.
*
* @example (swap Players P1 P2)
*/
public static Moves construct
(
final SwapPlayersType takeType,
@Or final IntFunction player1,
@Or final RoleType role1,
@Or2 final IntFunction player2,
@Or2 final RoleType role2,
@Opt final Then then
)
{
int numNonNull = 0;
if (player1 != null)
numNonNull++;
if (role1 != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Swap(): Exactly one player1 or role1 parameter must be non-null.");
int numNonNull2 = 0;
if (player2 != null)
numNonNull2++;
if (role2 != null)
numNonNull2++;
if (numNonNull2 != 1)
throw new IllegalArgumentException("Swap(): Exactly one player2 or role2 parameter must be non-null.");
switch (takeType)
{
case Players:
return new SwapPlayers(player1, role1, player2, role2, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Swap(): A SwapSitesType is not implemented.");
}
//-------------------------------------------------------------------------
private Swap()
{
super(null);
// Ensure that compiler does pick up default constructor
}
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Swap.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return super.gameFlags(game);
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("Swap.canMoveTo(): Should never be called directly.");
}
//-------------------------------------------------------------------------
}
| 4,175 | 25.1 | 106 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/swap/SwapPlayersType.java | package game.rules.play.moves.nonDecision.effect.state.swap;
/**
* Defines the types of the super ludeme Swap for players.
*
* @author Eric.Piette
*/
public enum SwapPlayersType
{
/** To swap the players. */
Players,
}
| 227 | 16.538462 | 60 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/swap/SwapSitesType.java | package game.rules.play.moves.nonDecision.effect.state.swap;
/**
* Defines the types of the super ludeme Swap for sites.
*
* @author Eric.Piette
*/
public enum SwapSitesType
{
/** To swap the components of two sites. */
Pieces,
}
| 238 | 17.384615 | 60 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/swap/package-info.java | /**
* The {\tt (swap ...)} `super' ludeme swaps two pieces or two players.
*/
package game.rules.play.moves.nonDecision.effect.state.swap;
| 141 | 27.4 | 71 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/swap/players/SwapPlayers.java | package game.rules.play.moves.nonDecision.effect.state.swap.players;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.play.RoleType;
import other.action.others.ActionSwap;
import other.action.state.ActionSetNextPlayer;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Swap two players.
*
* @author Eric.Piette
*/
@Hide
public final class SwapPlayers extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The first player to swap. */
private final IntFunction player1;
/** The second player to swap. */
private final IntFunction player2;
/**
* @param player1 The index of the first player.
* @param role1 The role of the first player.
* @param player2 The index of the second player.
* @param role2 The role of the second player.
* @param then The moves applied after that move is applied.
*/
public SwapPlayers
(
@Or final IntFunction player1,
@Or final RoleType role1,
@Or2 final IntFunction player2,
@Or2 final RoleType role2,
@Opt final Then then
)
{
super(then);
this.player1 = (player1 == null) ? RoleType.toIntFunction(role1) : player1;
this.player2 = (player2 == null) ? RoleType.toIntFunction(role2) : player2;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final int pid1 = player1.eval(context);
final int pid2 = player2.eval(context);
final Moves moves = new BaseMoves(super.then());
final ActionSwap actionSwap = new ActionSwap(pid1, pid2);
actionSwap.setDecision(true);
final Move swapMove = new Move(actionSwap);
swapMove.actions().add(new ActionSetNextPlayer(context.state().mover()));
swapMove.setMover(context.state().mover());
moves.moves().add(swapMove);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = player1.gameFlags(game) | player2.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
if (isDecision())
concepts.set(Concept.SwapPlayersDecision.id(), true);
else
concepts.set(Concept.SwapPlayersEffect.id(), true);
concepts.or(super.concepts(game));
concepts.or(player1.concepts(game));
concepts.or(player2.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(player1.writesEvalContextRecursive());
writeEvalContext.or(player2.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(player1.readsEvalContextRecursive());
readEvalContext.or(player2.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= player1.missingRequirement(game);
missingRequirement |= player2.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= player1.willCrash(game);
willCrash |= player2.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
player1.preprocess(game);
player2.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "swap the players" + thenString;
}
}
| 5,428 | 25.227053 | 93 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/state/swap/sites/SwapPieces.java | package game.rules.play.moves.nonDecision.effect.state.swap.sites;
import java.util.BitSet;
import annotations.And;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.functions.ints.last.LastFrom;
import game.functions.ints.last.LastTo;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.action.Action;
import other.action.move.ActionAdd;
import other.action.move.move.ActionMove;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Swaps two pieces on the board.
*
* @author Eric.Piette
*/
@Hide
public final class SwapPieces extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The locA to swap */
private final IntFunction locAFn;
/** The locB to swap */
private final IntFunction locBFn;
//-------------------------------------------------------------------------
/**
* @param locA The first location [(lastFrom)].
* @param locB The second location [(lastTo)].
* @param then The moves applied after that move is applied.
*/
public SwapPieces
(
@And @Opt final IntFunction locA,
@And @Opt final IntFunction locB,
@Opt final Then then
)
{
super(then);
locAFn = (locA == null) ? new LastFrom(null) : locA;
locBFn = (locB == null) ? new LastTo(null) : locB;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int locA = locAFn.eval(context);
final int locB = locBFn.eval(context);
final ContainerState cs = context.containerState(context.containerId()[locB]);
final int whatB = cs.whatCell(locB);
final Action actionMove = ActionMove.construct(SiteType.Cell, locA, Constants.UNDEFINED, SiteType.Cell, locB, Constants.OFF, Constants.UNDEFINED, Constants.OFF, Constants.OFF, false);
if (isDecision())
actionMove.setDecision(true);
final Move swapMove = new Move(actionMove);
final Action actionAdd = new ActionAdd(null, locA, whatB, 1, Constants.UNDEFINED, Constants.UNDEFINED,
Constants.UNDEFINED, null);
swapMove.actions().add(actionAdd);
swapMove.setFromNonDecision(locA);
swapMove.setToNonDecision(locB);
swapMove.setMover(context.state().mover());
moves.moves().add(swapMove);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.UsesFromPositions | locAFn.gameFlags(game) | locBFn.gameFlags(game)
| super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
if (isDecision())
concepts.set(Concept.SwapPiecesDecision.id(), true);
concepts.or(super.concepts(game));
concepts.or(locAFn.concepts(game));
concepts.or(locBFn.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(locAFn.writesEvalContextRecursive());
writeEvalContext.or(locBFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(locAFn.readsEvalContextRecursive());
readEvalContext.or(locBFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= locAFn.missingRequirement(game);
missingRequirement |= locBFn.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= locAFn.willCrash(game);
willCrash |= locBFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return locAFn.isStatic() && locBFn.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
locAFn.preprocess(game);
locBFn.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "Swap the pieces at " + locAFn.toEnglish(game) + "and" + locBFn.toEnglish(game) + thenString;
}
@Override
public boolean canMoveTo(final Context context, final int target)
{
return false;
}
}
| 5,854 | 25.981567 | 185 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/take/Take.java | package game.rules.play.moves.nonDecision.effect.take;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.functions.ints.IntFunction;
import game.functions.region.RegionFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.effect.take.control.TakeControl;
import game.rules.play.moves.nonDecision.effect.take.simple.TakeDomino;
import game.types.board.SiteType;
import game.types.play.RoleType;
import other.context.Context;
/**
* Takes a piece or the control of pieces.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class Take extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For taking a domino.
*
* @param takeType The type of property to take.
* @param then The moves applied after that move is applied.
*
* @example (take Domino)
*/
public static Moves construct
(
final TakeSimpleType takeType,
@Opt final Then then
)
{
switch (takeType)
{
case Domino:
return new TakeDomino(then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Take(): A TakeSimpleType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For taking the control of the pieces of another player.
*
* @param takeType The type of property to take.
* @param of The roleType of the pieces to take control of.
* @param Of The player index of the pieces to take control of.
* @param by The roleType taking the control.
* @param By The player index of the player taking control.
* @param at The site to take the control.
* @param to The region to take the control.
* @param type The graph element type [default SiteType of the board].
* @param then The moves applied after that move is applied.
*
* @example (take Control of:P1 by:Mover)
*/
public static Moves construct
(
final TakeControlType takeType,
@Or @Name final RoleType of,
@Or @Name final IntFunction Of,
@Or2 @Name final RoleType by,
@Or2 @Name final IntFunction By,
@Opt @Or @Name final IntFunction at,
@Opt @Or @Name final RegionFunction to,
@Opt final SiteType type,
@Opt final Then then
)
{
int numNonNull = 0;
if (of != null)
numNonNull++;
if (Of != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException(
"take(): With TakeControlType one of or Of parameter must be non-null.");
int numNonNull2 = 0;
if (by != null)
numNonNull2++;
if (By != null)
numNonNull2++;
if (numNonNull2 != 1)
throw new IllegalArgumentException(
"take(): With TakeControlType one by or By parameter must be non-null.");
numNonNull = 0;
if (at != null)
numNonNull++;
if (to != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException(
"take(): With TakeControlType zero or one at or to parameter can be non-null.");
switch (takeType)
{
case Control:
return new TakeControl(of, Of, by, By, at, to, type, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Take(): A TakeControlType is not implemented.");
}
private Take()
{
super(null);
// Ensure that compiler does pick up default constructor
}
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Take.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return 0L;
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("Take.canMoveTo(): Should never be called directly.");
}
} | 4,528 | 25.48538 | 96 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/take/TakeControlType.java | package game.rules.play.moves.nonDecision.effect.take;
/**
* Defines the types to take the control of pieces for the super ludeme Take.
*
* @author Eric.Piette
*/
public enum TakeControlType
{
/** To take the control of enemy pieces. */
Control,
}
| 256 | 18.769231 | 77 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/take/TakeSimpleType.java | package game.rules.play.moves.nonDecision.effect.take;
/**
* Defines the types of properties which can be take for the Take super ludeme
* with no parameter.
*
* @author Eric.Piette
*/
public enum TakeSimpleType
{
/** To take a domino from the bag. */
Domino,
}
| 271 | 18.428571 | 78 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/take/package-info.java | /**
* The {\tt (take ...)} `super' ludeme is used to take piece or the control of
* enemy pieces.
*/
package game.rules.play.moves.nonDecision.effect.take;
| 159 | 25.666667 | 78 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/take/control/TakeControl.java | package game.rules.play.moves.nonDecision.effect.take.control;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.equipment.component.Component;
import game.functions.ints.IntFunction;
import game.functions.ints.board.Id;
import game.functions.region.RegionFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.types.play.RoleType;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.IntArrayFromRegion;
import other.action.Action;
import other.action.move.ActionAdd;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Modifies the owner of some pieces on the board.
*
* @author Eric.Piette
*
* @remarks To modify the owner, modify the index of the piece to the equivalent
* of the other player. To work, an equivalent piece for the player who
* has to take the control must exist. For example for Ploy 4P.
*/
@Hide
public final class TakeControl extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The new owner. */
private final IntFunction newOwnerFn;
/** The role used for the new owner. */
private final RoleType newOwnerRole;
/** The current owner. */
private final IntFunction ownerFn;
/** The role used for the current owner. */
private final RoleType ownerRole;
/** The region to take the control. */
private final IntArrayFromRegion region;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param of The roleType of the pieces to take control of.
* @param Of The player index of the pieces to take control of.
* @param by The roleType taking the control.
* @param By The player index of the player taking control.
* @param at The site to take the control.
* @param to The region to take the control.
* @param type The graph element type [default SiteType of the board].
* @param then The moves applied after that move is applied.
*/
public TakeControl
(
@Or @Name final RoleType of,
@Or @Name final IntFunction Of,
@Or2 @Name final RoleType by,
@Or2 @Name final IntFunction By,
@Opt @Or @Name final IntFunction at,
@Opt @Or @Name final RegionFunction to,
@Opt final SiteType type,
@Opt final Then then
)
{
super(then);
newOwnerFn = by != null ? RoleType.toIntFunction(by) : By;
ownerFn = of != null ? new Id(null, of) : Of;
this.type = type;
newOwnerRole = by;
ownerRole = of;
region = (at == null && to == null) ? null : new IntArrayFromRegion(at, to);
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Return intersected list of moves
final Moves moves = new BaseMoves(super.then());
final int newOwner = newOwnerFn.eval(context);
final int owner = ownerFn.eval(context);
final TIntArrayList ownedSites = new TIntArrayList();
if (ownerRole.equals(RoleType.All))
{
for (int pid = 0; pid <= context.players().size(); pid++)
ownedSites.addAll(context.state().owned().sites(pid));
}
else
ownedSites.addAll(context.state().owned().sites(owner));
// We look only the sites which are in the region if a region is defined.
if (region != null)
{
final TIntArrayList sites = new TIntArrayList(region.eval(context));
for (int i = ownedSites.size() - 1; i >= 0; i--)
{
final int ownedSite = ownedSites.get(i);
if (!sites.contains(ownedSite))
ownedSites.removeAt(i);
}
}
for (int i = 0; i < ownedSites.size(); i++)
{
final int site = ownedSites.getQuick(i);
final ContainerState cs = context.containerState(context.containerId()[site]);
final int what = cs.what(site, type);
final int state = cs.state(site, type);
final int rotation = cs.rotation(site, type);
final int value = cs.value(site, type);
final int count = cs.count(site, type);
final Component componentOwned = context.components()[what];
final String name = componentOwned.getNameWithoutNumber();
int newWhat = Constants.UNDEFINED;
for (int indexComponent = 1; indexComponent < context.components().length; indexComponent++)
{
final Component newComponentOwned = context.components()[indexComponent];
if (newComponentOwned.owner() == newOwner && newComponentOwned.getNameWithoutNumber().equals(name))
{
newWhat = indexComponent;
break;
}
}
if (newWhat == Constants.UNDEFINED)
continue;
final Action actionRemove = other.action.move.remove.ActionRemove.construct(type, site, Constants.UNDEFINED, true);
final ActionAdd actionAdd = new ActionAdd(type, site, newWhat, count, state, rotation, value,
null);
final Move move = new Move(actionRemove);
move.actions().add(actionAdd);
move.setFromNonDecision(site);
move.setToNonDecision(site);
moves.moves().add(move);
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
gameFlags |= newOwnerFn.gameFlags(game);
gameFlags |= ownerFn.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (region != null)
gameFlags |= region.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(super.concepts(game));
concepts.or(newOwnerFn.concepts(game));
concepts.or(ownerFn.concepts(game));
concepts.set(Concept.TakeControl.id(), true);
if (region != null)
concepts.or(region.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(newOwnerFn.writesEvalContextRecursive());
writeEvalContext.or(ownerFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
if (region != null)
writeEvalContext.or(region.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(newOwnerFn.readsEvalContextRecursive());
readEvalContext.or(ownerFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
if (region != null)
readEvalContext.or(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
// We check if the slave player role is correct.
if (ownerRole != null)
{
final int indexOwnerPhase = ownerRole.owner();
if (((indexOwnerPhase < 1 && !ownerRole.equals(RoleType.Neutral) && !ownerRole.equals(RoleType.All)
&& !ownerRole.equals(RoleType.Mover))
&& !ownerRole.equals(RoleType.Prev)
&& !ownerRole.equals(RoleType.Next)) || indexOwnerPhase > game.players().count())
{
game.addRequirementToReport(
"In (take Control ...) the RoleType used for the of: parameter is incorrect: " + ownerRole
+ ".");
missingRequirement = true;
}
}
// We check if the master player role is correct.
if (newOwnerRole != null)
{
final int indexOwnerPhase = newOwnerRole.owner();
if (((indexOwnerPhase < 1 && !newOwnerRole.equals(RoleType.Neutral) && !newOwnerRole.equals(RoleType.Mover))
&& !newOwnerRole.equals(RoleType.Prev)
&& !newOwnerRole.equals(RoleType.Next)) || indexOwnerPhase > game.players().count())
{
game.addRequirementToReport(
"In (take Control ...) the RoleType used for the by: parameter is incorrect: " + newOwnerRole
+ ".");
missingRequirement = true;
}
}
missingRequirement |= super.missingRequirement(game);
missingRequirement |= newOwnerFn.missingRequirement(game);
missingRequirement |= ownerFn.missingRequirement(game);
if (region != null)
missingRequirement |= region.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= newOwnerFn.willCrash(game);
willCrash |= ownerFn.willCrash(game);
if (region != null)
willCrash |= region.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
newOwnerFn.preprocess(game);
ownerFn.preprocess(game);
if (region != null)
region.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "take control of the piece on " + type.name() + " " + region.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 10,146 | 29.289552 | 118 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/effect/take/simple/TakeDomino.java | package game.rules.play.moves.nonDecision.effect.take.simple;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.equipment.container.Container;
import game.equipment.container.other.Hand;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.action.move.ActionAdd;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Takes a domino from those remaining.
*
* @author Eric.Piette and cambolbro
*/
@Hide
public final class TakeDomino extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param then The moves applied after that move is applied.
*/
public TakeDomino
(
@Opt final Then then
)
{
super(then);
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final TIntArrayList remainingDominoes = context.state().remainingDominoes();
if (remainingDominoes.isEmpty())
return moves;
int site = Constants.OFF;
for (final Container container: context.containers())
{
if (container.isHand())
{
final Hand hand = (Hand) container;
final ContainerState cs = context.containerState(hand.index());
if (hand.owner() == context.state().mover())
{
final int pid = hand.index();
final int siteFrom = context.sitesFrom()[pid];
for (int siteHand = siteFrom; siteHand < siteFrom + hand.numSites(); siteHand++)
if (cs.whatCell(siteHand) == 0)
{
site = siteHand;
break;
}
break;
}
}
}
if (site == Constants.OFF)
return moves;
final int index = context.rng().nextInt(remainingDominoes.size());
final int what = remainingDominoes.getQuick(index);
final ActionAdd actionAdd = new ActionAdd(SiteType.Cell, site, what, 1, 0, Constants.UNDEFINED, Constants.UNDEFINED, null);
final Move move = new Move(actionAdd);
moves.moves().add(move);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = GameType.LargePiece | GameType.Dominoes | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.Domino.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasDominoes())
{
game.addRequirementToReport("The ludeme (take Domino ...) is used but the equipment has no dominoes.");
missingRequirement = true;
}
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
// Nothing todo
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "take a domino" + thenString;
}
//-------------------------------------------------------------------------
}
| 4,967 | 23.472906 | 125 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operator/Operator.java | package game.rules.play.moves.nonDecision.operator;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.NonDecision;
import game.rules.play.moves.nonDecision.effect.Then;
import other.context.Context;
/**
* Defines operations that combine lists of moves, then optionally perform some additional effects.
*
* @author Eric.Piette and cambolbro
*
* The input moves can be decision moves or effect moves (or both).
*/
public abstract class Operator extends NonDecision
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param then The effect of the moves.
*/
public Operator
(
final Then then
)
{
super(then);
}
//-------------------------------------------------------------------------
/**
* Trick ludeme into joining the grammar.
*/
@Override
public Moves eval(final Context context)
{
return null;
}
//-------------------------------------------------------------------------
}
| 1,032 | 21.456522 | 99 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operator/package-info.java | /**
* Move operators are functions that combine moves together,
* then optionally perform some additional effects.
* The input moves can be a mixture of decision moves and effect moves.
*/
package game.rules.play.moves.nonDecision.operator;
| 249 | 34.714286 | 73 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEach.java | package game.rules.play.moves.nonDecision.operators.foreach;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.IntFunction;
import game.functions.region.RegionFunction;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operators.foreach.die.ForEachDie;
import game.rules.play.moves.nonDecision.operators.foreach.direction.ForEachDirection;
import game.rules.play.moves.nonDecision.operators.foreach.group.ForEachGroup;
import game.rules.play.moves.nonDecision.operators.foreach.level.ForEachLevel;
import game.rules.play.moves.nonDecision.operators.foreach.piece.ForEachPiece;
import game.rules.play.moves.nonDecision.operators.foreach.player.ForEachPlayer;
import game.rules.play.moves.nonDecision.operators.foreach.site.ForEachSite;
import game.rules.play.moves.nonDecision.operators.foreach.team.ForEachTeam;
import game.rules.play.moves.nonDecision.operators.foreach.value.ForEachValue;
import game.rules.start.forEach.ForEachTeamType;
import game.types.board.SiteType;
import game.types.play.RoleType;
import game.util.directions.Direction;
import game.util.directions.StackDirection;
import game.util.moves.To;
import other.context.Context;
/**
* Iterates over a set of items.
*
* @author Eric.Piette
*
* Use this ludeme to iterate over a set of items such as pieces, players, directions or regions,
* and apply specified actions to each item.
*/
@SuppressWarnings("javadoc")
public final class ForEach extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For iterating through levels at a site.
*
* @param forEachType The type of property to iterate.
* @param type The type of the graph elements of the group [default SiteType of the board].
* @param site The site to iterate through.
* @param stackDirection The direction to count in the stack [FromTop].
* @param moves The moves.
* @param then The moves applied after that move is applied.
*
* @example (forEach Level (last To) (if (= (id "King" Mover) (what at:(last To) level:(level))) (addScore Mover 1)))
*/
public static Moves construct
(
final ForEachLevelType forEachType,
@Opt final SiteType type,
final IntFunction site,
@Opt final StackDirection stackDirection,
final Moves moves,
@Opt final Then then
)
{
return new ForEachLevel(type, site, stackDirection, moves, then);
}
//-------------------------------------------------------------------------
/**
* For iterating on teams.
*
* @param forEachType The type of property to iterate.
* @param moves The moves.
* @param then The moves applied after that move is applied.
*
* @example (forEach Team (forEach (team) (set Hidden What at:(site)
* to:Player)))
*/
public static Moves construct
(
final ForEachTeamType forEachType,
final Moves moves,
@Opt final Then then
)
{
return new ForEachTeam(moves, then);
}
//-------------------------------------------------------------------------
/**
* For iterating through the groups.
*
* @param forEachType The type of property to iterate.
* @param type The type of the graph elements of the group.
* @param directions The directions of the connection between elements in the
* group [Adjacent].
* @param If The condition on the pieces to include in the group.
* @param moves The moves to apply.
* @param then The moves applied after that move is applied.
*
* @example (forEach Group (addScore Mover (count Sites in:(sites))))
*/
public static Moves construct
(
final ForEachGroupType forEachType,
@Opt final SiteType type,
@Opt final Direction directions,
@Opt @Name final BooleanFunction If,
final Moves moves,
@Opt final Then then
)
{
switch (forEachType)
{
case Group:
return new ForEachGroup(type,directions, If, moves, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachGroupType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the dice.
*
* @param forEachType The type of property to iterate.
* @param handDiceIndex The index of the dice container [0].
* @param combined True if the combination is allowed [False].
* @param replayDouble True if double allows a second move [False].
* @param If The condition to satisfy to move [True].
* @param moves The moves to apply.
* @param then The moves applied after that move is applied.
*
* @example (forEach Die (if (= (pips) 5) (or (forEach Piece "Pawn") (forEach
* Piece "King_noCross")) (if (= (pips) 4) (forEach Piece "Elephant")
* (if (= (pips) 3) (forEach Piece "Knight") (if (= (pips) 2) (forEach
* Piece "Boat") ) ) ) ) )
*/
public static Moves construct
(
final ForEachDieType forEachType,
@Opt final IntFunction handDiceIndex,
@Opt @Name final BooleanFunction combined,
@Opt @Name final BooleanFunction replayDouble,
@Opt @Name final BooleanFunction If,
final Moves moves,
@Opt final Then then
)
{
switch (forEachType)
{
case Die:
return new ForEachDie(handDiceIndex, combined, replayDouble, If, moves, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachDieType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the directions.
*
* @param forEachType The type of property to iterate.
* @param from The origin of the movement [(from)].
* @param directions The directions of the move [Adjacent].
* @param between The data on the locations between the from location and
* the to location [(between (exact 1))].
* @param to The data on the location to move.
* @param moves The moves to applied on these directions.
* @param then The moves applied after that move is applied.
*
* @example (forEach Direction (from (to)) (directions {FR FL}) (to if:(or (is
* In (to) (sites Empty)) (is Enemy (who at:(to)))) (apply (fromTo
* (from) (to if:(or (is Empty (to)) (is Enemy (who at:(to)))) (apply
* (remove (to))) ) ) ) ) )
*/
public static Moves construct
(
final ForEachDirectionType forEachType,
@Opt final game.util.moves.From from,
@Opt final Direction directions,
@Opt final game.util.moves.Between between,
@Or final To to,
@Or final Moves moves,
@Opt final Then then
)
{
int numNonNull = 0;
if (to != null)
numNonNull++;
if (moves != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException(
"ForEach(): With ForEachDirectionType one to, moves parameter must be non-null.");
switch (forEachType)
{
case Direction:
return new ForEachDirection(from, directions, between, to, moves,then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachDirectionType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the sites of a region.
*
* @param forEachType The type of property to iterate.
* @param regionFn The region used.
* @param generator The move to apply.
* @param noMoveYet The moves to apply if the list of moves resulting from the
* generator is empty.
* @param then The moves applied after that move is applied.
*
* @example (forEach Site (intersection (sites Around (last To)) (sites Occupied
* by:Next) ) (and (remove (site)) (add (piece (id "Ball" Mover)) (to
* (site))) ) )
*/
public static Moves construct
(
final ForEachSiteType forEachType,
final RegionFunction regionFn,
final Moves generator,
@Opt @Name final Moves noMoveYet,
@Opt final Then then
)
{
switch (forEachType)
{
case Site:
return new ForEachSite(regionFn, generator, noMoveYet, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachSiteType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through values from an IntArrayFunction.
*
* @param forEachType The type of property to iterate.
* @param values The IntArrayFunction.
* @param generator The move to apply.
* @param then The moves applied after that move is applied.
*
* @example (forEach Value (values Remembered) (move (from) (to (trackSite Move
* steps:(value)))))
*/
public static Moves construct
(
final ForEachValueType forEachType,
final IntArrayFunction values,
final Moves generator,
@Opt final Then then
)
{
switch (forEachType)
{
case Value:
return new ForEachValue(values, generator, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachValueType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through values between two.
*
* @param forEachType The type of property to iterate.
* @param min The minimal value.
* @param max The maximal value.
* @param generator The move to apply.
* @param then The moves applied after that move is applied.
*
* @example (forEach Value min:1 max:5 (move (from) (to (trackSite Move
* steps:(value)))))
*/
public static Moves construct
(
final ForEachValueType forEachType,
@Name final IntFunction min,
@Name final IntFunction max,
final Moves generator,
@Opt final Then then
)
{
switch (forEachType)
{
case Value:
return new ForEachValue(min, max, generator, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachValueType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the pieces.
*
* @param forEachType The type of property to iterate.
* @param on Type of graph element.
* @param item The name of the piece.
* @param items The names of the pieces.
* @param container The index of the container.
* @param containerName The name of the container.
* @param specificMoves The specific moves to apply to the pieces.
* @param player The owner of the piece [(mover)].
* @param role The role type of the owner of the piece [Mover].
* @param top To apply the move only to the top piece in case of a
* stack [False].
* @param then The moves applied after that move is applied.
*
* @example (forEach Piece)
*
* @example (forEach Piece "Bear" (step (to if:(= (what at:(to)) (id "Seal1")))
* ) )
*/
public static Moves construct
(
final ForEachPieceType forEachType,
@Opt @Name final SiteType on,
@Opt @Or final String item,
@Opt @Or final String[] items,
@Opt @Name @Or2 final IntFunction container,
@Opt @Or2 final String containerName,
@Opt final Moves specificMoves,
@Opt @Or2 final game.util.moves.Player player,
@Opt @Or2 final RoleType role,
@Opt @Name final BooleanFunction top,
@Opt final Then then
)
{
int numNonNull = 0;
if (item != null)
numNonNull++;
if (items != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException(
"ForEach(): With ForEachPieceType zero or one item, items parameter must be non-null.");
int numNonNull2 = 0;
if (container != null)
numNonNull2++;
if (containerName != null)
numNonNull2++;
if (numNonNull2 > 1)
throw new IllegalArgumentException(
"ForEach(): With ForEachPieceType zero or one container, containerName parameter must be non-null.");
int numNonNull3 = 0;
if (player != null)
numNonNull3++;
if (role != null)
numNonNull3++;
if (numNonNull3 > 1)
throw new IllegalArgumentException(
"ForEach(): With ForEachPieceType zero or one player, role parameter must be non-null.");
switch (forEachType)
{
case Piece:
return new ForEachPiece(on, item, items, container, containerName, specificMoves, player, role, top,
then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachPieceType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the players.
*
* @param moves The moves.
* @param then The moves applied after that move is applied.
*
* @example (forEach Player (addScore (player (player)) 1))
*/
public static Moves construct
(
final ForEachPlayerType forEachType,
final Moves moves,
@Opt final Then then
)
{
switch (forEachType)
{
case Player:
return new ForEachPlayer(moves, then);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachPlayerType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the players in using an IntArrayFunction.
*
* @param players The list of players.
* @param moves The moves.
* @param then The moves applied after that move is applied.
*
* @example (forEach (players Ally of:(next)) (addScore (player (player)) 1))
*/
public static Moves construct
(
final IntArrayFunction players,
final Moves moves,
@Opt final Then then
)
{
return new ForEachPlayer(players, moves, then);
}
private ForEach()
{
super(null);
// Ensure that compiler does pick up default constructor
}
@Override
public Moves eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("ForEach.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return 0L;
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean canMoveTo(Context context, int target)
{
// Should never be there
throw new UnsupportedOperationException("ForEach.canMoveTo(): Should never be called directly.");
}
//-------------------------------------------------------------------------
}
| 16,190 | 30.998024 | 118 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachDieType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the die which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachDieType
{
/**
* To generate moves according to the values of the dice.
*/
Die,
}
| 273 | 17.266667 | 69 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachDirectionType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the direction which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachDirectionType
{
/**
* To apply a move for each site reached according to a direction.
*/
Direction,
}
| 300 | 19.066667 | 75 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachGroupType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the group which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachGroupType
{
/**
* To generate moves according to group.
*/
Group,
}
| 262 | 16.533333 | 71 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachLevelType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the level which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachLevelType
{
/**
* To iterate through the levels of a site, generating moves based on their
* positions.
*/
Level,
} | 311 | 19.8 | 76 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachPieceType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the piece which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachPieceType
{
/** To iterate through the pieces, generating moves based on their positions. */
Piece,
}
| 292 | 21.538462 | 81 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachPlayerType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the player which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachPlayerType
{
/**
* To iterate through the players to generate moves.
*/
Player,
}
| 277 | 17.533333 | 72 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachSiteType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the site which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachSiteType
{
/** To apply a move for each site in a region. */
Site,
}
| 258 | 18.923077 | 70 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/ForEachValueType.java | package game.rules.play.moves.nonDecision.operators.foreach;
/**
* Defines the values which can be iterated in the ForEach super ludeme.
*
* @author Eric.Piette
*/
public enum ForEachValueType
{
/** To apply a move for each value from one value to another (included). */
Value,
}
| 288 | 21.230769 | 76 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/package-info.java | /**
* Move generators are functions that iterate over playable sites and generate moves according to specified criteria.
*/
package game.rules.play.moves.nonDecision.operators.foreach;
| 187 | 36.6 | 117 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/die/ForEachDie.java | package game.rules.play.moves.nonDecision.operators.foreach.die;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.equipment.container.other.Dice;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import main.Constants;
import main.collections.FastArrayList;
import other.action.die.ActionUpdateDice;
import other.action.die.ActionUseDie;
import other.action.state.ActionSetTemp;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
/**
* Generates moves according to the values of the dice.
*
* @author Eric.Piette
*
* @remarks This ludeme is used in dice games, and works for any combination of dice.
*/
@Hide
public final class ForEachDie extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The index of the hand of dice. */
private final IntFunction handDiceIndexFn;
/** To combine dice. */
private final BooleanFunction combined;
/** If double rules (e.g. Backgammon). */
private final BooleanFunction replayDoubleFn;
/** The rule to respect. */
private final BooleanFunction rule;
/** The moves to apply */
private final Moves moves;
/**
* @param handDiceIndex The index of the dice container [0].
* @param combined True if the combination is allowed [False].
* @param replayDouble True if double allows a second move [False].
* @param If The condition to satisfy to move [True].
* @param moves The moves to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachDie
(
@Opt final IntFunction handDiceIndex,
@Opt @Name final BooleanFunction combined,
@Opt @Name final BooleanFunction replayDouble,
@Opt @Name final BooleanFunction If,
final Moves moves,
@Opt final Then then
)
{
super(then);
handDiceIndexFn = (handDiceIndex == null) ? new IntConstant(0) : handDiceIndex;
rule = (If == null) ? new BooleanConstant(true) : If;
this.combined = (combined == null) ? new BooleanConstant(false) : combined;
replayDoubleFn = (replayDouble == null) ? new BooleanConstant(false) : replayDouble;
this.moves = moves;
}
@Override
public Moves eval(final Context context)
{
final Moves returnMoves = new BaseMoves(super.then());
final int handDiceIndex = handDiceIndexFn.eval(context);
if (context.state().currentDice() == null)
return returnMoves;
final int[] dieValues = context.state().currentDice(handDiceIndex);
final int containerIndex = context.game().getHandDice(handDiceIndex).index();
boolean replayDouble = replayDoubleFn.eval(context);
if (replayDouble)
{
final int firstDieValue = dieValues[0];
for (int i = 0; i < dieValues.length; i++)
{
if (dieValues[i] != firstDieValue)
{
replayDouble = false;
break;
}
}
}
final int origDieValue = context.pipCount();
for (int i = 0 ; i < dieValues.length; i++)
{
final int pipCount = dieValues[i];
context.setPipCount(pipCount);
if (rule.eval(context))
{
final Moves computedMoves = moves.eval(context);
final FastArrayList<Move> moveList = computedMoves.moves();
final int site = context.sitesFrom()[containerIndex] + i;
final ActionUseDie action = new ActionUseDie(handDiceIndex, i, site);
for (final Move m : moveList)
m.actions().add(action);
// Double
if (replayDouble && context.state().temp() == Constants.UNDEFINED)
{
final ActionSetTemp setTemp = new ActionSetTemp(pipCount);
for (final Move m : moveList)
m.actions().add(setTemp);
}
else if (replayDouble)
{
final ActionSetTemp setTemp = new ActionSetTemp(Constants.UNDEFINED);
for (final Move m : moveList)
m.actions().add(setTemp);
}
else if (context.state().temp() != Constants.UNDEFINED)
{
for (final Dice dice : context.game().handDice())
{
if ((context.state().temp() - 1) < dice.getNumFaces())
for (int loc = context.sitesFrom()[dice.index()]; loc < context.sitesFrom()[dice.index()]
+ dice.numLocs(); loc++)
{
final ActionUpdateDice actionState = new ActionUpdateDice(loc,
context.state().temp() - 1);
for (final Move m : moveList)
m.actions().add(actionState);
}
}
}
returnMoves.moves().addAll(computedMoves.moves());
}
}
if (combined.eval(context))
{
if (dieValues.length == 2)
{
final int dieValue1 = dieValues[0];
final int dieValue2 = dieValues[1];
if (dieValue1 != 0 && dieValue2 != 0)
{
context.setPipCount(dieValue1 + dieValue2);
if (rule.eval(context))
{
final Moves computedMoves = moves.eval(context);
final FastArrayList<Move> moveList = computedMoves.moves();
final int siteFrom = context.sitesFrom()[containerIndex];
final ActionUseDie actionDie1 = new ActionUseDie(handDiceIndex, 0, siteFrom);
final ActionUseDie actionDie2 = new ActionUseDie(handDiceIndex, 1, siteFrom + 1);
for (final Move m : moveList)
{
m.actions().add(actionDie1);
m.actions().add(actionDie2);
}
returnMoves.moves().addAll(computedMoves.moves());
}
}
}
else if (dieValues.length == 3)
{
// Sum of the three dice
final int dieValue1 = dieValues[0];
final int dieValue2 = dieValues[1];
final int dieValue3 = dieValues[2];
if(dieValue1 != 0 && dieValue2 != 0 && dieValue3 != 0)
{
context.setPipCount(dieValue1 + dieValue2 + dieValue3);
if (rule.eval(context))
{
final Moves computedMoves = moves.eval(context);
final FastArrayList<Move> moveList = computedMoves.moves();
final int siteFrom = context.sitesFrom()[containerIndex];
final ActionUseDie actionDie1 = new ActionUseDie(handDiceIndex, 0, siteFrom);
final ActionUseDie actionDie2 = new ActionUseDie(handDiceIndex, 1, siteFrom +1);
final ActionUseDie actionDie3 = new ActionUseDie(handDiceIndex, 2, siteFrom +2);
for (final Move m : moveList)
{
m.actions().add(actionDie1);
m.actions().add(actionDie2);
m.actions().add(actionDie3);
}
returnMoves.moves().addAll(computedMoves.moves());
}
}
// Each combination of two dices
for (int i = 0; i < 2; i++)
{
for (int j = i + 1; j < 3; j++)
{
final int d1 = dieValues[i];
final int d2 = dieValues[j];
if (d1 != 0 && d2 != 0)
{
context.setPipCount(d1 + d2);
if (rule.eval(context))
{
final Moves computedMoves = moves.eval(context);
final FastArrayList<Move> moveList = computedMoves.moves();
final int siteFrom = context.sitesFrom()[containerIndex];
final ActionUseDie actionDie1 = new ActionUseDie(handDiceIndex, i, siteFrom +i);
final ActionUseDie actionDie2 = new ActionUseDie(handDiceIndex, j, siteFrom +j);
for (final Move m : moveList)
{
m.actions().add(actionDie1);
m.actions().add(actionDie2);
}
returnMoves.moves().addAll(computedMoves.moves());
}
}
}
}
}
// TO DO more than two dices
}
context.setPipCount(origDieValue);
if (then() != null)
for (int j = 0; j < returnMoves.moves().size(); j++)
returnMoves.moves().get(j).then().add(then().moves());
return returnMoves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = handDiceIndexFn.gameFlags(game) | super.gameFlags(game);
gameFlags |= rule.gameFlags(game);
gameFlags |= moves.gameFlags(game);
gameFlags |= replayDoubleFn.gameFlags(game);
gameFlags |= combined.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(handDiceIndexFn.concepts(game));
concepts.set(Concept.Dice.id(), true);
concepts.set(Concept.ByDieMove.id(), true);
concepts.or(rule.concepts(game));
concepts.or(moves.concepts(game));
concepts.or(replayDoubleFn.concepts(game));
concepts.or(combined.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(handDiceIndexFn.writesEvalContextRecursive());
writeEvalContext.or(rule.writesEvalContextRecursive());
writeEvalContext.or(moves.writesEvalContextRecursive());
writeEvalContext.or(replayDoubleFn.writesEvalContextRecursive());
writeEvalContext.or(combined.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.PipCount.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(handDiceIndexFn.readsEvalContextRecursive());
readEvalContext.or(rule.readsEvalContextRecursive());
readEvalContext.or(moves.readsEvalContextRecursive());
readEvalContext.or(replayDoubleFn.readsEvalContextRecursive());
readEvalContext.or(combined.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasHandDice())
{
game.addRequirementToReport("The ludeme (forEach Die ...) is used but the equipment has no dice.");
missingRequirement = true;
}
missingRequirement |= super.missingRequirement(game);
missingRequirement |= handDiceIndexFn.missingRequirement(game);
missingRequirement |= rule.missingRequirement(game);
missingRequirement |= moves.missingRequirement(game);
missingRequirement |= replayDoubleFn.missingRequirement(game);
missingRequirement |= combined.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= handDiceIndexFn.willCrash(game);
willCrash |= rule.willCrash(game);
willCrash |= moves.willCrash(game);
willCrash |= replayDoubleFn.willCrash(game);
willCrash |= combined.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return handDiceIndexFn.isStatic() && rule.isStatic() && moves.isStatic() && replayDoubleFn.isStatic()
&& combined.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
handDiceIndexFn.preprocess(game);
rule.preprocess(game);
moves.preprocess(game);
replayDoubleFn.gameFlags(game);
combined.gameFlags(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String englishString = "according to the value of the dice, " + moves.toEnglish(game);
if (combined.toEnglish(game).equals("true"))
englishString += ", the values for dice are combined";
if (replayDoubleFn.toEnglish(game).equals("true"))
englishString += ", the double rules apply";
return englishString;
}
//-------------------------------------------------------------------------
}
| 12,341 | 30.088161 | 103 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/direction/ForEachDirection.java | package game.rules.play.moves.nonDecision.operators.foreach.direction;
import java.util.BitSet;
import java.util.List;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.component.Component;
import game.functions.booleans.BooleanFunction;
import game.functions.directions.Directions;
import game.functions.directions.DirectionsFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.functions.ints.iterator.From;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.util.directions.AbsoluteDirection;
import game.util.directions.DirectionFacing;
import game.util.graph.Radial;
import game.util.graph.Step;
import game.util.moves.To;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
import other.move.MoveUtilities;
import other.topology.Topology;
import other.topology.TopologyElement;
/**
* Applies a move for each site reached according to a direction.
*
* @author Eric.Piette
*
* @remarks In case of different directions and conditions to follow before
* applying a move (e.g. Xiangqi).
*/
@Hide
public final class ForEachDirection extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Location of the piece. */
private final IntFunction startLocationFn;
/** Min limit of the move. */
private final IntFunction min;
/** Limit to apply the moves. */
private final IntFunction limit;
/** Direction chosen. */
private final DirectionsFunction dirnChoice;
/** The rule to respect on the location to go. */
private final BooleanFunction rule;
/** The rule to respect on the sites to cross. */
private final BooleanFunction betweenRule;
/** Moves to apply from each direction respecting the direction */
private final Moves movesToApply;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param from Description of the ``from'' location [(from)].
* @param directions The directions of the move [Adjacent].
* @param between Description of location(s) between ``from'' and ``to''
* [(between (exact 1))].
* @param to Description of the ``to'' location.
* @param moves Description of the decision moves to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachDirection
(
@Opt final game.util.moves.From from,
@Opt final game.util.directions.Direction directions,
@Opt final game.util.moves.Between between,
@Or final To to,
@Or final Moves moves,
@Opt final Then then
)
{
super(then);
// From
startLocationFn = (from == null) ? new From(null) : from.loc();
type = (from == null) ? null : from.type();
// Directions
dirnChoice = (directions != null) ? directions.directionsFunctions()
: new Directions(AbsoluteDirection.Adjacent, null);
// Between
limit = (between == null || between.range() == null) ? new IntConstant(1) : between.range().maxFn();
min = (between == null || between.range() == null) ? new IntConstant(1) : between.range().minFn();
betweenRule = (between != null) ? between.condition() : null;
// To
rule = (to != null) ? to.cond() : null;
movesToApply = (to != null && to.effect() != null) ? to.effect().effect() : moves;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int from = startLocationFn.eval(context);
if (from <= Constants.OFF)
return moves;
final Topology graph = context.topology();
final SiteType realType = (type != null) ? type : context.game().board().defaultSite();
if (from >= graph.getGraphElements(realType).size())
return moves;
final List<DirectionFacing> directionsSupported = graph.supportedDirections(realType);
final int minPathLength = min.eval(context);
final int maxPathLength = limit.eval(context);
final int contextFrom = context.from();
final int contextTo = context.to();
final Component component = context.components()
[context.containerState(context.containerId()[contextFrom]).what(contextFrom, type)];
if (component == null)
return moves;
DirectionFacing newDirection = null;
if (contextTo != Constants.UNDEFINED)
{
for (final DirectionFacing direction : directionsSupported)
{
final AbsoluteDirection absoluteDirection = direction.toAbsolute();
final List<game.util.graph.Step> steps = graph.trajectories().steps(realType, context.from(), realType,
absoluteDirection);
for (final Step step : steps)
{
if (step.to().id() == contextTo)
{
newDirection = direction;
break;
}
}
}
}
final int origFrom = context.from();
final int origBetween = context.between();
final int origTo = context.to();
final TopologyElement fromV = graph.getGraphElements(realType).get(from);
final List<AbsoluteDirection> directions = dirnChoice.convertToAbsolute(realType, fromV, component,
newDirection, null, context);
for (final AbsoluteDirection direction : directions)
{
final List<Radial> radials = graph.trajectories().radials(type, fromV.index(), direction);
for (final Radial radial : radials)
{
for (int toIdx = 1; toIdx < radial.steps().length && toIdx <= maxPathLength; toIdx++)
{
final int to = radial.steps()[toIdx].id();
// Check the middle rule
if (betweenRule != null && minPathLength > 1 && toIdx < minPathLength)
{
context.setBetween(to);
if (!betweenRule.eval(context))
break;
context.setBetween(origBetween);
}
context.setTo(to);
if (rule == null || rule.eval(context))
{
if (toIdx >= minPathLength)
{
final Moves movesApplied = movesToApply.eval(context);
for (final Move m : movesApplied.moves())
{
final int saveFrom = context.from();
final int saveTo = context.to();
context.setFrom(to);
context.setTo(Constants.OFF);
MoveUtilities.chainRuleCrossProduct(context, moves, null, m, false);
context.setTo(saveTo);
context.setFrom(saveFrom);
}
}
}
else
{
break;
}
}
}
}
context.setTo(origTo);
context.setBetween(origBetween);
context.setFrom(origFrom);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
gameFlags |= movesToApply.gameFlags(game);
if (startLocationFn != null)
gameFlags |= startLocationFn.gameFlags(game);
if (min != null)
gameFlags |= min.gameFlags(game);
if (limit != null)
gameFlags |= limit.gameFlags(game);
if (rule != null)
gameFlags |= rule.gameFlags(game);
if (betweenRule != null)
gameFlags |= betweenRule.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(SiteType.concepts(type));
concepts.or(movesToApply.concepts(game));
if (startLocationFn != null)
concepts.or(startLocationFn.concepts(game));
if (min != null)
concepts.or(min.concepts(game));
if (limit != null)
concepts.or(limit.concepts(game));
if (rule != null)
concepts.or(rule.concepts(game));
if (betweenRule != null)
concepts.or(betweenRule.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
if (dirnChoice != null)
concepts.or(dirnChoice.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(movesToApply.writesEvalContextRecursive());
if (startLocationFn != null)
writeEvalContext.or(startLocationFn.writesEvalContextRecursive());
if (min != null)
writeEvalContext.or(min.writesEvalContextRecursive());
if (limit != null)
writeEvalContext.or(limit.writesEvalContextRecursive());
if (rule != null)
writeEvalContext.or(rule.writesEvalContextRecursive());
if (betweenRule != null)
writeEvalContext.or(betweenRule.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
if (dirnChoice != null)
writeEvalContext.or(dirnChoice.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.To.id(), true);
writeEvalContext.set(EvalContextData.From.id(), true);
writeEvalContext.set(EvalContextData.Between.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(movesToApply.readsEvalContextRecursive());
if (startLocationFn != null)
readEvalContext.or(startLocationFn.readsEvalContextRecursive());
if (min != null)
readEvalContext.or(min.readsEvalContextRecursive());
if (limit != null)
readEvalContext.or(limit.readsEvalContextRecursive());
if (rule != null)
readEvalContext.or(rule.readsEvalContextRecursive());
if (betweenRule != null)
readEvalContext.or(betweenRule.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
if (dirnChoice != null)
readEvalContext.or(dirnChoice.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= movesToApply.missingRequirement(game);
if (startLocationFn != null)
missingRequirement |= startLocationFn.missingRequirement(game);
if (min != null)
missingRequirement |= min.missingRequirement(game);
if (limit != null)
missingRequirement |= limit.missingRequirement(game);
if (rule != null)
missingRequirement |= rule.missingRequirement(game);
if (betweenRule != null)
missingRequirement |= betweenRule.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= movesToApply.willCrash(game);
if (startLocationFn != null)
willCrash |= startLocationFn.willCrash(game);
if (min != null)
willCrash |= min.willCrash(game);
if (limit != null)
willCrash |= limit.willCrash(game);
if (rule != null)
willCrash |= rule.willCrash(game);
if (betweenRule != null)
willCrash |= betweenRule.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
if (startLocationFn != null && !startLocationFn.isStatic())
return false;
if (rule != null && !rule.isStatic())
return false;
if (betweenRule != null && !betweenRule.isStatic())
return false;
return movesToApply.isStatic();
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
super.preprocess(game);
if (rule != null)
rule.preprocess(game);
if (betweenRule != null)
betweenRule.preprocess(game);
movesToApply.preprocess(game);
if (startLocationFn != null)
startLocationFn.preprocess(game);
if (min != null)
min.preprocess(game);
if (limit != null)
limit.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return movesToApply.toEnglish(game) + ", for all sites that can be reached from " + type.name().toLowerCase() + " " + startLocationFn.toEnglish(game) + " in the direction " + dirnChoice.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 12,988 | 26.460888 | 204 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/group/ForEachGroup.java | package game.rules.play.moves.nonDecision.operators.foreach.group;
import java.util.BitSet;
import java.util.List;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.directions.Directions;
import game.functions.directions.DirectionsFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.util.directions.AbsoluteDirection;
import game.util.directions.Direction;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
import other.move.MoveUtilities;
import other.state.container.ContainerState;
import other.topology.Topology;
import other.topology.TopologyElement;
/**
* Applies a move for each group.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachGroup extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The condition */
private final BooleanFunction condition;
/** Direction chosen. */
private final DirectionsFunction dirnChoice;
/** Moves to apply from each direction respecting the direction */
private final Moves movesToApply;
/** The graph element type. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The type of the graph elements of the group.
* @param directions The directions of the connection between elements in the
* group [Adjacent].
* @param If The condition on the pieces to include in the group.
* @param moves The moves to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachGroup
(
@Opt final SiteType type,
@Opt final Direction directions,
@Opt @Name final BooleanFunction If,
final Moves moves,
@Opt final Then then
)
{
super(then);
movesToApply = moves;
this.type = type;
condition = If;
dirnChoice = (directions != null) ? directions.directionsFunctions()
: new Directions(AbsoluteDirection.Adjacent, null);
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final Topology topology = context.topology();
final int maxIndexElement = context.topology().getGraphElements(type).size();
final ContainerState cs = context.containerState(0);
final int origFrom = context.from();
final int origTo = context.to();
final Region origRegion = context.region();
final int who = context.state().mover();
// We get the minimum set of sites to look.
final TIntArrayList sitesToCheck = new TIntArrayList();
if (condition != null)
{
for (int i = 0; i <= context.game().players().size(); i++)
{
final TIntArrayList allSites = context.state().owned().sites(i);
for (int j = 0; j < allSites.size(); j++)
{
final int site = allSites.get(j);
if (site < maxIndexElement)
sitesToCheck.add(site);
}
}
}
else
{
for (int j = 0; j < context.state().owned().sites(who).size(); j++)
{
final int site = context.state().owned().sites(who).get(j);
if (site < maxIndexElement)
sitesToCheck.add(site);
}
}
// We get each group.
final TIntArrayList sitesChecked = new TIntArrayList();
for (int k = 0; k < sitesToCheck.size(); k++)
{
final int from = sitesToCheck.get(k);
if (sitesChecked.contains(from))
continue;
final TIntArrayList groupSites = new TIntArrayList();
context.setFrom(from);
context.setTo(from);
if ((who == cs.who(from, type) && condition == null) || (condition != null && condition.eval(context)))
groupSites.add(from);
if (groupSites.size() > 0)
{
context.setFrom(from);
final TIntArrayList sitesExplored = new TIntArrayList();
int i = 0;
while (sitesExplored.size() != groupSites.size())
{
final int site = groupSites.get(i);
final TopologyElement siteElement = topology.getGraphElements(type).get(site);
final List<AbsoluteDirection> directions = dirnChoice.convertToAbsolute(type, siteElement, null,
null, null, context);
for (final AbsoluteDirection direction : directions)
{
final List<game.util.graph.Step> steps = topology.trajectories().steps(type,
siteElement.index(), type, direction);
for (final game.util.graph.Step step : steps)
{
final int to = step.to().id();
// If we already have it we continue to look the others.
if (groupSites.contains(to))
continue;
context.setTo(to);
if ((condition == null && who == cs.who(to, type)
|| (condition != null && condition.eval(context))))
groupSites.add(to);
}
}
sitesExplored.add(site);
i++;
}
context.setRegion(new Region(groupSites.toArray()));
final Moves movesApplied = movesToApply.eval(context);
for (final Move m : movesApplied.moves())
{
final int saveFrom = context.from();
final int saveTo = context.to();
context.setFrom(Constants.OFF);
context.setTo(Constants.OFF);
MoveUtilities.chainRuleCrossProduct(context, moves, null, m, false);
context.setTo(saveTo);
context.setFrom(saveFrom);
}
sitesChecked.addAll(groupSites);
}
}
context.setTo(origTo);
context.setFrom(origFrom);
context.setRegion(origRegion);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (then() != null)
gameFlags |= then().gameFlags(game);
gameFlags |= movesToApply.gameFlags(game);
if (condition != null)
gameFlags |= condition.gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.set(Concept.Group.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
concepts.or(movesToApply.concepts(game));
if (condition != null)
concepts.or(condition.concepts(game));
if (dirnChoice != null)
concepts.or(dirnChoice.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
writeEvalContext.or(movesToApply.writesEvalContextRecursive());
if (condition != null)
writeEvalContext.or(condition.writesEvalContextRecursive());
if (dirnChoice != null)
writeEvalContext.or(dirnChoice.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.To.id(), true);
writeEvalContext.set(EvalContextData.From.id(), true);
writeEvalContext.set(EvalContextData.Region.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
readEvalContext.or(movesToApply.readsEvalContextRecursive());
if (condition != null)
readEvalContext.or(condition.readsEvalContextRecursive());
if (dirnChoice != null)
readEvalContext.or(dirnChoice.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (then() != null)
missingRequirement |= then().missingRequirement(game);
missingRequirement |= movesToApply.missingRequirement(game);
if (condition != null)
missingRequirement |= condition.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (then() != null)
willCrash |= then().willCrash(game);
willCrash |= movesToApply.willCrash(game);
if (condition != null)
willCrash |= condition.willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
movesToApply.preprocess(game);
type = SiteType.use(type, game);
if (condition != null)
condition.preprocess(game);
}
//------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "for all groups on a " + type.name() + " if " + condition + " (" + dirnChoice.toEnglish(game) + ") " + movesToApply.toEnglish(game) + thenString;
}
//--------------------------------------------------------------------------
}
| 9,561 | 26.011299 | 154 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/level/ForEachLevel.java | package game.rules.play.moves.nonDecision.operators.foreach.level;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.types.board.SiteType;
import game.util.directions.StackDirection;
import main.collections.FastArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
import other.state.container.ContainerState;
/**
* Applies a move for each level of a site.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachLevel extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Location of the piece. */
private final IntFunction siteFn;
/** The moves to apply. */
private final Moves generator;
/** The siteType to look */
private SiteType type;
/** To start from the bottom or the top of the stack. */
private final StackDirection stackDirection;
/**
* @param type The type of the graph elements of the group [default SiteType of the board].
* @param siteFn The site to iterate through.
* @param stackDirection The direction to count in the stack [FromTop].
* @param generator The move to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachLevel
(
@Opt final SiteType type,
final IntFunction siteFn,
@Opt final StackDirection stackDirection,
final Moves generator,
@Opt final Then then
)
{
super(then);
this.siteFn = siteFn;
this.generator = generator;
this.type = type;
this.stackDirection = (stackDirection == null) ? StackDirection.FromTop : stackDirection;
}
@Override
public Moves eval(final Context context)
{
final int site = siteFn.eval(context);
final Moves moves = new BaseMoves(super.then());
final int savedTo = context.to();
final int originSiteValue = context.site();
final int cid = site >= context.containerId().length ? 0 : context.containerId()[site];
SiteType realType = type;
if (cid > 0)
realType = SiteType.Cell;
else if (realType == null)
realType = context.board().defaultSite();
final ContainerState cs = context.state().containerStates()[cid];
final int stackSize = cs.sizeStack(site, realType);
if(stackDirection.equals(StackDirection.FromBottom))
{
for (int level = 0; level < stackSize; level++)
{
context.setLevel(level);
final FastArrayList<Move> generatedMoves = generator.eval(context).moves();
moves.moves().addAll(generatedMoves);
}
}
else
{
for (int level = stackSize-1; level >= 0; level--)
{
context.setLevel(level);
final FastArrayList<Move> generatedMoves = generator.eval(context).moves();
moves.moves().addAll(generatedMoves);
}
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
context.setTo(savedTo);
context.setSite(originSiteValue);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = siteFn.gameFlags(game) | generator.gameFlags(game) | super.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(super.concepts(game));
concepts.or(siteFn.concepts(game));
concepts.or(generator.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(siteFn.writesEvalContextRecursive());
writeEvalContext.or(generator.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.Level.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(siteFn.readsEvalContextRecursive());
readEvalContext.or(generator.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= siteFn.missingRequirement(game);
missingRequirement |= generator.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= siteFn.willCrash(game);
willCrash |= generator.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
if (type == null)
type = game.board().defaultSite();
super.preprocess(game);
siteFn.preprocess(game);
generator.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return "for each level at " + ((type == null) ? game.board().defaultSite().name() : type.name()) + " " + siteFn.toEnglish(game) + " (" + stackDirection.name() + ") " + generator.toEnglish(game) + thenString;
}
//-------------------------------------------------------------------------
}
| 6,453 | 26.117647 | 210 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/piece/ForEachPiece.java | package game.rules.play.moves.nonDecision.operators.foreach.piece;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiPredicate;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.equipment.component.Component;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.state.Mover;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import game.types.board.SiteType;
import game.types.play.RoleType;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import other.ContainerId;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.location.Location;
import other.move.Move;
import other.move.MovesIterator;
import other.state.State;
import other.state.container.ContainerState;
import other.state.owned.Owned;
import other.state.stacking.BaseContainerStateStacking;
/**
* Iterates through the pieces, generating moves based on their positions.
*
* @author mrraow and cambolbro and Eric.Piette
*
* @remarks To generate a set of legal moves by type of piece. If some specific
* moves are described in this ludeme, they are applied to all the
* pieces described on that ludeme, if not the moves of each component
* are used.
*/
@Hide
public final class ForEachPiece extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* Useful only if we need a special move by piece and the generator on the
* piece.
*/
protected final Moves specificMoves;
/** Useful only to specify the moves only for a specific item. */
protected final String[] items;
/** To apply ByPiece on this player. */
protected final IntFunction player;
/** Which container. */
protected final ContainerId containerId;
/** If true only the piece in the top of the stack. */
protected final BooleanFunction topFn;
/** The value set by the description before the null check. */
protected final BooleanFunction topValueSet;
/** Cell/Edge/Vertex. */
protected SiteType type;
/** The roleType of the owner of the pieces. */
protected RoleType role;
/**
* For every player, a small array with component indices relevant for that
* player. This is precomputed in the preprocess() step.
*/
protected int[][] compIndicesPerPlayer;
//-------------------------------------------------------------------------
/**
* @param on Type of graph element where the pieces are.
* @param item The name of the piece.
* @param items The names of the pieces.
* @param container The index of the container.
* @param containerName The name of the container.
* @param specificMoves The specific moves to apply to the pieces.
* @param player The owner of the piece [(player (mover))].
* @param role RoleType of the owner of the piece [Mover].
* @param top To apply the move only to the top piece in case of a
* stack [False].
* @param then The moves applied after that move is applied.
*/
public ForEachPiece
(
@Opt @Name final SiteType on,
@Opt @Or final String item,
@Opt @Or final String[] items,
@Opt @Or2 @Name final IntFunction container,
@Opt @Or2 final String containerName,
@Opt final Moves specificMoves,
@Opt @Or2 final game.util.moves.Player player,
@Opt @Or2 final RoleType role,
@Opt @Name final BooleanFunction top,
@Opt final Then then
)
{
super(then);
if (items != null)
this.items = items;
else
this.items = (item == null) ? new String[0] : new String[]{ item };
this.specificMoves = specificMoves;
this.player = (player == null) ? ((role == null) ? new Mover() : RoleType.toIntFunction(role)) : player.index();
containerId = new ContainerId(container, containerName, null, null, null);
topValueSet= top;
topFn = (top == null) ? new BooleanConstant(false) : top;
type = on;
this.role = role;
}
//-------------------------------------------------------------------------
@Override
public MovesIterator movesIterator(final Context context)
{
return new MovesIterator()
{
// A bunch of things we only need to compute once for our iterator
private final int specificPlayer = player.eval(context);
private final Owned owned = context.state().owned();
private final List<? extends Location>[] ownedComponents = owned.positions(specificPlayer);
private final Component[] components = context.components();
private final int cont = containerId.eval(context);
private final ContainerState cs = context.containerState(cont);
private final SiteType realType = (type != null) ? type : context.game().board().defaultSite();
private final int minIndex = cs == null ? 0 : context.game().equipment().sitesFrom()[cont];
private final int maxIndex = cs == null ? 0 : minIndex + ((cont != 0) ? context.containers()[cont].numSites()
: context.topology().getGraphElements(realType).size());
private final boolean top = topFn.eval(context);
private final int[] moverCompIndices = compIndicesPerPlayer[specificPlayer];
// Keep track of where we are with our iterator
private int compIdx = 0; // Index for components list
private int locIdx = 0; // Index for list of locations
private int moveIdx = 0; // Index for list of moves generated by a specific piece
private Moves pieceMoves = null; // List of moves generated by a specific piece
// Compute our first move to return
private Move nextMove = context.trial().over() ? null : computeNextMove();
@Override
public boolean hasNext()
{
return (nextMove != null);
}
@Override
public Move next()
{
final Move ret = nextMove;
if (then() != null)
ret.then().add(then().moves());
nextMove = computeNextMove();
ret.setMover(context.state().mover());
return ret;
}
/**
* Computes the move to return for the subsequent next() call
*
* @return
*/
private Move computeNextMove()
{
if(cs == null)
return null;
while (true)
{
if (pieceMoves != null)
{
// We may still have more moves to return in list already generated
if (moveIdx < pieceMoves.moves().size())
{
return pieceMoves.moves().get(moveIdx++);
}
else
{
pieceMoves = null; // Time to move on to next piece
// moveIdx = 0;
// locIdx = 0;
// ++compIdx;
}
}
else
{
if (compIdx < moverCompIndices.length)
{
final int componentId = moverCompIndices[compIdx];
List<? extends Location> positions = null;
if (role != RoleType.All && role != RoleType.Each)
{
positions = ownedComponents[owned.mapCompIndex(specificPlayer, componentId)];
}
else
{
final int ownerComponent = context.components()[componentId].owner();
final List<? extends Location>[] ownedCurrentComponent = owned
.positions(ownerComponent);
positions = ownedCurrentComponent[owned.mapCompIndex(ownerComponent, componentId)];
}
// If the ludeme asks for a specific site type we keep only the correct ones.
List<Location> filteredPositions = null;
if (type != null && !positions.isEmpty())
{
filteredPositions = new ArrayList<Location>();
for (int i = positions.size() - 1; i >= 0; i--)
{
final Location loc = positions.get(i);
if (loc.siteType().equals(type))
filteredPositions.add(loc);
}
}
final List<? extends Location> correctPositions = (filteredPositions == null) ? positions
: filteredPositions;
if (correctPositions != null && !correctPositions.isEmpty())
{
final Component component = components[componentId];
if (locIdx < correctPositions.size())
{
final int location = correctPositions.get(locIdx).site();
if (location >= minIndex && location < maxIndex)
{
final int level = correctPositions.get(locIdx).level();
// We're incrementing locIdx, so reset moveIdx
moveIdx = 0;
++locIdx;
if (top)
{
final BaseContainerStateStacking css = (BaseContainerStateStacking) cs;
if (css.sizeStack(location, realType) != (level + 1))
continue;
}
final int origFrom = context.from();
final int origLevel = context.level();
final State state = context.state();
context.setFrom(location);
context.setLevel(level);
if (specificMoves == null)
{
// If the specific player is the mover or shared we compute directly the
// pieces movement.
if (specificPlayer == state.mover()
|| specificPlayer > context.game().players().count()
|| specificPlayer == 0)
{
pieceMoves = component.generate(context);
}
else
{
// We have to modify the context in case that the specific player is
// Next.
final int oldPrev = state.prev();
final int oldMover = state.mover();
final int oldNext = state.next();
state.setPrev(oldMover);
state.setMover(specificPlayer);
state.setNext(oldMover);
pieceMoves = component.generate(context);
state.setPrev(oldPrev);
state.setMover(oldMover);
state.setNext(oldNext);
// Old code: commented because making full copy of context
// seems unnecessary and slow
//
// final Context newContext = new TempContext(context);
// newContext.state().setPrev(context.state().mover());
// newContext.state().setMover(specificPlayer);
// newContext.state().setNext(context.state().mover());
// pieceMoves = component.generate(newContext);
}
}
else
{
// If the specific player is the mover or shared we compute directly the
// pieces movement.
if (specificPlayer == state.mover()
|| specificPlayer > context.game().players().count()
|| specificPlayer == 0)
{
pieceMoves = specificMoves.eval(context);
}
else
{
// We have to modify the context in case that the specific player is
// Next.
final int oldPrev = state.prev();
final int oldMover = state.mover();
final int oldNext = state.next();
state.setPrev(oldMover);
state.setMover(specificPlayer);
state.setNext(oldMover);
pieceMoves = specificMoves.eval(context);
state.setPrev(oldPrev);
state.setMover(oldMover);
state.setNext(oldNext);
// Old code: commented because making full copy of context
// seems unnecessary and slow
//
// final Context newContext = new Context(context);
// newContext.state().setPrev(context.state().mover());
// newContext.state().setMover(specificPlayer);
// newContext.state().setNext(context.state().mover());
// pieceMoves = specificMoves.eval(newContext);
}
}
context.setFrom(origFrom);
context.setLevel(origLevel);
}
else
{
// We're incrementing locIdx, so reset moveIdx
moveIdx = 0;
++locIdx;
}
}
else
{
// We're incrementing index for list of comp indices, so reset locIdx and
// moveIdx
moveIdx = 0;
locIdx = 0;
++compIdx;
}
}
else
{
// We're incrementing index for list of comp indices, so reset locIdx and
// moveIdx
moveIdx = 0;
locIdx = 0;
++compIdx;
}
}
else
{
// No more moves to be found
return null;
}
}
}
}
@Override
public boolean canMoveConditionally(final BiPredicate<Context, Move> predicate)
{
while (nextMove != null)
{
if (then() != null)
nextMove.then().add(then().moves());
nextMove.setMover(specificPlayer);
if (predicate.test(context, nextMove))
return true;
else
nextMove = computeNextMove();
}
return false;
}
};
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Iterator<Move> it = movesIterator(context);
final Moves moves = new BaseMoves(super.then());
while (it.hasNext())
moves.moves().add(it.next());
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = player.gameFlags(game) | super.gameFlags(game);
gameFlags |= topFn.gameFlags(game);
if(topValueSet != null)
gameFlags |= GameType.Stacking;
if (type != null)
gameFlags |= SiteType.gameFlags(type);
if (then() != null)
gameFlags |= then().gameFlags(game);
if (specificMoves != null)
{
gameFlags |= specificMoves.gameFlags(game);
}
else
{
final Component[] components = game.equipment().components();
for (int e = 1; e < components.length; ++e)
{
final Moves generator = components[e].generator();
if (generator != null)
{
gameFlags |= generator.gameFlags(game);
}
}
}
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (type != null)
concepts.or(SiteType.concepts(type));
concepts.or(player.concepts(game));
concepts.or(topFn.concepts(game));
concepts.set(Concept.ForEachPiece.id(), true);
if (then() != null)
concepts.or(then().concepts(game));
if (specificMoves != null)
{
concepts.or(specificMoves.concepts(game));
}
else
{
final Component[] components = game.equipment().components();
for (int e = 1; e < components.length; ++e)
{
final Moves generator = components[e].generator();
if (generator != null)
{
concepts.or(generator.concepts(game));
}
}
}
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(player.writesEvalContextRecursive());
writeEvalContext.or(topFn.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
if (specificMoves != null)
writeEvalContext.or(specificMoves.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.From.id(), true);
writeEvalContext.set(EvalContextData.Level.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(player.readsEvalContextRecursive());
readEvalContext.or(topFn.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
if (specificMoves != null)
readEvalContext.or(specificMoves.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= player.missingRequirement(game);
missingRequirement |= topFn.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
if (specificMoves != null)
{
missingRequirement |= specificMoves.missingRequirement(game);
}
else
{
final Component[] components = game.equipment().components();
for (int e = 1; e < components.length; ++e)
{
final Moves generator = components[e].generator();
if (generator != null)
{
missingRequirement |= generator.missingRequirement(game);
}
}
}
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= player.willCrash(game);
willCrash |= topFn.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
if (specificMoves != null)
{
willCrash |= specificMoves.willCrash(game);
}
else
{
final Component[] components = game.equipment().components();
for (int e = 1; e < components.length; ++e)
{
final Moves generator = components[e].generator();
if (generator != null)
{
willCrash |= generator.willCrash(game);
}
}
}
return willCrash;
}
@Override
public boolean isStatic()
{
boolean isStatic = player.isStatic();
isStatic = isStatic && topFn.isStatic();
if (specificMoves != null)
return isStatic && specificMoves.isStatic();
else
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
if (specificMoves != null)
specificMoves.preprocess(game);
final Component[] comps = game.equipment().components();
for (int e = 1; e < comps.length; ++e)
{
final Component comp = comps[e];
if (comp.generator() != null)
{
comp.generator().preprocess(game);
}
}
final boolean allPlayers = (role == RoleType.All || role == RoleType.Each);
compIndicesPerPlayer = new int[game.players().size() + 1][];
for (int p = 0; p <= game.players().size(); ++p)
{
final TIntArrayList compIndices = new TIntArrayList();
for (int e = 1; e < comps.length; ++e)
{
final Component comp = comps[e];
if (comp.owner() == p || (allPlayers && p == game.players().size()))
{
if (items.length == 0)
{
compIndices.add(e);
}
else
{
for (final String item : items)
{
if (comp.getNameWithoutNumber() != null && comp.getNameWithoutNumber().equals(item))
{
compIndices.add(e);
break;
}
}
}
}
}
compIndicesPerPlayer[p] = compIndices.toArray();
}
}
//-------------------------------------------------------------------------
/**
* @return Specific moves for the piece
*/
public Moves specificMoves()
{
return specificMoves;
}
/**
* @return The kind of items.
*/
public String[] items()
{
return items;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "move one of your pieces"; //"During your turn, move one of your pieces";
}
}
| 19,766 | 27.814869 | 114 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/player/ForEachPlayer.java | package game.rules.play.moves.nonDecision.operators.foreach.player;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.functions.intArray.IntArrayFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import main.collections.FastArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
/**
* Iterates through the players, generating moves based on the indices of the
* players.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachPlayer extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The moves to generate according to the players. */
private final Moves moves;
/** The list of players. */
private final IntArrayFunction playersFn;
//-------------------------------------------------------------------------
/**
* @param moves The moves.
* @param then The moves applied after that move is applied.
*/
public ForEachPlayer
(
final Moves moves,
@Opt final Then then
)
{
super(then);
this.moves = moves;
playersFn = null;
}
/**
* @param players The list of players.
* @param moves The moves.
* @param then The moves applied after that move is applied.
*/
public ForEachPlayer
(
final IntArrayFunction players,
final Moves moves,
@Opt final Then then
)
{
super(then);
playersFn = players;
this.moves = moves;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves movesToReturn = new BaseMoves(super.then());
final int savedPlayer = context.player();
if (playersFn == null)
{
for (int pid = 1; pid < context.game().players().size(); pid++)
{
context.setPlayer(pid);
final FastArrayList<Move> generatedMoves = moves.eval(context).moves();
movesToReturn.moves().addAll(generatedMoves);
}
}
else
{
final int[] players = playersFn.eval(context);
for (int i = 0 ; i < players.length ;i++)
{
final int pid = players[i];
if (pid < 0 || pid > context.game().players().size())
continue;
context.setPlayer(pid);
final FastArrayList<Move> generatedMoves = moves.eval(context).moves();
movesToReturn.moves().addAll(generatedMoves);
}
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
movesToReturn.moves().get(j).then().add(then().moves());
context.setPlayer(savedPlayer);
return movesToReturn;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = moves.gameFlags(game) | super.gameFlags(game);
if (playersFn != null)
gameFlags |= playersFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
if (playersFn != null)
concepts.or(playersFn.concepts(game));
concepts.or(moves.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
if (playersFn != null)
writeEvalContext.or(playersFn.writesEvalContextRecursive());
writeEvalContext.or(moves.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.Player.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
if (playersFn != null)
readEvalContext.or(playersFn.readsEvalContextRecursive());
readEvalContext.or(moves.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
if (playersFn != null)
missingRequirement |= (playersFn.missingRequirement(game));
missingRequirement |= moves.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
if (playersFn != null)
willCrash |= (playersFn.willCrash(game));
willCrash |= moves.willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
moves.preprocess(game);
if (playersFn != null)
playersFn.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String playerString = "for all players";
if (playersFn != null)
playerString = "for each player in " + playersFn.toEnglish(game);
return playerString + " " + moves.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 5,878 | 22.516 | 77 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/site/ForEachSite.java | package game.rules.play.moves.nonDecision.operators.foreach.site;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.region.RegionFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import game.util.equipment.Region;
import main.collections.FastArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
/**
* Applies a move for each site in a region.
*
* @author mrraow and cambolbro and Eric.Piette
*
* @remarks Useful when a move has to be applied to all sites of a region
* according to some conditions.
*/
@Hide
public final class ForEachSite extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Location of the piece. */
private final RegionFunction regionFn;
/** The moves to apply. */
private final Moves generator;
/**
* The moves to apply if the list of moves resulting from the generator is
* empty.
*/
private final Moves elseMoves;
/**
* @param regionFn The region used.
* @param generator The move to apply.
* @param noMoveYet The moves to apply if the list of moves resulting from the
* generator is empty.
* @param then The moves applied after that move is applied.
*/
public ForEachSite
(
final RegionFunction regionFn,
final Moves generator,
@Opt @Name final Moves noMoveYet,
@Opt final Then then
)
{
super(then);
this.regionFn = regionFn;
this.generator = generator;
elseMoves = noMoveYet;
}
@Override
public Moves eval(final Context context)
{
final Region sites = regionFn.eval(context);
final Moves moves = new BaseMoves(super.then());
final int savedTo = context.to();
final int originSiteValue = context.site();
for (int site = sites.bitSet().nextSetBit(0); site >= 0; site = sites.bitSet().nextSetBit(site + 1))
{
context.setTo(site);
context.setSite(site);
final FastArrayList<Move> generatedMoves = generator.eval(context).moves();
moves.moves().addAll(generatedMoves);
}
if (moves.moves().isEmpty() && elseMoves != null)
return elseMoves.eval(context);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
context.setTo(savedTo);
context.setSite(originSiteValue);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = regionFn.gameFlags(game) | generator.gameFlags(game) | super.gameFlags(game);
if (elseMoves != null)
gameFlags |= elseMoves.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(regionFn.concepts(game));
concepts.or(generator.concepts(game));
if (elseMoves != null)
concepts.or(elseMoves.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(regionFn.writesEvalContextRecursive());
writeEvalContext.or(generator.writesEvalContextRecursive());
if (elseMoves != null)
writeEvalContext.or(elseMoves.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.To.id(), true);
writeEvalContext.set(EvalContextData.Site.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(regionFn.readsEvalContextRecursive());
readEvalContext.or(generator.readsEvalContextRecursive());
if (elseMoves != null)
readEvalContext.or(elseMoves.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= regionFn.missingRequirement(game);
missingRequirement |= generator.missingRequirement(game);
if (elseMoves != null)
missingRequirement |= elseMoves.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= regionFn.willCrash(game);
willCrash |= generator.willCrash(game);
if (elseMoves != null)
willCrash |= elseMoves.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
regionFn.preprocess(game);
generator.preprocess(game);
if (elseMoves != null)
elseMoves.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String text = "";
if(regionFn != null)
text = "Each turn, where the site is within " + regionFn.toEnglish(game) + ", " + generator.toEnglish(game);
return text;
}
}
| 6,077 | 24.86383 | 111 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/team/ForEachTeam.java | package game.rules.play.moves.nonDecision.operators.foreach.team;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import gnu.trove.list.array.TIntArrayList;
import main.collections.FastArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
/**
* Applies a move for each value from a value to another (included).
*
* @author Eric.Piette
*/
@Hide
public final class ForEachTeam extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The moves to apply. */
private final Moves generator;
/**
* @param generator The move to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachTeam
(
final Moves generator,
@Opt final Then then
)
{
super(then);
this.generator = generator;
}
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int[] savedTeam = context.team();
for (int tid = 1; tid < context.game().players().size(); tid++)
{
final TIntArrayList team = new TIntArrayList();
for (int pid = 1; pid < context.game().players().size(); pid++)
{
if (context.state().playerInTeam(pid, tid))
team.add(pid);
}
if (!team.isEmpty())
{
context.setTeam(team.toArray());
final FastArrayList<Move> generatedMoves = generator.eval(context).moves();
moves.moves().addAll(generatedMoves);
}
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
context.setTeam(savedTeam);
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return generator.gameFlags(game) | super.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(generator.concepts(game));
concepts.or(super.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(generator.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.Team.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(generator.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
generator.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= generator.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= generator.willCrash(game);
return willCrash;
}
}
| 3,659 | 22.921569 | 79 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/foreach/value/ForEachValue.java | package game.rules.play.moves.nonDecision.operators.foreach.value;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.IntFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import game.rules.play.moves.nonDecision.effect.Then;
import main.collections.FastArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
import other.move.Move;
/**
* Applies a move for each value from a value to another (included).
*
* @author Eric.Piette
*/
@Hide
public final class ForEachValue extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The value from. */
private final IntFunction minFn;
/** The value to. */
private final IntFunction maxFn;
/** The IntArrayFunction to get the values. */
private final IntArrayFunction valuesFn;
/** The moves to apply. */
private final Moves generator;
/**
* @param values The values.
* @param generator The move to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachValue
(
final IntArrayFunction values,
final Moves generator,
@Opt final Then then
)
{
super(then);
minFn = null;
maxFn = null;
valuesFn = values;
this.generator = generator;
}
/**
* @param min The minimal value.
* @param max The maximal value.
* @param generator The move to apply.
* @param then The moves applied after that move is applied.
*/
public ForEachValue
(
@Name final IntFunction min,
@Name final IntFunction max,
final Moves generator,
@Opt final Then then
)
{
super(then);
minFn = min;
maxFn = max;
valuesFn = null;
this.generator = generator;
}
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final int savedValue = context.value();
if (valuesFn != null)
{
final int[] values = valuesFn.eval(context);
for (final int value : values)
{
context.setValue(value);
final FastArrayList<Move> generatedMoves = generator.eval(context).moves();
moves.moves().addAll(generatedMoves);
}
}
else
{
final int min = minFn.eval(context);
final int max = maxFn.eval(context);
for (int value = min; value <= max; value++)
{
context.setValue(value);
final FastArrayList<Move> generatedMoves = generator.eval(context).moves();
moves.moves().addAll(generatedMoves);
}
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
context.setValue(savedValue);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = generator.gameFlags(game)
| super.gameFlags(game);
if (maxFn != null)
gameFlags |= maxFn.gameFlags(game);
if (minFn != null)
gameFlags |= minFn.gameFlags(game);
if (valuesFn != null)
gameFlags |= valuesFn.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (minFn != null)
concepts.or(minFn.concepts(game));
if (maxFn != null)
concepts.or(maxFn.concepts(game));
if (valuesFn != null)
concepts.or(valuesFn.concepts(game));
concepts.or(generator.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
if (minFn != null)
writeEvalContext.or(minFn.writesEvalContextRecursive());
if (maxFn != null)
writeEvalContext.or(maxFn.writesEvalContextRecursive());
if (valuesFn != null)
writeEvalContext.or(valuesFn.writesEvalContextRecursive());
writeEvalContext.or(generator.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.Value.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (minFn != null)
readEvalContext.or(minFn.readsEvalContextRecursive());
if (maxFn != null)
readEvalContext.or(maxFn.readsEvalContextRecursive());
if (valuesFn != null)
readEvalContext.or(valuesFn.readsEvalContextRecursive());
readEvalContext.or(generator.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (minFn != null)
missingRequirement |= minFn.missingRequirement(game);
if (maxFn != null)
missingRequirement |= maxFn.missingRequirement(game);
if (valuesFn != null)
missingRequirement |= valuesFn.missingRequirement(game);
missingRequirement |= generator.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (minFn != null)
willCrash |= minFn.willCrash(game);
if (maxFn != null)
willCrash |= maxFn.willCrash(game);
if (valuesFn != null)
willCrash |= valuesFn.willCrash(game);
willCrash |= generator.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
if (minFn != null)
minFn.preprocess(game);
if (maxFn != null)
maxFn.preprocess(game);
if (valuesFn != null)
valuesFn.preprocess(game);
generator.preprocess(game);
}
//------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String rangeString = "";
if (valuesFn != null)
rangeString = "in " + valuesFn.toEnglish(game);
else
rangeString = "between " + minFn.toEnglish(game) + " and " + maxFn.toEnglish(game);
return "for all values " + rangeString + " " + generator.toEnglish(game);
}
//--------------------------------------------------------------------------
}
| 7,067 | 24.06383 | 86 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/AllCombinations.java | package game.rules.play.moves.nonDecision.operators.logical;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import main.collections.FastArrayList;
import other.context.Context;
import other.move.Move;
/**
* Generates all combinations (i.e. the cross product) between two lists of moves.
*
* @author Cameron Browne
*/
public final class AllCombinations extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The first list to cross. */
private final Moves listA;
/** The second list to cross. */
private final Moves listB;
//-------------------------------------------------------------------------
/**
* @param listA The first list.
* @param listB The second list.
* @param then The moves applied after that move is applied.
*
* @example (allCombinations (add (piece (id "Disc0") state:(mover)) (to
* (site))) (flip (between)) )
*/
public AllCombinations
(
final Moves listA,
final Moves listB,
@Opt final Then then
)
{
super(then);
this.listA = listA;
this.listB = listB;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Return intersected list of moves
final Moves moves = new BaseMoves(super.then());
final FastArrayList<Move> ev1 = listA.eval(context).moves();
final FastArrayList<Move> ev2 = listB.eval(context).moves();
for (final Move m1 : ev1)
{
for (final Move m2 : ev2)
{
// System.out.println("---\n" + m1);
// System.out.println("---\n" + m2);
final Move newMove = new Move(m1, m2);
if (then() != null)
{
newMove.then().add(then().moves());
}
moves.moves().add(newMove);
}
}
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = listA.gameFlags(game) | listB.gameFlags(game) | super.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(listB.concepts(game));
concepts.or(listA.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(listB.writesEvalContextRecursive());
writeEvalContext.or(listA.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(listB.readsEvalContextRecursive());
readEvalContext.or(listA.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= listB.missingRequirement(game);
missingRequirement |= listA.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= listB.willCrash(game);
willCrash |= listA.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return listA.isStatic() && listB.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
listA.preprocess(game);
listB.preprocess(game);
}
@Override
public String toEnglish(final Game game)
{
return listA.toEnglish(game) + ", then " + listB.toEnglish(game);
}
}
| 4,456 | 22.962366 | 89 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/And.java | package game.rules.play.moves.nonDecision.operators.logical;
import java.util.BitSet;
import java.util.function.BiPredicate;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.move.MovesIterator;
/**
* Moves all the moves in the list if used in a consequence else only one move in the list.
*
* @author Eric.Piette
*/
public final class And extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
final Moves[] list;
//-------------------------------------------------------------------------
/**
* For making a move between two sets of moves.
*
* @param movesA The first move.
* @param movesB The second move.
* @param then The moves applied after that move is applied.
*
* @example (and (set Score P1 100) (set Score P2 100))
*/
public And
(
final Moves movesA,
final Moves movesB,
@Opt final Then then
)
{
super(then);
list = new Moves[2];
list[0] = movesA;
list[1] = movesB;
}
/**
* For making a move between many sets of moves.
*
* @param list The list of moves.
* @param then The moves applied after that move is applied.
*
* @example (and { (set Score P1 100) (set Score P2 100) (set Score P3 100) })
*/
public And
(
final Moves[] list,
@Opt final Then then
)
{
super(then);
this.list = list;
}
//-------------------------------------------------------------------------
@Override
public MovesIterator movesIterator(final Context context)
{
return new MovesIterator()
{
protected int listIdx = 0;
protected MovesIterator itr = computeNextItr();
@Override
public boolean hasNext()
{
return (itr != null);
}
@Override
public Move next()
{
final Move next = itr.next();
if (!itr.hasNext())
{
itr = computeNextItr();
}
if (then() != null)
next.then().add(then().moves());
return next;
}
/**
* @return Computes and returns our next moves iterator
*/
private MovesIterator computeNextItr()
{
while (true)
{
if (list.length <= listIdx)
return null;
final MovesIterator nextItr = list[listIdx++].movesIterator(context);
if (nextItr.hasNext())
return nextItr;
}
}
@Override
public boolean canMoveConditionally(final BiPredicate<Context, Move> predicate)
{
if (itr == null)
return false;
while (true)
{
if (itr.canMoveConditionally(predicate))
return true;
if (list.length <= listIdx)
return false;
itr = list[listIdx++].movesIterator(context);
}
}
};
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
for (int i = 0; i < list.length; ++i)
{
moves.moves().addAll(list[i].eval(context).moves());
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
for (final Moves moves : list)
{
if (moves.canMoveTo(context, target))
return true;
}
return false;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "And(" + list + ")";
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
for (final Moves moves : list)
gameFlags |= moves.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
for (final Moves moves : list)
concepts.or(moves.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.Union.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
for (final Moves moves : list)
writeEvalContext.or(moves.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
for (final Moves moves : list)
readEvalContext.or(moves.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
for (final Moves moves : list)
missingRequirement |= moves.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
for (final Moves moves : list)
willCrash |= moves.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
for (final Moves moves : list)
if (!moves.isStatic())
return false;
return true;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
for (final Moves moves : list)
moves.preprocess(game);
}
//-------------------------------------------------------------------------
/**
* @return Array of moves
*/
public Moves[] list()
{
return list;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
if (list.length == 0)
return "no moves";
String text = "";
for (final Moves move : list)
text += move.toEnglish(game) + " and ";
text = text.substring(0, text.length()-5);
if(then() != null)
text += " " + then().moves().toEnglish(game);
return text;
}
}
| 6,828 | 20.340625 | 91 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/Append.java | package game.rules.play.moves.nonDecision.operators.logical;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.NonDecision;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import main.collections.FastArrayList;
import other.context.Context;
import other.move.Move;
/**
* Appends a list of moves to each move in a list.
*
* @author Eric.Piette and cambolbro
*/
public final class Append extends Operator
{
private static final long serialVersionUID = 1L;
/** The list of moves to append. */
private final Moves list;
//-------------------------------------------------------------------------
/**
* @param list The moves to merge.
* @param then The moves applied after that move is applied.
*
* @example (append (custodial (between if:(is Enemy (state at:(between)))
* (apply (allCombinations (add (piece "Disc0" state:(mover)) (to
* (site))) (flip (between)) ) ) ) (to if:(is Friend (state at:(to))))
* ) (then (and (set Score P1 (count Sites in:(sites State 1)) ) (set
* Score P2 (count Sites in:(sites State 2)) ) ) ) )
*/
public Append
(
final NonDecision list,
@Opt final Then then
)
{
super(then);
this.list = list;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
final FastArrayList<Move> evaluated = list.eval(context).moves();
for (final Move m : evaluated)
m.setDecision(true);
if (evaluated.size() == 0)
return moves;
final Move newMove = new Move(evaluated);
newMove.setMover(context.state().mover());
moves.moves().add(newMove);
if (then() != null)
newMove.then().add(then().moves());
// Store the Moves in the computed moves.
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).setMovesLudeme(this);
return moves;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game) | list.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.or(list.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
writeEvalContext.or(list.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
readEvalContext.or(list.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
missingRequirement |= list.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
willCrash |= list.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return super.isStatic() && list.isStatic();
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
list.preprocess(game);
}
@Override
public String toEnglish(final Game game)
{
String text = list.toEnglish(game);
if(then() != null)
text+=", then "+ then().toEnglish(game);
return text;
}
}
| 4,307 | 23.067039 | 80 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/If.java | package game.rules.play.moves.nonDecision.operators.logical;
import java.util.BitSet;
import java.util.function.BiPredicate;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.move.MovesIterator;
/**
* Returns, depending on the condition, a list of legal moves or an other list.
*
* @author Eric.Piette and cambolbro
*/
public final class If extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which condition. */
final BooleanFunction cond;
/** If the condition is true. */
final Moves list;
/** If the condition if false. */
final Moves elseList;
//-------------------------------------------------------------------------
/**
* @param cond The condition to satisfy to get the first list of legal
* moves.
* @param list The first list of legal moves.
* @param elseList The other list of legal moves if the condition is not
* satisfied.
* @param then The moves applied after that move is applied.
*
* @example (if (is Mover P1) (moveAgain))
*
* @example (if (is Mover P1) (moveAgain) (remove (last To)))
*/
public If
(
final BooleanFunction cond,
final Moves list,
@Opt final Moves elseList,
@Opt final Then then
)
{
super(then);
this.cond = cond;
this.list = list;
this.elseList = elseList;
}
//-------------------------------------------------------------------------
@Override
public MovesIterator movesIterator(final Context context)
{
return new MovesIterator()
{
protected MovesIterator itr = computeItr();
@Override
public boolean hasNext()
{
return itr != null && itr.hasNext();
}
@Override
public Move next()
{
final Move next = itr.next();
if (then() != null)
next.then().add(then().moves());
return next;
}
/**
* Computes which iterator to use for given context
* @return Moves iterator
*/
private MovesIterator computeItr()
{
if (cond.eval(context))
return list.movesIterator(context);
else if (elseList != null)
return elseList.movesIterator(context);
else
return null;
}
@Override
public boolean canMoveConditionally(final BiPredicate<Context, Move> predicate)
{
return itr.canMoveConditionally(predicate);
}
};
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
if (cond.eval(context))
{
final Moves moves = list.eval(context);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return moves;
}
else if (elseList != null)
{
final Moves moves = elseList.eval(context);
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return moves;
}
final Moves moves = new BaseMoves(super.then());
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return new BaseMoves(super.then());
}
//-------------------------------------------------------------------------
@Override
public boolean canMove(final Context context)
{
if (cond.eval(context))
return list.canMove(context);
else if (elseList != null)
return elseList.canMove(context);
return false;
}
@Override
public boolean canMoveTo(final Context context, final int target)
{
if (cond.eval(context))
return list.canMoveTo(context, target);
else if (elseList != null)
return elseList.canMoveTo(context, target);
else
return false;
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
if (cond != null)
gameFlags |= cond.gameFlags(game);
if (list != null)
gameFlags |= list.gameFlags(game);
if (elseList != null)
gameFlags |= elseList.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
if (cond != null)
concepts.or(cond.concepts(game));
if (list != null)
concepts.or(list.concepts(game));
if (elseList != null)
concepts.or(elseList.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.ConditionalStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
if (cond != null)
writeEvalContext.or(cond.writesEvalContextRecursive());
if (list != null)
writeEvalContext.or(list.writesEvalContextRecursive());
if (elseList != null)
writeEvalContext.or(elseList.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
if (cond != null)
readEvalContext.or(cond.readsEvalContextRecursive());
if (list != null)
readEvalContext.or(list.readsEvalContextRecursive());
if (elseList != null)
readEvalContext.or(elseList.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
if (cond != null)
missingRequirement |= cond.missingRequirement(game);
if (list != null)
missingRequirement |= list.missingRequirement(game);
if (elseList != null)
missingRequirement |= elseList.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
if (cond != null)
willCrash |= cond.willCrash(game);
if (list != null)
willCrash |= list.willCrash(game);
if (elseList != null)
willCrash |= elseList.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
if (cond != null && !cond.isStatic())
return false;
if (list != null && !list.isStatic())
return false;
if (elseList != null && !elseList.isStatic())
return false;
return true;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
if (cond != null)
cond.preprocess(game);
if (list != null)
list.preprocess(game);
if (elseList != null)
elseList.preprocess(game);
}
//-------------------------------------------------------------------------
/**
* @return Our condition
*/
public BooleanFunction cond()
{
return cond;
}
/**
* @return Move generator if condition holds
*/
public Moves list()
{
return list;
}
/**
* @return Move generator if condition does not hold
*/
public Moves elseList()
{
return elseList;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String text = "if " + cond.toEnglish(game);
if(list != null && !list.toEnglish(game).equals(""))
text += ", " + list.toEnglish(game);
if(elseList != null && !elseList.toEnglish(game).equals(""))
text+= ", else "+ elseList.toEnglish(game);
return text;
}
}
| 8,325 | 21.563686 | 84 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/Or.java | package game.rules.play.moves.nonDecision.operators.logical;
import java.util.BitSet;
import java.util.function.BiPredicate;
import annotations.Opt;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Then;
import game.rules.play.moves.nonDecision.operator.Operator;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
import other.move.MovesIterator;
/**
* Moves one of the moves in the list.
*
* @author Eric.Piette
*/
public final class Or extends Operator
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
final Moves[] list;
//-------------------------------------------------------------------------
/**
* For making a move between two sets of moves.
*
* @param movesA The first move.
* @param movesB The second move.
* @param then The moves applied after that move is applied.
*
* @example (or (set Score P1 100) (set Score P2 100))
*/
public Or
(
final Moves movesA,
final Moves movesB,
@Opt final Then then
)
{
super(then);
list = new Moves[2];
list[0] = movesA;
list[1] = movesB;
}
/**
* For making a move between many sets of moves.
*
* @param list The list of moves.
* @param then The moves applied after that move is applied.
*
* @example (or { (set Score P1 100) (set Score P2 100) (set Score P3 100) })
*/
public Or
(
final Moves[] list,
@Opt final Then then
)
{
super(then);
this.list = list;
}
//-------------------------------------------------------------------------
@Override
public MovesIterator movesIterator(final Context context)
{
return new MovesIterator()
{
protected int listIdx = 0;
protected MovesIterator itr = computeNextItr();
@Override
public boolean hasNext()
{
return (itr != null);
}
@Override
public Move next()
{
final Move next = itr.next();
if (!itr.hasNext())
{
itr = computeNextItr();
}
if (then() != null)
next.then().add(then().moves());
return next;
}
/**
* @return Computes and returns our next moves iterator
*/
private MovesIterator computeNextItr()
{
while (true)
{
if (list.length <= listIdx)
return null;
final MovesIterator nextItr = list[listIdx++].movesIterator(context);
if (nextItr.hasNext())
return nextItr;
}
}
@Override
public boolean canMoveConditionally(final BiPredicate<Context, Move> predicate)
{
if (itr == null)
return false;
while (true)
{
if (itr.canMoveConditionally(predicate))
return true;
if (list.length <= listIdx)
return false;
itr = list[listIdx++].movesIterator(context);
}
}
};
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
final Moves moves = new BaseMoves(super.then());
for (int i = 0; i < list.length; ++i)
{
moves.moves().addAll(list[i].eval(context).moves());
}
if (then() != null)
for (int j = 0; j < moves.moves().size(); j++)
moves.moves().get(j).then().add(then().moves());
return moves;
}
//-------------------------------------------------------------------------
@Override
public boolean canMoveTo(final Context context, final int target)
{
for (final Moves moves : list)
{
if (moves.canMoveTo(context, target))
return true;
}
return false;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "Or(" + list + ")";
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
for (final Moves moves : list)
gameFlags |= moves.gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
for (final Moves moves : list)
concepts.or(moves.concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
concepts.set(Concept.Union.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
for (final Moves moves : list)
writeEvalContext.or(moves.writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
for (final Moves moves : list)
readEvalContext.or(moves.readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
for (final Moves moves : list)
missingRequirement |= moves.missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
for (final Moves moves : list)
willCrash |= moves.willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
for (final Moves moves : list)
if (!moves.isStatic())
return false;
return true;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
for (final Moves moves : list)
moves.preprocess(game);
}
//-------------------------------------------------------------------------
/**
* @return Array of moves
*/
public Moves[] list()
{
return list;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
if (list.length == 0)
return "no moves";
String text = "";
for (final Moves move : list)
text += move.toEnglish(game) + " or ";
text = text.substring(0, text.length()-4);
if(then() != null)
text += " " + then().moves().toEnglish(game);
return text;
}
}
| 6,768 | 20.153125 | 84 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/Seq.java | package game.rules.play.moves.nonDecision.operators.logical;
import java.util.BitSet;
import game.Game;
import game.rules.play.moves.BaseMoves;
import game.rules.play.moves.Moves;
import game.rules.play.moves.nonDecision.effect.Effect;
import other.concept.Concept;
import other.context.Context;
import other.context.TempContext;
import other.move.Move;
/**
* Applies a sequence of moves one by one. Each move will use the new (temporary) state/context created by the previous move applied in the sequence.
*
* @author Eric.Piette
*/
public final class Seq extends Effect
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The sequence of moves. */
final Moves[] moves;
//-------------------------------------------------------------------------
/**
* @param moves Moves to apply one by one.
*
* @example (seq {(remove 1) (remove 2)})
*/
public Seq
(
final Moves[] moves
)
{
super(null);
this.moves = moves;
}
//-------------------------------------------------------------------------
@Override
public Moves eval(final Context context)
{
// Return intersected list of moves
final Moves result = new BaseMoves(super.then());
if(moves.length == 0)
return result;
Context tempContext = new TempContext(context);
for(int i = 0; i < moves.length; i++)
{
final Moves movesToApply = moves[i];
for (final Move m : movesToApply.eval(tempContext).moves())
{
final Move appliedMove = (Move) m.apply(tempContext, true);
result.moves().add(appliedMove);
}
}
// End result of the previous prior code
// if (then() != null)
// for (int j = 0; j < result.moves().size(); j++)
// result.moves().get(j).then().add(then().moves());
return result;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = super.gameFlags(game);
for(int i = 0; i < moves.length; i++)
gameFlags |= moves[i].gameFlags(game);
if (then() != null)
gameFlags |= then().gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(super.concepts(game));
concepts.set(Concept.CopyContext.id(), true);
for(int i = 0; i < moves.length; i++)
concepts.or(moves[i].concepts(game));
if (then() != null)
concepts.or(then().concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(super.writesEvalContextRecursive());
for(int i = 0; i < moves.length; i++)
writeEvalContext.or(moves[i].writesEvalContextRecursive());
if (then() != null)
writeEvalContext.or(then().writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(super.readsEvalContextRecursive());
for(int i = 0; i < moves.length; i++)
readEvalContext.or(moves[i].readsEvalContextRecursive());
if (then() != null)
readEvalContext.or(then().readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= super.missingRequirement(game);
for(int i = 0; i < moves.length; i++)
missingRequirement |= moves[i].missingRequirement(game);
if (then() != null)
missingRequirement |= then().missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= super.willCrash(game);
for(int i = 0; i < moves.length; i++)
willCrash |= moves[i].willCrash(game);
if (then() != null)
willCrash |= then().willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
super.preprocess(game);
for(int i = 0; i < moves.length; i++)
moves[i].preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String nextString = "";
for(int i = 0; i < moves.length-1; i++)
nextString += moves[i].toEnglish(game) + " ,";
if(moves.length != 0)
nextString += moves[moves.length-1].toEnglish(game);
String thenString = "";
if (then() != null)
thenString = " then " + then().toEnglish(game);
return nextString + thenString;
}
//-------------------------------------------------------------------------
}
| 4,621 | 22.343434 | 149 | java |
Ludii | Ludii-master/Core/src/game/rules/play/moves/nonDecision/operators/logical/package-info.java | /**
* Logical move generators are used to combine or filter existing lists of moves.
*/
package game.rules.play.moves.nonDecision.operators.logical;
| 151 | 29.4 | 81 | java |
Ludii | Ludii-master/Core/src/game/rules/start/Deal.java | package game.rules.start;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import annotations.Opt;
import game.Game;
import game.equipment.component.Component;
import game.equipment.container.Container;
import game.equipment.container.other.Deck;
import game.types.board.SiteType;
import game.types.component.DealableType;
import game.types.state.GameType;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.action.Action;
import other.action.move.move.ActionMove;
import other.context.Context;
import other.move.Move;
import other.state.container.ContainerState;
/**
* To deal different components between players.
*
* @author Eric.Piette
*/
public final class Deal extends StartRule
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The number to deal. */
private final int count;
/** The number to deal. */
private final DealableType type;
//-------------------------------------------------------------------------
/**
* @param type Type of deal.
* @param count The number of components to deal [1].
*
* @example (deal Dominoes 7)
*/
public Deal
(
final DealableType type,
@Opt final Integer count
)
{
this.type = type;
this.count = (count == null) ? 1 : count.intValue();
}
//-------------------------------------------------------------------------
@Override
public void eval(final Context context)
{
if (type == DealableType.Cards)
{
evalCards(context);
}
else if (type == DealableType.Dominoes)
{
evalDominoes(context);
}
}
/**
* To deal cards.
*
* @param context
*/
public void evalCards(final Context context)
{
// If no deck nothing to do.
if (context.game().handDeck().isEmpty())
return;
final List<Integer> handIndex = new ArrayList<>();
for (final Container c : context.containers())
if (c.isHand() && !c.isDeck() && !c.isDice())
handIndex.add(Integer.valueOf(context.sitesFrom()[c.index()]));
// If each player does not have a hand, nothing to do.
if (handIndex.size() != context.game().players().count())
return;
final Deck deck = context.game().handDeck().get(0);
final ContainerState cs = context.containerState(deck.index());
final int indexSiteDeck = context.sitesFrom()[deck.index()];
final int sizeDeck = cs.sizeStackCell(indexSiteDeck);
if (sizeDeck < count * handIndex.size())
throw new IllegalArgumentException("You can not deal so much cards in the initial state.");
int hand = 0;
// int level = 0;
for (int indexCard = 0; indexCard < count * handIndex.size(); indexCard++)
{
final Action dealAction = ActionMove.construct(SiteType.Cell, indexSiteDeck,cs.sizeStackCell(indexSiteDeck) - 1, SiteType.Cell, handIndex.get(hand).intValue(), Constants.OFF, Constants.OFF, Constants.OFF, Constants.OFF, false);
dealAction.apply(context, true);
context.trial().addMove(new Move(dealAction));
context.trial().addInitPlacement();
if (hand == context.game().players().count() - 1)
{
hand = 0;
// level++;
}
else
hand++;
}
}
/**
* To deal dominoes.
*
* @param context
*/
public void evalDominoes(final Context context)
{
final TIntArrayList handIndex = new TIntArrayList();
for (final Container c : context.containers())
if (c.isHand() && !c.isDeck() && !c.isDice())
handIndex.add(context.sitesFrom()[c.index()]);
// If each player does not have a hand, nothing to do.
if (handIndex.size() != context.game().players().count())
return;
final Component[] components = context.components();
if (components.length < count * handIndex.size())
throw new IllegalArgumentException("You can not deal so much dominoes in the initial state.");
final TIntArrayList toDeal = new TIntArrayList();
for (int i = 1; i < components.length; i++)
toDeal.add(i);
final int nbPlayers = context.players().size() - 1;
final ArrayList<boolean[]> masked = new ArrayList<>();
for (int i = 1; i <= nbPlayers; i++)
{
masked.add(new boolean[nbPlayers]);
for (int j = 1; j <= nbPlayers; j++)
{
if (i == j)
masked.get(i - 1)[j - 1] = false;
else
masked.get(i - 1)[j - 1] = true;
}
}
int dealed = 0;
while (dealed < (count * 2))
{
final int index = context.rng().nextInt(toDeal.size());
final int indexComponent = toDeal.getQuick(index);
final Component component = components[indexComponent];
final int currentPlayer = dealed % nbPlayers;
Start.placePieces(context, handIndex.getQuick(currentPlayer) + (dealed / nbPlayers), component.index(), 1,
Constants.OFF, Constants.OFF, Constants.UNDEFINED, false,
SiteType.Cell);
toDeal.removeAt(index);
dealed++;
}
}
//-------------------------------------------------------------------------
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
return readEvalContext;
}
@Override
public boolean isStatic()
{
return true;
}
@Override
public long gameFlags(final Game game)
{
if (type == DealableType.Cards)
return GameType.Card;
else if (type == DealableType.Dominoes)
return GameType.LargePiece | GameType.Dominoes | GameType.Stochastic | GameType.HiddenInfo;
else
return 0L;
}
@Override
public void preprocess(final Game game)
{
// Do nothing
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
final String str = "(Deal" + type + ")";
return str;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "deal " + count + " " + type.name().toLowerCase() + " to each player";
}
//-------------------------------------------------------------------------
}
| 6,032 | 24.892704 | 231 | java |
Ludii | Ludii-master/Core/src/game/rules/start/Start.java | package game.rules.start;
import java.io.Serializable;
import annotations.Or;
import game.Game;
import game.types.board.SiteType;
import other.BaseLudeme;
import other.action.BaseAction;
import other.action.move.ActionAdd;
import other.context.Context;
import other.move.Move;
/**
* Defines a starting position.
*
* @author cambolbro
* @remarks For any game with starting rules, like pieces already placed on the
* board.
*/
public class Start extends BaseLudeme implements Serializable
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The starting rules. */
private final StartRule[] rules;
//-------------------------------------------------------------------------
/**
* @param rules The starting rules.
* @param rule The starting rule.
* @example (start { (place "Pawn1" {"F4" "F5" "F6" "F7" "F8" "F9" "G5" "G6"
* "G7" "G8"}) (place "Knight1" {"F3" "G4" "G9" "F10"}) (place "Pawn2"
* {"K4" "K5" "K6" "K7" "K8" "K9" "J5" "J6" "J7" "J8"}) (place
* "Knight2" {"K3" "J4" "J9" "K10"}) })
*/
public Start
(
@Or final StartRule[] rules,
@Or final StartRule rule
)
{
int numNonNull = 0;
if (rules != null)
numNonNull++;
if (rule != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Exactly one Or parameter must be non-null.");
if (rules != null)
this.rules = rules;
else
{
this.rules = new StartRule[1];
this.rules[0] = rule;
}
}
//-------------------------------------------------------------------------
/**
* @return The starting rules.
*/
public StartRule[] rules()
{
return rules;
}
//-------------------------------------------------------------------------
/**
* Evaluate the starting rules.
*
* @param context The context.
*/
public void eval(final Context context)
{
for (final StartRule rule : rules)
rule.eval(context);
}
//-------------------------------------------------------------------------
/**
* Place piece on a site.
*
* @param context The context.
* @param locn The location of the piece.
* @param what The index of the piece.
* @param count The count of the piece.
* @param state The state of the site.
* @param rotation The rotation of the site.
* @param value The piece value of the site.
* @param isStack True if the piece has to be placed in a stack.
* @param type The graph element type of the site.
*/
public static void placePieces
(
final Context context, final int locn, final int what, final int count,
final int state, final int rotation, final int value, final boolean isStack, final SiteType type
)
{
if (isStack)
{
final BaseAction actionAtomic = new ActionAdd(type, locn, what, 1, state,
rotation, value, Boolean.TRUE);
actionAtomic.apply(context, true);
context.trial().addMove(new Move(actionAtomic));
context.trial().addInitPlacement();
}
else
{
final BaseAction actionAtomic = new ActionAdd(type, locn, what, count, state, rotation, value, null);
actionAtomic.apply(context, true);
context.trial().addMove(new Move(actionAtomic));
context.trial().addInitPlacement();
}
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "at the start of the game";
}
}
| 3,485 | 25.014925 | 104 | java |
Ludii | Ludii-master/Core/src/game/rules/start/StartRule.java | package game.rules.start;
import game.Game;
import game.rules.Rule;
import other.BaseLudeme;
/**
* Sets the initial setup rule for the start of each trial (i.e. game).
*
* @author cambolbro
*/
public abstract class StartRule extends BaseLudeme implements Rule
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param game The game.
* @return The state of the starting rule.
*/
@SuppressWarnings("static-method")
public int state(final Game game)
{
return 0;
}
/**
* @param game The game.
* @return The count of the starting rule.
*/
@SuppressWarnings("static-method")
public int count(final Game game)
{
return 0;
}
/**
* @param game The game.
* @return The number of component to place.
*/
@SuppressWarnings("static-method")
public int howManyPlace(final Game game)
{
return 0;
}
/**
* @return True if the starting rule is a set rule.
*/
@SuppressWarnings("static-method")
public boolean isSet()
{
return false;
}
}
| 1,065 | 17.701754 | 76 | java |
Ludii | Ludii-master/Core/src/game/rules/start/package-info.java | /**
* The {\tt start} rules describe the initial setup of equipment before play commences.
*/
package game.rules.start;
| 122 | 23.6 | 87 | java |
Ludii | Ludii-master/Core/src/game/rules/start/deductionPuzzle/Set.java | package game.rules.start.deductionPuzzle;
import java.util.Arrays;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.rules.start.StartRule;
import game.types.board.SiteType;
import game.types.state.GameType;
import other.action.BaseAction;
import other.action.puzzle.ActionSet;
import other.concept.Concept;
import other.context.Context;
import other.move.Move;
/**
* Sets a variable to a specified value in a deduction puzzle.
*
* @author Eric.Piette and cambolbro
*
* @remarks Applies to deduction puzzles.
*/
public final class Set extends StartRule
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
protected final Integer[] vars;
protected final Integer[] values;
protected final SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [Cell].
* @param pairs The first element of the pair is the index of the variable, the
* second one the value of the variable.
* @example (set { {1 9} {6 4} {11 8} {12 5} {16 1} {20 1} {25 6} {26 8} {30 1}
* {34 3} {40 4} {41 5} {42 7} {46 5} {50 7} {55 7} {58 9} {60 2} {65
* 3} {66 6} {72 8} })*/
public Set
(
@Opt final SiteType type,
final Integer[]... pairs
)
{
if (pairs == null)
{
values = null;
vars = null;
}
else
{
values = new Integer[pairs.length];
vars = new Integer[pairs.length];
for (int n = 0; n < pairs.length; n++)
{
vars[n] = pairs[n][0];
values[n] = pairs[n][1];
}
}
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public void eval(final Context context)
{
final SiteType realType = (type == null) ? context.board().defaultSite() : type;
final int minSize = Math.min(vars.length, values.length);
for (int i = 0 ; i < minSize ; i++)
{
final BaseAction actionAtomic = new ActionSet(realType, vars[i].intValue(), values[i].intValue());
actionAtomic.apply(context, true);
context.trial().addMove(new Move(actionAtomic));
context.trial().addInitPlacement();
}
}
//-------------------------------------------------------------------------
/**
* @return vars
*/
public Integer[] vars()
{
return vars;
}
/**
* @return values
*/
public Integer[] values()
{
return values;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return GameType.DeductionPuzzle;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.set(Concept.DeductionPuzzle.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
return readEvalContext;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (game.players().count() != 1)
{
game.addCrashToReport(
"The ludeme (set ...) in the starting rules is used but the number of players is not 1.");
willCrash = true;
}
return willCrash;
}
@Override
public void preprocess(final Game game)
{
// Do nothing.
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
String str = "(set ";
final int minSize = Math.min(vars.length, values.length);
for (int i = 0 ; i < minSize ; i++)
str += values[i] + " on " + vars[i] + " ";
str+=")";
return str;
}
@Override
public boolean isSet()
{
return true;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "set the variables " + Arrays.toString(vars) + " to values " + Arrays.toString(values);
}
//-------------------------------------------------------------------------
}
| 4,208 | 20.80829 | 101 | java |
Ludii | Ludii-master/Core/src/game/rules/start/deductionPuzzle/package-info.java | /**
* Start rules specific to deduction puzzles typically involve setting hint values for puzzle challenges.
*/
package game.rules.start.deductionPuzzle;
| 156 | 30.4 | 105 | java |
Ludii | Ludii-master/Core/src/game/rules/start/forEach/ForEach.java | package game.rules.start.forEach;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.IntFunction;
import game.functions.region.RegionFunction;
import game.rules.Rule;
import game.rules.play.moves.nonDecision.operators.foreach.ForEachPlayerType;
import game.rules.play.moves.nonDecision.operators.foreach.ForEachSiteType;
import game.rules.start.StartRule;
import game.rules.start.forEach.player.ForEachPlayer;
import game.rules.start.forEach.site.ForEachSite;
import game.rules.start.forEach.team.ForEachTeam;
import game.rules.start.forEach.value.ForEachValue;
import other.context.Context;
/**
* Iterates over a set of items.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class ForEach extends StartRule
{
private static final long serialVersionUID = 1L;
/**
* For iterating on teams.
*
* @param forEachType The type of property to iterate.
* @param regionFn The original region.
* @param If The condition to satisfy.
* @param startingRule The starting rule to apply.
*
* @example (forEach Team (forEach (team) (set Hidden What at:1 to:Player)))
*/
public static Rule construct
(
final ForEachTeamType forEachType,
final StartRule startingRule
)
{
switch (forEachType)
{
case Team:
return new ForEachTeam(startingRule);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachTeam is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating on values between two.
*
* @param forEachType The type of property to iterate.
* @param regionFn The original region.
* @param If The condition to satisfy.
* @param startingRule The starting rule to apply.
*
* @example (forEach Site (sites Top) if:(is Even (site)) (place "Pawn1"
* (site)))
*/
public static Rule construct
(
final ForEachSiteType forEachType,
final RegionFunction regionFn,
@Opt @Name final BooleanFunction If,
final StartRule startingRule
)
{
switch (forEachType)
{
case Site:
return new ForEachSite(regionFn, If, startingRule);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachSiteType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating on values between two.
*
* @param forEachType The type of property to iterate.
* @param min The minimal value.
* @param max The maximal value.
* @param startingRule The starting rule to apply.
*
* @example (forEach Value min:1 max:5 (set Hidden What at:10 level:(value)
* to:P1))
*/
public static Rule construct
(
final ForEachStartValueType forEachType,
@Name final IntFunction min,
@Name final IntFunction max,
final StartRule startingRule
)
{
switch (forEachType)
{
case Value:
return new ForEachValue(min, max, startingRule);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachStartValueType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the players.
*
* @param forEachType The type of property to iterate.
* @param startingRule The starting rule to apply.
*
* @example (forEach Player (set Hidden What at:1 to:Player))
*/
public static Rule construct
(
final ForEachPlayerType forEachType,
final StartRule startingRule
)
{
switch (forEachType)
{
case Player:
return new ForEachPlayer(startingRule);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("ForEach(): A ForEachPlayerType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For iterating through the players in using an IntArrayFunction.
*
* @param players The list of players.
* @param startingRule The starting rule to apply.
*
* @example (forEach (players Ally of:(next)) (set Hidden What at:1 to:Player))
*/
public static Rule construct
(
final IntArrayFunction players,
final StartRule startingRule
)
{
return new ForEachPlayer(players, startingRule);
}
//-------------------------------------------------------------------------
private ForEach()
{
// Ensure that compiler does pick up default constructor
}
@Override
public void eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("ForEach.eval(): Should never be called directly.");
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// Should never be there
return false;
}
@Override
public long gameFlags(final Game game)
{
// Should never be there
return 0L;
}
@Override
public void preprocess(final Game game)
{
// Nothing to do.
}
//-------------------------------------------------------------------------
}
| 5,472 | 25.061905 | 95 | java |
Ludii | Ludii-master/Core/src/game/rules/start/forEach/ForEachStartValueType.java | package game.rules.start.forEach;
/**
* Defines the values which can be iterated in the ForEach super start ludeme.
*
* @author Eric.Piette
*/
public enum ForEachStartValueType
{
/** To apply a starting rule for each value from one value to another (included). */
Value,
}
| 281 | 20.692308 | 85 | java |
Ludii | Ludii-master/Core/src/game/rules/start/forEach/ForEachTeamType.java | package game.rules.start.forEach;
/**
* To iterate through the teams.
*
* @author Eric.Piette
*/
public enum ForEachTeamType
{
/** Team iterator. */
Team;
}
| 165 | 11.769231 | 33 | java |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.