Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
カードをランダムにシャッフルするサンプル
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomShuffleTest : MonoBehaviour {
// カードを保持するリスト
List<Card> cardList;
// 各マーク(スート)での枚数を設定
const int numbersInSuit = 13;
// 各マーク(スート)のIDを決める
const int Spade = 0;
const int Club = 1;
const int Heart = 2;
const int Diamond = 3;
List<int> suits = new List<int>(){Spade, Club, Heart, Diamond};
void Start(){
InitializeCardList();
ShuffleCards();
ShowCardsName(cardList);
}
void InitializeCardList(){
// 山札を初期化する
cardList = new List<Card>();
// 各マーク(スート)ごとにカードを生成
foreach (int suit in suits){
for (int i = 1; i <= numbersInSuit; i++){
Card card = new Card(suit, i, false);
cardList.Add(card);
}
}
// ジョーカーを追加
Card joker = new Card(0, 0, true);
cardList.Add(joker);
}
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;
}
string GetSuitMark(int suit){
// マーク(スート)のIDに応じた文字を返す
string suitMark;
switch (suit){
case Spade:
suitMark = "♠︎";
break;
case Club:
suitMark = "♣︎";
break;
case Heart:
suitMark = "❤︎";
break;
case Diamond:
suitMark = "♦︎";
break;
default:
suitMark = "JOKER";
break;
}
return suitMark;
}
string GetSpecialNumber(int number){
if (number < 11){
return number.ToString();
}
string special = "None";
switch (number){
case 11:
special = "J";
break;
case 12:
special = "Q";
break;
case 13:
special = "K";
break;
}
return special;
}
void ShuffleCards(){
// カードをシャッフルする
for (int i = 0; i < cardList.Count; i++){
Card temp = cardList[i];
int randomIndex = Random.Range(0, cardList.Count);
cardList[i] = cardList[randomIndex];
cardList[randomIndex] = temp;
}
}
}
public struct Card{
// カードのマーク(スート)を保持
public int suit;
// カードの数字
public int number;
// ジョーカーかどうか
public bool isJoker;
public Card(int suit, int number, bool isJoker){
this.suit = suit;
this.number = number;
this.isJoker = isJoker;
}
}
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.