/// <summary> /// 棋子是否可以移动 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以移动</returns> public virtual bool CanMove(BlankPosition blankPosition, int gridRows, int gridColumns) { return(this.CanMoveUp(blankPosition, gridRows, gridColumns) || this.CanMoveDown(blankPosition, gridRows, gridColumns) || this.CanMoveLeft(blankPosition, gridRows, gridColumns) || this.CanMoveRight(blankPosition, gridRows, gridColumns)); }
/// <summary> /// 设置棋子可移动至的新位置 /// </summary> /// <param name="moveDirection">移动方向</param> /// <param name="gridColumns">网格列数</param> /// <param name="blankPosition">空白网格</param> /// <param name="callBack">设置完成后的回调</param> public virtual void SetNewPosition(Direction moveDirection, int gridColumns, BlankPosition blankPosition, SetNewPositionDelegate callBack = null) { this.NextPosition = this.Position; if (moveDirection == Direction.Up) { this.NextPosition -= gridColumns; } if (moveDirection == Direction.Down) { this.NextPosition += gridColumns; } if (moveDirection == Direction.Left) { this.NextPosition -= 1; } if (moveDirection == Direction.Right) { this.NextPosition += 1; } if (callBack != null) { callBack(this, moveDirection, blankPosition); } }
/// <summary> /// 棋子是否可以向左移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向左移动一格</returns> public override bool CanMoveLeft(BlankPosition blankPosition, int gridRows, int gridColumns) { if (this.Position % gridColumns == 0) { return(false); } int temp = this.Position - 1; return(temp == blankPosition.Position1 || temp == blankPosition.Position2); }
/// <summary> /// 棋子是否可以向右移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向右移动一格</returns> public override bool CanMoveRight(BlankPosition blankPosition, int gridRows, int gridColumns) { if (this.Position % gridColumns >= 2) { return(false); } int temp = this.Position + 2; return(temp == blankPosition.Position1 || temp == blankPosition.Position2); }
/// <summary> /// 棋子是否可以向上移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向上移动一格</returns> public override bool CanMoveUp(BlankPosition blankPosition, int gridRows, int gridColumns) { if (this.Position < gridColumns) { return(false); } int temp = this.Position - gridColumns; return(temp == blankPosition.Position1 || temp == blankPosition.Position2); }
/// <summary> /// 棋子是否可以向下移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向下移动一格</returns> public override bool CanMoveDown(BlankPosition blankPosition, int gridRows, int gridColumns) { if (this.Position >= (gridRows - 2) * gridColumns) { return(false); } int temp = this.Position + 2 * gridColumns; return(temp == blankPosition.Position1 || temp == blankPosition.Position2); }
/// <summary> /// 棋子是否可以向左移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向左移动一格</returns> public override bool CanMoveLeft(BlankPosition blankPosition, int gridRows, int gridColumns) { if (this.Position % gridColumns == 0) { return(false); } int temp = this.Position - 1; int temp2 = temp + gridColumns; return((temp == blankPosition.Position1 && temp2 == blankPosition.Position2) || (temp == blankPosition.Position2 && temp2 == blankPosition.Position1)); }
/// <summary> /// 棋子是否可以向下移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向下移动一格</returns> public override bool CanMoveDown(BlankPosition blankPosition, int gridRows, int gridColumns) { if (this.Position >= (gridRows - 2) * gridColumns) { return(false); } int temp = this.Position + 2 * gridColumns; int temp2 = temp + 1; return((temp == blankPosition.Position1 && temp2 == blankPosition.Position2) || (temp == blankPosition.Position2 && temp2 == blankPosition.Position1)); }
public void EvaBlankPosition(bool is_ai, BlankPosition bp) { Piece piece = is_ai ? Piece.WHITE : Piece.BLACK; board[bp.pos.x, bp.pos.y] = (int)piece; float score1 = CalBlankScore(bp.pos.x, bp.pos.y, piece, 0, 1); float score2 = CalBlankScore(bp.pos.x, bp.pos.y, piece, 1, 0); float score3 = CalBlankScore(bp.pos.x, bp.pos.y, piece, 1, 1); float score4 = CalBlankScore(bp.pos.x, bp.pos.y, piece, -1, 1); bp.score += Mathf.Max(score1, score2, score3, score4); board[bp.pos.x, bp.pos.y] = 0; }
/// <summary> /// 移动棋子(该方法用于玩家模式)并返回新的空白棋子位置 /// </summary> /// <param name="blankPosition">当前空白棋子位置</param> /// <param name="moveDirection">当前棋子移动方向</param> /// <param name="gridColumns">网格列数</param> /// <returns>新的空白棋子位置</returns> public override BlankPosition ChessMove(BlankPosition blankPosition, Direction moveDirection, int gridColumns) { if (blankPosition.Position1 == this.NextPosition) { blankPosition.Position1 = this.Position; } else { blankPosition.Position2 = this.Position; } return(new BlankPosition { Position1 = blankPosition.Position1, Position2 = blankPosition.Position2 }); }
/// <summary> /// 尝试移动棋子(该方法用于自动求解) /// </summary> /// <param name="blankPosition">当前空白棋子位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <param name="callBack">设置完成后的回调</param> public override void TryChessMove(BlankPosition blankPosition, int gridRows, int gridColumns, SetNewPositionDelegate callBack) { BlankPosition tmpBlankPosition; //下 if (this.CanMoveDown(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Down, gridColumns, blankPosition, callBack); tmpBlankPosition = ChessMove(blankPosition, Direction.Down, gridColumns); this.Position = this.NextPosition; //连续下2 if (this.CanMoveDown(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Down, gridColumns, tmpBlankPosition, callBack); return; } } //左 if (this.CanMoveLeft(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Left, gridColumns, blankPosition, callBack); } //右 if (this.CanMoveRight(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Right, gridColumns, blankPosition, callBack); } //上 if (this.CanMoveUp(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Up, gridColumns, blankPosition, callBack); tmpBlankPosition = ChessMove(blankPosition, Direction.Up, gridColumns); this.Position = this.NextPosition; //连续上2 if (this.CanMoveUp(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Up, gridColumns, tmpBlankPosition, callBack); return; } } }
/// <summary> /// 移动棋子(该方法用于玩家模式)并返回新的空白棋子位置 /// </summary> /// <param name="blankPosition">当前空白棋子位置</param> /// <param name="moveDirection">当前棋子移动方向</param> /// <param name="gridColumns">网格列数</param> /// <returns>新的空白棋子位置</returns> public override BlankPosition ChessMove(BlankPosition blankPosition, Direction moveDirection, int gridColumns) { if (moveDirection == Direction.Up) { if (blankPosition.Position1 == this.NextPosition) { blankPosition.Position1 = this.Position + gridColumns; } else { blankPosition.Position2 = this.Position + gridColumns; } } if (moveDirection == Direction.Down) { if (blankPosition.Position1 == this.NextPosition + gridColumns) { blankPosition.Position1 = this.Position; } else { blankPosition.Position2 = this.Position; } } if (moveDirection == Direction.Left || moveDirection == Direction.Right) { if (blankPosition.Position1 == this.NextPosition) { blankPosition.Position1 = this.Position; blankPosition.Position2 = this.Position + gridColumns; } else { blankPosition.Position1 = this.Position + gridColumns; blankPosition.Position2 = this.Position; } } return(new BlankPosition { Position1 = blankPosition.Position1, Position2 = blankPosition.Position2 }); }
public List <BlankPosition> GetSortBlankList(bool is_ai) { profiler.Enter(ProfilerFunction.GET_BLANK_LIST); List <BlankPosition> blank_list = new List <BlankPosition>(); for (int m = 0; m < COLUMN; m++) { for (int n = 0; n < ROW; n++) { if (!HasPiece(new Position(m, n))) { BlankPosition bp = new BlankPosition(); bp.score = 0; if (last_puton_black.x >= 0 && Mathf.Abs(m - last_puton_black.x) <= 1 && Mathf.Abs(n - last_puton_black.y) <= 1) { bp.score += 10; } if (last_puton_white.x >= 0 && Mathf.Abs(m - last_puton_white.x) <= 1 && Mathf.Abs(n - last_puton_white.y) <= 1) { bp.score += 10; } if (HasNeibour(new Position(m, n))) { bp.score += 1; } bp.pos = new Position(m, n); EvaBlankPosition(is_ai, bp); if (bp.score > 0) { blank_list.Add(bp); } } } } blank_list.Sort((a, b) => - a.score.CompareTo(b.score)); profiler.Leave(ProfilerFunction.GET_BLANK_LIST); return(blank_list); }
/// <summary> /// 尝试移动棋子(该方法用于自动求解) /// </summary> /// <param name="blankPosition">当前空白棋子位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <param name="callBack">设置完成后的回调</param> public virtual void TryChessMove(BlankPosition blankPosition, int gridRows, int gridColumns, SetNewPositionDelegate callBack) { if (this.CanMoveDown(blankPosition, gridRows, gridColumns)) { this.SetNewPosition(Direction.Down, gridColumns, blankPosition, callBack); } if (this.CanMoveLeft(blankPosition, gridRows, gridColumns)) { this.SetNewPosition(Direction.Left, gridColumns, blankPosition, callBack); } if (this.CanMoveRight(blankPosition, gridRows, gridColumns)) { this.SetNewPosition(Direction.Right, gridColumns, blankPosition, callBack); } if (this.CanMoveUp(blankPosition, gridRows, gridColumns)) { this.SetNewPosition(Direction.Up, gridColumns, blankPosition, callBack); } }
/// <summary> /// 移动棋子(该方法用于玩家模式)并返回新的空白棋子位置 /// </summary> /// <param name="blankPosition">当前空白棋子位置</param> /// <param name="moveDirection">当前棋子移动方向</param> /// <param name="gridColumns">网格列数</param> /// <returns>新的空白棋子位置</returns> public virtual BlankPosition ChessMove(BlankPosition blankPosition, Direction moveDirection, int gridColumns) { return(new BlankPosition { Position1 = blankPosition.Position1, Position2 = blankPosition.Position2 }); }
/// <summary> /// 尝试移动棋子(该方法用于自动求解) /// </summary> /// <param name="blankPosition">当前空白棋子位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <param name="callBack">设置完成后的回调</param> public override void TryChessMove(BlankPosition blankPosition, int gridRows, int gridColumns, SetNewPositionDelegate callBack) { BlankPosition tmpBlankPosition; //下 if (this.CanMoveDown(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Down, gridColumns, blankPosition, callBack); tmpBlankPosition = ChessMove(blankPosition, Direction.Down, gridColumns); this.Position = this.NextPosition; //下 相当于可以向下连续移动2格 if (this.CanMoveDown(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Down, gridColumns, tmpBlankPosition, callBack); return; } //左 相当于先向下,再向左移动2格 if (this.CanMoveLeft(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Left, gridColumns, tmpBlankPosition, callBack); return; } //右 相当于先向下,再向右移动2格 if (this.CanMoveRight(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Right, gridColumns, tmpBlankPosition, callBack); return; } } //左 if (this.CanMoveLeft(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Left, gridColumns, blankPosition, callBack); tmpBlankPosition = ChessMove(blankPosition, Direction.Left, gridColumns); this.Position = this.NextPosition; //上 相当于先向左,再向上移动2格 if (this.CanMoveUp(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Up, gridColumns, tmpBlankPosition, callBack); return; } //下 相当于先向左,再向下移动2格 if (this.CanMoveDown(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Down, gridColumns, tmpBlankPosition, callBack); return; } //左 相当于可以向左连续移动2格 if (this.CanMoveLeft(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Left, gridColumns, tmpBlankPosition, callBack); return; } } //右 if (this.CanMoveRight(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Right, gridColumns, blankPosition, callBack); tmpBlankPosition = ChessMove(blankPosition, Direction.Right, gridColumns); this.Position = this.NextPosition; //上 相当于先向右,再向上移动2格 if (this.CanMoveUp(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Up, gridColumns, tmpBlankPosition, callBack); return; } //下 相当于先向右,再向下移动2格 if (this.CanMoveDown(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Down, gridColumns, tmpBlankPosition, callBack); return; } //右 相当于可以向右连续移动2格 if (this.CanMoveRight(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Right, gridColumns, tmpBlankPosition, callBack); return; } } //上 if (this.CanMoveUp(blankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Up, gridColumns, blankPosition, callBack); tmpBlankPosition = ChessMove(blankPosition, Direction.Up, gridColumns); this.Position = this.NextPosition; //上 相当于可以向上连续移动2格 if (this.CanMoveUp(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Up, gridColumns, tmpBlankPosition, callBack); return; } //左 相当于先向上,再向左移动2格 if (this.CanMoveLeft(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Left, gridColumns, tmpBlankPosition, callBack); return; } //右 相当于先向上,再向右移动2格 if (this.CanMoveRight(tmpBlankPosition, gridRows, gridColumns)) { base.SetNewPosition(Direction.Right, gridColumns, tmpBlankPosition, callBack); return; } } }
/// <summary> /// 棋子是否可以向下移动一格 /// </summary> /// <param name="blankPosition">空白格子的位置</param> /// <param name="gridRows">网格行数</param> /// <param name="gridColumns">网格列数</param> /// <returns>棋子是否可以向下移动一格</returns> public virtual bool CanMoveDown(BlankPosition blankPosition, int gridRows, int gridColumns) { return(false); }