Visual Studio 2008 Express Edition でトランプゲームを作ろう
最終確認
Form1の内容
| #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; |
カードの配列 1-52を入れ その後1-13、101-113、201-213、301-313にする 最初の5枚と後からの5枚のカードデータを収納 上記を下1ケタにする 313なら13とする ペアの解析に使用する ルーチンの通過を制御する チェックに使用する ? 点数に使用する |
| void DispHai( int nohai,int xx){ Graphics^ gr=this->CreateGraphi cs(); int X0=10,Y0=10; String^ filename; filename="c:\\card\\1_sp.gif"; if(nohai==0 ) filename="c:\\card\\0_r1.gif" ; //Pen^ pen1=gcnew Pen(col); if(nohai>0 && nohai<14) filename="c:\\card\\"+nohai+"_ sp.gif" ; if(nohai>100 && nohai<114 ){nohai=nohai-100; filename="c:\\card\\"+nohai+"_ cl.gif" ;} if(nohai>200 && nohai<214 ){nohai=nohai-200; filename="c:\\card\\"+nohai+"_ di.gif" ;} if(nohai>300 && nohai<314){nohai=nohai-300; filename="c:\\card\\"+nohai+"_ he.gif" ;} //原画像を左上に描画する if(xx==1){ Bitmap^ bmap1; //bmapの確保 bmap1=gcnew Bitmap(filename ); gr->DrawImage(bmap1,X0,Y0); } if(xx==2){ Bitmap^ bmap2; //bmapの確保 bmap2=gcnew Bitmap(filename ); gr->DrawImage(bmap2,X0+100 ,Y0); } if(xx==3){ Bitmap^ bmap3; //bmapの確保 bmap3=gcnew Bitmap(filenam e); gr->DrawImage(bmap3,X0+20 0,Y0); } if(xx==4){ Bitmap^ bmap4; //bmapの確保 bmap4=gcnew Bitmap(filenam e); gr->DrawImage(bmap4,X0+30 0,Y0); } if(xx==5){ Bitmap^ bmap5; //bmapの確保 bmap5=gcnew Bitmap(filename ); gr->DrawImage(bmap5,X0+40 0,Y0); } } |
トランプの図柄をファイルから読み込む Gifを読み込むためにモードをセット カードのファイル名の用意 カードの収納位置 カードのファイル名のセット とりあえず、スペードの1にする 0の時、カードの裏面のファイルをセット 1-13の時スペードをセット 101から113の時クローバをセット ? 201-213のときダイヤをセット ?301-313の時、ハートをセット ?? xx=1のとき1枚目(左)を表示する ?? xx=2のとき2枚目(左から2枚目)を表示する ? xx=3のとき3枚目(左から3枚目)を表示する ? xx=4のとき4枚目(左から4枚目)を表示する ? xx=5のとき5枚目(いちばん右)を表示する ? ? |
| #pragma endregion private: System::Void Form1_Paint(System::Object^ sender, System::Windows::For ms::Pai ntEventArgs^ e) { //int X0=10,Y0=10; Graphics^ gr=e->Graphics; //Pen^ pen; //Color color_src,color_dest; } |
図を描く用意をする 必要なし 必要なし 必要なし |
|
| 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->CreateGrap hics(); //文字の用意 String^ string1; String^ string2; Brush^ brush=gcnew SolidBrush (Color::Black); System::Drawing::Font^ font=g cnew System::Drawing::Font("M S ゴシック",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,b rush,(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を押した場合のプログラム【ゲーム開始】 一度通過したら2度目を禁止する 文字列の用意 グラフックの用意 文字列の用意 文字列の用意 省略可能 省略可能 省略可能 文字の用意終了 乱数の初期化 ? 52枚をだぶらないように配布 初期化0を入れる y < 20に成るまでLOOPの間を繰り返す 乱数1から52 ダブりの確認値0 同じ数字が出たらダブり確認値1にする ダブらない時のみ乱数を代入する 配列card(z)に乱数を保存する 文字で確認 乱数値をbufに書き込む ストリングに移す 毎回出てきた乱数を加算 loop数yを1増加させる While y < 53の終了 テキストボックス3にこの結果を表示 1から52のカードをわかりやすく 14から26は101−113変換 27から39は201−213変換 40から52は 301−313に変更 13以下はクラブ card(y) > 13の場合card(y)に100を足し13を引く たとえば 14の場合101となる。ダイヤとする card(y) > 26の場合card(y)に200を足し26を引く たとえば 27の場合202となる。ハートとする card(y) > 39の場合card(y)に300を足 発生された乱数値を繋げていく テキストボックス4にこの結果を表 この数値をトランプとして表示する 表示ルーチンに飛ぶ チックボックス1値を0に初期化 チックボックス2値を0に初期化 チックボックス3値を0に初期化 チックボックス4値を0に初期化 チックボックス5値を0に初期化 ? ? ? |
|
private: System::Void checkBox1_CheckedChanged(Sy tem::Object^ sender, System::E |
チックボックス1にレ点がついたら値を1にする ? チックボックス2にレ点がついたら値を1にする ? チックボックス3にレ点がついたら値を1にする ? チックボックス4にレ点がついたら値を1にする ? チックボックス5にレ点がついたら値を1にする |
|
|

