請教一下檔案連結 |
答題得分者是:dllee
|
rr7788
一般會員 發表:2 回覆:6 積分:1 註冊:2008-05-13 發送簡訊給我 |
這是參考『精彩C++Builder』一書所打出來的程式
內容是3*3的智慧盤 我也把4*4,5*5的改好了, 但是不知道怎麼從3*3的程式連到4*4的程式... 也就是說要怎樣才能讓"Button1連到4*4", "Button2連到5*5" 3*3->4*4,5*5 4*4->3*3,5*5 5*5->3*3,4*4 並讓3*3完成後在"ShowMessage"按下OK後執行4*4的程式 4*4完成後到5*5 3*3開啟4*4時3*3要關閉或隱藏 因為我是直接從原本的3*3改成4*4,只有把所有的9改成16把8改成15,改陣列,加圖片.....我也只會這樣.... 以下是3*3的程式碼 [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- #include #include #include <math.h><br />TImage *piclist[9]; TImage *img[9] ; TImage *cmd[2]; TLabel *feedback; int poslist[9][2]; int nolist[9]; int game_status,p_time,free_pos; //建立移動陣列資料 int table[9][9] = { {0, 1, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 1, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0, 1, 0} }; void change_position(); void random_list(int *,int); bool check_finished(); void __fastcall TForm1::FormActivate(TObject *Sender) { char *picname[]={"b01.jpg", "b02.jpg", "b03.jpg", "b04.jpg", "b05.jpg", "b06.jpg", "b07.jpg", "b08.jpg", "b09.jpg"}; String str1,str2; int i; str1="圖片\\"; // 建立圖形元件與保存圖形的原始位置 for(i=8;i>=0;i--){ img[i]=new TImage(this); img[i]->Parent=this; img[i]->Visible=true; img[i]->AutoSize=true; img[i]->Tag=i; img[i]->Left=108 (i%3)*103; img[i]->Top=120 (int)(i/3)*84; //設定事件程式 img[i]->imgClick; poslist[i][0] = img[i]->Left; //保存水平座標 poslist[i][1] = img[i]->Top; //保存垂直座標 str2=str1; str2=str2 picname[i]; img[i]->Picture->LoadFromFile(str2); } //建立按鈕圖形資料 cmd[0]=new TImage(this); cmd[0]->Picture->LoadFromFile("圖片\\start.jpg"); cmd[1]=new TImage(this); cmd[1]->Picture->LoadFromFile("圖片\\stop.jpg"); feedback=Label3; feedback->BringToFront(); feedback->Visible=false; randomize(); game_status=0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Image2Click(TObject *Sender) { int i; switch(game_status){ case 0: //按下start 鈕 feedback->Visible=false; p_time = 0 ; Label2->Caption=p_time; for(i=0;i<8;i ) nolist[i]=i; random_list(nolist,8); free_pos = 8; nolist[8] = free_pos; change_position(); game_status=1; Image2->Picture=cmd[1]->Picture; break; case 1: //按下stop 鈕 Image2->Picture=cmd[0]->Picture; feedback->Caption="Stop!"; feedback->Visible=true; game_status=0; break; }; } //--------------------------------------------------------------------------- //計時程式 void __fastcall TForm1::Timer1Timer(TObject *Sender) { if(game_status==1){ p_time ; Label2->Caption=p_time; } } //--------------------------------------------------------------------------- void __fastcall TForm1::imgClick(TObject *Sender){ TImage *obj=(TImage *)Sender; int x, dno,no,i; dno=obj->Tag; //取得所按圖片的標籤編號 if(game_status==1){ no = nolist[dno] ; i=dno; if (table[free_pos][no] == 1) { //判斷是否為可移動的圖片 //將圖片移到空白位置 img[i]->Left=poslist[free_pos][0]; img[i]->Top=poslist[free_pos][1]; nolist[dno] = nolist[8]; nolist[8] = no; free_pos = no; if(check_finished()==true){ feedback->Caption="Successful!"; ShowMessage("完成"); feedback->Visible=true; game_status=0; } } } } // 隨機排列串列資料 void random_list(int *xlist,int size) { int i,j,x; for(i=0; i x = xlist[i]; xlist[i] = xlist[j]; xlist[j] = x; } } // 更換圖形位置的副程式 void change_position(){ int i, j; for(i=0; i<8; i ) { j=nolist[i]; img[i]->Left=poslist[j][0]; img[i]->Top=poslist[j][1]; } } // 檢查是否已經完成的副程式 bool check_finished() { bool flag = true; for (int i=0;i<8;i ) { if (nolist[i]!=i){ flag=false; break; } } return flag; } [/code] 以下是4*4的程式碼 [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- #include #include #include <math.h><br />void loadpictures(int); TImage *piclist[16]; TImage *img[16] ; TImage *cmd[2]; TLabel *feedback; int poslist[16][2]; int nolist[16]; int game_status,p_time,free_pos; //建立移動陣列資料 int table[16][16] = { {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0}, }; void change_position(); void random_list(int *,int); bool check_finished(); void __fastcall TForm1::FormActivate(TObject *Sender) { char *picname[]={"b01.jpg", "b02.jpg", "b03.jpg", "b04.jpg", "b05.jpg", "b06.jpg", "b07.jpg", "b08.jpg", "b09.jpg", "b10.jpg", "b11.jpg", "b12.jpg", "b13.jpg" , "b14.jpg", "b15.jpg", "b16.jpg"}; String str1,str2; int i; str1="圖片\\"; // 建立圖形元件與保存圖形的原始位置 for(i=15;i>=0;i--){ img[i]=new TImage(this); img[i]->Parent=this; img[i]->Visible=true; img[i]->AutoSize=true; img[i]->Tag=i; img[i]->Left=144 (i%4)*103; img[i]->Top=160 (int)(i/4)*84; //設定事件程式 img[i]->imgClick; poslist[i][0] = img[i]->Left; //保存水平座標 poslist[i][1] = img[i]->Top; //保存垂直座標 str2=str1; str2=str2 picname[i]; img[i]->Picture->LoadFromFile(str2); } //建立按鈕圖形資料 cmd[0]=new TImage(this); cmd[0]->Picture->LoadFromFile("圖片\\start.jpg"); cmd[1]=new TImage(this); cmd[1]->Picture->LoadFromFile("圖片\\stop.jpg"); feedback=Label3; feedback->BringToFront(); feedback->Visible=false; randomize(); game_status=0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Image2Click(TObject *Sender) { int i; switch(game_status){ case 0: //按下start 鈕 feedback->Visible=false; p_time = 0 ; Label2->Caption=p_time; for(i=0;i<15;i ) nolist[i]=i; random_list(nolist,15); free_pos = 15; nolist[15] = free_pos; change_position(); game_status=1; Image2->Picture=cmd[1]->Picture; break; case 1: //按下stop 鈕 Image2->Picture=cmd[0]->Picture; feedback->Caption="Stop!"; feedback->Visible=true; game_status=0; break; }; } //--------------------------------------------------------------------------- //計時程式 void __fastcall TForm1::Timer1Timer(TObject *Sender) { if(game_status==1){ p_time ; Label2->Caption=p_time; } } //--------------------------------------------------------------------------- void __fastcall TForm1::imgClick(TObject *Sender){ TImage *obj=(TImage *)Sender; int x, dno,no,i; dno=obj->Tag; //取得所按圖片的標籤編號 if(game_status==1){ no = nolist[dno] ; i=dno; if (table[free_pos][no] == 1) { //判斷是否為可移動的圖片 //將圖片移到空白位置 img[i]->Left=poslist[free_pos][0]; img[i]->Top=poslist[free_pos][1]; nolist[dno] = nolist[15]; nolist[15] = no; free_pos = no; if(check_finished()==true){ feedback->Caption="Successful!"; feedback->Visible=true; game_status=0; } } } } // 隨機排列串列資料 void random_list(int *xlist,int size) { int i,j,x; for(i=0; i x = xlist[i]; xlist[i] = xlist[j]; xlist[j] = x; } } // 更換圖形位置的副程式 void change_position(){ int i, j; for(i=0; i<15; i ) { j=nolist[i]; img[i]->Left=poslist[j][0]; img[i]->Top=poslist[j][1]; } } // 檢查是否已經完成的副程式 bool check_finished() { bool flag = true; for (int i=0;i<15;i ) { if (nolist[i]!=i){ flag=false; break; } } return flag; } [/code] 至於5*5的我想就不用貼了吧~! |
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
如果您已完成 3x3, 4x4, 5x5
請分別把表單名稱作修改,由目前都是 Form1 改名為 Form3x3, Form4x4, Form5x5 並把目前可能都是放在 Unit1.cpp/Unit1.h SaveAs 為 Unit3x3, Unit4x4, Unit5x5 把它們都放在同一個 Project 內。 將 Project Options 內設定所有表單都 Auto Create. 將 Form3x3 設為 MainForm,這樣直接 rebuild 後,應該會與只有 3x3 時是一樣的。 在 Unit3x3.cpp 內 include Unit4x4.h 在 Form3x3 秀出完成訊息時,接著 [code cpp] Form3x3->Hide(); // 隱藏 3x3 表單 Form4x4->Show(); // 叫出 4x4 表單 [/code] 同理,在 Unit4x4.cpp 內 include Unit5x5.h 在 Form4x4 秀出完成訊息時,接著 [code cpp] Form4x4->Hide(); // 隱藏 4x4 表單 Form5x5->Show(); // 叫出 5x5 表單 [/code] 您可以試試看,如果不行,請找找多表單的範例,再參考如何作出多表單的程式, 這樣,您就可以很容易把多個表單串在一起了。 ■ VMASK - ViewMove Automation Software Kernel ■ VMIO-Server/SECS/GEM ■ dllee's blog ■ dllee's StatPlus ■
------
http://www.ViewMove.com |
rr7788
一般會員 發表:2 回覆:6 積分:1 註冊:2008-05-13 發送簡訊給我 |
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
imgClick 是指定 Image Click 的事件,新的表單重新命名,
在這個部分因為原本就是手動建立的事件,也因此需要手動命名。 原本的: void __fastcall TForm1::imgClick(TObject *Sender) 在表單重新命名後,要手動命名: void __fastcall TForm3x3::imgClick(TObject *Sender) void __fastcall TForm4x4::imgClick(TObject *Sender) void __fastcall TForm5x5::imgClick(TObject *Sender) 您在作重新命名及整合時,應該會有錯誤訊息吧... 要學習如何看錯誤訊息,以錯誤訊息去找資料或試著修正它, 不然,從書本、網路來的程式,很少不用修就可以直接套用到您的程式中的。 ■ VMASK - ViewMove Automation Software Kernel ■ VMIO-Server/SECS/GEM ■ dllee's blog ■ dllee's StatPlus ■
------
http://www.ViewMove.com |
rr7788
一般會員 發表:2 回覆:6 積分:1 註冊:2008-05-13 發送簡訊給我 |
那部份似乎是自動的我有重試了一次..還是不行
總覺得有地方相衝到 因為Remove From到只剩4x4就可以正常執行~! 開啟4x4 按下START後錯誤顯示 Project CH14_001.exe raised exception class EAccessViolation with message 'Access violation at address0042B764 in module'CH14_001.exe 'Read of address00000048' Process stoopped. Use Step or Run to continue. 檔案: http://delphi.ktop.com.tw/board.php?cid=31&fid=97&tid=94090
編輯記錄
rr7788 重新編輯於 2008-06-15 22:41:28, 註解 無‧
|
rr7788
一般會員 發表:2 回覆:6 積分:1 註冊:2008-05-13 發送簡訊給我 |
我終於了解了...
因為全域變數的關西吧 [code cpp] void change_position(); void random_list(int *,int); bool check_finished(); [/code] 在4X4裡改成 [code cpp] void change_position4x4(); void random_list4x4(int *,int); bool check_finished4x4(); [/code] 就可以了 雖然對全域變數還不是很了解... 但好像是在void __fastcall TForm4x4::FormActivate(TObject *Sender)(開頭) 之上變的數是全域變數的樣子 |
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
我沒留意,因為我通常不使用 Global Variables,除非只是寫來玩玩。
比較好的方式,把所有的 Global Var 都移到表單 .h 內的 private 或 protected 或 publish 內, 並將一般函式 void change_position(); void random_list(int *,int); bool check_finished(); 也都放在 private 或 protected 或 publish 內,在 .cpp 把它們改成 void TForm4x4::change_position() void TForm4x4::random_list(int *,int) bool TForm4x4::check_finished() 這樣就不會因為 Global Var 而相衝了。 在您的例子中,只有 table 是可以讓多個表單共用的 Global Variable. ■ VMASK - ViewMove Automation Software Kernel ■ VMIO-Server/SECS/GEM ■ dllee's blog ■ dllee's StatPlus ■
------
http://www.ViewMove.com |
rr7788
一般會員 發表:2 回覆:6 積分:1 註冊:2008-05-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |