gpt4 book ai didi

c# - 一维阵列碰撞

转载 作者:行者123 更新时间:2023-11-30 15:36:57 26 4
gpt4 key购买 nike

我正在努力研究一维阵列的碰撞检测类型方法。

我有一个最多有 4 个玩家的主机游戏,每个玩家轮流掷骰子并在棋盘上走一步。

规则是同一时间棋盘上只能有一名玩家。

因此,如果玩家一掷出 1,他就在第一格。如果玩家 2 在轮到他时掷出 1,他就在二号方格上。如果玩家 3 在轮到他时掷出 1,那么他在第三格。等等……

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;
}

关于c# - 一维阵列碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13200025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com