Chess/Rule/CheckMoveChessPieceRule.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess.Rule
{
    /// <summary>
    /// 移动是否合法规则
    /// </summary>
    public class CheckMoveChessPieceRule : IRule
    {
        private ChessBoard _chessBoard;
        private Player _player;
        private ChessPiece _chessPiece;
        private Dir _dir;
        
        public CheckMoveChessPieceRule(ChessBoard chessBoard, Player player, ChessPiece chessPiece,Dir dir) {
            this._chessBoard = chessBoard;
            this._player = player;
            this._chessPiece = chessPiece;
            this._dir = dir;
        }

        public bool Execute()
        {
            var pos = _chessPiece.GetPosition();
            int row = pos.row;
            int col = pos.col;

            switch (_dir) {
                case Dir.LEFT:
                    col--;
                    break;
                case Dir.RIGHT:
                    col++;
                    break;
                case Dir.TOP:
                    row--;
                    break;
                case Dir.BOTTOM:
                    row++;
                    break;
            }
            //检查位置是否合法
            if (!_chessBoard.CheckPositionVaild(row, col)) return false;

            //检查此处是否有棋子
            if (_chessBoard.IsEmpty(row, col)) return true;

            //检查是否是自己的棋子
            if (_player.GetChessPiece(row, col) != null) return false;

            //是对方的棋子,检查是否能吃掉
            //检查跳过去的位置
            switch (_dir)
            {
                case Dir.LEFT:
                    col--;
后面还有 22 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。