Esempio n. 1
0
        /**
         * Move a Player one step in a given direction.
         * @param player The player to move.
         * @param direction The direction in which to move.
         * @throws InvalidOperationException if it's not the player's turn or the player tries to move to an
         * illegal location.
         */
        public void Move(Player player, Direction direction)
        {
            int curX = player.X;
            int curY = player.Y;

            if (!player.Equals(_currentPlayer))
            {
                throw new InvalidOperationException("Cannot move a piece other than your own.");
            }
            int newX = curX + direction.DeltaX;
            int newY = curY + direction.DeltaY;

            if (newX < 0 || newX > _maxX || newY < 0 || newY > _maxY)
            {
                throw new InvalidOperationException("Cannot move to (" + newX + "," + newY + "); it is off the board");
            }
            BoardObject newOccupant = _grid[newX, newY];

            if (newOccupant.BlocksMovement())
            {
                throw new InvalidOperationException("Cannot move to (" + newX + "," + newY + "); it is occupied by " + newOccupant);
            }
            if (newOccupant is Prize)
            {
                Prize prize = (Prize)newOccupant;
                player.IncreaseScore(prize.Points);
                _prizes.Remove(prize);
            }
            _grid[newX, newY] = player;
            player.SetPosition(newX, newY);
            _grid[curX, curY] = new EmptySpace();
            _grid[curX, curY].SetPosition(curX, curY);

            Console.WriteLine($"Player {GameEngine.GetPlayerLetter(player)} decided to move {direction}\n");
        }
Esempio n. 2
0
 protected Direction?GetDirection(Board board, BoardObject target)
 {
     // try X first
     if (target.X > X && IsOpen(board, Direction.RIGHT))
     {
         return(Direction.RIGHT);
     }
     if (target.X < X && IsOpen(board, Direction.LEFT))
     {
         return(Direction.LEFT);
     }
     if (target.Y > Y && IsOpen(board, Direction.UP))
     {
         return(Direction.UP);
     }
     if (target.Y < Y && IsOpen(board, Direction.DOWN))
     {
         return(Direction.DOWN);
     }
     return(null);
 }