Exemplo n.º 1
0
        internal bool MoveIsValidForRule(GameState currentState, MoveRule rule, Piece piece, Coords from, Coords to, Context ctx)
        {
            if (from != null) piece = board.PieceAt(currentState, from, currentState.CurrentPlayer);
            Piece oppPiece = board.PieceAt(currentState, to, currentState.CurrentPlayer);

            // Rule must be applicable to current piece
            if (rule.PieceType != piece.Type) return false;

            // Target should be empty
            if (rule.TargetMustBeEmpty && oppPiece != null) return false;

            if (rule.Condition != null)
            {
                bool cond = (bool)rule.Condition.Eval(currentState, ctx);
                if (!cond) return false;
            }

            if (rule.From == null)
            {
                if (!currentState.CurrentPlayer.GetOffboard(currentState).Contains(piece)) return false;
                //var toT = board.Transformation(CurrentPlayer, to);
                ///var toTExpr = board.Transformation(CurrentPlayer, (Coords)rule.To.Eval(ctx));
                if (!Coords.Match(to, (Coords)rule.To.Eval(currentState, ctx))) return false;
            }
            else if (rule.To == null)
            {
                throw new NotImplementedException();
            }
            else
            {
                // Check coords
                //var fromT = board.Transformation(CurrentPlayer, from);
                //var toT = board.Transformation(CurrentPlayer, to);
                //var fromTExpr = board.Transformation(CurrentPlayer, (Coords)rule.From.Eval(ctx));
                //var toTExpr = board.Transformation(CurrentPlayer, (Coords)rule.To.Eval(ctx));

                if (!Coords.Match(from, (Coords)rule.From.Eval(currentState, ctx))) return false;
                if (!Coords.Match(to, (Coords)rule.To.Eval(currentState, ctx))) return false;
            }
            return true;
        }
