トランプをモデル化
トランプのカードをプログラムの中でデータ化するには? 何が必要? トランプ trump
- 4つのマークsuit → ♥♠♦♣
- 1~13の数字
- 1→A 11→J 12→Q 13→K 表示されている数
- ジョーカー
それぞれかぶりなく全種類13*4+1枚
#include <iostream> #include <string> #include <iomanip> using std::cout; using std::endl; using std::cin; using std::string; //トランプ trump //4つのマークsuit -> //1~13の数字 //1->A 11->J 12->Q 13->K 表示されている数 // //トランプのカード1枚当たりのデータ struct aCard { string suit[5] = { "S", "H", "D", "C", "B"}; //空白はジョーカー用 int s_num; //スペード:0 ハート:1 ダイヤ:2 クラブ:3 ジョーカー:4 int c_value;//カードの数字 1~13 + ジョーカー:0? string c_dsp_val[14] = { "B","A","2","3","4","5","6","7","8","9","10","J","Q","K" }; //2~9,A,J,Q,Kの表示用文字 + ジョーカー B }; const int num_of_cards = 53; //13 * 4 + 1 struct aCard cards[num_of_cards];//13 * 4 各スートのカード + ババ 1枚 //カード1デッキにマークと数字をセットする関数 //第1引数 struct aCard* cards : カードを表す配列のポインタ(アドレス) //第2引数 int num : カードの枚数(多分53) void initCards(struct aCard* cards, int num) { //cards[0~12] : スペードのカード->j=0 //cards[13~25] : ハートのカード->j=1 //cards[26~38] : ダイヤのカード ->j=2 //cards[39~51] : クラブのカード ->j=3 //cards[52] : ジョーカー //♠1( 0) ♠2( 1) ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K -> 0~12 j*13= 0 j*13+1= 1 //♥1(13) ♥2(14) ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥10 ♥J ♥Q ♥K ->13~25 j*13=13 j*13+1=14 //♦1(26) ♦2(27) ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K ->26~38 j*13=26 j*13+1=27 //♣1(39) ♣2(40) ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K ->39~51 j*13=39 j*13+1=40 for (int j = 0; j < 4; j++) { for (int i = 1; i <= 13; i++) { cards[j * 13 + i -1].s_num = j; cards[j * 13 + i -1].c_value = i; } } cards[num_of_cards - 1].s_num = 4; cards[num_of_cards-1].c_value = 0; } //0~53枚目まで、トランプのカードを1枚につき1行で表示する //ハートの5:♥5 スペードの12:♠Q ジョーカー : B //という表示を53枚=num_of_cards分全て行う関数 void printAllCards(struct aCard* cards, int num) { for (int i = 0; i < num_of_cards; i++) { cout << cards[i].suit[cards[i].s_num] << cards[i].c_dsp_val[cards[i].c_value] << endl; } } int main() { initCards(cards, num_of_cards); printAllCards(cards, num_of_cards); }
カードのシャッフル
ちょっと、まずいところがあったので修正を入れます。
グローバル変数と、関数の引数の名前が同じなのは混乱を招くのでよくないです。
#include <iostream> #include <string> #include <iomanip> using std::cout; using std::endl; using std::cin; using std::string; //トランプ trump //4つのマークsuit -> //1~13の数字 //1->A 11->J 12->Q 13->K 表示されている数 // //トランプのカード1枚当たりのデータ struct aCard { string suit[5] = { "S", "H", "D", "C", "B"}; //空白はジョーカー用 int s_num; //スペード:0 ハート:1 ダイヤ:2 クラブ:3 ジョーカー:4 int c_value;//カードの数字 1~13 + ジョーカー:0? string c_dsp_val[14] = { "B","A","2","3","4","5","6","7","8","9","10","J","Q","K" }; //2~9,A,J,Q,Kの表示用文字 + ジョーカー B }; const int num_of_cards = 53; //13 * 4 + 1 struct aCard cards[num_of_cards];//13 * 4 各スートのカード + ババ 1枚 //カード1デッキにマークと数字をセットする関数 //第1引数 struct aCard* _c : カードを表す配列のポインタ(アドレス) //第2引数 int num : カードの枚数(多分53) void initCards(struct aCard* _c, int num) { //cards[0~12] : スペードのカード->j=0 //cards[13~25] : ハートのカード->j=1 //cards[26~38] : ダイヤのカード ->j=2 //cards[39~51] : クラブのカード ->j=3 //cards[52] : ジョーカー //♠1( 0) ♠2( 1) ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K -> 0~12 j*13= 0 j*13+1= 1 //♥1(13) ♥2(14) ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥10 ♥J ♥Q ♥K ->13~25 j*13=13 j*13+1=14 //♦1(26) ♦2(27) ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K ->26~38 j*13=26 j*13+1=27 //♣1(39) ♣2(40) ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K ->39~51 j*13=39 j*13+1=40 for (int j = 0; j < 4; j++) { for (int i = 1; i <= 13; i++) { _c[j * 13 + i -1].s_num = j; _c[j * 13 + i -1].c_value = i; } } _c [num_of_cards - 1].s_num = 4; _c [num_of_cards-1].c_value = 0; } //0~53枚目まで、トランプのカードを1枚につき1行で表示する //ハートの5:♥5 スペードの12:♠Q ジョーカー : B //という表示を53枚=num_of_cards分全て行う関数 void printAllCards(struct aCard* _c, int num) { for (int i = 0; i < num_of_cards; i++) { cout << _c[i].suit[_c[i].s_num] << _c[i].c_dsp_val[_c[i].c_value] << " "; } cout << endl; } //シャッフル=カードをばらばらにする。 //struct aCard cards[num_of_cards]; //cards[0]~cards[52]の53枚のカードがあるとき //これをシャッフルする方法を考えてください void shuffleCards(struct aCard* _c, int num) { //randを使ってどうにかしてカードをシャッフルする for (auto i = 0; i < 100; i++) { int s1 = rand() % 53; int s2 = rand() % 53; //struct aCard tmp; //tmp = _c[s1]; //_c[s2] = _c[s1]; //_c[s1] = tmp; std::swap(_c[s1], _c[s2]); } } int main() { srand((unsigned int)time(nullptr));//乱数の初期化 initCards(cards, num_of_cards); //カードを初期化 printAllCards(cards, num_of_cards);//カードを表示 shuffleCards(cards, num_of_cards);//カードをシャッフル printAllCards(cards, num_of_cards);//カードを表示 }