Example #1
0
        public void JumpTo(Slot destination)
        {
            if (destination.Empty == false)
                throw new ArgumentException(string.Format("Cannot jump to the location {0},{1} - it is not empty", destination.X, destination.Y));

            UpdateStateForValidMove(destination);
        }
Example #2
0
        private void MoveTo(Slot destination, Direction direction, IMessageLog log)
        {
            if (Position == destination)
                return;

            if (destination.Empty)
            {
                UpdateStateForValidMove(destination, log);
                return;
            }
            else
            {
                // Is the move blocked by another player?
                if (destination.Player != null)
                    return;

                // Is the move blocked by a piece we don't control?
                if (destination.Piece.Owner != this)
                    return;

                var pushSlot = Game.Board.GetSlotForDirection(destination, direction);

                // Is the push blocked by something behind it?
                if (pushSlot.Empty == false)
                    return;

                Game.Board.MovePieceToSlot(destination, pushSlot, log);
                UpdateStateForValidMove(destination, log);
            }
        }
Example #3
0
        public Board(int width, int height)
        {
            Width = width;
            Height = height;
            random = new Random();

            slotGrid = new Slot[width, height];
            slots = new List<Slot>(width * height);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    var slot = new Slot(i, j);
                    slotGrid[i, j] = slot;
                    slots.Add(slot);
                }
            }

            StartingPositions = new List<Slot>()
            {
                slotGrid[0,0], slotGrid[width-1, height -1], slotGrid[0, height-1], slotGrid[width-1, 0],
                slotGrid[0, height / 2], slotGrid[width / 2, 0], slotGrid[width-1, height / 2], slotGrid[width / 2, height-1]
            };
        }
Example #4
0
        public void AddPiece(Slot slot)
        {
            JsonObject obj = BuildGameMessageJson(GameMessageType.PieceAdded, msg =>
            {
                msg.owner = slot.Piece.Owner.Name;
                msg.x = slot.X;
                msg.y = slot.Y;
            });

            Messages.Add(obj);
        }
Example #5
0
        public void MovePiece(Slot origin, Slot destination)
        {
            JsonObject obj = BuildGameMessageJson(GameMessageType.PieceMoved, msg =>
            {
                msg.owner = destination.Piece.Owner.Name;
                msg.origin = new JsonObject();
                msg.origin.x = origin.X;
                msg.origin.y = origin.Y;
                msg.destination = new JsonObject();
                msg.destination.x = destination.X;
                msg.destination.y = destination.Y;
            });

            Messages.Add(obj);
        }
Example #6
0
        public void MovePlayer(Player player, Slot slot)
        {
            JsonObject obj = BuildGameMessageJson(GameMessageType.PlayerMoved, msg =>
            {
                msg.player = player.Name;
                msg.x = slot.X;
                msg.y = slot.Y;
            });

            Messages.Add(obj);
        }
Example #7
0
        private void UpdateStateForValidMove(Slot destination, IMessageLog log = null)
        {
            if (Position != null)
                Position.Player = null;

            Position = destination;
            destination.Player = this;

            if(log != null)
                log.MovePlayer(this, destination);
        }
Example #8
0
 public void AddNewPieceToSlot(Slot slot, Player owner)
 {
     var newPiece = new Piece { Owner = owner };
     slot.Piece = newPiece;
 }
Example #9
0
        public void MovePieceToSlot(Slot origin, Slot destination, IMessageLog log)
        {
            if (origin.Piece == null)
                throw new InvalidOperationException("Cannot move piece because the origin slot is empty");

            destination.Piece = origin.Piece;
            origin.Piece = null;

            if(log != null)
                log.MovePiece(origin, destination);
        }
Example #10
0
        public Slot GetSlotForDirection(Slot origin, Direction direction)
        {
            int xDelta = 0;
            int yDelta = 0;

            switch (direction)
            {
                case Direction.Up:
                    yDelta = -1;
                    break;
                case Direction.Down:
                    yDelta = +1;
                    break;
                case Direction.Left:
                    xDelta = -1;
                    break;
                case Direction.Right:
                    xDelta = +1;
                    break;
            }

            int newX = Math.Min(Width - 1, Math.Max(0, origin.X + xDelta));
            int newY = Math.Min(Height - 1, Math.Max(0, origin.Y + yDelta));

            return SlotAt(newX, newY);
        }