You need to enable JavaScript to run this app. C#蛇梯棋开发:爬梯方向修正与胜利判定功能实现
优惠活动
大模型
产品
解决方案
定价
更多

C#蛇梯棋开发:爬梯方向修正与胜利判定功能实现

蛇梯棋代码问题定位与修复方案

核心Bug识别

  • 梯子移动方向异常根因
    1. 梯子垂直跨度计算笔误:现有CheckForLadders中的deltaRows = _Ladders[iCount][indexRBottom] + _Ladders[iCount][indexRBottom]为无效计算,未按照“梯子底部高行号到顶部低行号”的实际垂直距离取值,导致奇偶行判断完全失准。
    2. 状态更新索引越界:ChangeUp函数中写入玩家状态时使用_Players[_currentPlayer + 1][0]的索引,和玩家数组0索引的结构不匹配,会直接超出数组长度边界,导致状态写入错误或程序崩溃。
    3. 方向切换逻辑失效:ChangeUp函数内写为_IsMovingLR *= +1,乘1的操作完全无法实现“跨奇数行翻转移动方向”的设计目的,和注释描述的功能脱节。
    4. 初始值设置错误:CheckForLadders中deltaRows初始值设为1,即使玩家没有踩中任何梯子,也会因1为奇数触发不必要的方向切换。
  • 胜利判定逻辑缺失
    现有代码仅预留了CheckForWin空方法,未实现“恰好到达100格获胜、超格退回原位”的规则。
  • 冗余调用干扰状态
    MoveHorizontal每次水平移动完成后都调用ChangeUp,但水平移动本身不需要更改左右移动方向,额外调用会触发无意义的状态修改。

修复代码片段

修正梯子检测逻辑

private void CheckForLadders(BoxView player)
{
    int iCount = 0;
    int indexRBottom = 0, indexCBottom = 1; // 梯子底部(低位)坐标
    int indexRTop = 2, indexCTop = 3;       // 梯子顶部(高位)坐标
    int playerRow, playerCol;
    int deltaRows = 0; // 修正初始值,避免未踩梯时误触发方向切换

    playerRow = (int)player.GetValue(Grid.RowProperty);
    playerCol = (int)player.GetValue(Grid.ColumnProperty);

    while (iCount < NUMBER_OF_LADDERS)
    {
        if((playerRow == _Ladders[iCount][indexRBottom]) &&
           (playerCol == _Ladders[iCount][indexCBottom]))
        {
            lblUpdates.Text = "You landed on a ladder";
            // 移动到梯子顶部位置
            player.SetValue(Grid.RowProperty, _Ladders[iCount][indexRTop]);
            player.SetValue(Grid.ColumnProperty, _Ladders[iCount][indexCTop]);
            // 修正垂直跨度计算:底部行号 - 顶部行号,得到向上移动的总行数
            deltaRows = _Ladders[iCount][indexRBottom] - _Ladders[iCount][indexRTop];
        }
        iCount++;
    }
    // 跨奇数行时,和蛇的逻辑保持一致翻转移动方向
    if (deltaRows % 2 == 1)
    {
        ChangeDirection();
    }
}

修正方向状态更新逻辑

private void UpdatePlayerMovementState()
{
    // 跨奇数行时翻转移动方向,替换原无效的*1操作
    _IsMovingLR *= -1;
    _currentEdgeCol = _IsMovingLR == 1 ? EDGE_RIGHT : EDGE_LEFT;
    // 修正越界索引,匹配0索引的玩家数组结构
    _Players[_currentPlayer - 1][0] = _IsMovingLR;
    _Players[_currentPlayer - 1][1] = _currentEdgeCol;
}

注意:删除原MoveHorizontal中多余的ChangeUp()调用,水平移动不会触发方向变更。

实现胜利判定逻辑

// 在MovePiece方法开头,记录玩家本次移动前的坐标
private async Task MovePiece()
{
    string currPlayer = "bvP" + _currentPlayer.ToString();
    BoxView bvPlayer = (BoxView)GrdBoard.FindByName(currPlayer);
    // 记录移动前的位置,用于超格退回
    int preMoveRow = (int)bvPlayer.GetValue(Grid.RowProperty);
    int preMoveCol = (int)bvPlayer.GetValue(Grid.ColumnProperty);

    // 保留原有移动流程逻辑不变

    // 移动完成后校验胜利条件
    CheckForWin(bvPlayer, preMoveRow, preMoveCol);
}

private void CheckForWin(BoxView player, int preMoveRow, int preMoveCol)
{
    int playerRow = (int)player.GetValue(Grid.RowProperty);
    int playerCol = (int)player.GetValue(Grid.ColumnProperty);

    // 恰好落在胜利格时判定获胜
    if (playerRow == WIN_ROW && playerCol == WIN_COL)
    {
        lblUpdates.Text = $"玩家{_currentPlayer} 获胜!";
    }
    // 移动后超出胜利行范围时,退回本次移动前的位置
    else if (playerRow < WIN_ROW)
    {
        player.SetValue(Grid.RowProperty, preMoveRow);
        player.SetValue(Grid.ColumnProperty, preMoveCol);
        lblUpdates.Text = "掷骰点数超出棋盘范围,退回原位置";
    }
}

内容的提问来源于stack exchange,提问作者Akeem Jokosenumi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.16 17:54:03