예제 #1
0
        public override IEnumerable <BoardGames.Action <Vector2i> > GetActions(BoardGames.Piece <Vector2i> piece)
        {
            //See how far to the left and right the piece can move.
            for (int xDir = -1; xDir <= 1; xDir += 2)
            {
                int x = piece.CurrentPos.Value.x + xDir;
                while (x >= 0 && x < BoardSize)
                {
                    bool        isBlocking;
                    Action_Move move = TryMove(piece.CurrentPos,
                                               new Vector2i(x, piece.CurrentPos.Value.y),
                                               out isBlocking);

                    if (move != null)
                    {
                        yield return(move);
                    }

                    if (isBlocking)
                    {
                        break;
                    }

                    x += xDir;
                }
            }
            //See how far up/down the piece can move.
            for (int yDir = -1; yDir <= 1; yDir += 2)
            {
                int y = piece.CurrentPos.Value.y + yDir;
                while (y >= 0 && y < BoardSize)
                {
                    bool        isBlocking;
                    Action_Move move = TryMove(piece.CurrentPos,
                                               new Vector2i(piece.CurrentPos.Value.x, y),
                                               out isBlocking);

                    if (move != null)
                    {
                        yield return(move);
                    }

                    if (isBlocking)
                    {
                        break;
                    }

                    y += yDir;
                }
            }
        }
예제 #2
0
        public Board()
        {
            //Create the board.
            theBoard = new Piece[BoardSize, BoardSize];
            for (int y = 0; y < BoardSize; ++y)
            {
                for (int x = 0; x < BoardSize; ++x)
                {
                    theBoard[x, y] = null;
                }
            }


            //Create the pieces.

            int centerPos = (BoardSize / 2);

            //King:
            Piece king = new Piece(true, new Vector2i(centerPos, centerPos), Player_Defender, this);

            theBoard[centerPos, centerPos] = king;
            //Defenders:
            for (int i = 1; i <= 2; ++i)
            {
                theBoard[centerPos - i, centerPos] =
                    new Piece(false, new Vector2i(centerPos - i, centerPos), Player_Defender, this);
                theBoard[centerPos + i, centerPos] =
                    new Piece(false, new Vector2i(centerPos + i, centerPos), Player_Defender, this);
                theBoard[centerPos, centerPos - i] =
                    new Piece(false, new Vector2i(centerPos, centerPos - i), Player_Defender, this);
                theBoard[centerPos, centerPos + i] =
                    new Piece(false, new Vector2i(centerPos, centerPos + i), Player_Defender, this);
            }
            //Attackers:
            for (int i = -1; i <= 1; ++i)
            {
                theBoard[0, centerPos + i] =
                    new Piece(false, new Vector2i(0, centerPos + i), Player_Attacker, this);
                theBoard[BoardSize - 1, centerPos + i] =
                    new Piece(false, new Vector2i(BoardSize - 1, centerPos + i), Player_Attacker, this);
                theBoard[centerPos + i, 0] =
                    new Piece(false, new Vector2i(centerPos + i, 0), Player_Attacker, this);
                theBoard[centerPos + i, BoardSize - 1] =
                    new Piece(false, new Vector2i(centerPos + i, BoardSize - 1), Player_Attacker, this);
            }
            theBoard[0, 0] =
                new Piece(false, new Vector2i(0, 0), Player_Attacker, this);
            theBoard[0, BoardSize - 1] =
                new Piece(false, new Vector2i(0, BoardSize - 1), Player_Attacker, this);
            theBoard[BoardSize - 1, 0] =
                new Piece(false, new Vector2i(BoardSize - 1, 0), Player_Attacker, this);
            theBoard[BoardSize - 1, BoardSize - 1] =
                new Piece(false, new Vector2i(BoardSize - 1, BoardSize - 1), Player_Attacker, this);

            //When a piece moves, switch its place in the grid.
            OnAction += (thisBoard, action) =>
            {
                Action_Move movement = (Action_Move)action;

                theBoard[movement.EndPos.x, movement.EndPos.y] =
                    theBoard[movement.StartPos.x, movement.StartPos.y];
                theBoard[movement.StartPos.x, movement.StartPos.y] = null;
            };

            //If the king is captured, the attackers win.
            //If there are no attackers left, the defenders win.
            OnPieceCaptured += (thisBoard, captured, capturer) =>
            {
                if (captured.IsKing)
                {
                    FinishedGame(Player_Attacker);
                }
                else if (GetPieces(p => p.Owner.Value == Player_Attacker).Count() == 0)
                {
                    FinishedGame(Player_Defender);
                }
            };
            //If the king reaches the end of the board, the defenders win.
            king.CurrentPos.OnChanged += (theKing, oldPos, newPos) =>
            {
                if (newPos.x == 0 || newPos.x == BoardSize - 1 ||
                    newPos.y == 0 || newPos.y == BoardSize - 1)
                {
                    FinishedGame(Player_Defender);
                }
            };
        }