[英]c# 1D array collision detection
我的任務是在大學使用c#創建一個Bard游戲。
會有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輪。
如果有幫助,此刻游戲僅在控制台中運行。 對不起,這個問題的格式,以前從未在這里提出過。
非常感謝
那么,您只需要簡單地檢查兩個玩家是否共享同一位置,如果是這種情況,則允許“活動玩家”再移動一個
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.