Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
カードをランダムにシャッフルするサンプル
void ShowCardsName(List<Card> showCardList){
// コンソールに出力する文字列を生成
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string separator = ", ";
foreach(Card card in showCardList){
if (card.isJoker){
sb.Append("JOKER").Append(separator);
} else {
sb.Append(GetSuitMark(card.suit)).Append(GetSpecialNumber(card.number)).Append(separator);
}
}
string cardText = sb.ToString();
// 1行あたり15枚のカードを表示する
int cardInLine = 15;
int cardCount = 0;
// 区切り文字を使って、カードを1枚ごとの配列に格納する
string[] separatorChar = {separator};
string[] cards = cardText.Split(separatorChar, System.StringSplitOptions.RemoveEmptyEntries);
sb = new System.Text.StringBuilder();
for (int i = 0; i < cards.Length; i++){
sb.Append(cards[i]).Append(separator);
cardCount++;
// 表示枚数に達したらコンソールに表示
if (cardCount < cardInLine){
continue;
}
string line = sb.ToString();
if (i == cards.Length - 1){
line = RemoveLastSeparator(line, separator);
}
Debug.Log(line);
sb = new System.Text.StringBuilder();
cardCount = 0;
}
// 最後のカンマだけ削除する
string lastLine = sb.ToString();
if (lastLine.Length > 0){
lastLine = RemoveLastSeparator(lastLine, separator);
Debug.Log(lastLine);
}
}
string RemoveLastSeparator(string line, string separator){
int index = line.LastIndexOf(separator);
if (index != -1){
line = line.Remove(index);
}
return line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.