Last active
June 26, 2018 09:12
-
-
Save todorok1/f0530eaa7cd575614e03624a16f758f3 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
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