⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operator.cs

📁 一个由.NET WindowsForm上开发的华容道游戏
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


namespace HuaRongDao
{
    class Operator
    {
        Board _board = null;

        public Operator(Board board)
        {
            _board = board;
            _board.MouseDown += new System.Windows.Forms.MouseEventHandler(_board_MouseDown);
       }

        void _board_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (_board.FocusedCharacter != null)
            {
                Move(_board.FocusedCharacter,_board.GetLogicalPoint(e.Location));
            }
        }

        public void Move(Character character, Point target)
        {
            List<Point> path = GetPossiblePath(character, target, this._board);
            if (path.Count>0)
            {
                foreach (Point p in path)
                {
                    character.LogicLocation = p;
                    System.Diagnostics.Debug.WriteLine(p.ToString());
                }
                System.Diagnostics.Debug.WriteLine(character.LogicName + " moves to:" + character.LogicLocation.ToString());
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(character.LogicName + " can not moves to:" + target.ToString());
                return;
            }

            if (character.IsCC)
            {
                if (IsCharacterOnExit(character))
                {
                    System.Diagnostics.Debug.WriteLine("CC is out");
                }
            }
        }

        public bool CanMove(Character character, Point target)
        {
            List<Point> path = GetPossiblePath(character, target, this._board);
            return (path.Count > 0);
        }

        public bool IsCharacterOnExit(Character character)
        {
            return character.LogicBounds.Contains(_board.LogicExit);
        }

        #region Find a path to move character
        private List<Point> GetPossiblePath(Character character, Point target, Board board)
        {
            List<Point> ret = new List<Point>();

            for (int i = 0; i < character.LogicSize.Width; i++)
            {
                for (int j = 0; j < character.LogicSize.Height; j++)
                {
                    Point point = target;
                    point.Offset(i*-1, j*-1);
                    ret = GetPath(character, point, board);
                    if (ret.Count > 0)
                    {
                        return ret;
                    }
                }
            }

            return ret;
        }

        private List<Point> GetPath(Character character, Point target, Board board)
        {
            List<Point> path = new List<Point>();

            if (this._board.OthersIsHere(new Rectangle(target, character.LogicSize), character))
            {
                return path;
            }

            List<Point> retPath = new List<Point>();

            path.Add(character.LogicLocation);
            Moving(character, character.LogicLocation, target, board, path, retPath);

            return retPath;
        }

        private void Moving(Character character, Point start, Point target, Board board, List<Point> path, List<Point> shortestPath)
        {
            Point mover = start;
            int steps = path.Count;

            // Move to left
            mover.Offset(-1, 0);
            MoveTo(character, mover, target, board, path, shortestPath);

            // Move to right
            mover = start;
            mover.Offset(1, 0);
            MoveTo(character, mover, target, board, path, shortestPath);

            // Move to up
            mover = start;
            mover.Offset(0, -1);
            MoveTo(character, mover, target, board, path, shortestPath);

            // Move to down
            mover = start;
            mover.Offset(0, 1);
            MoveTo(character, mover, target, board, path, shortestPath);
        }

        private void MoveTo(Character character, Point mover, Point target, Board board, List<Point> path, List<Point> shortestPath)
        {

            if (CanMoveToHere(character, board, mover, path))
            {
                if (mover == target)
                {
                    path.Add(target);
                    if (shortestPath.Count == 0 || shortestPath.Count > path.Count)
                    {
                        shortestPath.Clear();
                        shortestPath.AddRange(path);
                    }
                }
                else
                {
                    int steps = path.Count;
                    path.Add(mover);
                    Moving(character, mover, target, board, path, shortestPath);
                    path.RemoveRange(steps, path.Count - steps);
                }
            }


        }

        private bool CanMoveToHere(Character character, Board board, Point point, List<Point> path)
        {
            Rectangle rect = new Rectangle(point, character.LogicSize);

            if (!board.LogicBounds.Contains(rect) || path.Contains(point) || board.OthersIsHere(rect,character))
            {
                return false;
            }

            return true;
        }
        #endregion

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -