Visual Studio 2008 Express Edition でトランプゲームを作ろう
3日目の学習
トランプの表示が完成したので
��乱数を発生し トランプをシャッフルします。
#pragma once
の直下に定義します
次の配列を作ります。ここではこのゲームに必要なすべての配列を
パブリックで作ります(すべてのサブルーチンから参照可能)
赤い部分を追加
#pragma once
static int card[55];
static int hai[12];
static int keta[12];
static int yaku[12];
static int start_flag=0;
static int hoji1=0;
static int hoji2=0;
static int hoji3=0;
static int hoji4=0;
static int hoji5=0;
static int tokuten=0;
namespace toranpu {
��stdaf.hをクリックして
#include <stdio.h>
を追加する 昔のCと互換性を持たせる

Form1にテキストボックス1−4を貼り付ける
��Form1デザイン→ button1をダブルクリックして訂正
青い部分は削除
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
DispHai( 0,1);
DispHai( 1,2);
DispHai( 111,3);
DispHai( 212,4);
DispHai( 313,5);
赤の部分を追加
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if(start_flag ==0){ //一度通過したら2度目を禁止する
int amax,xrand,nohai;
int xx,yy,y;
char buf[80];
Graphics^ gr=this->CreateGraphics();
//文字の用意
String^ string1;
String^ string2;
Brush^ brush=gcnew SolidBrush(Color::Black);
//System::Drawing::Font^ font=gcnew System::Drawing::Font("MS ゴシック",10);
//文字の用意終了
Random^ rand=gcnew Random(); //で乱数randを生成しておき、
//2) 0以上int amax未満の乱数を発生したい場合には、
string2="乱数値";
amax=52;
for(xx=1;xx<54;xx++) card[xx]=0;
yy=1;
while(yy<20){
xrand=rand->Next(amax)+1;
int flag=0;
for(xx=1;xx<53;xx++) {if(card[xx]==xrand) flag=1;}
if(flag==0){ card[yy]=xrand;
//文字で乱数を確認
sprintf_s(buf,80," %3d ",xrand);
string1=gcnew String(buf);
//gr->DrawString(string1,font,brush,(float)16*yy,(float)450);
string2=string2+string1;
//文字で確認終了
yy++;}
}
//文字で表示
//MessageBox::Show(string2);
textBox3->Text=string2;
string2="変換値";
for(y=1;y<53;y++){
if( card[y] > 13 && card[y] <= 26) card[y] = card[y] + 100 - 13;
if( card[y] > 26 && card[y] <= 39) card[y] = card[y] + 200 - 26;
if( card[y] > 39 && card[y] <= 52) card[y] = card[y] + 300 - 39;
} //end for y
for(xx=1;xx<6;xx++){
hai[xx]=card[xx];
//文字で乱数を確認
sprintf_s(buf,80," %3d ",hai[xx]);
string1=gcnew String(buf);
string2=string2+string1;
//文字で確認終了
}
//文字で表示
textBox4->Text=string2;
for(xx=1;xx<6;xx++){
nohai=hai[xx];
DispHai(nohai,xx);
}
hoji1=0;
hoji2=0;
hoji3=0;
hoji4=0;
hoji5=0;
start_flag=1; //一度通過したら2度目を禁止するフラッグ
}//end if start_flag=1;一度通過したら2度目を禁止する
}

を押して実行してみる

結果
これが表示されれば成功です
説明
乱数 1から52を発生させ 1から52がダブらないようにして card[xx]に代入する
1から13はそのまま スペードとする
13から26は101から113 ダイヤとする
27から39は201から213 ハートとする
40から52は301か313 クローバとする
そのうち1から52の10枚の数値を表示する
card[1から5]のカードの図を表示する。



