コード例 #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");
        }
コード例 #2
0
 public Board(int x, int y)
 {
     _maxX = x - 1;
     _maxY = y - 1;
     _grid = new BoardObject[x, y];
     for (var i = 0; i < _grid.GetLength(0); i++)
     {
         for (var j = 0; j < _grid.GetLength(1); j++)
         {
             _grid[i, j] = new EmptySpace();
             _grid[i, j].SetPosition(i, j);
         }
     }
 }