Created
June 26, 2018 10:05
-
-
Save todorok1/3a70a4a79a22e536a57db04e916b9b19 to your computer and use it in GitHub Desktop.
山札から何枚かのカードを手札に加えるサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// プレイヤーのリスト | |
List<Card> playerCardList; | |
void DistributeCard(int numRequired){ | |
// 指定された枚数の手札を山札から配布する | |
playerCardList = new List<Card>(); | |
// 残ったカードを格納するリスト | |
List<Card> leftCardList = new List<Card>(); | |
int numToChoose = numRequired; | |
for (int i = 0; i < cardList.Count; i++){ | |
if (numToChoose <= 0){ | |
// 残ったカードをそのまま残りリストに追加する | |
leftCardList.Add(cardList[i]); | |
} else { | |
int numLeft = cardList.Count - i; | |
float prob = (float)numToChoose / (float)numLeft; | |
if (Random.value <= prob){ | |
numToChoose--; | |
playerCardList.Add(cardList[i]); | |
} else { | |
leftCardList.Add(cardList[i]); | |
} | |
} | |
} | |
// 残ったカードを山札とする | |
cardList = leftCardList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment