【问题标题】:c# 1D array collision detectionc# 一维数组碰撞检测
【发布时间】:2012-11-02 02:28:28
【问题描述】:

我在大学的任务是使用 c# 创建一个吟游诗人游戏。

有 2-4 名玩家,每个玩家轮流掷骰子。目标是到达网格上的最后一个方格。

与这个问题唯一相关的规则是,同一方格中不能有超过一个玩家。

例如

两名球员都从位置 0 开始。

玩家 (A) 在方格 1 掷出 1 = 玩家(A)。 玩家 (B) 掷出 1 = 玩家(B) “跳过”玩家 (A) 并降落在第 2 格。

我省略了掷骰子方法,据我所知,主要的方法与问题无关。

private static void PlayerTurn(int playerNo)
        {
        playerPositions[playerNo] = playerPositions[playerNo] + RollDice();  
        // The selected player rolls the dice and moves x amount of squares 
        //(dependant on dice roll value)
        }

这就是移动每个玩家的方法。

我正在努力的是以下方法。

static bool RocketInSquare(int squareNo)
       {
        //TODO: write a method that checks through the 
        //rocket positions and returns true if there is a rocket in the given square
       }

该方法需要检查数组中的冲突。因此,如果玩家 (A) 在第一轮投出 1,而玩家 (B) 在第一轮投出 1,我需要让玩家 (B) '跳跃式' 玩家 (A) 进入方格 2。

如果有帮助的话,目前游戏只是在控制台中运行。很抱歉这个问题的格式,以前从未在这里问过。

非常感谢

【问题讨论】:

    标签: c# arrays collision


    【解决方案1】:

    你只需要检查两个玩家是否共享相同的位置,如果是这种情况,“活跃玩家”可以再移动一个

    if(playerpositions[otherPlayer] == playerpositions[currentPlayer])
        playerpositions[currentPlayer]++;
    

    因此,如果您需要为此创建一个函数,那就是:

    static bool RocketInSquare(int squareNo)
    {
        return playerpositions[0] == squareNo ||
               playerpositions[1] == squareNo ||
               playerpositions[2] == squareNo ||
               playerpositions[3] == squareNo;
    }
    

    然后

    int dice = RollDice();
    
    if(RocketInSquare(playerPositions[playerNo] + dice))
    {
        playerPositions[playerNo] += dice +1;
    }
    else
    {
        playerPositions[playerNo] += dice;
    }
    

    【讨论】:

    • 好的,所以我有 int currentplayer = playerNo 我需要检查最多 3 个其他玩家,我需要为每个玩家创建一个单独的变量吗?为回复干杯
    • 好的,这太棒了。为你的帮助伙伴干杯
    • 留意 RocketInSquare 函数,我在 Playernumbers 中有一个错字。不客气。
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多