Exemplo n.º 2
0
        public Game(string rules, out GameState firstState)
        {
            try
            {
                var input = new ANTLRStringStream(rules);
                var lexer = new BoardGameLexer(input);
                var tokens = new CommonTokenStream(lexer);
                var parser = new BoardGameParser(tokens);

                var root = parser.parse();

                var errors = parser.GetErrors();

                if (errors.Length > 0)
                {
                    var msg = string.Join("\n", errors);
                    throw new InvalidGameException(string.Format("Number of errors: {0} \n\n{1}", errors.Length, msg));
                }

                var currentState = new GameState(this);
                currentState.GlobalContext = new GlobalContext(currentState);
                InitGlobals(currentState);

                CommonTree t = (CommonTree)root.Tree;

                int playerCount = (int)t.GetChild("SETTINGS").GetChild("PlayerCount").GetOnlyChild().ParseExpr().Eval(currentState, currentState.GlobalContext);
                for (int i = 0; i < playerCount; i++) players.Add(new Player(i+1));

                var size = (Coords)t.GetChild("SETTINGS").GetChild("BoardDimensions").GetOnlyChild().ParseExpr().Eval(currentState, currentState.GlobalContext);
                board = new Board(size);

                for (int i = 0; i < t.GetChild("SETTINGS").GetChild("PieceTypes").ChildCount; i++)
                {
                    string pt = t.GetChild("SETTINGS").GetChild("PieceTypes").GetChild(i).Text;
                    pieceTypes.Add(pt);
                    currentState.GlobalContext.SetVariable(pt, pt);
                }

                bool mirrorEvents = false;
                bool mirrorMoves = false;

                if (t.GetChild("SETTINGS").HasChild("RulesOfPlayer2"))
                {
                    string value = t.GetChild("SETTINGS").GetChild("RulesOfPlayer2").GetOnlyChild().Text;
                    switch (value)
                    {
                        case "SameAsPlayer1":
                            mirrorEvents = true;
                            mirrorMoves = true;
                            break;
                        case "RotationOfPlayer1":
                            mirrorEvents = true;
                            mirrorMoves = true;
                            board.Transformation = (asker, coord) =>
                            {
                                if (asker == GetPlayer(2))
                                {
                                    return new Coords(new int[] { Size[0] + 1 - coord[0], Size[0] + 1 - coord[1] }, coord.PlaceHolders);
                                }
                                else
                                {
                                    return coord;
                                }
                            };
                            break;
                        case "MirrorOfPlayer1":
                            mirrorEvents = true;
                            mirrorMoves = true;
                            board.Transformation = (asker, coord) =>
                            {
                                if (asker == GetPlayer(2))
                                {
                                    return new Coords(new int[] { coord[0], Size[0] + 1 - coord[1] }, coord.PlaceHolders);
                                }
                                else
                                {
                                    return coord;
                                }
                            };
                            break;
                        default:
                            throw new InvalidGameException(string.Format("Unsupported rule transformation: {0}", value));
                    }
                }

                if (t.HasChild("FUNCDEFLIST"))
                {
                    var functions = t.GetChild("FUNCDEFLIST");
                    for (int i = 0; i < functions.ChildCount; i++)
                    {
                        var funcNode = functions.GetChild(i);
                        System.Diagnostics.Debug.Assert(funcNode.Text == "FUNCDEF");
                        string name = funcNode.GetChild("REF").GetOnlyChild().Text;
                        var paramList = new List<string>();
                        for (int j = 0; j < funcNode.GetChild("PARAMLIST").ChildCount; j++)
                        {
                            paramList.Add(funcNode.GetChild("PARAMLIST").GetChild(j).Text);
                        }
                        var body = funcNode.GetChild("STATEMENTS").ParseStmtList();
                        var func = new UserFunction(paramList.ToArray(), body);
                        currentState.GlobalContext.SetVariable(name, func);
                    }
                }

                if (t.HasChild("INIT"))
                {
                    var stmt = t.GetChild("INIT").GetChild("STATEMENTS").ParseStmtList();
                    stmt.Run(currentState, currentState.GlobalContext);
                }

                if (t.HasChild("STARTINGBOARD"))
                {
                    for (int i = 0; i < t.GetChild("STARTINGBOARD").ChildCount; i++)
                    {
                        var ch = t.GetChild("STARTINGBOARD").GetChild(i);
                        switch (ch.Text)
                        {
                            case "Invalid":
                                board.Valid = Board.RuleType.Invalid;
                                for (int j = 0; j < ch.ChildCount; j++)
                                {
                                    board.AddRule(ch.GetChild(j).ParseExpr());
                                }
                                break;
                            case "Valid":
                                board.Valid = Board.RuleType.Valid;
                                for (int j = 0; j < ch.ChildCount; j++)
                                {
                                    board.AddRule(ch.GetChild(j).ParseExpr());
                                }
                                break;
                            case "STARTINGPIECES":
                                int ownerInt = (int)ch.GetChild("PLAYERREF").GetOnlyChild().ParseExpr().Eval(currentState, currentState.GlobalContext);
                                Player owner = GetPlayer(ownerInt);
                                currentState.CurrentPlayerID = ownerInt - 1;

                                for (int j = 0; j < ch.GetChild("LIST").ChildCount; j++)
                                {
                                    ITree pieceNode = ch.GetChild("LIST").GetChild(j);
                                    string type = pieceNode.Text;
                                    if (!IsValidPiece(type))
                                    {
                                        throw new InvalidGameException(pieceNode.FileCoords() + " - Invalid piece type '" + type + "'");
                                    }
                                    Coords coords = null;
                                    if (pieceNode.HasChild("LIT_COORDS"))
                                    {
                                        coords = (Coords)pieceNode.GetChild("LIT_COORDS").ParseExpr().Eval(currentState, currentState.GlobalContext);
                                    }
                                    else if (pieceNode.HasChild("Offboard"))
                                    { /* Do nothing */ }
                                    else
                                    {
                                        throw new Exception("Cannot happen");
                                    }
                                    var p = new Piece(type, owner, this);
                                    if (coords == null)
                                    {
                                        currentState.CurrentPlayer.AddOffboard(currentState, p);
                                    }
                                    else if (!board.TryPut(currentState, coords, p, null))
                                    {
                                        throw new InvalidGameException(pieceNode.FileCoords() + " - Invalid coords for '" + type + "'");
                                    }
                                    if (pieceNode.HasChild("TAG"))
                                    {
                                        string tag = pieceNode.GetChild("TAG").GetOnlyChild().Text;
                                        currentState.GlobalContext.SetVariable(tag, p);
                                    }
                                }

                                break;
                        }
                    }
                }

                if (t.HasChild("PATTERNS"))
                {
                    for (int i = 0; i < t.GetChild("PATTERNS").ChildCount; i++)
                    {
                        var patternNode = t.GetChild("PATTERNS").GetChild(i);
                        var patternName = patternNode.GetChild("PATTERNNAME").GetOnlyChild().Text;
                        var elements = new List<RegExp.PatternElement>();
                        for (int j = 0; j < patternNode.GetChild("PATTERNPARTS").ChildCount; j++)
                        {
                            var elementNode = patternNode.GetChild("PATTERNPARTS").GetChild(j);
                            RegExp.PatternElement element = new RegExp.PatternElement();
                            var predNode = elementNode.GetChild("PATTERNPREDICATE").GetOnlyChild();
                            if (predNode.Text == "Empty")
                            {
                                element.Predicate = (state, coords) => board.PieceAt(state, coords, state.CurrentPlayer) == null;
                            }
                            else
                            {
                                Expression expr = predNode.ParseExpr();
                                element.Predicate = (state, coords) =>
                                {
                                    Context ctx = new Context(state.GlobalContext);
                                    ctx.SetXYZ(coords, state.CurrentPlayer);
                                    var retVal = expr.Eval(state, ctx);
                                    Player pl = retVal as Player;
                                    if (pl != null)
                                    {
                                        Piece piece = board.PieceAt(state, coords, state.CurrentPlayer);
                                        if (piece == null) return false;
                                        return piece.Owner == pl;
                                    }
                                    throw new NotSupportedException(string.Format("Unsupported pattern predicate: {0}", retVal));
                                };
                            }
                            var countNode = elementNode.GetChild("PATTERNCOUNT").GetOnlyChild();
                            if (countNode.Text == "Target")
                            {
                                element.IsTarget = true;
                                element.Count = new int[] { 1 };
                            }
                            else
                            {
                                Context ctx = new Context(currentState.GlobalContext);
                                Expression expr = countNode.ParseExpr();
                                object count = expr.Eval(currentState, ctx);
                                if (count is int)
                                {
                                    element.Count = new int[] { (int)count };
                                }
                                else if (count is IEnumerable<int>)
                                {
                                    element.Count = (IEnumerable<int>)count;
                                }
                                else if (count is IEnumerable<object>)
                                {
                                    element.Count = from obj in (IEnumerable<object>)count
                                                    select (int)obj;
                                }
                                else
                                {
                                    throw new InvalidGameException("Invalid value for pattern range.");
                                }
                            }
                            elements.Add(element);
                        }
                        var pattern = new RegExp.Pattern(elements);
                        currentState.GlobalContext.SetVariable(patternName, pattern);
                    }
                }

                ITree moveRoot = t.GetChild("MOVES");
                if (mirrorMoves)
                {
                    moveRoot.Rewrite(TreeHelpers.RewriteMirrorPlayer);
                }

                for (int i = 0; i < t.GetChild("MOVES").ChildCount; i++)
                {
                    var moveNode = t.GetChild("MOVES").GetChild(i);
                    string piece = moveNode.Text;
                    var moveOpNode = moveNode.GetChild("OP_MOVE");
                    var moveFromNode = moveOpNode.GetChild("MOVE_FROM");

                    CoordExpr moveFrom = null;
                    CoordExpr moveTo = null;

                    if (!moveFromNode.HasChild("Offboard"))
                    {
                        moveFrom = (CoordExpr)moveFromNode.GetOnlyChild().ParseExpr();
                    }
                    var moveToNode = moveOpNode.GetChild("MOVE_TO");
                    if (!moveToNode.HasChild("Offboard"))
                    {
                        moveTo = (CoordExpr)moveToNode.GetOnlyChild().ParseExpr();
                    }
                    var moveOptionsNode = moveOpNode.GetChild("MOVE_OPTIONS");
                    bool emptyTarget = moveOptionsNode.HasChild("Empty");

                    string label = null;
                    if (moveOpNode.HasChild("Label"))
                    {
                        label = moveOpNode.GetChild("Label").GetOnlyChild().Text;
                        currentState.GlobalContext.SetVariable(label, label);
                    }

                    Expression condition = null;
                    if (moveOpNode.HasChild("IF"))
                    {
                        condition = moveOpNode.GetChild("IF").GetOnlyChild().ParseExpr();
                    }

                    Statement stmt = null;
                    if (moveOpNode.HasChild("STATEMENTS"))
                    {
                        stmt = moveOpNode.GetChild("STATEMENTS").ParseStmtList();
                    }

                    var rule = new MoveRule(piece, moveFrom, moveTo, emptyTarget, label, condition, stmt, this);
                    moveRules.Add(rule);
                }

                if (mirrorEvents)
                {
                    t.GetChild("EVENTS").Rewrite(TreeHelpers.RewriteMirrorPlayer);
                }

                for (int i = 0; i < t.GetChild("EVENTS").ChildCount; i++)
                {
                    var eventNode = t.GetChild("EVENTS").GetChild(i);
                    var stmt = eventNode.GetChild("STATEMENTS").ParseStmtList();

                    // Register event for all players
                    for (int eventI = 0; eventI < eventNode.GetChild("EVENTTYPES").ChildCount; eventI++)
                    {
                        var eventTypeNode = eventNode.GetChild("EVENTTYPES").GetChild(eventI);
                        Player p = (Player)eventTypeNode.GetChild("PLAYERREF").ParseExpr().Eval(currentState, new Context(currentState));
                        string eventType = eventTypeNode.GetChild(1).Text;
                        switch (eventType)
                        {
                            case "CannotMove":
                                p.AddCannotMove(stmt);
                                break;
                            case "FinishedMove":
                                p.AddPostMove(stmt);
                                break;
                            case "BeforeMove":
                                p.AddPreMove(stmt);
                                break;
                            default:
                                throw new InvalidGameException("Invalid event: " + eventType);
                        }
                    }

                }

                currentState.CurrentPlayerID = 0;
                PreMoveActions(currentState);
                firstState = currentState;
            }
            catch (InvalidGameException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new InvalidGameException(e);
            }
        }