我正在努力研究一维数组的碰撞检测类型方法。
我有一个最多 4 名玩家的主机游戏,每个玩家轮流掷骰子并在棋盘上移动。
规则是棋盘上同时只能有一名玩家。
因此,如果玩家 1 掷出 1,他就位于第 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/