/** verifies whether the specified move conforms to this rule in the provided context */ public boolean matches(Move move, Context context) throws Exception { GameState st = context.getState(); if (element.matches(move.element, context)) if ((state == null) || (state.matches(st.elementStates.get(move.element), context))) if ((currentPlayer == null) || (currentPlayer.matches(st.currentPlayer, context))) if (from.matches(move.from, context)) if (to.matches(move.to, context)) return condition.eval(context).isTrue(); return false; }
/** * list of moves that can be performed from this state with the element specified in the first * argument * * @return the list or null, if no such moves exist */ public ArrayList<Move> getMatchingMoves(Element el, GameSpecification specs, Context context) throws Exception { GameState st = context.getState(); ArrayList<Move> moves = new ArrayList<Move>(); if (element.matches(el.name.fullName, context)) if ((state == null) || (state.matches(st.elementStates.get(el.name.fullName), context))) if ((currentPlayer == null) || (currentPlayer.matches(st.currentPlayer, context))) { String tryFrom = st.elementLocations.get(el.name.fullName); if (from.matches(tryFrom, context)) for (Location tryTo : specs.locations.values()) if (st.locationElements.get(tryTo.name.fullName) == null) if (to.matches(tryTo.name.fullName, context)) if (condition.eval(context).isTrue()) moves.add(new Move(tryFrom, tryTo.name.fullName, el.name.fullName, specs)); } if (moves.size() > 0) return moves; return null; }
/** construct an empty rule with the specified name */ public GameRule(String name) { this.name = name; scorePlayer = new ArrayList<Expr>(); scoreAmount = new ArrayList<Expr>(); condition = Expr.booleanExpr(true); }
/** perform the follow-up action, after this rule was matched */ public void performAction(Context context) throws Exception { if (action != null) action.eval(context); }