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/functions/ints/iterator/To.java | package game.functions.ints.iterator;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
import other.context.EvalContextData;
/**
* Returns the ``to'' value of the context.
*
* @author mrraow and cambolbro
*
* @remarks This ludeme returns the destination location the current component is moving to.
* It is used to generate component moves and for all decision moves.
*/
public final class To extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Our singleton instance */
private static final To INSTANCE = new To();
//-------------------------------------------------------------------------
/**
* Returns the "to" value of the context.
*/
private To()
{
// Private constructor; singleton!
}
//-------------------------------------------------------------------------
/**
* @return Singleton instance
*/
public static To instance()
{
return INSTANCE;
}
/**
* @return Returns our singleton instance as a ludeme
* @example (to)
*/
public static To construct()
{
return INSTANCE;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.to();
}
//-------------------------------------------------------------------------
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
return readsEvalContextFlat();
}
@Override
public BitSet readsEvalContextFlat()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.set(EvalContextData.To.id(), true);
return readEvalContext;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0L;
}
@Override
public void preprocess(final Game game)
{
// nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "To()";
}
@Override
public String toEnglish(final Game game)
{
return "to";
}
}
| 2,300 | 18.336134 | 93 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/iterator/Track.java | package game.functions.ints.iterator;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
import other.context.EvalContextData;
/**
* Returns the ``track'' value of the context.
*
* @author Eric.Piette
* @remarks Used in a (forEach Track ...) ludeme to set the value to the index
* of each track.
*/
public final class Track extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @example (track)
*/
public Track()
{
// Nothing to do.
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.track();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public void preprocess(final Game game)
{
// nothing to do
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
return readsEvalContextFlat();
}
@Override
public BitSet readsEvalContextFlat()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.set(EvalContextData.Track.id(), true);
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasTrack())
{
game.addRequirementToReport("The ludeme (track) is used but the board has no tracks.");
missingRequirement = true;
}
return missingRequirement;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "(track)";
}
}
| 1,938 | 18.39 | 90 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/iterator/package-info.java | /**
* Iterator functions returning an integer value typically used as temporary
* variables during move planning for chaining nontrivial move sequences or in
* looping through many values with ludemes such as (forEach ...).
*/
package game.functions.ints.iterator;
| 269 | 37.571429 | 78 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/Last.java | package game.functions.ints.last;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import other.context.Context;
/**
* Returns a site related to the last move.
*
* @author Eric Piette
*/
@SuppressWarnings("javadoc")
public final class Last extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param lastType The site to return.
* @param afterConsequence True, to check the location related to the last decision; False, to check the to location related to the last consequence. [False].
*
* @example (last To)
* @example (last From)
* @example (last LevelFrom)
* @example (last LevelTo)
*/
public static IntFunction construct
(
final LastType lastType,
@Opt @Name final BooleanFunction afterConsequence
)
{
switch (lastType)
{
case From:
return new LastFrom(afterConsequence);
case LevelFrom:
return new LastLevelFrom(afterConsequence);
case To:
return new LastTo(afterConsequence);
case LevelTo:
return new LastLevelTo(afterConsequence);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Last(): A LastType is not implemented.");
}
private Last()
{
// Make grammar pick up construct() and not default constructor
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Last.eval(): Should never be called directly.");
// return new Region();
}
@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.
}
} | 2,126 | 22.633333 | 159 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/LastFrom.java | package game.functions.ints.last;
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.BaseIntFunction;
import main.Constants;
import other.context.Context;
import other.move.Move;
/**
* Returns the ``from'' location of the last decision.
*
* @author Eric.Piette
*/
@Hide
public final class LastFrom extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** To get the last "from" loc after subsequents have been applied. */
private final BooleanFunction afterSubsequentsFn;
//-------------------------------------------------------------------------
/**
* @param afterSubsequents Whether to return the ``from'' location after applying the consequences [False].
*/
public LastFrom
(
@Opt @Name final BooleanFunction afterSubsequents
)
{
afterSubsequentsFn = (afterSubsequents == null) ? new BooleanConstant(false) : afterSubsequents;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final Move action = context.trial().lastMove();
if (action != null)
if (afterSubsequentsFn.eval(context))
return action.fromAfterSubsequents();
else
return action.fromNonDecision();
return Constants.UNDEFINED;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return afterSubsequentsFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(afterSubsequentsFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(afterSubsequentsFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(afterSubsequentsFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
afterSubsequentsFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= afterSubsequentsFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= afterSubsequentsFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the from location of the last move";
}
//-------------------------------------------------------------------------
}
| 3,079 | 22.692308 | 108 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/LastLevelFrom.java | package game.functions.ints.last;
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.BaseIntFunction;
import main.Constants;
import other.context.Context;
import other.move.Move;
/**
* Returns the ``level from'' of the last move.
*
* @author Eric.Piette
*/
@Hide
public final class LastLevelFrom extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** To get the last "level" after subsequents have been applied. */
private final BooleanFunction afterSubsequentsFn;
//-------------------------------------------------------------------------
/**
* @param afterSubsequents Whether to return the ``level'' after applying subsequents [False].
*/
public LastLevelFrom
(
@Opt @Name final BooleanFunction afterSubsequents
)
{
afterSubsequentsFn = (afterSubsequents == null) ? new BooleanConstant(false) : afterSubsequents;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final Move action = context.trial().lastMove();
if (action != null)
if (afterSubsequentsFn.eval(context))
return action.levelFromAfterSubsequents();
else
return action.levelFrom();
return Constants.UNDEFINED;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return afterSubsequentsFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(afterSubsequentsFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(afterSubsequentsFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(afterSubsequentsFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
afterSubsequentsFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= afterSubsequentsFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= afterSubsequentsFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the level from location of the last move";
}
//-------------------------------------------------------------------------
}
| 3,073 | 22.646154 | 98 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/LastLevelTo.java | package game.functions.ints.last;
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.BaseIntFunction;
import main.Constants;
import other.context.Context;
import other.move.Move;
/**
* Returns the ``level to'' of the last move.
*
* @author Eric.Piette
*/
@Hide
public final class LastLevelTo extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** To get the last "level" after subsequents have been applied. */
private final BooleanFunction afterSubsequentsFn;
//-------------------------------------------------------------------------
/**
* @param afterSubsequents Whether to return the ``level'' after applying subsequents [False].
*/
public LastLevelTo
(
@Opt @Name final BooleanFunction afterSubsequents
)
{
afterSubsequentsFn = (afterSubsequents == null) ? new BooleanConstant(false) : afterSubsequents;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final Move action = context.trial().lastMove();
if (action != null)
if (afterSubsequentsFn.eval(context))
return action.levelToAfterSubsequents();
else
return action.levelTo();
return Constants.UNDEFINED;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return afterSubsequentsFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(afterSubsequentsFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(afterSubsequentsFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(afterSubsequentsFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
afterSubsequentsFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= afterSubsequentsFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= afterSubsequentsFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the level to location of the last move";
}
//-------------------------------------------------------------------------
}
| 3,060 | 22.546154 | 98 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/LastTo.java | package game.functions.ints.last;
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.BaseIntFunction;
import main.Constants;
import other.context.Context;
import other.move.Move;
/**
* Returns the ``to'' location of the last decision.
*
* @author Eric.Piette
*/
@Hide
public final class LastTo extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** To get the last "to" loc after applied the subsequents. */
private final BooleanFunction afterSubsequentsFn;
//-------------------------------------------------------------------------
/**
* @param afterSubsequents Whether to return the ``to'' location after applying
* the consequences [False].
*/
public LastTo
(
@Opt @Name final BooleanFunction afterSubsequents
)
{
afterSubsequentsFn = (afterSubsequents == null) ? new BooleanConstant(false) : afterSubsequents;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final Move move = context.trial().lastMove();
if (afterSubsequentsFn.eval(context))
{
if (move == null)
return Constants.UNDEFINED;
return move.toAfterSubsequents();
}
else
{
if (move == null)
return Constants.UNDEFINED;
return move.toNonDecision();
}
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return afterSubsequentsFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(afterSubsequentsFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(afterSubsequentsFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(afterSubsequentsFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
afterSubsequentsFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= afterSubsequentsFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= afterSubsequentsFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
final String str = "(LastTo)";
return str;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the to location of the last move";
}
//-------------------------------------------------------------------------
}
| 3,314 | 21.862069 | 98 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/LastType.java | package game.functions.ints.last;
/**
* Defines the types of Last integer ludeme.
*
* @author Eric.Piette
*/
public enum LastType
{
/** To return the ``from'' site of the last move. */
From,
/** To return the ``level from'' of the last move. */
LevelFrom,
/** To return the ``to'' site of the last move. */
To,
/** To return the ``level to'' site of the last move. */
LevelTo,
}
| 400 | 17.227273 | 57 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/last/package-info.java | /**
* Last is a `super' ludeme that returns a site related to the last move.
*/
package game.functions.ints.last;
| 116 | 22.4 | 73 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/match/MatchScore.java | package game.functions.ints.match;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.play.RoleType;
import main.Constants;
import other.context.Context;
/**
* Returns the match score of a player.
*
* @author Eric.Piette
*/
public final class MatchScore extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The match score of the player. */
private final IntFunction idPlayerFn;
//-------------------------------------------------------------------------
/**
* @param role The roleType of the player.
* @example (matchScore P1)
*/
public MatchScore(final RoleType role)
{
idPlayerFn = RoleType.toIntFunction(role);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int pid = idPlayerFn.eval(context);
if (context.parentContext() != null)
return context.parentContext().score(pid);
else if (context.isAMatch())
return context.score(pid);
return Constants.UNDEFINED;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return idPlayerFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(idPlayerFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(idPlayerFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(idPlayerFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
// nothing to do
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasSubgames())
{
game.addRequirementToReport("The ludeme (matchScore) is used but the game is not a match.");
missingRequirement = true;
}
missingRequirement |= idPlayerFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= idPlayerFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "MatchScore()";
}
}
| 2,732 | 20.864 | 95 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/match/package-info.java | /**
* Match functions return an integer value based on the state of the current match.
*/
package game.functions.ints.match;
| 127 | 24.6 | 83 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Abs.java | package game.functions.ints.math;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Return the absolute value of a value.
*
* @author Eric Piette
*/
public final class Abs extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which value. */
protected final IntFunction value;
/** Pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* Return the absolute value of a value.
*
* @param value The value.
* @example (abs (value Piece at:(to)))
*/
public Abs
(
final IntFunction value
)
{
this.value = value;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
return Math.abs(value.eval(context));
}
//-------------------------------------------------------------------------
/**
* @return The value.
*/
public IntFunction value()
{
return value;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return value.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return value.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(value.concepts(game));
concepts.set(Concept.Absolute.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(value.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(value.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
value.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= value.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= value.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
return "The absolute value of " + value.toEnglish(game);
}
}
| 2,767 | 19.352941 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Add.java | package game.functions.ints.math;
import java.util.BitSet;
import annotations.Alias;
import annotations.Or;
import game.Game;
import game.functions.intArray.IntArrayConstant;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Adds many values.
*
* @author Eric.Piette and cambolbro
*/
@Alias(alias = "+")
public final class Add extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The array to sum. */
private final IntArrayFunction array;
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* To add two values.
*
* @param a The first value.
* @param b The second value.
* @example (+ (value Piece at:(from)) (value Piece at:(to)))
*/
public Add
(
final IntFunction a,
final IntFunction b
)
{
array = new IntArrayConstant(new IntFunction[]
{ a, b });
}
/**
* To add all the values of a list.
*
* @param list The list of the values.
* @param array The array of values to sum.
* @example (+ {(value Piece at:(from)) (value Piece at:(to)) (value Piece
* at:(between))})
*/
public Add
(
@Or final IntFunction[] list,
@Or final IntArrayFunction array
)
{
int numNonNull = 0;
if (list != null)
numNonNull++;
if (array != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Add(): One 'list' or 'array' parameters must be non-null.");
this.array = (array != null) ? array : new IntArrayConstant(list);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
final int[] values = array.eval(context);
int sum = 0;
for (final int val : values)
sum += val;
return sum;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return array.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return array.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(array.concepts(game));
concepts.set(Concept.Addition.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(array.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(array.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= array.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= array.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
array.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public String toEnglish(final Game game)
{
return "add the following values: " + array.toEnglish(game);
}
}
| 3,588 | 20.620482 | 99 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Div.java | package game.functions.ints.math;
import java.util.BitSet;
import annotations.Alias;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* To divide a value by another.
*
* @author Eric.Piette and cambolbro
* @remarks The result will be an integer and round down the result.
*/
@Alias(alias = "/")
public final class Div extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The value to divide. */
private final IntFunction a;
/** To divide by b. */
private final IntFunction b;
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* To divide a value by another.
*
* @param a The value to divide.
* @param b To divide by b.
* @example (/ (value Piece at:(from)) (value Piece at:(to)))
*/
public Div
(
final IntFunction a,
final IntFunction b
)
{
this.a = a;
this.b = b;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
final int evalB = b.eval(context);
if (evalB == 0)
throw new IllegalArgumentException("Division by zero.");
return a.eval(context) / evalB;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return a.isStatic() && b.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return a.gameFlags(game) | b.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(a.concepts(game));
concepts.or(b.concepts(game));
concepts.set(Concept.Division.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(a.writesEvalContextRecursive());
writeEvalContext.or(b.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(a.readsEvalContextRecursive());
readEvalContext.or(b.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
a.preprocess(game);
b.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= a.missingRequirement(game);
missingRequirement |= b.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= a.willCrash(game);
willCrash |= b.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return a.toEnglish(game) + " divided by " + b.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 3,399 | 21.666667 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/If.java | package game.functions.ints.math;
import java.util.BitSet;
import game.Game;
import game.functions.booleans.BooleanConstant.FalseConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import other.concept.Concept;
import other.context.Context;
/**
* Returns a value according to a condition.
*
* @author Eric Piette
* @remarks This ludeme is used to get a different int depending on a condition in a value of a
* ludeme.
*/
public final class If extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which condition. */
private final BooleanFunction cond;
/** Value A. */
private final IntFunction valueA;
/** Value B. */
private final IntFunction valueB;
//-------------------------------------------------------------------------
/**
* Return a value according to a condition.
*
* @param cond The condition.
* @param valueA The integer returned if the condition is true.
* @param valueB The integer returned if the condition is false.
* @example (if (is Mover P1) 1 2)
*/
public If
(
final BooleanFunction cond,
final IntFunction valueA,
final IntFunction valueB
)
{
this.cond = cond;
this.valueA = valueA;
this.valueB = valueB;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (cond.eval(context))
return valueA.eval(context);
return valueB.eval(context);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return valueA.isStatic() && valueB.isStatic() && cond.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return cond.gameFlags(game) | valueA.gameFlags(game) | valueB.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(cond.concepts(game));
concepts.or(valueA.concepts(game));
concepts.or(valueB.concepts(game));
concepts.set(Concept.ConditionalStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(cond.writesEvalContextRecursive());
writeEvalContext.or(valueA.writesEvalContextRecursive());
writeEvalContext.or(valueB.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(cond.readsEvalContextRecursive());
readEvalContext.or(valueA.readsEvalContextRecursive());
readEvalContext.or(valueB.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
cond.preprocess(game);
valueA.preprocess(game);
valueB.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (cond instanceof FalseConstant)
{
game.addRequirementToReport("One of the condition of a (if ...) ludeme is \"false\" which is wrong.");
missingRequirement = true;
}
missingRequirement |= cond.missingRequirement(game);
missingRequirement |= valueA.missingRequirement(game);
missingRequirement |= valueB.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= cond.willCrash(game);
willCrash |= valueA.willCrash(game);
willCrash |= valueB.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "if " + cond.toEnglish(game) + " then " + valueA.toEnglish(game) + " otherwise " + valueB.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 4,053 | 24.496855 | 115 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Max.java | package game.functions.ints.math;
import java.util.BitSet;
import game.Game;
import game.functions.intArray.IntArrayConstant;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the maximum of specified values.
*
* @author Eric Piette
*/
public final class Max extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The array to minimise. */
private final IntArrayFunction array;
//-------------------------------------------------------------------------
/**
* For returning the maximum value between two values.
*
* @param valueA The first value.
* @param valueB The second value.
*
* @example (max (mover) (next))
*/
public Max
(
final IntFunction valueA,
final IntFunction valueB
)
{
array = new IntArrayConstant(new IntFunction[]
{ valueA, valueB });
}
/**
* For returning the maximum value between an array of values.
*
* @param array The array of values to maximise.
*
* @example (max (results from:(last To) to:(sites LineOfSight at:(from) All)
* (count Steps All (from) (to))))
*/
public Max(final IntArrayFunction array)
{
this.array = array;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int[] values = array.eval(context);
if (values.length == 0)
return Constants.UNDEFINED;
int max = values[0];
for (int i = 1; i < values.length; i++)
max = Math.max(max, values[i]);
return max;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return array.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return array.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(array.concepts(game));
concepts.set(Concept.Maximum.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(array.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(array.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
array.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= array.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= array.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
return "the maximum of the following values: " + array.toEnglish(game);
}
}
| 3,156 | 20.923611 | 78 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Min.java | package game.functions.ints.math;
import java.util.BitSet;
import game.Game;
import game.functions.intArray.IntArrayConstant;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the minimum of specified values.
*
* @author Eric.Piette
*/
public final class Min extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The array to minimise. */
private final IntArrayFunction array;
//-------------------------------------------------------------------------
/**
* For returning the minimum value between two values.
*
* @param valueA The first value.
* @param valueB The second value.
*
* @example (min (mover) (next))
*/
public Min
(
final IntFunction valueA,
final IntFunction valueB
)
{
array = new IntArrayConstant(new IntFunction[]
{ valueA, valueB });
}
/**
* For returning the minimum value between an array of values.
*
* @param array The array of values to minimise.
*
* @example (min (results from:(last To) to:(sites LineOfSight at:(from) All)
* (count Steps All (from) (to))))
*/
public Min(final IntArrayFunction array)
{
this.array = array;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int[] values = array.eval(context);
if (values.length == 0)
return Constants.UNDEFINED;
int min = values[0];
for (int i = 1; i < values.length; i++)
min = Math.min(min, values[i]);
return min;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return array.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return array.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(array.concepts(game));
concepts.set(Concept.Minimum.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(array.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(array.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
array.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= array.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= array.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
return "the minimum of the following values: " + array.toEnglish(game);
}
}
| 3,156 | 20.923611 | 78 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Mod.java | package game.functions.ints.math;
import java.util.BitSet;
import annotations.Alias;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the modulo of a value.
*
* @author Eric Piette
*/
@Alias(alias = "%")
public final class Mod extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Value. */
private final IntFunction value;
/** Modulos. */
private final IntFunction modulus;
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* @param value The value.
* @param modulo The modulo.
* @example (% (count Moves) 3)
*/
public Mod
(
final IntFunction value,
final IntFunction modulo
)
{
this.value = value;
modulus = modulo;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
return value.eval(context) % modulus.eval(context);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return value.isStatic() && modulus.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return value.gameFlags(game) | modulus.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(value.concepts(game));
concepts.or(modulus.concepts(game));
concepts.set(Concept.Modulo.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(value.writesEvalContextRecursive());
writeEvalContext.or(modulus.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(value.readsEvalContextRecursive());
readEvalContext.or(modulus.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
value.preprocess(game);
modulus.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= value.missingRequirement(game);
missingRequirement |= modulus.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= value.willCrash(game);
willCrash |= modulus.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return value.toEnglish(game) + " modulo " + modulus.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 3,239 | 21.816901 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Mul.java | package game.functions.ints.math;
import java.util.BitSet;
import annotations.Alias;
import annotations.Or;
import game.Game;
import game.functions.intArray.IntArrayConstant;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the product of values.
*
* @author Eric.Piette
*/
@Alias(alias = "*")
public final class Mul extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The array to multiply. */
private final IntArrayFunction array;
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* For the product of two values.
*
* @param a The first value.
* @param b The second value.
*
* @example (* (mover) (next))
*/
public Mul
(
final IntFunction a,
final IntFunction b
)
{
array = new IntArrayConstant(new IntFunction[]
{ a, b });
}
/**
* For the product of many values.
*
* @param list The list of the values.
* @param array The array of values to multiply.
* @example (* { (mover) (next) (prev) })
*/
public Mul
(
@Or final IntFunction[] list,
@Or final IntArrayFunction array
)
{
int numNonNull = 0;
if (list != null)
numNonNull++;
if (array != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Mul(): One 'list' or 'array' parameters must be non-null.");
this.array = (array != null) ? array : new IntArrayConstant(list);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
final int[] values = array.eval(context);
if (values.length == 0)
return 0;
int product = 1;
for (final int val : values)
product *= val;
return product;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return array.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return array.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(array.concepts(game));
concepts.set(Concept.Multiplication.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(array.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(array.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
array.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= array.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= array.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "multiply all the values in " + array.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 3,743 | 20.641618 | 99 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Pow.java | package game.functions.ints.math;
import java.util.BitSet;
import annotations.Alias;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Computes the first parameter to the power of the second parameter.
*
* @author cambolbro
*/
@Alias(alias = "^")
public final class Pow extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The value. */
private final IntFunction a;
/** The power. */
private final IntFunction b;
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* @param a The value.
* @param b The power.
* @example (^ (value Piece at:(last To)) 2)
*/
public Pow
(
final IntFunction a,
final IntFunction b
)
{
this.a = a;
this.b = b;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
return (int)(Math.pow(a.eval(context), b.eval(context)));
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return a.isStatic() && b.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return a.gameFlags(game) | b.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(a.concepts(game));
concepts.or(b.concepts(game));
concepts.set(Concept.Exponentiation.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(a.writesEvalContextRecursive());
writeEvalContext.or(b.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(a.readsEvalContextRecursive());
readEvalContext.or(b.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
a.preprocess(game);
b.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= a.missingRequirement(game);
missingRequirement |= b.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= a.willCrash(game);
willCrash |= b.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return a.toEnglish(game) + " to the power of " + b.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 3,185 | 21.27972 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/Sub.java | package game.functions.ints.math;
import java.util.BitSet;
import annotations.Alias;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the subtraction A minus B.
*
* @author Eric Piette
*/
@Alias(alias = "-")
public final class Sub extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which value A. */
private final IntFunction valueA;
/** Which value B. */
private final IntFunction valueB;
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* @param valueA The first value (to subtract from) [0].
* @param valueB The second value (to be subtracted from the first value).
*
* @example (- 1)
* @example (- (value Piece at:(last To)) (value Piece at:(last From)))
*/
public Sub
(
@Opt final IntFunction valueA,
final IntFunction valueB
)
{
this.valueA = valueA != null ? valueA : new IntConstant(0);
this.valueB = valueB;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
return valueA.eval(context) - valueB.eval(context);
}
//-------------------------------------------------------------------------
/**
* @return The original value.
*/
public IntFunction a()
{
return valueA;
}
/**
* @return The value to subtract.
*/
public IntFunction b()
{
return valueB;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return valueA.isStatic() && valueB.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return valueA.gameFlags(game) | valueB.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(valueA.concepts(game));
concepts.or(valueB.concepts(game));
concepts.set(Concept.Subtraction.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(valueA.writesEvalContextRecursive());
writeEvalContext.or(valueB.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(valueA.readsEvalContextRecursive());
readEvalContext.or(valueB.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
valueA.preprocess(game);
valueB.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= valueA.missingRequirement(game);
missingRequirement |= valueB.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= valueA.willCrash(game);
willCrash |= valueB.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
return valueA.toEnglish(game) + " minus " + valueB.toEnglish(game);
}
}
| 3,621 | 21.6375 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/math/package-info.java | /**
* Math functions return an integer value based on given inputs.
*/
package game.functions.ints.math;
| 107 | 20.6 | 64 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/Size.java | package game.functions.ints.size;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.size.array.SizeArray;
import game.functions.ints.size.connection.SizeGroup;
import game.functions.ints.size.connection.SizeTerritory;
import game.functions.ints.size.largePiece.SizeLargePiece;
import game.functions.ints.size.site.SizeStack;
import game.functions.region.RegionFunction;
import game.types.board.SiteType;
import game.types.play.RoleType;
import game.util.directions.AbsoluteDirection;
import game.util.directions.Direction;
import other.context.Context;
/**
* Returns the size of the specified property.
*
* @author Eric Piette
*/
@SuppressWarnings("javadoc")
public final class Size extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For the size of a stack.
*
* @param sizeType The property to return the size.
* @param array The array.
*
* @example (size Array (values Remembered))
*/
public static IntFunction construct
(
final SizeArrayType sizeType,
final IntArrayFunction array
)
{
switch (sizeType)
{
case Array:
return new SizeArray(array);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Size(): A SizeArrayType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For the size of a stack.
*
* @param sizeType The property to return the size.
* @param type The graph element type [default SiteType of the board].
* @param in The region to count.
* @param at The site from which to compute the count [(last To)].
*
* @example (size Stack at:(last To))
*/
public static IntFunction construct
(
final SizeSiteType sizeType,
@Opt final SiteType type,
@Opt @Or @Name final RegionFunction in,
@Opt @Or @Name final IntFunction at
)
{
int numNonNull = 0;
if (in != null)
numNonNull++;
if (at != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException(
"Size(): With SizeSiteType zero or one 'in' or 'at' parameters must be non-null.");
switch (sizeType)
{
case Stack:
return new SizeStack(type, in, at);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Size(): A SizeSiteType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For the size of large pieces currently placed.
*
* @param sizeType The property to return the size.
* @param type The graph element type [default site type of the board].
* @param in The region to look for large pieces.
* @param at The site to look for large piece.
*
* @example (size LargePiece at:(last To))
*/
public static IntFunction construct
(
final SizeLargePieceType sizeType,
@Opt final SiteType type,
@Or @Name final RegionFunction in,
@Or @Name final IntFunction at
)
{
int numNonNull = 0;
if (in != null)
numNonNull++;
if (at != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException(
"Size(): With SizeLargePiece one 'in' or 'at' parameters must be non-null.");
switch (sizeType)
{
case LargePiece:
return new SizeLargePiece(type, in, at);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Size(): A SizeLargePiece is not implemented.");
}
/**
* For the size of a group.
*
* @param sizeType The property to return the size.
* @param type The graph element type [default SiteType of the board].
* @param at The site to compute the group [(last To)].
* @param directions The type of directions from the site to compute the group
* [Adjacent].
* @param If The condition of the members of the group [(= (mover) (who
* at:(to)))].
*
* @example (size Group at:(last To) Orthogonal)
*/
public static IntFunction construct
(
final SizeGroupType sizeType,
@Opt final SiteType type,
@Name final IntFunction at,
@Opt final Direction directions,
@Opt @Name final BooleanFunction If
)
{
switch (sizeType)
{
case Group:
return new SizeGroup(type, at, directions, If);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Size(): A SizeGroupType is not implemented.");
}
/**
* For the size of a territory.
*
* @param sizeType The property to return the size.
* @param type The graph element type [default SiteType of the board].
* @param role The roleType of the player owning the components in the
* territory.
* @param player The index of the player owning the components in the
* territory.
* @param direction The type of directions from the site to compute the group
* [Adjacent].
*
* @example (size Territory P1)
*/
public static IntFunction construct
(
final SizeTerritoryType sizeType,
@Opt final SiteType type,
@Or final RoleType role,
@Or final game.util.moves.Player player,
@Opt final AbsoluteDirection direction
)
{
int numNonNull = 0;
if (role != null)
numNonNull++;
if (player != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Size(): With SizeTerritoryType only one role or player parameter must be non-null.");
switch (sizeType)
{
case Territory:
return new SizeTerritory(type, role, player, direction);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Size(): A SizeTerritoryType is not implemented.");
}
private Size()
{
// Make grammar pick up construct() and not default constructor
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Size.eval(): Should never be called directly.");
// return new Region();
}
@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.
}
} | 7,017 | 26.20155 | 124 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/SizeArrayType.java | package game.functions.ints.size;
/**
* Defines the types of array which the size can be returned.
*
* @author Eric.Piette
*/
public enum SizeArrayType
{
/** Size of an IntArrayFunction. */
Array,
} | 206 | 16.25 | 61 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/SizeGroupType.java | package game.functions.ints.size;
/**
* Defines the types of group which the size can be returned.
*
* @author Eric.Piette
*/
public enum SizeGroupType
{
/** Size of a group from a site. */
Group,
}
| 207 | 15 | 61 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/SizeLargePieceType.java | package game.functions.ints.size;
/**
* Defines the types of large piece which the size can be returned.
*
* @author Eric.Piette
*/
public enum SizeLargePieceType
{
/** Size of the sites used by large piece(s). */
LargePiece,
}
| 236 | 17.230769 | 67 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/SizeSiteType.java | package game.functions.ints.size;
/**
* Defines the types of sites which the size can be returned.
*
* @author Eric.Piette
*/
public enum SizeSiteType
{
/** Size of the stack at a location (if any). */
Stack,
}
| 219 | 15.923077 | 61 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/SizeTerritoryType.java | package game.functions.ints.size;
/**
* Defines the types of territory which the size can be returned.
*
* @author Eric.Piette
*/
public enum SizeTerritoryType
{
/** Size of the region surrounded by a specific player. */
Territory,
}
| 242 | 17.692308 | 65 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/package-info.java | /**
* Size is a `super' ludeme that returns the size of a specified property within
* the game, such as a stack, a group or a territory.
*/
package game.functions.ints.size;
| 177 | 28.666667 | 80 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/array/SizeArray.java | package game.functions.ints.size.array;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.intArray.IntArrayFunction;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
/**
* Returns the size of an IntArrayFunction.
*
* @author Eric.Piette
*/
@Hide
public final class SizeArray extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which array. */
private final IntArrayFunction array;
/**
* @param array The IntArrayFunction to return the size.
*/
public SizeArray
(
final IntArrayFunction array
)
{
this.array = array;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int[] intArray = array.eval(context);
return intArray.length;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return array.isStatic();
}
@Override
public long gameFlags(final Game game)
{
final long gameFlags = array.gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(array.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(array.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(array.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
array.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= array.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= array.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the size of " + array.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 2,443 | 19.537815 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/connection/SizeGroup.java | package game.functions.ints.size.connection;
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.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.util.directions.AbsoluteDirection;
import game.util.directions.Direction;
import gnu.trove.list.array.TIntArrayList;
import other.context.Context;
import other.state.container.ContainerState;
import other.topology.Topology;
import other.topology.TopologyElement;
/**
* Returns the size of the group from a site.
*
* @author Eric.Piette
*/
@Hide
public final class SizeGroup extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The starting location of the Group. */
private final IntFunction startLocationFn;
/** The condition */
private final BooleanFunction condition;
/** Direction chosen. */
private final DirectionsFunction dirnChoice;
/** The graph element type. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The type of the graph elements of the group.
* @param at The specific starting position needs to connect.
* @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.
*/
public SizeGroup
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt final Direction directions,
@Opt @Name final BooleanFunction If
)
{
startLocationFn = at;
this.type = type;
condition = If;
dirnChoice = (directions != null) ? directions.directionsFunctions()
: new Directions(AbsoluteDirection.Adjacent, null);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final Topology topology = context.topology();
final int from = startLocationFn.eval(context);
final ContainerState cs = context.containerState(0);
final int origFrom = context.from();
final int origTo = context.to();
final TIntArrayList groupSites = new TIntArrayList();
context.setTo(from);
if (condition == null || condition.eval(context))
groupSites.add(from);
final int what = cs.what(from, type);
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 && what == cs.what(to, type)
|| (condition != null && condition.eval(context))))
{
groupSites.add(to);
}
}
}
sitesExplored.add(site);
i++;
}
}
context.setTo(origTo);
context.setFrom(origFrom);
return groupSites.size();
}
//--------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = 0l;
gameFlags |= SiteType.gameFlags(type);
gameFlags |= startLocationFn.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.or(startLocationFn.concepts(game));
if (condition != null)
concepts.or(condition.concepts(game));
if (dirnChoice != null)
concepts.or(dirnChoice.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(startLocationFn.writesEvalContextRecursive());
if (condition != null)
writeEvalContext.or(condition.writesEvalContextRecursive());
if (dirnChoice != null)
writeEvalContext.or(dirnChoice.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(startLocationFn.readsEvalContextRecursive());
if (condition != null)
readEvalContext.or(condition.readsEvalContextRecursive());
if (dirnChoice != null)
readEvalContext.or(dirnChoice.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
if (condition != null)
condition.preprocess(game);
startLocationFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= startLocationFn.missingRequirement(game);
if (condition != null)
missingRequirement |= condition.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= startLocationFn.willCrash(game);
if (condition != null)
willCrash |= condition.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String conditionString = "";
if (condition != null)
conditionString = " if " + condition.toEnglish(game);
String directionString = "";
if (dirnChoice != null)
directionString = " in direction " + dirnChoice.toEnglish(game);
String typeString = "";
if (type != null)
typeString = " of type " + type.name();
return "the size of the group at " + startLocationFn.toEnglish(game) + directionString + typeString + conditionString;
}
//-------------------------------------------------------------------------
}
| 6,744 | 25.660079 | 120 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/connection/SizeTerritory.java | package game.functions.ints.size.connection;
import java.util.BitSet;
import java.util.List;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.types.play.RoleType;
import game.util.directions.AbsoluteDirection;
import game.util.graph.Step;
import gnu.trove.list.array.TIntArrayList;
import other.PlayersIndices;
import other.concept.Concept;
import other.context.Context;
import other.state.container.ContainerState;
import other.topology.Edge;
import other.topology.Topology;
import other.topology.TopologyElement;
/**
* Returns the total number of sites enclosed by a specific Player.
*
* @author eric.piette
*/
@Hide
public final class SizeTerritory extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The index of player. **/
private final IntFunction who;
/** The roleType. */
private final RoleType role;
/** Direction of the connection. */
private final AbsoluteDirection dirnChoice;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param role The roleType of the player owning the components in the
* territory.
* @param player The index of the player owning the components in the
* territory.
* @param direction The type of directions from the site to compute the group
* [Adjacent].
*/
public SizeTerritory
(
@Opt final SiteType type,
@Or final RoleType role,
@Or final game.util.moves.Player player,
@Opt final AbsoluteDirection direction
)
{
this.dirnChoice = (direction != null) ? direction : AbsoluteDirection.Adjacent;
this.who = (player == null) ? RoleType.toIntFunction(role) : player.index();
this.role = role;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
int sizeTerritory = 0;
final Topology topology = context.topology();
final ContainerState cs = context.state().containerStates()[0];
// Get all possible territory sites.
final TIntArrayList emptySites = new TIntArrayList(cs.emptyRegion(type).sites());
// Code to handle specific roleType.
final int whoId = who.eval(context);
final TIntArrayList idPlayers = PlayersIndices.getIdPlayers(context, role, whoId);
// Look if each empty site is in the territory of a player.
final TIntArrayList sitesExplored = new TIntArrayList();
for(int i = 0; i < emptySites.size(); i++)
{
final int site = emptySites.get(i);
if(sitesExplored.contains(site))
continue;
// Get group of empty sites from that site.
final TIntArrayList groupSites = new TIntArrayList();
groupSites.add(site);
final TIntArrayList groupSitesExplored = new TIntArrayList();
int indexGroup = 0;
while (groupSitesExplored.size() != groupSites.size())
{
final int siteGroup = groupSites.get(indexGroup);
final TopologyElement siteElement = topology.getGraphElements(type).get(siteGroup);
final List<game.util.graph.Step> steps = topology.trajectories().steps(type, siteElement.index(),
type, dirnChoice);
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 (cs.isEmpty(to, type))
groupSites.add(to);
}
groupSitesExplored.add(site);
indexGroup++;
}
sitesExplored.addAll(groupSites);
// Check if that group is owned by the right players.
if(checkTerritory(groupSites,topology,dirnChoice,type,cs,idPlayers))
sizeTerritory += groupSites.size();
}
return sizeTerritory;
}
/**
*
* @param sites The sites.
* @param graph The graph.
* @param dirnChoice The direction.
* @param type The type of graph element.
* @param cs The container state.
* @param playerTerritory The indices of the player supposed to own the territory.
* @return True if the sites are surrounded only by the expected players.
*/
static final boolean checkTerritory
(
final TIntArrayList sites,
final Topology graph,
final AbsoluteDirection dirnChoice,
final SiteType type,
final ContainerState cs,
final TIntArrayList playerTerritory
)
{
if (type.equals(SiteType.Edge))
{
for (int i = 0; i < sites.size();i++)
{
final int site = sites.get(i);
final Edge edge = graph.edges().get(site);
for (final Edge edgeAdj : edge.adjacent())
{
final int territorySite = edgeAdj.index();
if (!sites.contains(territorySite))
{
final int who = cs.who(territorySite, type);
if(!playerTerritory.contains(who))
return false;
}
}
}
}
else
{
for (int i = 0; i < sites.size();i++)
{
final int site = sites.get(i);
final List<Step> steps = graph.trajectories().steps(type, site, dirnChoice);
for (final Step step : steps)
{
if (step.from().siteType() != step.to().siteType())
continue;
final int to = step.to().id();
if (!sites.contains(to))
{
final int who = cs.who(to, type);
if(!playerTerritory.contains(who))
return false;
}
}
}
}
return true;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long flags = who.gameFlags(game);
flags |= SiteType.gameFlags(type);
return flags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(who.concepts(game));
concepts.or(SiteType.concepts(type));
concepts.set(Concept.Territory.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(who.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(who.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= who.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= who.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
who.preprocess(game);
type = SiteType.use(type, game);
}
}
| 7,090 | 25.657895 | 102 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/largePiece/SizeLargePiece.java | package game.functions.ints.size.largePiece;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.component.Component;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.region.RegionFunction;
import game.types.board.SiteType;
import gnu.trove.list.array.TIntArrayList;
import other.IntArrayFromRegion;
import other.context.Context;
import other.state.container.ContainerState;
/**
* Returns the size of large pieces currently placed..
*
* @author Eric.Piette
*/
@Hide
public final class SizeLargePiece extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** the type of player. */
private final IntArrayFromRegion region;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default site type of the board].
* @param in The region to look for large pieces.
* @param at The site to look for large piece.
*/
public SizeLargePiece
(
@Opt final SiteType type,
@Or @Name final RegionFunction in,
@Or @Name final IntFunction at
)
{
this.type = type;
this.region = new IntArrayFromRegion(at, in);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
int count = 0;
final SiteType realType = (type != null) ? type : context.game().board().defaultSite();
final int[] sites = region.eval(context);
for (int i = 0; i < sites.length; i++)
{
final int site = sites[i];
final int cid = (realType.equals(SiteType.Cell) ? context.containerId()[site] : 0);
final ContainerState cs = context.containerState(cid);
final int what = cs.what(site, realType);
if (what != 0)
{
final Component component = context.components()[what];
if (component.isLargePiece())
{
final TIntArrayList locs = component.locs(context,
context.topology().centre(realType).get(0).index(), 0, context.topology());
count += locs.size();
}
else
count++;
}
}
return count;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long flags = region.gameFlags(game);
flags |= SiteType.gameFlags(type);
return flags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(region.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(region.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
region.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasLargePiece())
{
game.addRequirementToReport(
"The ludeme (size LargePiece ...) is used but the equipment has no large pieces.");
missingRequirement = true;
}
missingRequirement |= region.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= region.willCrash(game);
return willCrash;
}
}
| 3,887 | 23.45283 | 89 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/size/site/SizeStack.java | package game.functions.ints.size.site;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or2;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.last.LastTo;
import game.functions.region.RegionFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import other.IntArrayFromRegion;
import other.context.Context;
import other.state.stacking.BaseContainerStateStacking;
/**
* Returns the size of a stack.
*
* @author Eric.Piette
*/
@Hide
public final class SizeStack extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which region. */
private final IntArrayFromRegion region;
/** Cell/Edge/Vertex. */
private SiteType type;
/**
* @param type The graph element type.
* @param in The region to count the size of the stacks.
* @param at The location to count the site of the stack.
*/
public SizeStack
(
@Opt final SiteType type,
@Opt @Or2 @Name final RegionFunction in,
@Opt @Or2 @Name final IntFunction at
)
{
region = new IntArrayFromRegion(
(in == null && at != null ? at : in == null ? new LastTo(null) : null),
(in != null) ? in : null);
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int[] sites;
int count = 0;
sites = region.eval(context);
for (final int site : sites)
{
final BaseContainerStateStacking state = (BaseContainerStateStacking) context.state()
.containerStates()[context.containerId()[site]];
count += state.sizeStack(site, type);
}
return count;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public String toString()
{
return "Stack()";
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = region.gameFlags(game) | GameType.Stacking;
gameFlags |= SiteType.gameFlags(type);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(region.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(region.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
region.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= region.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= region.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
final SiteType realType = (type == null) ? game.board().defaultSite() : type;
return "the size of the stack on " + realType.name().toLowerCase() + " " + region.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 3,691 | 22.515924 | 100 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/stacking/TopLevel.java | package game.functions.ints.stacking;
import java.util.BitSet;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.context.Context;
import other.state.container.ContainerState;
/**
* Returns the top level of a stack.
*
* @author Eric.Piette
*
* @remarks If the game is not a stacking game, then level 0 is returned.
*/
public final class TopLevel extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The location of the stack. */
private final IntFunction locn;
/** The graph element type. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param at The site of the stack.
*
* @example (topLevel at:(last To))
*/
public TopLevel
(
@Opt final SiteType type,
@Name final IntFunction at
)
{
locn = at;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (!context.game().isStacking())
return 0;
final int loc = locn.eval(context);
if (loc == Constants.UNDEFINED)
return 0;
final int cid = loc >= context.containerId().length ? 0 : context.containerId()[loc];
SiteType realType = type;
if (cid > 0)
{
realType = SiteType.Cell;
if ((loc - context.sitesFrom()[cid]) >= context.containers()[cid].topology().getGraphElements(realType).size())
return Constants.OFF;
}
else
{
if (loc >= context.containers()[cid].topology().getGraphElements(realType).size())
return Constants.OFF;
if (realType == null)
realType = context.board().defaultSite();
}
final ContainerState cs = context.state().containerStates()[cid];
final int sizeStack = cs.sizeStack(loc, realType);
if (sizeStack != 0)
return sizeStack - 1;
return 0;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = locn.gameFlags(game) | GameType.Stacking;
gameFlags |= SiteType.gameFlags(type);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(locn.concepts(game));
concepts.or(SiteType.concepts(type));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(locn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(locn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
locn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= locn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= locn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "Top(" + locn + ")";
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the top level of the stack on " + type.name().toLowerCase() + " " + locn.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 4,022 | 21.857955 | 114 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/stacking/package-info.java | /**
* Stacking functions return an integer value based on the state of a specified stack of components.
*/
package game.functions.ints.stacking;
| 147 | 28.6 | 100 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Amount.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Or;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.play.RoleType;
import game.types.state.GameType;
import main.Constants;
import other.context.Context;
/**
* Returns the amount of a player.
*
* @author Eric.Piette
*/
public final class Amount extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* The owner of the amount.
*/
final IntFunction playerFn;
//-------------------------------------------------------------------------
/**
* @param role The role of the player.
* @param player The index of the player.
*
* @example (amount Mover)
*/
public Amount
(
@Or final RoleType role,
@Or final game.util.moves.Player player
)
{
int numNonNull = 0;
if (role != null)
numNonNull++;
if (player != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Exactly one Or parameter must be non-null.");
playerFn = (player == null) ? RoleType.toIntFunction(role) : player.index();
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int player = playerFn.eval(context);
if (player < context.game().players().size() && player > 0)
return context.state().amount(player);
return Constants.UNDEFINED;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return playerFn.gameFlags(game) | GameType.Bet;
}
@Override
public BitSet concepts(final Game game)
{
return playerFn.concepts(game);
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(playerFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(playerFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
playerFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= playerFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= playerFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "Amount()";
}
@Override
public String toEnglish(final Game game)
{
return "the amount owned by " + playerFn.toEnglish(game);
}
}
| 2,984 | 20.170213 | 84 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Counter.java | package game.functions.ints.state;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the automatic counter of the game.
*
* @author Eric.Piette
* @remarks To use a counter automatically incremented at each move done, this
* can be set to another value by the move (setCounter).
*/
public final class Counter extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @example (counter)
*/
public Counter()
{
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.state().counter();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.set(Concept.InternalCounter.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 void preprocess(final Game game)
{
// nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "Counter()";
}
@Override
public String toEnglish(final Game game)
{
return "Counter";
}
}
| 1,779 | 17.93617 | 78 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Mover.java | package game.functions.ints.state;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
/**
* Returns the index of the current player.
*
* @author mrraow
* @remarks To apply some specific condition/rules to the current player.
*/
public final class Mover extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @example (mover)
*/
public Mover()
{
// Nothing to do.
}
//-------------------------------------------------------------------------
@Override
public final int eval(final Context context)
{
return context.state().mover();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// nothing to do
}
@Override
public String toEnglish(final Game game)
{
return "current moving player";
}
}
| 1,507 | 16.952381 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Next.java | package game.functions.ints.state;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
/**
* Returns the index of the next player.
*
* @author Eric.Piette
*
* @remarks This ludeme is used to apply some specific condition or rule to the next player.
*/
public final class Next extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Our singleton instance */
private static final Next INSTANCE = new Next();
//-------------------------------------------------------------------------
/**
* @example (next)
*/
private Next()
{
// Private constructor; singleton!
}
/**
* @return Singleton instance
*/
public static Next instance()
{
return INSTANCE;
}
/**
* @return Returns our singleton instance as a ludeme
*
* @example (next)
*/
public static Next construct()
{
return INSTANCE;
}
//-------------------------------------------------------------------------
@Override
public final int eval(final Context context)
{
return context.state().next();
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the next mover";
}
//-------------------------------------------------------------------------
}
| 2,028 | 17.445455 | 92 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Pot.java | package game.functions.ints.state;
import java.util.BitSet;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
/**
* Returns the pot of the game.
*
* @author Eric.Piette
*/
public final class Pot extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @example (pot)
*/
public Pot()
{
// Nothing to do.
}
//-------------------------------------------------------------------------
@Override
public final int eval(final Context context)
{
return context.state().pot();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the pot";
}
//-------------------------------------------------------------------------
}
| 1,562 | 16.965517 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Prev.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.types.play.PrevType;
import other.context.Context;
/**
* Returns the index of the previous player.
*
* @author Eric.Piette
* @remarks To apply some specific conditions/rules to the previous player.
*/
public final class Prev extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The type of the previous state. */
final PrevType type;
/**
* @param type The type of the previous state [Mover].
* @example (prev)
*/
public Prev
(
@Opt final PrevType type
)
{
this.type = (type == null) ? PrevType.Mover: type;
}
//-------------------------------------------------------------------------
@Override
public final int eval(final Context context)
{
if (type.equals(PrevType.Mover))
return context.state().prev();
else
return context.trial().lastTurnMover(context.state().mover());
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the previous " + type.name().toLowerCase();
}
//-------------------------------------------------------------------------
}
| 2,039 | 19.4 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Rotation.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.state.container.ContainerState;
import other.state.stacking.BaseContainerStateStacking;
/**
* Returns the rotation value of a specified site.
*
* @author Eric Piette
*/
public final class Rotation extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which location. */
private final IntFunction locn;
/** Which level (for a stacking game). */
private final IntFunction level;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check [0].
*
* @example (rotation at:(last To))
*/
public Rotation
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
this.locn = at;
this.level = level;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int loc = locn.eval(context);
if (loc == Constants.OFF)
return 0;
final int containerId = context.containerId()[loc];
if (context.game().isStacking() && containerId == 0)
{
// Is stacking game
final BaseContainerStateStacking state = (BaseContainerStateStacking) context.state()
.containerStates()[containerId];
if (level == null)
return state.rotation(loc, type);
return state.rotation(loc, level.eval(context), type);
}
final ContainerState cs = context.state().containerStates()[containerId];
return cs.rotation(loc, type);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// we're looking at state in a specific context, so not static
return false;
// return site.isStatic() && level.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long stateFlag = locn.gameFlags(game) | GameType.Rotation;
stateFlag |= SiteType.gameFlags(type);
if (level != null)
stateFlag |= level.gameFlags(game);
return stateFlag;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(locn.concepts(game));
concepts.set(Concept.PieceRotation.id(), true);
if (level != null)
concepts.or(level.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(locn.writesEvalContextRecursive());
if (level != null)
writeEvalContext.or(level.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(locn.readsEvalContextRecursive());
if (level != null)
readEvalContext.or(level.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
locn.preprocess(game);
if (level != null)
level.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= locn.missingRequirement(game);
if (level != null)
missingRequirement |= level.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= locn.willCrash(game);
if (level != null)
willCrash |= level.willCrash(game);
return willCrash;
}
}
| 4,113 | 22.11236 | 88 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Score.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Or;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.play.RoleType;
import game.types.state.GameType;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the score of one specific player.
*
* @author Eric.Piette
*/
public final class Score extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The index of the player. */
private final IntFunction playerFn;
//-------------------------------------------------------------------------
/**
* Score of one specific player.
*
* @param player The index of the player.
* @param role The roleType of the player.
* @example (score Mover)
*/
public Score
(
@Or final game.util.moves.Player player,
@Or final RoleType role
)
{
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.");
if (player != null)
playerFn = player.index();
else
playerFn = RoleType.toIntFunction(role);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.score(playerFn.eval(context));
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
final long gameFlags = GameType.Score;
return gameFlags | playerFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(playerFn.concepts(game));
concepts.set(Concept.Scoring.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(playerFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(playerFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
playerFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= playerFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= playerFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the score of Player " + playerFn.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 3,106 | 21.192857 | 84 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/State.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.state.container.ContainerState;
import other.state.stacking.BaseContainerStateStacking;
/**
* Returns the local state value of a specified site.
*
* @author Eric Piette
*
* @remarks This ludeme is used for games with local state values associated
* with pieces.
*/
public final class State extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which location. */
private final IntFunction locn;
/** Which level (for a stacking game). */
private final IntFunction level;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check [0].
*
* @example (state at:(last To))
*/
public State
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
locn = at;
this.level = level;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int loc = locn.eval(context);
if (loc == Constants.OFF)
return 0;
final int containerId = context.containerId()[loc];
if (context.game().isStacking() && containerId == 0)
{
// Is stacking game
final BaseContainerStateStacking state =
(BaseContainerStateStacking) context.state().containerStates()[containerId];
if (level == null)
return state.state(loc, type);
return state.state(loc, level.eval(context), type);
}
final ContainerState cs = context.state().containerStates()[containerId];
return cs.state(loc, type);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// we're looking at state in a specific context, so not static
return false;
//return site.isStatic() && level.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long stateFlag = locn.gameFlags(game) | GameType.SiteState;
stateFlag |= SiteType.gameFlags(type);
if (level != null)
stateFlag |= level.gameFlags(game);
return stateFlag;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(locn.concepts(game));
concepts.set(Concept.SiteState.id(), true);
if (level != null)
concepts.or(level.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(locn.writesEvalContextRecursive());
if (level != null)
writeEvalContext.or(level.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(locn.readsEvalContextRecursive());
if (level != null)
readEvalContext.or(level.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
locn.preprocess(game);
if (level != null)
level.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= locn.missingRequirement(game);
if (level != null)
missingRequirement |= level.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= locn.willCrash(game);
if (level != null)
willCrash |= level.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
return "State at " + locn.toEnglish(game);
}
}
| 4,315 | 21.957447 | 81 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Var.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.types.state.GameType;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the value stored in the var variable from the context.
*
* @author mrraow
* @remarks To identify the value stored previously with a key in the context.
* If no key specified, the var variable of the context is returned.
*/
public final class Var extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The key of the variable. */
private final String key;
//-------------------------------------------------------------------------
/**
* @param key The key String value to check.
* @example (var "current")
*/
public Var(@Opt final String key)
{
this.key = key;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (key == null)
return context.state().temp();
return context.state().getValue(key);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return GameType.MapValue;
}
@Override
public void preprocess(final Game game)
{
// nothing to do
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.set(Concept.Variable.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 String toString() {
return "GetVariable [key=" + key + "]";
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "state variable" + key;
}
//-------------------------------------------------------------------------
}
| 2,347 | 20.345455 | 78 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/What.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import main.Constants;
import other.context.Context;
import other.state.container.ContainerState;
import other.state.stacking.BaseContainerStateStacking;
/**
* Returns the index of the component at a specific location/level.
*
* @author Eric Piette and cambolbro
*/
public final class What extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which loc. */
private final IntFunction loc;
/** Which level (for a stacking game). */
private final IntFunction level;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check [0].
*
* @example (what at:(last To))
*/
public What
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
loc = at;
this.level = (level == null) ? new IntConstant(0) : level;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int siteIdx = loc.eval(context);
if (siteIdx == Constants.OFF)
return Constants.NO_PIECE;
final int containerId = context.containerId()[siteIdx];
if (context.game().isStacking())
{
// Is stacking game
final BaseContainerStateStacking state =
(BaseContainerStateStacking)context.state().containerStates()[containerId];
if (level.eval(context) == -1)
return state.what(siteIdx, type);
return state.what(siteIdx, level.eval(context), type);
}
final ContainerState cs = context.state().containerStates()[containerId];
return cs.what(siteIdx, type);
}
//-------------------------------------------------------------------------
/**
* @return The location to check.
*/
public IntFunction loc()
{
return loc;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// we're looking at the "what" in a specific context, so not static
return false;
//return site.isStatic() && level.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = loc.gameFlags(game) | level.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(loc.concepts(game));
concepts.or(level.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(loc.writesEvalContextRecursive());
writeEvalContext.or(level.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(loc.readsEvalContextRecursive());
readEvalContext.or(level.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
loc.preprocess(game);
level.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= loc.missingRequirement(game);
missingRequirement |= level.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= loc.willCrash(game);
willCrash |= level.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString() {
String str="(What ";
str += loc;
str += "," + level;
str += ")";
return str;
}
@Override
public String toEnglish(final Game game)
{
return "Piece at " + loc.toEnglish(game);
}
}
| 4,399 | 22.655914 | 80 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/Who.java | package game.functions.ints.state;
import java.util.BitSet;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.context.Context;
import other.state.container.ContainerState;
import other.state.stacking.BaseContainerStateStacking;
/**
* Returns the index of the owner at a specific location/level.
*
* @author Eric Piette and cambolbro
*/
public final class Who extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which location. */
private final IntFunction loc;
/** Which level (for a stacking game). */
private final IntFunction level;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check.
*
* @return Return the index of the owner of a specific location/level.
*
* @example (who at:(last To))
*/
public static IntFunction construct
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
if (level == null || (level.isStatic() && level.eval(null) == Constants.UNDEFINED))
return new WhoNoLevel(at, type);
else
return new Who(type, at, level);
}
/**
* Return the index of the owner of a specific location/level.
*
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check.
*
* @example (who at:(last To))
*/
private Who
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
loc = at;
this.level = (level == null) ? new IntConstant(Constants.UNDEFINED) : level;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int location = loc.eval(context);
if (location == Constants.OFF)
return Constants.NOBODY;
final int containerId = context.containerId()[location];
if ((context.game().gameFlags() & GameType.Stacking) != 0)
{
// Is stacking game
final BaseContainerStateStacking state = (BaseContainerStateStacking) context.state()
.containerStates()[containerId];
if (level.eval(context) == -1)
return state.who(location, type);
else
return state.who(location, level.eval(context), type);
}
final ContainerState cs = context.state().containerStates()[containerId];
return cs.who(loc.eval(context), type);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
// we're looking at the "who" in a specific context, so not static
return false;
//return site.isStatic() && level.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = loc.gameFlags(game) | level.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(loc.concepts(game));
concepts.or(level.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(loc.writesEvalContextRecursive());
writeEvalContext.or(level.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(loc.readsEvalContextRecursive());
readEvalContext.or(level.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= loc.missingRequirement(game);
missingRequirement |= level.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= loc.willCrash(game);
willCrash |= level.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
loc.preprocess(game);
level.preprocess(game);
}
//-------------------------------------------------------------------------
/**
* @return The function that generates the site we want to look at
*/
public IntFunction site()
{
return loc;
}
@Override
public String toEnglish(final Game game)
{
return "Player at " + loc.toEnglish(game);
}
//-------------------------------------------------------------------------
/**
* An optimised version of Who ludeme, without levels
*
* @author Eric Piette and cambolbro and Dennis Soemers
*/
public static class WhoNoLevel extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//---------------------------------------------------------------------
/** Which site. */
protected final IntFunction site;
/** Cell/Edge/Vertex. */
private SiteType type;
//---------------------------------------------------------------------
/**
* Constructor.
*
* @param site
* @param type
*/
public WhoNoLevel(final IntFunction site, @Opt final SiteType type)
{
this.site = site;
this.type = type;
}
//---------------------------------------------------------------------
@Override
public final int eval(final Context context)
{
final int location = site.eval(context);
if (location < 0)
return Constants.NOBODY;
final int containerId = type.equals(SiteType.Cell) ? context.containerId()[location] : 0;
final ContainerState cs = context.state().containerStates()[containerId];
return cs.who(location, type);
}
//---------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = site.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(site.concepts(game));
return concepts;
}
@Override
public void preprocess(final Game game)
{
if (type == null)
type = game.board().defaultSite();
site.preprocess(game);
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(site.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(site.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= site.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= site.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
return "Player at " + site.toEnglish(game);
}
}
}
| 7,727 | 22.925697 | 92 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/state/package-info.java | /**
* State functions return an integer value based on the current game state.
*/
package game.functions.ints.state;
| 119 | 23 | 75 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/tile/PathExtent.java | package game.functions.ints.tile;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.component.Component;
import game.functions.directions.Directions;
import game.functions.directions.DirectionsFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.last.LastTo;
import game.functions.ints.state.Mover;
import game.functions.region.RegionFunction;
import game.functions.region.sites.simple.SitesLastTo;
import game.types.board.RelationType;
import game.types.board.SiteType;
import game.util.directions.AbsoluteDirection;
import game.util.directions.RelativeDirection;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.state.container.ContainerState;
import other.topology.Cell;
import other.topology.Topology;
/**
* Returns the maximum extent of a path.
*
* @author Eric.Piette and cambolbro
*
* @remarks The path extent is the maximum board width and/or height that the path extends to.
* This is used in tile-based games with paths, such as Trax.
*/
public final class PathExtent extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The colour of the path. */
private final IntFunction colourFn;
/** The starting point of the path. */
private final IntFunction startFn;
/** The starting points of the path. */
private final RegionFunction regionStartFn;
//-------------------------------------------------------------------------
/**
* @param colour The colour of the path [(mover)].
* @param start The starting point of the path [(lastTo)].
* @param regionStart The starting points of the path [(regionLastToMove)].
*
* @example (pathExtent (mover))
*/
public PathExtent
(
@Opt IntFunction colour,
@Or @Opt IntFunction start,
@Or @Opt RegionFunction regionStart
)
{
int numNonNull = 0;
if (start != null)
numNonNull++;
if (regionStart != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException("Zero or one Or parameter can be non-null.");
this.colourFn = (colour == null) ? new Mover() : colour;
this.startFn = (start == null) ? new LastTo(null) : start;
this.regionStartFn = (regionStart == null) ? ((start == null) ? new SitesLastTo() : null) : regionStart;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int[] regionToCheck;
if (regionStartFn != null)
{
final Region region = regionStartFn.eval(context);
regionToCheck = region.sites();
}
else
{
regionToCheck = new int[1];
regionToCheck[0] = startFn.eval(context);
}
int maxExtent = 0;
for (int p = 0; p < regionToCheck.length; p++)
{
final int from = regionToCheck[p];
if (from == Constants.UNDEFINED)
return maxExtent;
// final int length = extentFn.eval(context);
final int colourLoop = colourFn.eval(context);
final Topology graph = context.topology();
final Cell fromCell = graph.cells().get(from);
final int fromRow = fromCell.row();
final int fromCol = fromCell.col();
final int cid = context.containerId()[from];
final ContainerState cs = context.state().containerStates()[cid];
final int whatSideId = cs.what(from, SiteType.Cell);
if (whatSideId == 0 || !context.components()[whatSideId].isTile())
return maxExtent;
final DirectionsFunction directionFunction = new Directions(RelativeDirection.Forward, null,
RelationType.Orthogonal, null);
final int ratioAdjOrtho = context.topology().numEdges();
final TIntArrayList tileConnected = new TIntArrayList();
final TIntArrayList originTileConnected = new TIntArrayList();
tileConnected.add(from);
originTileConnected.add(from);
for (int index = 0; index < tileConnected.size(); index++)
{
final int site = tileConnected.getQuick(index);
final Cell cell = graph.cells().get(site);
final int what = cs.what(site, SiteType.Cell);
final Component component = context.components()[what];
final int rotation = (cs.rotation(site, SiteType.Cell) * 2) / ratioAdjOrtho;
final game.equipment.component.tile.Path[] paths = Arrays.copyOf(component.paths(),
component.paths().length);
for (int i = 0; i < paths.length; i++)
{
final game.equipment.component.tile.Path path = paths[i];
if (path.colour().intValue() == colourLoop)
{
// Side1
final List<AbsoluteDirection> directionsStep1 = directionFunction.convertToAbsolute(SiteType.Cell,
cell, null, null, Integer.valueOf(path.side1(rotation,
graph.numEdges())),
context);
final AbsoluteDirection directionSide1 = directionsStep1.get(0);
final List<game.util.graph.Step> stepsSide1 = graph.trajectories().steps(SiteType.Cell,
cell.index(),
SiteType.Cell, directionSide1);
if (stepsSide1.size() != 0)
{
final int site1Connected = stepsSide1.get(0).to().id();
final Cell cell1Connected = graph.cells().get(site1Connected);
final int rowCell1 = cell1Connected.row();
final int colCell1 = cell1Connected.col();
final int drow = Math.abs(rowCell1 - fromRow);
final int dcol = Math.abs(colCell1 - fromCol);
if (drow > maxExtent)
maxExtent = drow;
if (dcol > maxExtent)
maxExtent = dcol;
final int whatSide1 = cs.what(site1Connected, SiteType.Cell);
if (originTileConnected.getQuick(index) != site1Connected && whatSide1 != 0
&& context.components()[whatSide1].isTile())
{
tileConnected.add(site1Connected);
originTileConnected.add(site);
}
}
// Side 2
final List<AbsoluteDirection> directionsSide2 = directionFunction.convertToAbsolute(
SiteType.Cell, cell, null, null,
Integer.valueOf(path.side2(rotation, graph.numEdges())), context);
final AbsoluteDirection directionSide2 = directionsSide2.get(0);
final List<game.util.graph.Step> stepsSide2 = graph.trajectories().steps(SiteType.Cell,
cell.index(), SiteType.Cell, directionSide2);
if (stepsSide2.size() != 0)
{
final int site2Connected = stepsSide2.get(0).to().id();
final Cell cell2Connected = graph.cells().get(site2Connected);
final int rowCell2 = cell2Connected.row();
final int colCell2 = cell2Connected.col();
final int drow = Math.abs(rowCell2 - fromRow);
final int dcol = Math.abs(colCell2 - fromCol);
if (drow > maxExtent)
maxExtent = drow;
if (dcol > maxExtent)
maxExtent = dcol;
final int whatSide2 = cs.what(site2Connected, SiteType.Cell);
if (originTileConnected.getQuick(index) != site2Connected && whatSide2 != 0
&& context.components()[whatSide2].isTile())
{
tileConnected.add(site2Connected);
originTileConnected.add(site);
}
}
}
}
}
}
return maxExtent;
}
//-----------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = colourFn.gameFlags(game) | startFn.gameFlags(game);
if (regionStartFn != null)
gameFlags |= regionStartFn.gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(colourFn.concepts(game));
concepts.or(startFn.concepts(game));
concepts.set(Concept.PathExtent.id(), true);
if (regionStartFn != null)
concepts.or(regionStartFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(colourFn.writesEvalContextRecursive());
writeEvalContext.or(startFn.writesEvalContextRecursive());
if (regionStartFn != null)
writeEvalContext.or(regionStartFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
colourFn.preprocess(game);
if (startFn != null)
startFn.preprocess(game);
if (regionStartFn != null)
regionStartFn.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
boolean gameHasTile = false;
for (int i = 1; i < game.equipment().components().length; i++)
if (game.equipment().components()[i].isTile())
{
gameHasTile = true;
break;
}
if (!gameHasTile)
{
game.addRequirementToReport("The ludeme (pathExtent ...) is used but the equipment has no tiles.");
missingRequirement = true;
}
missingRequirement |= colourFn.missingRequirement(game);
missingRequirement |= startFn.missingRequirement(game);
if (regionStartFn != null)
missingRequirement |= regionStartFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= colourFn.willCrash(game);
willCrash |= startFn.willCrash(game);
if (regionStartFn != null)
willCrash |= regionStartFn.willCrash(game);
return willCrash;
}
}
| 9,644 | 29.23511 | 106 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/tile/package-info.java | /**
* Tile functions return an integer value based on the current state of specified {\tt Tile} components.
*/
package game.functions.ints.tile;
| 147 | 28.6 | 104 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/TrackSite.java | package game.functions.ints.trackSite;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.trackSite.first.TrackSiteFirstTrack;
import game.functions.ints.trackSite.move.TrackSiteMove;
import game.functions.ints.trackSite.position.TrackSiteEndTrack;
import game.types.play.RoleType;
import other.context.Context;
/**
* Returns a site on a track.
*
* @author Eric Piette
*/
@SuppressWarnings("javadoc")
public final class TrackSite extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
/**
* For the first site in a track.
*
* @param trackSiteType The type of site on the track.
* @param player The index of the player.
* @param role The role of the player.
* @param name The name of the track ["Track"].
* @param from The site from where to look [First site of the track].
* @param If The condition to verify for that site [True].
*
* @example (trackSite FirstSite if:(is Empty (to)))
*/
public static IntFunction construct
(
final TrackSiteFirstType trackSiteType,
@Or @Opt final game.util.moves.Player player,
@Or @Opt final RoleType role,
@Opt final String name,
@Name @Opt final IntFunction from,
@Name @Opt final BooleanFunction If
)
{
switch (trackSiteType)
{
case FirstSite:
return new TrackSiteFirstTrack(player, role, name, from, If);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("TrackSite(): A TrackSiteFirstType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For the last site in a track.
*
* @param trackSiteType The type of site on the track.
* @param player The index of the player.
* @param role The role of the player.
* @param name The name of the track ["Track"].
*
* @example (trackSite EndSite)
*/
public static IntFunction construct
(
final TrackSiteType trackSiteType,
@Or @Opt final game.util.moves.Player player,
@Or @Opt final RoleType role,
@Opt final String name
)
{
switch (trackSiteType)
{
case EndSite:
return new TrackSiteEndTrack(player, role, name);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("TrackSite(): A TrackSiteType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For getting the site in a track from a site after some steps.
*
* @param trackSiteType The type of site on the track.
* @param from The current location [(from)].
* @param player The owner of the track [(mover)].
* @param role The role of the owner of the track [Mover].
* @param name The name of the track.
* @param steps The distance to move on the track.
*
* @example (trackSite Move steps:(count Pips))
*/
public static IntFunction construct
(
final TrackSiteMoveType trackSiteType,
@Opt @Name final IntFunction from,
@Opt @Or final RoleType role,
@Opt @Or final game.util.moves.Player player,
@Opt @Or final String name,
@Name final IntFunction steps
)
{
switch (trackSiteType)
{
case Move:
return new TrackSiteMove(from, role, player, name, steps);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("TrackSite(): A TrackSiteMoveType is not implemented.");
}
//-------------------------------------------------------------------------
private TrackSite()
{
// Make grammar pick up construct() and not default constructor
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("TrackSite.eval(): Should never be called directly.");
// return new Region();
}
@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.
}
} | 4,697 | 27.822086 | 96 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/TrackSiteFirstType.java | package game.functions.ints.trackSite;
/**
* Defines the types of sites related to a track.
*
* @author Eric.Piette
*/
public enum TrackSiteFirstType
{
/** The first site of a track. */
FirstSite,
}
| 207 | 15 | 49 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/TrackSiteMoveType.java | package game.functions.ints.trackSite;
/**
* Defines the types of sites related to a track.
*
* @author Eric.Piette
*/
public enum TrackSiteMoveType
{
/**
* The corresponding site on the track from a site and according to a number of
* steps.
*/
Move,
}
| 268 | 15.8125 | 80 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/TrackSiteType.java | package game.functions.ints.trackSite;
/**
* Defines the types of sites related to a track.
*
* @author Eric.Piette
*/
public enum TrackSiteType
{
/** The end site of a track. */
EndSite,
}
| 198 | 14.307692 | 49 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/package-info.java | /**
* TrackSite is a `super' ludeme that returns a site on a track.
*/
package game.functions.ints.trackSite;
| 112 | 21.6 | 64 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/first/TrackSiteFirstTrack.java | package game.functions.ints.trackSite.first;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.container.board.Track;
import game.functions.booleans.BooleanConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.play.RoleType;
import main.Constants;
import other.context.Context;
import other.context.EvalContextData;
/**
* Returns the first site of a track.
*
* @author Eric.Piette
*/
@Hide
public final class TrackSiteFirstTrack extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The name of the track. */
private final String name;
/** Player Id function. */
private final IntFunction pidFn;
/** The site from which to look. */
private final IntFunction fromFn;
/** The condition to verify. */
private final BooleanFunction condFn;
//-------------------------------------------------------------------------
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* @param player The index of the player.
* @param role The role of the player.
* @param name The name of the track.
* @param from The site from where to look [First site of the track].
* @param If The condition to verify for that site [True].
*/
public TrackSiteFirstTrack
(
@Or @Opt final game.util.moves.Player player,
@Or @Opt final RoleType role,
@Opt final String name,
@Name @Opt final IntFunction from,
@Name @Opt final BooleanFunction If
)
{
this.name = name;
pidFn = (player != null) ? player.index() : (role != null) ? RoleType.toIntFunction(role) : null;
fromFn = (from == null) ? null : from;
condFn = (If == null) ? new BooleanConstant(true) : If;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
final int playerId = (pidFn != null) ? pidFn.eval(context) : 0;
Track track = null;
for (final Track t : context.tracks())
{
if (name != null && playerId == 0)
{
if (t.name().contains(name))
{
track = t;
break;
}
}
else if (name != null)
{
if (name != null)
{
if (t.name().contains(name) && t.owner() == playerId)
{
track = t;
break;
}
}
}
else if (t.owner() == playerId || t.owner() == 0)
{
track = t;
break;
}
}
if (track == null)
{
if (context.game().board().tracks().size() == 0)
return Constants.UNDEFINED; // no track at all.
else
track = context.game().board().tracks().get(0);
}
// Get first site.
final int from = (fromFn == null) ? Constants.UNDEFINED : fromFn.eval(context);
boolean found = false;
int i = 0;
for(; i < track.elems().length; i++)
{
final int site = track.elems()[i].site;
if(from == Constants.UNDEFINED || site == from)
{
found = true;
break;
}
}
if(!found)
return Constants.UNDEFINED;
found = false;
final int origTo = context.to();
// Check the condition.
for(int j = i; j < track.elems().length + i; j++)
{
final int index = j % track.elems().length;
final int site = track.elems()[index].site;
context.setTo(site);
if(condFn.eval(context))
{
found = true;
i = index;
break;
}
}
context.setTo(origTo);
if(!found)
return Constants.UNDEFINED;
return track.elems()[i].site;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
if(pidFn != null && !pidFn.isStatic())
return false;
if(condFn != null && !condFn.isStatic())
return false;
return pidFn == null || pidFn.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long flags = 0L;
if (pidFn != null)
flags |= pidFn.gameFlags(game);
if (fromFn != null)
flags |= fromFn.gameFlags(game);
if (condFn != null)
flags |= condFn.gameFlags(game);
return flags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
if (pidFn != null)
concepts.or(pidFn.concepts(game));
if (fromFn != null)
concepts.or(fromFn.concepts(game));
if (condFn != null)
concepts.or(condFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
if (pidFn != null)
writeEvalContext.or(pidFn.writesEvalContextRecursive());
if (fromFn != null)
writeEvalContext.or(fromFn.writesEvalContextRecursive());
if (condFn != null)
writeEvalContext.or(condFn.writesEvalContextRecursive());
writeEvalContext.set(EvalContextData.To.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
if (pidFn != null)
readEvalContext.or(pidFn.readsEvalContextRecursive());
if (fromFn != null)
readEvalContext.or(fromFn.readsEvalContextRecursive());
if (condFn != null)
readEvalContext.or(condFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
if (pidFn != null)
pidFn.preprocess(game);
if (fromFn != null)
fromFn.preprocess(game);
if (condFn != null)
condFn.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasTrack())
{
game.addRequirementToReport("The ludeme (trackSite EndTrack ...) is used but the board has no tracks.");
missingRequirement = true;
}
if (pidFn != null)
missingRequirement |= pidFn.missingRequirement(game);
if (fromFn != null)
missingRequirement |= fromFn.missingRequirement(game);
if (condFn != null)
missingRequirement |= condFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (pidFn != null)
willCrash |= pidFn.willCrash(game);
if (fromFn != null)
willCrash |= fromFn.willCrash(game);
if (condFn != null)
willCrash |= condFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "";
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the first site of track \"" + name + "\"";
}
//-------------------------------------------------------------------------
} | 7,063 | 21.787097 | 107 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/move/TrackSiteMove.java | package game.functions.ints.trackSite.move;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.container.board.Track;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.iterator.From;
import game.functions.ints.state.Mover;
import game.types.play.RoleType;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.context.Context;
import other.state.container.ContainerState;
import other.state.track.OnTrackIndices;
/**
* Returns the new site on the player track on function of the current position
* of the component and the number of steps to move forward.
*
* @author Eric Piette
*
* @remarks Applies to any game with a defined track.
*/
@Hide
public final class TrackSiteMove extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which Index. */
private final IntFunction currentLocation;
/** Which Index. */
private final IntFunction steps;
/** Which player. */
private final IntFunction player;
/** Track name. */
private final String name;
//-------------------------------------------------------------------------
/** Pre-computed track if we are sure of the track. */
private final Track preComputedTrack = null;
//-------------------------------------------------------------------------
/**
* @param from The current location [(from)].
* @param player The owner of the track [(mover)].
* @param role The role of the owner of the track [Mover].
* @param name The name of the track.
* @param steps The distance to move on the track.
*/
public TrackSiteMove
(
@Opt @Name final IntFunction from,
@Opt @Or final RoleType role,
@Opt @Or final game.util.moves.Player player,
@Opt @Or final String name,
@Name final IntFunction steps
)
{
int numNonNull = 0;
if (player != null)
numNonNull++;
if (name != null)
numNonNull++;
if (role != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException("Zero or one Or parameter can be non-null.");
this.player = (player == null && role == null) ? new Mover()
: (role != null) ? RoleType.toIntFunction(role) : player.index();
this.steps = steps;
currentLocation = (from == null) ? new From(null) : from;
this.name = name;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int playerId = player.eval(context);
Track track = preComputedTrack;
if (name != null)
for (final Track t : context.game().board().tracks())
if (t.name().contains(name) && t.owner() == playerId)
{
track = t;
break;
}
if (track == null && name != null) // The track was not precomputed because it is not owned by a player.
for (final Track t : context.game().board().tracks())
if (t.name().contains(name))
{
track = t;
break;
}
if (track == null) // The track was not precomputed because it is owned by a player.
{
final Track[] tracks = context.board().ownedTracks(playerId);
if (tracks.length != 0)
{
track = tracks[0];
}
else
{
final Track[] tracksWithNoOwner = context.board().ownedTracks(0);
if (tracksWithNoOwner.length != 0)
track = tracksWithNoOwner[0];
}
}
if (track == null)
return Constants.OFF; // no track for this player
int i = track.elems().length;
if (currentLocation == null)
{
i = Constants.OFF;
}
else
{
final int currentLoc = currentLocation.eval(context);
if (!track.islooped() && context.game().hasInternalLoopInTrack())
{
if (currentLoc < 0)
return Constants.OFF;
// We get the component on the current location.
final ContainerState cs = context.containerState(context.containerId()[currentLoc]);
int what = cs.what(currentLoc, context.board().defaultSite());
// For stacking game, we get a piece owned by the owner of the track.
final int sizeStack = cs.sizeStack(currentLoc, context.board().defaultSite());
for (int lvl = 0; lvl < sizeStack; lvl++)
{
final int who = cs.who(currentLoc, lvl, context.board().defaultSite());
if (who == playerId)
{
what = cs.what(currentLoc, lvl, context.board().defaultSite());
break;
}
}
// We get the current index on the track according to the onTrackIndices
// structure.
if (what != 0)
{
final OnTrackIndices onTrackIndices = context.state().onTrackIndices();
final int trackIdx = track.trackIdx();
final TIntArrayList locsToIndex = onTrackIndices.locToIndex(trackIdx, currentLoc);
for (int j = 0; j < locsToIndex.size(); j++)
{
final int index = locsToIndex.getQuick(j);
final int count = onTrackIndices.whats(trackIdx, what, index);
if (count > 0)
{
i = index;
break;
}
}
}
else // If no piece, we just try to found the corresponding index for the current
// location.
{
for (i = 0; i < track.elems().length; i++)
if (track.elems()[i].site == currentLoc)
break;
}
} // If the track is a full loop like the mancala games, we just try to found the
// corresponding index for the current location.
else
{
for (i = 0; i < track.elems().length; i++)
if (track.elems()[i].site == currentLoc)
break;
}
}
final int numSteps = steps.eval(context);
i += numSteps >= 0 ? numSteps : 0;
if (i < track.elems().length)
return track.elems()[i].site;
// To manage the loop track
if (track.elems()[track.elems().length - 1].next != Constants.OFF)
{
while (true)
{
i -= track.elems().length;
if (i == 0)
return track.elems()[track.elems().length - 1].next;
if ((i - 1) < track.elems().length)
return track.elems()[i - 1].next;
}
}
return Constants.OFF;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
boolean isStatic = player.isStatic();
if (steps != null)
isStatic = isStatic && steps.isStatic();
if (currentLocation != null)
isStatic = isStatic && currentLocation.isStatic();
return isStatic;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = player.gameFlags(game);
if (steps != null)
gameFlags = gameFlags | steps.gameFlags(game);
if (currentLocation != null)
gameFlags = gameFlags | currentLocation.gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(player.concepts(game));
if (steps != null)
concepts.or(steps.concepts(game));
if (currentLocation != null)
concepts.or(currentLocation.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(player.writesEvalContextRecursive());
if (steps != null)
writeEvalContext.or(steps.writesEvalContextRecursive());
if (currentLocation != null)
writeEvalContext.or(currentLocation.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(player.readsEvalContextRecursive());
if (steps != null)
readEvalContext.or(steps.readsEvalContextRecursive());
if (currentLocation != null)
readEvalContext.or(currentLocation.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
player.preprocess(game);
if (steps != null)
steps.preprocess(game);
if (currentLocation != null)
currentLocation.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasTrack())
{
game.addRequirementToReport("The ludeme (trackSite Move ...) is used but the board has no tracks.");
missingRequirement = true;
}
missingRequirement |= player.missingRequirement(game);
if (steps != null)
missingRequirement |= steps.missingRequirement(game);
if (currentLocation != null)
missingRequirement |= currentLocation.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= player.willCrash(game);
if (steps != null)
willCrash |= steps.willCrash(game);
if (currentLocation != null)
willCrash |= currentLocation.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return steps.toEnglish(game) + " steps forward from site " + currentLocation.toEnglish(game) + " on track " + name;
}
//-------------------------------------------------------------------------
}
| 9,201 | 25.827988 | 117 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/trackSite/position/TrackSiteEndTrack.java | package game.functions.ints.trackSite.position;
import java.util.BitSet;
import annotations.Hide;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.equipment.container.board.Track;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.play.RoleType;
import main.Constants;
import other.context.Context;
/**
* Returns the last site of a track.
*
* @author Eric.Piette
*/
@Hide
public final class TrackSiteEndTrack extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The name of the track. */
private final String name;
/** Player Id function. */
private final IntFunction pidFn;
//-------------------------------------------------------------------------
/** The pre-computed value. */
private int precomputedValue = Constants.OFF;
//-------------------------------------------------------------------------
/**
* @param player The index of the player.
* @param role The role of the player.
* @param name The name of the track.
*/
public TrackSiteEndTrack
(
@Or @Opt final game.util.moves.Player player,
@Or @Opt final RoleType role,
@Opt final String name
)
{
this.name = name;
pidFn = (player != null) ? player.index() : (role != null) ? RoleType.toIntFunction(role) : null;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
if (precomputedValue != Constants.OFF)
return precomputedValue;
final int playerId = (pidFn != null) ? pidFn.eval(context) : 0;
Track track = null;
for (final Track t : context.tracks())
{
if (name != null && playerId == 0)
{
if (t.name().contains(name))
{
track = t;
break;
}
}
else if (name != null)
{
if (name != null)
{
if (t.name().contains(name) && t.owner() == playerId)
{
track = t;
break;
}
}
}
else if (t.owner() == playerId || t.owner() == 0)
{
track = t;
break;
}
}
if (track == null)
{
if (context.game().board().tracks().size() == 0)
return Constants.UNDEFINED; // no track at all.
else
track = context.game().board().tracks().get(0);
}
// Check if the track is empty.
if (track.elems().length == 0)
return Constants.UNDEFINED;
return track.elems()[track.elems().length - 1].site;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return pidFn == null || pidFn.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long flags = 0L;
if (pidFn != null)
flags |= pidFn.gameFlags(game);
return flags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
if (pidFn != null)
concepts.or(pidFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
if (pidFn != null)
writeEvalContext.or(pidFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
if (pidFn != null)
readEvalContext.or(pidFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
if (pidFn != null)
pidFn.preprocess(game);
if (isStatic())
precomputedValue = eval(new Context(game, null));
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (!game.hasTrack())
{
game.addRequirementToReport("The ludeme (trackSite EndTrack ...) is used but the board has no tracks.");
missingRequirement = true;
}
if (pidFn != null)
missingRequirement |= pidFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (pidFn != null)
willCrash |= pidFn.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "";
}
@Override
public String toEnglish(final Game game)
{
String trackName = name;
if (trackName == null)
trackName = "the board's track";
return "the last site of " + trackName;
}
//-------------------------------------------------------------------------
} | 4,599 | 20.395349 | 107 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/Value.java | package game.functions.ints.value;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.functions.ints.value.iterated.ValueIterated;
import game.functions.ints.value.piece.ValuePiece;
import game.functions.ints.value.player.ValuePlayer;
import game.functions.ints.value.random.ValueRandom;
import game.functions.ints.value.simple.ValuePending;
import game.functions.ints.value.simple.ValueMoveLimit;
import game.functions.ints.value.simple.ValueTurnLimit;
import game.functions.range.RangeFunction;
import game.types.board.SiteType;
import game.types.play.RoleType;
import other.context.Context;
/**
* Returns the value of the specified property.
*
* @author Eric Piette
*/
@SuppressWarnings("javadoc")
public final class Value extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For returning a random value within a range.
*
* @param valueType The property to return the value.
* @param range The range.
*
* @example (value Random (range 3 5))
*/
public static IntFunction construct
(
final ValueRandomType valueType,
final RangeFunction range
)
{
switch (valueType)
{
case Random:
return new ValueRandom(range);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Value(): A ValueRandomType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For returning the pending value.
*
* @param valueType The property to return the value.
*
* @example (value Pending)
*/
public static IntFunction construct
(
final ValueSimpleType valueType
)
{
switch (valueType)
{
case Pending:
return new ValuePending();
case MoveLimit:
return new ValueMoveLimit();
case TurnLimit:
return new ValueTurnLimit();
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Value(): A ValueSimpleType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For returning the player value.
*
* @param valueType The property to return the value.
* @param indexPlayer The index of the player.
* @param role The roleType of the player.
*
* @example (value Player (who at:(to)))
*/
public static IntFunction construct
(
final ValuePlayerType valueType,
@Or final IntFunction indexPlayer,
@Or final RoleType role
)
{
int numNonNull = 0;
if (indexPlayer != null)
numNonNull++;
if (role != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException(
"Value(): With ValuePlayerType exactly one indexPlayer or role parameter must be non-null.");
switch (valueType)
{
case Player:
return new ValuePlayer(indexPlayer, role);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Value(): A ValuePlayerType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For returning the piece value.
*
* @param valueType The property to return the value.
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check.
*
* @example (value Piece at:(to))
*/
public static IntFunction construct
(
final ValueComponentType valueType,
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
switch (valueType)
{
case Piece:
return new ValuePiece(type,at,level);
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Value(): A ValueComponentType is not implemented.");
}
//-------------------------------------------------------------------------
/**
* For returning the value iterated in (forEach Value ...).
*
* @example (value)
*/
public static IntFunction construct()
{
return new ValueIterated();
}
//-------------------------------------------------------------------------
private Value()
{
// Make grammar pick up construct() and not default constructor
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
// Should not be called, should only be called on subclasses
throw new UnsupportedOperationException("Value.eval(): Should never be called directly.");
// return new Region();
}
@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,156 | 23.557143 | 98 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/ValueComponentType.java | package game.functions.ints.value;
/**
* Defines the types of properties than can be returned by the super ludeme
* value according to a component.
*
* @author Eric.Piette
*/
public enum ValueComponentType
{
/**
* To get the value of a component.
*/
Piece,
}
| 272 | 16.0625 | 75 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/ValuePlayerType.java | package game.functions.ints.value;
/**
* Defines the types of properties than can be returned by the super ludeme
* value according to a player.
*
* @author Eric.Piette
*/
public enum ValuePlayerType
{
/**
* To get the value of a specific player.
*/
Player,
}
| 273 | 16.125 | 75 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/ValueRandomType.java | package game.functions.ints.value;
/**
* Defines the types of properties than can be returned by the super ludeme
* value with random.
*
* @author Eric.Piette
*/
public enum ValueRandomType
{
/**
* To get a random value within a range.
*/
Random,
}
| 262 | 15.4375 | 75 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/ValueSimpleType.java | package game.functions.ints.value;
/**
* Defines the types of properties than can be returned by the super ludeme
* value with no parameter.
*
* @author Eric.Piette
*/
public enum ValueSimpleType
{
/**
* To get the pending value if the previous state causes the current state to be
* pending with a specific value.
*/
Pending,
/**
* To get the move limit of a game.
*/
MoveLimit,
/**
* To get the turn limit of a game.
*/
TurnLimit,
}
| 466 | 16.296296 | 81 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/package-info.java | /**
* Value is a `super' ludeme that returns the value of a specified property
* within the game, such as a player or a component.
*/
package game.functions.ints.value;
| 172 | 27.833333 | 75 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/iterated/ValueIterated.java | package game.functions.ints.value.iterated;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
import other.context.EvalContextData;
/**
* Returns the ``value'' value stored in the context.
*
* @author Eric.Piette
*
* @remarks This ludeme is used by {\tt (forEach Value ...)} to iterate over a
* set of values.
*/
@Hide
public final class ValueIterated extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @example (value)
*/
public ValueIterated()
{
// Nothing to do.
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.value();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
return readsEvalContextFlat();
}
@Override
public BitSet readsEvalContextFlat()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.set(EvalContextData.Value.id(), true);
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
// nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toString()
{
return "value";
}
@Override
public String toEnglish(final Game game)
{
return "current value";
}
}
| 1,916 | 17.432692 | 78 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/piece/ValuePiece.java | package game.functions.ints.value.piece;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
import other.state.container.ContainerState;
import other.state.stacking.BaseContainerStateStacking;
/**
* Returns the value of a component.
*
* @author Eric Piette
* @remarks For any game with a value associated with a component.
*/
@Hide
public final class ValuePiece extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which location. */
private final IntFunction loc;
/** Which level (for a stacking game). */
private final IntFunction level;
/** Cell/Edge/Vertex. */
private SiteType type;
//-------------------------------------------------------------------------
/**
* @param type The graph element type [default SiteType of the board].
* @param at The location to check.
* @param level The level to check.
*/
public ValuePiece
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt @Name final IntFunction level
)
{
loc = at;
this.level = (level == null) ? new IntConstant(Constants.UNDEFINED) : level;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int location = loc.eval(context);
if (location == Constants.OFF)
return Constants.NOBODY;
final int containerId = context.containerId()[location];
if ((context.game().gameFlags() & GameType.Stacking) != 0)
{
// Is stacking game
final BaseContainerStateStacking state = (BaseContainerStateStacking) context.state()
.containerStates()[containerId];
if (level.eval(context) == -1)
return state.value(loc.eval(context), type);
else
return state.value(loc.eval(context), level.eval(context), type);
}
final ContainerState cs = context.state().containerStates()[containerId];
return cs.value(loc.eval(context), type);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
long gameFlags = loc.gameFlags(game) | level.gameFlags(game) | GameType.Value;
gameFlags |= SiteType.gameFlags(type);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(SiteType.concepts(type));
concepts.or(loc.concepts(game));
concepts.or(level.concepts(game));
concepts.set(Concept.PieceValue.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(loc.writesEvalContextRecursive());
writeEvalContext.or(level.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(loc.readsEvalContextRecursive());
readEvalContext.or(level.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= loc.missingRequirement(game);
missingRequirement |= level.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= loc.willCrash(game);
willCrash |= level.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
loc.preprocess(game);
level.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String levelString = "";
if (level != null)
levelString = " at level " + level.toEnglish(game);
return "the level of the piece on " + ((type == null) ? game.board().defaultSite().name().toLowerCase() : type.name().toLowerCase())+ " " + loc.toEnglish(game) + levelString;
}
//-------------------------------------------------------------------------
}
| 4,549 | 24.852273 | 176 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/player/ValuePlayer.java | package game.functions.ints.value.player;
import java.util.BitSet;
import annotations.Hide;
import annotations.Or;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.ints.IntFunction;
import game.types.play.RoleType;
import other.concept.Concept;
import other.context.Context;
/**
* To get the value of a specific player.
*
* @author Eric.Piette
*/
@Hide
public final class ValuePlayer extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Player Index. */
private final IntFunction playerId;
//-------------------------------------------------------------------------
/**
* @param indexPlayer The index of the player.
* @param role The roleType of the player.
*/
public ValuePlayer
(
@Or final IntFunction indexPlayer,
@Or final RoleType role
)
{
int numNonNull = 0;
if (indexPlayer != null)
numNonNull++;
if (role != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Exactly one Or parameter must be non-null.");
if (indexPlayer != null)
playerId = indexPlayer;
else
playerId = RoleType.toIntFunction(role);
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int pid = playerId.eval(context);
return context.state().getValue(pid);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return playerId.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(playerId.concepts(game));
concepts.set(Concept.PlayerValue.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(playerId.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(playerId.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
playerId.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= playerId.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= playerId.willCrash(game);
return willCrash;
}
//-------------------------------------------------------------------------
/**
* @return The role of the player.
*/
public IntFunction role()
{
return playerId;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the value of Player " + playerId.toEnglish(game);
}
//-------------------------------------------------------------------------
} | 3,189 | 20.849315 | 84 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/random/ValueRandom.java | package game.functions.ints.value.random;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.functions.range.RangeFunction;
import game.types.state.GameType;
import main.Constants;
import other.concept.Concept;
import other.context.Context;
/**
* Returns a random value within a range.
*
* @author Eric.Piette
*/
@Hide
public final class ValueRandom extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** The range. */
private final RangeFunction range;
/**
* @param range The range allowed.
*/
public ValueRandom(final RangeFunction range)
{
this.range = range;
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
final int min = range.minFn().eval(context);
final int max = range.maxFn().eval(context);
if (min > max)
return Constants.UNDEFINED;
final int randomValue = context.rng().nextInt(max - min + 1);
return randomValue + min;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return range.gameFlags(game) | GameType.Stochastic;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.set(Concept.Stochastic.id(), true);
range.concepts(game);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(range.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(range.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= range.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= range.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
range.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "a random value in the range " + range.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 2,680 | 20.448 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/simple/ValueMoveLimit.java | package game.functions.ints.value.simple;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
/**
* Returns the internal move limit of the game.
*
* @author Eric.Piette
*/
@Hide
public final class ValueMoveLimit extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
*/
public ValueMoveLimit()
{
// Nothing to do
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.game().getMaxMoveLimit();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0l;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// Nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the move limit";
}
//-------------------------------------------------------------------------
}
| 1,632 | 17.348315 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/simple/ValuePending.java | package game.functions.ints.value.simple;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.ints.BaseIntFunction;
import game.types.state.GameType;
import other.context.Context;
/**
* Returns the pending value if the previous state causes the current state to
* be pending with a specific value.
*
* @author Eric.Piette
* @remarks To store a temporary value in the state for one move turn. Returns 0
* if there are multiple different pending values or no pending value.
*/
@Hide
public final class ValuePending extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
*/
public ValuePending()
{
// Nothing to do
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
// pendingValues should mathematically be a set, so if it contains
// more than 1 value we don't know what to return and just return
// the default of 0 instead
if (context.state().pendingValues().size() == 1)
return context.state().pendingValues().iterator().next();
return 0;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return GameType.PendingValues;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// Nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the pending value";
}
//-------------------------------------------------------------------------
}
| 2,165 | 20.878788 | 80 | java |
Ludii | Ludii-master/Core/src/game/functions/ints/value/simple/ValueTurnLimit.java | package game.functions.ints.value.simple;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.ints.BaseIntFunction;
import other.context.Context;
/**
* Returns the internal move limit of the game.
*
* @author Eric.Piette
*/
@Hide
public final class ValueTurnLimit extends BaseIntFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
*/
public ValueTurnLimit()
{
// Nothing to do
}
//-------------------------------------------------------------------------
@Override
public int eval(final Context context)
{
return context.game().getMaxTurnLimit();
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0l;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// Nothing to do
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "the turn limit";
}
//-------------------------------------------------------------------------
}
| 1,632 | 17.348315 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/range/BaseRangeFunction.java | package game.functions.range;
import annotations.Hide;
import game.Game;
import game.functions.ints.IntFunction;
import other.BaseLudeme;
import other.context.Context;
/**
* Common functionality for RangeFunction - override where necessary.
*
* @author cambolbro and Eric.Piette
*/
@Hide
public abstract class BaseRangeFunction extends BaseLudeme implements RangeFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Lower extent of range (inclusive). */
protected final IntFunction minFn;
/** Upper extent of range (inclusive). */
protected final IntFunction maxFn;
/** Precompute once and cache if possible. */
protected Range precomputedRange = null;
//-------------------------------------------------------------------------
/**
* The base range function.
*
* @param min The minimum of the range.
* @param max The maximum of the range.
*/
public BaseRangeFunction
(
final IntFunction min,
final IntFunction max
)
{
minFn = min;
maxFn = max;
}
//-------------------------------------------------------------------------
@Override
public Range eval(final Context context)
{
System.out.println("BaseRangeFunction.eval(): Should not be called directly; call subclass.");
return null;
}
//-------------------------------------------------------------------------
@Override
public IntFunction minFn()
{
return minFn;
}
@Override
public IntFunction maxFn()
{
return maxFn;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "within the range [" + minFn.toEnglish(game) + "," + maxFn.toEnglish(game) + "]";
}
//-------------------------------------------------------------------------
}
| 1,848 | 21.54878 | 98 | java |
Ludii | Ludii-master/Core/src/game/functions/range/Range.java | package game.functions.range;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.functions.ints.IntFunction;
import other.context.Context;
/**
* Returns a range of values (inclusive) according to some specified condition.
*
* @author cambolbro and Eric.Piette
*/
public final class Range extends BaseRangeFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For a range between two int functions.
*
* @param min Lower extent of range (inclusive).
* @param max Upper extent of range (inclusive) [same as min].
*
* @example (range (from) (to))
*/
public Range
(
final IntFunction min,
@Opt final IntFunction max
)
{
super(min, max == null ? min : max);
}
//-------------------------------------------------------------------------
@Override
public Range eval(final Context context)
{
if (precomputedRange != null)
return precomputedRange;
return this; //new game.util.math.Range(Integer.valueOf(minFn.eval(context)), Integer.valueOf(maxFn.eval(context)));
}
//-------------------------------------------------------------------------
/**
* @param context The context.
*
* @return The minimum of the range.
*/
public int min(final Context context)
{
return minFn.eval(context);
}
/**
* @param context The context.
*
* @return The maximum of the range.
*/
public int max(final Context context)
{
return maxFn.eval(context);
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return minFn.isStatic() && maxFn.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return minFn.gameFlags(game) | maxFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(minFn.concepts(game));
concepts.or(maxFn.concepts(game));
return concepts;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= minFn.missingRequirement(game);
missingRequirement |= maxFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= minFn.willCrash(game);
willCrash |= maxFn.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
minFn.preprocess(game);
maxFn.preprocess(game);
if (isStatic())
precomputedRange = eval(new Context(game, null));
}
@Override
public String toString()
{
return "[" + max(null) + ";" + min(null) + "]";
}
//-------------------------------------------------------------------------
}
| 2,802 | 20.396947 | 119 | java |
Ludii | Ludii-master/Core/src/game/functions/range/RangeFunction.java | package game.functions.range;
import java.util.BitSet;
import game.Game;
import game.functions.ints.IntFunction;
import game.types.state.GameType;
import other.context.Context;
/**
* Returns a range.
*
* @author cambolbro and Eric.Piette
*/
// **
// ** Do not @Hide, or loses mapping in grammar!
// **
public interface RangeFunction extends GameType
{
/**
* @param context The context.
* @return The range generated by this context.
*/
Range eval(final Context context);
/**
* @return Function for the minimum of the range.
*/
IntFunction minFn();
/**
* @return Function for the maximum of the range.
*/
IntFunction maxFn();
/**
* @param game The game.
* @return Accumulated flags corresponding to the game concepts.
*/
BitSet concepts(final Game game);
/**
* @return Accumulated flags corresponding to read data in EvalContext.
*/
BitSet readsEvalContextRecursive();
/**
* @return Accumulated flags corresponding to write data in EvalContext.
*/
BitSet writesEvalContextRecursive();
/**
* @param game The game.
* @return True if a required ludeme is missing.
*/
boolean missingRequirement(final Game game);
/**
* @param game The game.
* @return True if the ludeme can crash the game during its play.
*/
boolean willCrash(final Game game);
/**
* @param game
* @return This Function in English.
*/
String toEnglish(Game game);
}
| 1,416 | 18.680556 | 73 | java |
Ludii | Ludii-master/Core/src/game/functions/range/package-info.java | /**
* @chapter Range functions are ludemes that define a range of integer values with a lower and upper bound (inclusive).
* Ranges are useful for restricting integer values to sensible limits, e.g. for capping maximum bets
* in betting games or for situations in which negative values should be avoided.
*
* @section The base {\tt range} function defines a range with upper and lower board (inclusive),
* optionally according to some specified condition.
*/
package game.functions.range;
| 500 | 49.1 | 120 | java |
Ludii | Ludii-master/Core/src/game/functions/range/math/Exact.java | package game.functions.range.math;
import java.util.BitSet;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.functions.range.BaseRangeFunction;
import game.functions.range.Range;
import other.context.Context;
/**
* Returns a range of exactly one value.
*
* @author Eric.Piette and cambolbro
*
* @remarks The exact value is both the minimum and maximum of its range.
*/
public final class Exact extends BaseRangeFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param value The value in question.
*
* @example (exact 4)
*/
public Exact
(
final IntFunction value
)
{
super(value, value);
}
//-------------------------------------------------------------------------
@Override
public Range eval(final Context context)
{
if (precomputedRange != null)
return precomputedRange;
//return new Range(Integer.valueOf(minFn.eval(context)), Integer.valueOf(maxFn.eval(context)));
return new Range(new IntConstant(minFn.eval(context)), new IntConstant(maxFn.eval(context)));
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return minFn.isStatic() && maxFn.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return minFn.gameFlags(game) | maxFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(minFn.concepts(game));
concepts.or(maxFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(minFn.writesEvalContextRecursive());
writeEvalContext.or(maxFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(minFn.readsEvalContextRecursive());
readEvalContext.or(maxFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= minFn.missingRequirement(game);
missingRequirement |= maxFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= minFn.willCrash(game);
willCrash |= maxFn.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
minFn.preprocess(game);
maxFn.preprocess(game);
if (isStatic())
precomputedRange = eval(new Context(game, null));
}
//-------------------------------------------------------------------------
}
| 2,846 | 22.336066 | 97 | java |
Ludii | Ludii-master/Core/src/game/functions/range/math/Max.java | package game.functions.range.math;
import java.util.BitSet;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.functions.range.BaseRangeFunction;
import game.functions.range.Range;
import main.Constants;
import other.context.Context;
/**
* Returns a range with a specified maximum (inclusive).
*
* @author cambolbro and Eric.Piette
*/
public final class Max extends BaseRangeFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param max Upper extent of range (inclusive).
*
* @example (max 4)
*/
public Max
(
final IntFunction max
)
{
super(new IntConstant(Constants.UNDEFINED), max);
}
//-------------------------------------------------------------------------
@Override
public Range eval(final Context context)
{
if (precomputedRange != null)
return precomputedRange;
//return new Range(Integer.valueOf(minFn.eval(context)), Integer.valueOf(maxFn.eval(context)));
return new Range(new IntConstant(minFn.eval(context)), new IntConstant(maxFn.eval(context)));
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return minFn.isStatic() && maxFn.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return minFn.gameFlags(game) | maxFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(minFn.concepts(game));
concepts.or(maxFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(minFn.writesEvalContextRecursive());
writeEvalContext.or(maxFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(minFn.readsEvalContextRecursive());
readEvalContext.or(maxFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= minFn.missingRequirement(game);
missingRequirement |= maxFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= minFn.willCrash(game);
willCrash |= maxFn.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
minFn.preprocess(game);
maxFn.preprocess(game);
if (isStatic())
precomputedRange = eval(new Context(game, null));
}
} | 2,758 | 22.581197 | 97 | java |
Ludii | Ludii-master/Core/src/game/functions/range/math/Min.java | package game.functions.range.math;
import java.util.BitSet;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.functions.range.BaseRangeFunction;
import game.functions.range.Range;
import main.Constants;
import other.context.Context;
/**
* Returns a range with a specified minimum (inclusive).
*
* @author cambolbro and Eric.Piette
*/
public final class Min extends BaseRangeFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* @param min Lower extent of range (inclusive).
*
* @example (min 4)
*/
public Min
(
final IntFunction min
)
{
super(min, new IntConstant(Constants.INFINITY));
}
//-------------------------------------------------------------------------
@Override
public Range eval(final Context context)
{
if (precomputedRange != null)
return precomputedRange;
//return new Range(Integer.valueOf(minFn.eval(context)), Integer.valueOf(maxFn.eval(context)));
return new Range(new IntConstant(minFn.eval(context)), new IntConstant(maxFn.eval(context)));
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return minFn.isStatic() && maxFn.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return minFn.gameFlags(game) | maxFn.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(minFn.concepts(game));
concepts.or(maxFn.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(minFn.writesEvalContextRecursive());
writeEvalContext.or(maxFn.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(minFn.readsEvalContextRecursive());
readEvalContext.or(maxFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= minFn.missingRequirement(game);
missingRequirement |= maxFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= minFn.willCrash(game);
willCrash |= maxFn.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
minFn.preprocess(game);
maxFn.preprocess(game);
if (isStatic())
precomputedRange = eval(new Context(game, null));
}
} | 2,757 | 22.57265 | 97 | java |
Ludii | Ludii-master/Core/src/game/functions/range/math/package-info.java | /**
* Math range functions return a range based on specified inputs.
*/
package game.functions.range.math;
| 109 | 21 | 65 | java |
Ludii | Ludii-master/Core/src/game/functions/region/BaseRegionFunction.java | package game.functions.region;
import annotations.Hide;
import game.Game;
import game.types.board.SiteType;
import other.BaseLudeme;
import other.context.Context;
/**
* Default implementations of region functions - override where necessary.
*
* @author mrraow
*/
@Hide
public abstract class BaseRegionFunction extends BaseLudeme implements RegionFunction
{
private static final long serialVersionUID = 1L;
/** Cell, Edge or Vertex. */
protected SiteType type;
//-------------------------------------------------------------------------
@Override
public boolean contains(final Context context, final int location)
{
return eval(context).contains(location);
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public boolean isHand()
{
return false;
}
@Override
public SiteType type(final Game game)
{
return (type != null) ? type : game.board().defaultSite();
}
}
| 922 | 18.229167 | 85 | java |
Ludii | Ludii-master/Core/src/game/functions/region/RegionConstant.java | package game.functions.region;
import annotations.Anon;
import annotations.Hide;
import game.Game;
import game.util.equipment.Region;
import other.context.Context;
/**
* A fixed region of sites that does not change during the game.
*
* @author cambolbro
*/
@Hide
public final class RegionConstant extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
/** Which region. */
private final Region region;
//-------------------------------------------------------------------------
/**
* @param region The constant region to set.
*/
public RegionConstant(@Anon final Region region)
{
this.region = region;
}
//-------------------------------------------------------------------------
@Override
public Region eval(final Context context)
{
return region;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return true;
}
@Override
public long gameFlags(final Game game)
{
return 0;
}
@Override
public void preprocess(final Game game)
{
// nothing to do
}
}
| 1,097 | 17.3 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/region/RegionFunction.java | package game.functions.region;
import java.util.BitSet;
import game.Game;
import game.types.board.SiteType;
import game.types.state.GameType;
import game.util.equipment.Region;
import other.context.Context;
/**
* Returns a region (collection of sites) within a container.
*
* @author cambolbro
*/
// **
// ** Do not @Hide, or loses mapping in grammar!
// **
public interface RegionFunction extends GameType
{
/**
* @param context
* @return The result of applying this function to this trial.
*/
public Region eval(final Context context);
/**
* @param context
* @param location
* @return True if and only if the region evaluated in given context would contain location
*/
public boolean contains(final Context context, final int location);
/**
* @param game The game we're playing
* @return The site type of the region returned by this function
*/
public SiteType type(final Game game);
/**
* @param game The game.
* @return Accumulated flags corresponding to the game concepts.
*/
public BitSet concepts(final Game game);
/**
* @return Accumulated flags corresponding to read data in EvalContext.
*/
public BitSet readsEvalContextRecursive();
/**
* @return Accumulated flags corresponding to write data in EvalContext.
*/
public BitSet writesEvalContextRecursive();
/**
* @param game The game.
* @return True if a required ludeme is missing.
*/
public boolean missingRequirement(final Game game);
/**
* @param game The game.
* @return True if the ludeme can crash the game during its play.
*/
public boolean willCrash(final Game game);
/**
* @param game
* @return RegionFunction described in English.
*/
public String toEnglish(final Game game);
/**
* @return if is in hand.
*/
public boolean isHand();
}
| 1,804 | 21.283951 | 92 | java |
Ludii | Ludii-master/Core/src/game/functions/region/package-info.java | /**
* @chapter Region functions are ludemes that return {\it regions} composed of collections of sites.
* These can be {\it static} regions defined in the game's {\tt equipment}, such as player
* homes or special target regions, or {\it dynamic} regions calculated on-the-fly during
* play according to the current game state, such as the region of unoccupied sites
* or the region of sites occupied by particular player or piece type.
*/
package game.functions.region;
| 516 | 56.444444 | 101 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/ForEach.java | package game.functions.region.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.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.functions.region.foreach.level.ForEachLevel;
import game.functions.region.foreach.player.ForEachPlayer;
import game.functions.region.foreach.sites.ForEachSite;
import game.functions.region.foreach.sites.ForEachSiteInRegion;
import game.functions.region.foreach.team.ForEachTeam;
import game.rules.start.forEach.ForEachTeamType;
import game.types.board.SiteType;
import game.util.directions.StackDirection;
import game.util.equipment.Region;
import other.context.Context;
/**
* Returns a region filtering with a condition or build according to different player indices.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class ForEach extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For iterating through levels of a site.
*
* @param forEachType The type of property to iterate.
* @param type The type of graph element.
* @param at The site.
* @param stackDirection The direction to count in the stack [FromTop].
* @param If The condition to satisfy.
* @param startAt The level to start to look at.
*
* @example (forEach Level at:(site))
*/
public static RegionFunction construct
(
final ForEachLevelType forEachType,
@Opt final SiteType type,
@Name final IntFunction at,
@Opt final StackDirection stackDirection,
@Opt @Name final BooleanFunction If,
@Opt @Name final IntFunction startAt
)
{
return new ForEachLevel(type, at, stackDirection, If, startAt);
}
//-------------------------------------------------------------------------
/**
* For iterating through teams.
*
* @param forEachType The type of property to iterate.
* @param region The region.
*
* @example (forEach Team (forEach (team) (sites Occupied by:Player)))
*/
public static RegionFunction construct
(
final ForEachTeamType forEachType,
final RegionFunction region
)
{
return new ForEachTeam(region);
}
//-------------------------------------------------------------------------
/**
* For filtering a region according to a condition.
*
* @param region The original region.
* @param If The condition to satisfy.
* @example (forEach (sites Occupied by:P1) if:(= (what at:(site)) (id
* "Pawn1")))
*/
public static RegionFunction construct
(
final RegionFunction region,
@Name final BooleanFunction If
)
{
return new ForEachSite(region, If);
}
/**
* For computing a region in iterating another with (site).
*
* @param of The region of sites.
* @param region The region to compute with each site of the first region.
* @example (forEach of:(sites Occupied by:Mover) (sites To (slide (from (site)))))
*/
public static RegionFunction construct
(
@Name final RegionFunction of,
final RegionFunction region
)
{
return new ForEachSiteInRegion(of, region);
}
/**
* For iterating on players.
*
* @param players The list of players.
* @param region The region.
*
* @example (forEach (players Ally of:(next)) (sites Occupied by:Player))
*/
public static RegionFunction construct
(
final IntArrayFunction players,
final RegionFunction region
)
{
return new ForEachPlayer(players, region);
}
private ForEach()
{
// Make grammar pick up construct() and not default constructor
}
//-------------------------------------------------------------------------
@Override
public final Region 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.
}
}
| 4,492 | 26.230303 | 94 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/ForEachLevelType.java | package game.functions.region.foreach;
/**
* Iterates through level of a site.
*
* @author Eric.Piette
*/
public enum ForEachLevelType
{
/**
* Level to iterate.
*/
Level,
}
| 185 | 11.4 | 38 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/package-info.java | /**
* To iterate sites or players to compute sites.
*/
package game.functions.region.foreach;
| 96 | 18.4 | 48 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/level/ForEachLevel.java | package game.functions.region.foreach.level;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.functions.region.BaseRegionFunction;
import game.types.board.SiteType;
import game.util.directions.StackDirection;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import main.Constants;
import other.context.Context;
import other.context.EvalContextData;
import other.state.container.ContainerState;
/**
* Iterates through the players, generating moves based on the indices of the
* players.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachLevel extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Region to return for each player. */
private final IntFunction siteFn;
/** The list of players. */
private final BooleanFunction cond;
/** To start from the bottom or the top of the stack. */
private final StackDirection stackDirection;
/** The level to start to look at. */
private final IntFunction startAtFn;
//-------------------------------------------------------------------------
/**
* @param type The type
* @param at The site.
* @param stackDirection The direction to count in the stack [FromTop].
* @param If The condition to satisfy.
* @param startAt The level to start to look at.
*/
public ForEachLevel
(
@Opt final SiteType type,
@Name final IntFunction at,
@Opt final StackDirection stackDirection,
@Opt @Name final BooleanFunction If,
@Opt @Name final IntFunction startAt
)
{
this.type = type;
this.siteFn = at;
this.cond = If;
this.stackDirection = (stackDirection == null) ? StackDirection.FromTop : stackDirection;
this.startAtFn = (startAt == null) ? new IntConstant(Constants.UNDEFINED) : startAt;
}
//-------------------------------------------------------------------------
@Override
public final Region eval(final Context context)
{
final TIntArrayList returnLevels = new TIntArrayList();
final int site = siteFn.eval(context);
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.containerState(cid);
final int stackSize = cs.sizeStack(site, type);
final int originLevel = context.level();
int startAt = startAtFn.eval(context);
if(stackDirection.equals(StackDirection.FromBottom))
{
startAt = (startAt < 0) ? 0 : startAt;
for(int lvl = startAt; lvl < stackSize; lvl++)
{
context.setLevel(lvl);
if(cond == null || cond.eval(context))
returnLevels.add(lvl);
}
}
else
{
startAt = (startAt < 0) ? stackSize -1 : startAt;
startAt = (startAt >= stackSize) ? stackSize -1 : startAt;
for(int lvl = startAt ; lvl >= 0; lvl--)
{
context.setLevel(lvl);
if(cond == null || cond.eval(context))
returnLevels.add(lvl);
}
}
context.setLevel(originLevel);
return new Region(returnLevels.toArray());
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = siteFn.gameFlags(game);
gameFlags |= SiteType.gameFlags(type);
if (cond != null)
gameFlags |= cond.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(siteFn.concepts(game));
if (cond != null)
concepts.or(cond.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(super.writesEvalContextRecursive());
if (cond != null)
writeEvalContext.or(cond.writesEvalContextRecursive());
writeEvalContext.or(siteFn.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();
if (cond != null)
readEvalContext.or(cond.readsEvalContextRecursive());
readEvalContext.or(siteFn.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (cond != null)
missingRequirement |= (cond.missingRequirement(game));
missingRequirement |= siteFn.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (cond != null)
willCrash |= (cond.willCrash(game));
willCrash |= siteFn.willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
if (type == null)
type = game.board().defaultSite();
siteFn.preprocess(game);
if (cond != null)
cond.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "for each level at " + siteFn.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 5,829 | 25.621005 | 91 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/player/ForEachPlayer.java | package game.functions.region.foreach.player;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.intArray.IntArrayFunction;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
/**
* Iterates through the players, generating moves based on the indices of the
* players.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachPlayer extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Region to return for each player. */
private final RegionFunction region;
/** The list of players. */
private final IntArrayFunction playersFn;
//-------------------------------------------------------------------------
/**
* @param players The list of players.
* @param region The region.
*/
public ForEachPlayer
(
final IntArrayFunction players,
final RegionFunction region
)
{
playersFn = players;
this.region = region;
}
//-------------------------------------------------------------------------
@Override
public final Region eval(final Context context)
{
final TIntArrayList returnSites = new TIntArrayList();
final int savedPlayer = context.player();
if (playersFn == null)
{
for (int pid = 1; pid < context.game().players().size(); pid++)
{
context.setPlayer(pid);
final int[] sites = region.eval(context).sites();
for (final int site : sites)
if (!returnSites.contains(site))
returnSites.add(site);
}
}
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 int[] sites = region.eval(context).sites();
for (final int site : sites)
if (!returnSites.contains(site))
returnSites.add(site);
}
}
context.setPlayer(savedPlayer);
return new Region(returnSites.toArray());
}
//-------------------------------------------------------------------------
@Override
public long gameFlags(final Game game)
{
long gameFlags = region.gameFlags(game);
if (playersFn != null)
gameFlags |= playersFn.gameFlags(game);
return gameFlags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
if (playersFn != null)
concepts.or(playersFn.concepts(game));
concepts.or(region.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
if (playersFn != null)
writeEvalContext.or(playersFn.writesEvalContextRecursive());
writeEvalContext.or(region.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();
if (playersFn != null)
readEvalContext.or(playersFn.readsEvalContextRecursive());
readEvalContext.or(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (playersFn != null)
missingRequirement |= (playersFn.missingRequirement(game));
missingRequirement |= region.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
if (playersFn != null)
willCrash |= (playersFn.willCrash(game));
willCrash |= region.willCrash(game);
return willCrash;
}
@Override
public boolean isStatic()
{
return false;
}
@Override
public void preprocess(final Game game)
{
region.preprocess(game);
if (playersFn != null)
playersFn.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "for each player in " + playersFn.toEnglish(game) + " " + region.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 4,599 | 22.232323 | 90 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/sites/ForEachSite.java | package game.functions.region.foreach.sites;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import game.Game;
import game.functions.booleans.BooleanFunction;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
/**
* Returns the sites satisfying a constraint from a given region.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachSite extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Region to filter. */
private final RegionFunction region;
/** Condition to check. */
private final BooleanFunction condition;
//-------------------------------------------------------------------------
/**
* @param region The original region.
* @param If The condition to satisfy.
* @example (forEach (sites Occupied by:P1) if:(= (what at:(site)) (id
* "Pawn1")))
*/
public ForEachSite
(
final RegionFunction region,
@Name final BooleanFunction If
)
{
this.region = region;
condition = If;
}
//-------------------------------------------------------------------------
@Override
public Region eval(final Context context)
{
final TIntArrayList originalSites = new TIntArrayList(region.eval(context).sites());
final TIntArrayList returnSites = new TIntArrayList();
final int originSiteValue = context.site();
for (int i = 0; i < originalSites.size(); i++)
{
final int site = originalSites.getQuick(i);
context.setSite(site);
if (condition.eval(context))
returnSites.add(site);
}
context.setSite(originSiteValue);
return new Region(returnSites.toArray());
}
@Override
public boolean isStatic()
{
return condition.isStatic() && region.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return condition.gameFlags(game) | region.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(condition.concepts(game));
concepts.or(region.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.set(EvalContextData.Site.id(), true);
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.Site.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(condition.readsEvalContextRecursive());
readEvalContext.or(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
condition.preprocess(game);
region.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= condition.missingRequirement(game);
missingRequirement |= region.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= condition.willCrash(game);
willCrash |= region.willCrash(game);
return willCrash;
}
@Override
public String toEnglish(final Game game)
{
if(condition == null)
return region.toEnglish(game);
else
return condition.toEnglish(game) + " " + region.toEnglish(game);
}
}
| 3,784 | 23.10828 | 86 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/sites/ForEachSiteInRegion.java | package game.functions.region.foreach.sites;
import java.util.BitSet;
import annotations.Hide;
import annotations.Name;
import game.Game;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
/**
* Returns the sites of a region in iterating another region.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachSiteInRegion extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Region to iterate. */
private final RegionFunction ofRegion;
/** Region to use the iterator. */
private final RegionFunction region;
//-------------------------------------------------------------------------
/**
* @param of The region of sites.
* @param region The region to compute with each site of the first region.
* @example (forEach (sites Occupied by:P1) if:(= (what at:(site)) (id
* "Pawn1")))
*/
public ForEachSiteInRegion
(
@Name final RegionFunction of,
final RegionFunction region
)
{
this.ofRegion = of;
this.region = region;
}
//-------------------------------------------------------------------------
@Override
public final Region eval(final Context context)
{
final TIntArrayList iteratedSites = new TIntArrayList(ofRegion.eval(context).sites());
final TIntArrayList returnSites = new TIntArrayList();
final int originSiteValue = context.site();
for (int i = 0; i < iteratedSites.size(); i++)
{
final int iteratedSite = iteratedSites.getQuick(i);
context.setSite(iteratedSite);
final TIntArrayList sites = new TIntArrayList(region.eval(context).sites());
for(int j = 0; j < sites.size() ; j ++)
{
final int site = sites.get(j);
if(!returnSites.contains(site))
returnSites.add(site);
}
}
context.setSite(originSiteValue);
return new Region(returnSites.toArray());
}
@Override
public boolean isStatic()
{
return ofRegion.isStatic() && region.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return ofRegion.gameFlags(game) | region.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(ofRegion.concepts(game));
concepts.or(region.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.set(EvalContextData.Site.id(), true);
return writeEvalContext;
}
@Override
public BitSet writesEvalContextFlat()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.set(EvalContextData.Site.id(), true);
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(ofRegion.readsEvalContextRecursive());
readEvalContext.or(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
ofRegion.preprocess(game);
region.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= ofRegion.missingRequirement(game);
missingRequirement |= region.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= ofRegion.willCrash(game);
willCrash |= region.willCrash(game);
return willCrash;
}
}
| 3,786 | 23.914474 | 88 | java |
Ludii | Ludii-master/Core/src/game/functions/region/foreach/team/ForEachTeam.java | package game.functions.region.foreach.team;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import gnu.trove.list.array.TIntArrayList;
import other.concept.Concept;
import other.context.Context;
import other.context.EvalContextData;
/**
* Iterates through the players, generating moves based on the indices of the
* players.
*
* @author Eric.Piette
*/
@Hide
public final class ForEachTeam extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Region to return for each player. */
private final RegionFunction region;
//-------------------------------------------------------------------------
/**
* @param region The region.
*/
public ForEachTeam(final RegionFunction region)
{
this.region = region;
}
//-------------------------------------------------------------------------
@Override
public final Region eval(final Context context)
{
final TIntArrayList returnSites = new TIntArrayList();
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 int[] sites = region.eval(context).sites();
for (final int site : sites)
if (!returnSites.contains(site))
returnSites.add(site);
}
}
context.setTeam(savedTeam);
return new Region(returnSites.toArray());
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return region.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return region.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(region.concepts(game));
concepts.set(Concept.ControlFlowStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = writesEvalContextFlat();
writeEvalContext.or(region.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(region.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public void preprocess(final Game game)
{
region.preprocess(game);
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= region.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= region.willCrash(game);
return willCrash;
}
}
| 3,274 | 22.392857 | 77 | java |
Ludii | Ludii-master/Core/src/game/functions/region/last/Last.java | package game.functions.region.last;
import game.Game;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import other.context.Context;
/**
* Returns sites related to the last move.
*
* @author Eric.Piette
*/
@SuppressWarnings("javadoc")
public final class Last extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
* For getting between sites of the last move played.
*
* @param regionType Type of sites to return.
*
* @example (last Between)
*/
public static RegionFunction construct(final LastRegionType regionType)
{
switch (regionType)
{
case Between:
return new LastBetween();
default:
break;
}
// We should never reach that except if we forget some codes.
throw new IllegalArgumentException("Sites(): A LastRegionType is not implemented.");
}
//-------------------------------------------------------------------------
private Last()
{
// Ensure that compiler does pick up default constructor
}
//-------------------------------------------------------------------------
@Override
public Region eval(final Context context)
{
return null;
}
//-------------------------------------------------------------------------
@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.
}
}
| 1,640 | 19.772152 | 86 | java |
Ludii | Ludii-master/Core/src/game/functions/region/last/LastBetween.java | package game.functions.region.last;
import java.util.BitSet;
import annotations.Hide;
import game.Game;
import game.functions.region.BaseRegionFunction;
import game.util.equipment.Region;
import other.context.Context;
import other.move.Move;
/**
* Returns the ``between'' sites of a set of moves.
*
* @author Eric Piette
*/
@Hide
public final class LastBetween extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/**
*
*/
public LastBetween()
{
}
//-------------------------------------------------------------------------
@Override
public Region eval(final Context context)
{
final Move move = context.trial().lastMove();
if (move == null)
return new Region();
return new Region(move.betweenNonDecision().toArray());
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return false;
}
@Override
public long gameFlags(final Game game)
{
return 0l;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
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 void preprocess(final Game game)
{
// Nothing to do.
}
@Override
public boolean missingRequirement(final Game game)
{
final boolean missingRequirement = false;
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
final boolean willCrash = false;
return willCrash;
}
}
| 1,799 | 17.367347 | 76 | java |
Ludii | Ludii-master/Core/src/game/functions/region/last/LastRegionType.java | package game.functions.region.last;
/**
* Defines the types of Last Region ludeme.
*
* @author Eric.Piette
*/
public enum LastRegionType
{
/** Between return the ``between'' site of the last move. */
Between,
}
| 219 | 15.923077 | 61 | java |
Ludii | Ludii-master/Core/src/game/functions/region/last/package-info.java | /**
* Last region functions returning region of sites based on the previous state.
*/
package game.functions.region.last;
| 124 | 24 | 79 | java |
Ludii | Ludii-master/Core/src/game/functions/region/math/Difference.java | package game.functions.region.math;
import java.util.BitSet;
import annotations.Or;
import game.Game;
import game.functions.ints.IntFunction;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import other.concept.Concept;
import other.context.Context;
/**
* Returns the set difference, i.e. elements of the source region are not in the
* subtraction region.
*
* @author mrraow and cambolbro and Eric.Piette
*/
public final class Difference extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which region 1. */
private final RegionFunction source;
/** Which region 2. */
private final RegionFunction subtraction;
/** Which site. */
private final IntFunction siteToRemove;
/** If we can, we'll precompute once and cache */
private Region precomputedRegion = null;
//-------------------------------------------------------------------------
/**
* @param source The original region.
* @param subtraction The region to remove from the original.
* @param siteToRemove The site to remove from the original.
* @example (difference (sites Occupied by:Mover) (sites Mover))
*/
public Difference
(
final RegionFunction source,
@Or final RegionFunction subtraction,
@Or final IntFunction siteToRemove
)
{
this.source = source;
int numNonNull = 0;
if (subtraction != null)
numNonNull++;
if (siteToRemove != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("one parameter must be non-null.");
this.siteToRemove = siteToRemove;
this.subtraction = subtraction;
}
//-------------------------------------------------------------------------
@Override
public Region eval(final Context context)
{
if (precomputedRegion != null)
return precomputedRegion;
final Region sites1 = new Region(source.eval(context));
if (subtraction != null)
{
final Region sites2 = subtraction.eval(context);
sites1.remove(sites2);
return sites1;
}
else
{
final int site2 = siteToRemove.eval(context);
if (site2 >= 0)
sites1.remove(site2);
return sites1;
}
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
if (subtraction != null && !subtraction.isStatic())
return false;
if (siteToRemove != null && !siteToRemove.isStatic())
return false;
return source.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long flag = source.gameFlags(game);
if (subtraction != null)
flag |= subtraction.gameFlags(game);
if (siteToRemove != null)
flag |= siteToRemove.gameFlags(game);
return flag;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(source.concepts(game));
if (subtraction != null)
concepts.or(subtraction.concepts(game));
if (siteToRemove != null)
concepts.or(siteToRemove.concepts(game));
concepts.set(Concept.Complement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(source.writesEvalContextRecursive());
if (subtraction != null)
writeEvalContext.or(subtraction.writesEvalContextRecursive());
if (siteToRemove != null)
writeEvalContext.or(siteToRemove.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(source.readsEvalContextRecursive());
if (subtraction != null)
readEvalContext.or(subtraction.readsEvalContextRecursive());
if (siteToRemove != null)
readEvalContext.or(siteToRemove.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= source.missingRequirement(game);
if (subtraction != null)
missingRequirement |= subtraction.missingRequirement(game);
if (siteToRemove != null)
missingRequirement |= siteToRemove.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= source.willCrash(game);
if (subtraction != null)
willCrash |= subtraction.willCrash(game);
if (siteToRemove != null)
willCrash |= siteToRemove.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
source.preprocess(game);
if (subtraction != null)
subtraction.preprocess(game);
if (siteToRemove != null)
siteToRemove.preprocess(game);
if (isStatic())
{
final Context context = new Context(game, null);
final Region sites1 = new Region(source.eval(context));
if (subtraction != null)
{
final Region sites2 = subtraction.eval(context);
sites1.remove(sites2);
}
else
{
final int site2 = siteToRemove.eval(context);
sites1.remove(site2);
}
precomputedRegion = sites1;
}
}
//-------------------------------------------------------------------------
/**
* @return Source region
*/
public RegionFunction source()
{
return source;
}
/**
* @return Region to subtract
*/
public RegionFunction subtraction()
{
return subtraction;
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
String differenceString = "";
if (siteToRemove != null)
differenceString = "site " + siteToRemove.toEnglish(game);
else
differenceString = subtraction.toEnglish(game);
final String englishString = "the difference between " + source.toEnglish(game) + " and " + differenceString;
return englishString;
}
//-------------------------------------------------------------------------
}
| 6,032 | 22.474708 | 111 | java |
Ludii | Ludii-master/Core/src/game/functions/region/math/Expand.java | package game.functions.region.math;
import java.util.BitSet;
import annotations.Name;
import annotations.Opt;
import annotations.Or;
import annotations.Or2;
import game.Game;
import game.functions.ints.IntConstant;
import game.functions.ints.IntFunction;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionFunction;
import game.types.board.SiteType;
import game.types.state.GameType;
import game.util.directions.AbsoluteDirection;
import game.util.equipment.Region;
import other.ContainerId;
import other.IntArrayFromRegion;
import other.context.Context;
/**
* Expands a given region/site in all directions the specified number of steps.
*
* @author cambolbro and Eric.Piette
*/
public final class Expand extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Which container. */
private final ContainerId containerId;
/** Which container. */
private final IntArrayFromRegion baseRegion;
/** How many steps to expand. */
private final IntFunction numSteps;
/** How many steps to expand. */
private final AbsoluteDirection direction;
/** If we can, we'll precompute once and cache. */
private Region precomputedRegion = null;
//-------------------------------------------------------------------------
/**
* @param containerIdFn The index of the container.
* @param containerName The name of the container.
* @param region The region.
* @param origin The site.
* @param steps The distance to expand [steps:1].
* @param dirn The absolute direction to expand.
* @param type The graph element type [default SiteType of the board].
*
* @example (expand (sites Bottom) steps:2)
*/
public Expand
(
@Opt @Or final IntFunction containerIdFn,
@Opt @Or final String containerName,
@Or2 final RegionFunction region,
@Or2 @Name final IntFunction origin,
@Opt @Name final IntFunction steps,
@Opt final AbsoluteDirection dirn,
@Opt final SiteType type
)
{
int numNonNull = 0;
if (containerIdFn != null)
numNonNull++;
if (containerName != null)
numNonNull++;
if (numNonNull > 1)
throw new IllegalArgumentException("Zero or one Or parameter must be non-null.");
numNonNull = 0;
if (region != null)
numNonNull++;
if (origin != null)
numNonNull++;
if (numNonNull != 1)
throw new IllegalArgumentException("Exactly one Or2 parameter must be non-null.");
containerId = new ContainerId(containerIdFn, containerName, null, null, null);
baseRegion = new IntArrayFromRegion(origin, region);
numSteps = (steps == null) ? new IntConstant(1) : steps;
direction = dirn;
this.type = type;
}
//-------------------------------------------------------------------------
@Override
public Region eval(final Context context)
{
if (precomputedRegion != null)
return precomputedRegion;
final int cid = containerId.eval(context);
final Region region = new Region(baseRegion.eval(context));
final int num = numSteps.eval(context);
if (num > 0)
{
final other.topology.Topology graph = context.containers()[cid].topology();
if (direction == null)
Region.expand(region, graph, num, type);
else
Region.expand(region, graph, num, direction, type);
}
return region;
}
//-------------------------------------------------------------------------
@Override
public boolean isStatic()
{
return baseRegion.isStatic() && numSteps.isStatic();
}
@Override
public long gameFlags(final Game game)
{
long flags = baseRegion.gameFlags(game) | numSteps.gameFlags(game);
if (type != null)
{
if (type.equals(SiteType.Edge) || type.equals(SiteType.Vertex))
flags |= GameType.Graph;
}
return flags;
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(baseRegion.concepts(game));
concepts.or(numSteps.concepts(game));
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(baseRegion.writesEvalContextRecursive());
writeEvalContext.or(numSteps.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(baseRegion.readsEvalContextRecursive());
readEvalContext.or(numSteps.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
missingRequirement |= baseRegion.missingRequirement(game);
missingRequirement |= numSteps.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= baseRegion.willCrash(game);
willCrash |= numSteps.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
type = SiteType.use(type, game);
baseRegion.preprocess(game);
numSteps.preprocess(game);
if (isStatic())
precomputedRegion = eval(new Context(game, null));
}
@Override
public String toEnglish(final Game game)
{
return baseRegion.toEnglish(game) + " expanded by " + numSteps.toEnglish(game) + " steps";
}
}
| 5,434 | 25.256039 | 92 | java |
Ludii | Ludii-master/Core/src/game/functions/region/math/If.java | package game.functions.region.math;
import java.util.BitSet;
import annotations.Opt;
import game.Game;
import game.functions.booleans.BooleanConstant.FalseConstant;
import game.functions.booleans.BooleanFunction;
import game.functions.region.BaseRegionFunction;
import game.functions.region.RegionConstant;
import game.functions.region.RegionFunction;
import game.util.equipment.Region;
import other.concept.Concept;
import other.context.Context;
/**
* Returns a region when the condition is satisfied and another when it is not.
*
* @author Eric.Piette and cambolbro
*/
public final class If extends BaseRegionFunction
{
private static final long serialVersionUID = 1L;
//-------------------------------------------------------------------------
/** Condition to check. */
private final BooleanFunction condition;
/** Value returned if the condition is true. */
private final RegionFunction ok;
/** Value returned if the condition is false. */
private final RegionFunction notOk;
//-------------------------------------------------------------------------
/**
* @param cond The condition to satisfy.
* @param ok The region returned when the condition is satisfied.
* @param notOk The region returned when the condition is not satisfied.
*
* @example (if (is Mover P1) (sites P1) (sites P2))
*/
public If
(
final BooleanFunction cond,
final RegionFunction ok,
@Opt final RegionFunction notOk
)
{
condition = cond;
this.ok = ok;
this.notOk = (notOk == null) ? new RegionConstant(new Region()) : notOk;
}
//-------------------------------------------------------------------------
@Override
public final Region eval(final Context context)
{
if (condition.eval(context))
return ok.eval(context);
else
return notOk.eval(context);
}
@Override
public boolean isStatic()
{
return condition.isStatic() && ok.isStatic() && notOk.isStatic();
}
@Override
public long gameFlags(final Game game)
{
return condition.gameFlags(game) | ok.gameFlags(game) | notOk.gameFlags(game);
}
@Override
public BitSet concepts(final Game game)
{
final BitSet concepts = new BitSet();
concepts.or(condition.concepts(game));
concepts.or(ok.concepts(game));
concepts.or(notOk.concepts(game));
concepts.set(Concept.ConditionalStatement.id(), true);
return concepts;
}
@Override
public BitSet writesEvalContextRecursive()
{
final BitSet writeEvalContext = new BitSet();
writeEvalContext.or(condition.writesEvalContextRecursive());
writeEvalContext.or(ok.writesEvalContextRecursive());
writeEvalContext.or(notOk.writesEvalContextRecursive());
return writeEvalContext;
}
@Override
public BitSet readsEvalContextRecursive()
{
final BitSet readEvalContext = new BitSet();
readEvalContext.or(condition.readsEvalContextRecursive());
readEvalContext.or(ok.readsEvalContextRecursive());
readEvalContext.or(notOk.readsEvalContextRecursive());
return readEvalContext;
}
@Override
public boolean missingRequirement(final Game game)
{
boolean missingRequirement = false;
if (condition instanceof FalseConstant)
{
game.addRequirementToReport("One of the condition of a (if ...) ludeme is \"false\" which is wrong.");
missingRequirement = true;
}
missingRequirement |= condition.missingRequirement(game);
missingRequirement |= ok.missingRequirement(game);
missingRequirement |= notOk.missingRequirement(game);
return missingRequirement;
}
@Override
public boolean willCrash(final Game game)
{
boolean willCrash = false;
willCrash |= condition.willCrash(game);
willCrash |= ok.willCrash(game);
willCrash |= notOk.willCrash(game);
return willCrash;
}
@Override
public void preprocess(final Game game)
{
condition.preprocess(game);
ok.preprocess(game);
notOk.preprocess(game);
}
//-------------------------------------------------------------------------
@Override
public String toEnglish(final Game game)
{
return "if " + condition.toEnglish(game);
}
//-------------------------------------------------------------------------
}
| 4,096 | 24.767296 | 105 | java |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.