Пример #1
0
        public void move(string direction)
        {
            Position currentHeadPosition = body[0];
            Position newHeadPosition     = null;

            switch (direction)
            {
            case "UP":
                if (currentHeadPosition.getRowNo() > mainBoard.getMinRowNo())
                {
                    newHeadPosition = new Position(currentHeadPosition.getRowNo() - 1, currentHeadPosition.getColNo());
                }
                break;

            case "DOWN":
                if (currentHeadPosition.getRowNo() < mainBoard.getMaxRowNo())
                {
                    newHeadPosition = new Position(currentHeadPosition.getRowNo() + 1, currentHeadPosition.getColNo());
                }
                break;

            case "LEFT":
                if (currentHeadPosition.getColNo() > mainBoard.getMinColNo())
                {
                    newHeadPosition = new Position(currentHeadPosition.getRowNo(), currentHeadPosition.getColNo() - 1);
                }
                break;

            case "RIGHT":
                if (currentHeadPosition.getColNo() < mainBoard.getMaxColNo())
                {
                    newHeadPosition = new Position(currentHeadPosition.getRowNo(), currentHeadPosition.getColNo() + 1);
                }
                break;

            default:
                //Do nothing
                break;
            }

            if (newHeadPosition != null)
            {
                body.Insert(0, newHeadPosition); //Add a new head positon 0
                body.RemoveAt(body.Count - 1);   //Remove the last position
            }
        }