コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: hgabor/boardgame
 private void SetSelected(Piece p)
 {
     selectedPiece = p;
     selectionFrame.Continue = false;
 }
コード例 #2
0
ファイル: Board.cs プロジェクト: hgabor/boardgame
        public bool TryPut(GameState state, Coords cIn, Piece p, Player asker)
        {
            Coords c = Transformation(asker, cIn);

            if (c.IsPlaceHolder) throw new ArgumentOutOfRangeException("Non-placeholder coords needed.");
            if (!IsValidPlace(state, c))
            {
                throw new ArgumentOutOfRangeException("Coords must be inside the board.");
            }
            if (state.Board.ContainsKey(c)) return false;
            state.Board.Add(c, p);
            return true;
        }
コード例 #3
0
ファイル: MainWindow.xaml.cs プロジェクト: hgabor/boardgame
        private void MakeMove()
        {
            if (fromOffboard != null && toCoord != null)
            {
                // From offboard to board
                var newGameState = game.TryMakeMoveFromOffboard(gameState, FromOffboard, toCoord);
                if (newGameState != null) gameState = newGameState;

                offBoard.Refresh();
                gamePanel.InvalidateVisual();
            }
            else if (fromCoord != null && toCoord != null)
            {
                // From coords to coords
                var newGameState = game.TryMakeMove(gameState, FromCoord, toCoord);
                if (newGameState != null) gameState = newGameState;

                offBoard.Refresh();
                gamePanel.InvalidateVisual();
            }

            if (game.GameOver)
            {
                if (game.PlayerCount == 1)
                {
                    if (game.Winners.Count() == 1)
                    {
                        System.Windows.MessageBox.Show("You won!");
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("You lost!");
                    }
                }
                else
                {
                    if (game.Winners.Count() == 0)
                    {
                        System.Windows.MessageBox.Show("Game over!\nThe game ended in a tie!");
                    }
                    else if (game.Winners.Count() == 1)
                    {
                        System.Windows.MessageBox.Show("Game over!\nThe winner is: " + game.Winners.First().ToString());
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Game over!\nThe winners are: " + string.Join(", ", game.Winners));
                    }
                }
                NewGame();
            }
            selectState = SelectState.From;
            FromCoord = null;
            FromOffboard = null;
            toCoord = null;
            currentPlayerLabel.Content = gameState.CurrentPlayer.ToString();
        }
コード例 #4
0
ファイル: Game.cs プロジェクト: hgabor/boardgame
        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;
        }
コード例 #5
0
ファイル: Game_globals.cs プロジェクト: hgabor/boardgame
 public static void Place(Context ctx, string piecetype, Coords c)
 {
     Piece p = new Piece(piecetype, ctx.GameState.CurrentPlayer, ctx.Game);
     ctx.Game.board.TryPut(ctx.GameState, c, p, ctx.GameState.CurrentPlayer);
 }
コード例 #6
0
ファイル: Game.cs プロジェクト: hgabor/boardgame
 public IEnumerable<MoveDefinition> EnumerateMovesFromOffboard(GameState state, Piece p)
 {
     var ret = EnumeratePossibleMoves(state).Where(md => md.From == null && md.PieceType == p.Type);
     return ret;
 }
コード例 #7
0
ファイル: Game.cs プロジェクト: hgabor/boardgame
        public GameState TryMakeMoveFromOffboard(GameState oldState, Piece piece, Coords to)
        {
            GameState newState = oldState.Clone();
            Context ctx = new Context(newState.GlobalContext);
            ctx.SetVariable("to", Transform(to, newState.CurrentPlayer));
            ctx.SetVariable("NewGameState", newState);

            // piece cannot be null
            if (piece == null) return null;
            // Cannot move opponent's piece
            if (piece.Owner != newState.CurrentPlayer) return null;

            bool isInlist = EnumerateMovesFromOffboard(newState, piece).Any(
                md => md.From == null && Coords.Match(md.To, to) && md.PieceType == piece.Type);
            if (!isInlist) return null;

            if (!MoveIsValidGlobal(newState, null, to, ctx)) return null;

            Piece oppPiece = board.PieceAt(newState, to, null);
            foreach (var rule in moveRules)
            {
                // Only offboard rules here
                if (!rule.OffboardRule) continue;

                if (!MoveIsValidForRule(newState, rule, piece, null, to, ctx)) continue;

                // Move is valid
                rule.RunAction(newState, ctx);

                // Perform the move
                if (!newState.CurrentPlayer.RemoveOffBoard(newState, piece))
                {
                    // Original piece was removed, should not happen!
                    throw new InvalidGameException("Cannot move from offboard, piece " + oppPiece.ToString() + " was removed!");
                }
                if (!board.TryPut(newState, to, piece, null))
                {
                    // Piece was not captured
                    throw new InvalidGameException("Cannot move to " + to.ToString() + ", piece " + oppPiece.ToString() + " is in the way!");
                }

                ctx.SetXYZ(to, newState.CurrentPlayer);

                // Move was performed
                oldStates.Add(oldState);

                newState.CurrentPlayer.PostMove(newState, ctx);
                PostMoveActions(newState);

                return newState;
            }
            // No suitable rule found.
            return null;
        }
コード例 #8
0
ファイル: Game.cs プロジェクト: hgabor/boardgame
        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);
            }
        }
コード例 #9
0
 public void AddHighlight(Piece p)
 {
     highlight.Add(p);
 }
コード例 #10
0
 public OffboardEventArgs(Piece piece)
 {
     this.Piece = piece;
 }
コード例 #11
0
ファイル: Player.cs プロジェクト: hgabor/boardgame
 internal void AddOffboard(GameState state, Piece p)
 {
     var pieces = state.GetOffboard(this);
     pieces.Add(p);
 }
コード例 #12
0
ファイル: Player.cs プロジェクト: hgabor/boardgame
 internal bool RemoveOffBoard(GameState state, Piece p)
 {
     var pieces = state.GetOffboard(this);
     if (pieces.Contains(p))
     {
         pieces.Remove(p);
         return true;
     }
     else
     {
         return false;
     }
 }