编程语言
首页 > 编程语言> > C#-一维数组冲突

C#-一维数组冲突

作者:互联网

我正在努力解决一维数组的碰撞检测类型方法.

我有一个主机游戏,最多可容纳4个玩家,每个玩家依次掷骰子并在棋盘上移动.

规则是,在同一时间板上只能有一个玩家.

因此,如果一名玩家掷出1,则他位于方格1.
如果玩家2在回合中掷出1,则他将处于第二方格.
如果玩家3在回合上掷出1,则他在第3格中.
等等…

private static void PlayerMove(int playerNo)
{
    // TODO: Makes a move for the given player

        for (int i = 0; i < NumberOfPlayers; i++)
        {
            NextMove = playerPositions[i] + playerPositions[i] + DiceThrow();
            playerPositions[i] = NextMove;
        }
}

这是我当前用于移动玩家的方法,这是一分钟的测试方法,表明每个玩家都可以移动.结果是每个玩家都落在1号正方形上.

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

    if (This conditional is what has me confused)
    {
        return true;
    }

}

这是让我头疼的方法.我一直在试验该条件,并使其工作了一半,但似乎无法正确解决.

提前谢谢了.

解决方法:

假设playerPositions []是一个整数数组,其中包含玩家所在的正方形的数字,则可以尝试:

static bool PlayerInSquare(int squareNo)
{
    return playerPositions.Any(pos => pos == squareNo);
}

较少的Linq-y解决方案(等同于同一件事)将是:

static bool PlayerInSquare(int squareNo)
{
    for (int i = 0; i < NumberOfPlayers; i++)
        if (playerPositions[i] == squareNo)
            return true;

    return false;
}

标签:collision-detection,arrays,c
来源: https://codeday.me/bug/20191031/1977701.html