執行的先後順序 |
尚未結案
|
north
一般會員 ![]() ![]() 發表:10 回覆:17 積分:5 註冊:2004-08-06 發送簡訊給我 |
請問如何去定義一段程式執行的先後順序呢?
例如:O→X→O→X→O→X→...
我目前的程式如下
procedure TForm1.Game(Sender: TObject); var p,c,:integer; begin P:=0; c:=0; if ((p<9) and (c<9)) then begin //2 if ((Sender as TSpeedButton).Caption='') then begin //3 p:=p 1; if (p>c) then begin //4 (Sender as TSpeedButton).Caption:='O'; c:=c 1; end; //4 if (p=c) then (Sender as TSpeedButton).Caption:='X'; end; //3 end; //2 end; //1可是他都是執行P=C說,不會執行P>C阿 我有試過用另一個變數設定他是1到9 變數MOD 2=1 執行O 變數MOD 2=0 執行X 可是還是一樣說,並不會照順序執行說 都只執行某一個O或X,請問要如何才能讓他按照順序去執行迴圈呢? 謝謝唷 |
seaturn99
版主 ![]() ![]() ![]() ![]() 發表:69 回覆:427 積分:214 註冊:2003-08-25 發送簡訊給我 |
north 您好 : 依照您程式的流程,只會執行一次,若您需要重複執行這個判斷區段的 Code 需要運作一個迴圈,否則判斷完後就會接續 Run 下去 .. 由於不太清楚您此段 Code 的目的,所以寫了一個 mod 的例子,希望對您有幫助.. Delay 1 sec 顯示 O→X→O→X→O→X→...
procedure TForm1.SpeedButton1Click(Sender: TObject); var i:integer; SpeedBtn :TSpeedButton; begin SpeedBtn := Sender as TSpeedButton; SpeedBtn.Caption := ''; for i:=0 to 9 do begin if (i mod 2) = 0 then SpeedBtn.Caption := '0' else SpeedBtn.Caption := 'X'; // 讓 Caption 顯示 Application.ProcessMessages; // Delay 顯示以看出效果 Sleep(1000); end; end;---- 我只會兩件事,這也不會,那也不會 眼見不一定為真 ---- |
north
一般會員 ![]() ![]() 發表:10 回覆:17 積分:5 註冊:2004-08-06 發送簡訊給我 |
SouthWind大大您好:
我想是我一開始發言時沒有解釋清楚,我是想寫一個使用9個SpeedButton組合起來得井字遊戲,我寫在一個名稱為GAME的procedure裡面,然後按SpeedButton時,再去呼叫,我現在煩惱的是如何去定義執行得順序,假設第一次按按鈕出現O,那第二次按別的按鈕就要出現X,可是我現在按下去都只會出現X說,我GAME的procedure如下:
procedure TForm1.game(Sender: TObject); var i,p,c,count,count1:integer; found:boolean; begin SpeedButton1.Tag:=8; SpeedButton2.Tag:=1; SpeedButton3.Tag:=6; SpeedButton4.Tag:=3; SpeedButton5.Tag:=5; SpeedButton6.Tag:=7; SpeedButton7.Tag:=4; SpeedButton8.Tag:=9; SpeedButton9.Tag:=2; c:=0; for p:=0 to 9 do //我改用FOR迴圈做,還有請問為什麼用FOR迴圈,就不能去設定p:=p 1呢?但是這樣一開始就是c=p,就不會執行O了說 begin if (p>c) then begin (Sender as TSpeedButton).Caption:='O'; c:=c 1; end; if (c=p) then (Sender as TSpeedButton).Caption:='X'; { //大括號這一段我不知道為什麼會錯誤,想請問一下,謝謝^^ for i:=0 to ComponentCount-1 do if (Components[i] is TSpeedButton) then begin if ((Components[i] as TSpeedButton).Caption:='O') and ( ((Components[i] as TSpeedButton).Tag (Components[i 1] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag=15) or ((Components[i] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag=15) or ((Components[i] as TSpeedButton).Tag (Components[i 3] as TSpeedButton).Tag (Components[i 6] as TSpeedButton).Tag=15) or ((Components[i] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag (Components[i 8] as TSpeedButton).Tag=15)) then ShowMessage('O Win') else ShowMessage('X Win'); end; } end; end; |
richtop
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
|
pigbaby
初階會員 ![]() ![]() 發表:2 回覆:84 積分:47 註冊:2002-09-02 發送簡訊給我 |
豬寶寶的做法
在最前面宣告一個PUBLIC 的PRESS_TIMES的變數
procedure TForm1.S1Click(Sender: TObject);
begin
case PRESS_TIMES MOD 2 of
0:TSPEEDBUTTON(SENDER).CAPTION:='O';
1:TSPEEDBUTTON(SENDER).CAPTION:='X';
end; PRESS_TIMES:=PRESS_TIMES 1;
end;
您可以試看看^^" 發表人 - pigbaby 於 2004/08/25 08:43:32 發表人 - pigbaby 於 2004/08/25 08:50:38
|
north
一般會員 ![]() ![]() 發表:10 回覆:17 積分:5 註冊:2004-08-06 發送簡訊給我 |
謝謝richtop和pigbaby兩位大大唷
我現在按鈕已經可以按順序動作了
謝謝唷
|
seaturn99
版主 ![]() ![]() ![]() ![]() 發表:69 回覆:427 積分:214 註冊:2003-08-25 發送簡訊給我 |
引言: 謝謝richtop和pigbaby兩位大大唷 我現在按鈕已經可以按順序動作了 謝謝唷 |
richtop
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
|
north
一般會員 ![]() ![]() 發表:10 回覆:17 積分:5 註冊:2004-08-06 發送簡訊給我 |
SouthWind大大您好:
我試過橫排、直列、對角8個狀況,我的寫法如下:
procedure TForm1.Game(Sender: TObject); var i:integer; begin SpeedButton1.Tag:=8; SpeedButton2.Tag:=1; SpeedButton3.Tag:=6; SpeedButton4.Tag:=3; SpeedButton5.Tag:=5; SpeedButton6.Tag:=7; SpeedButton7.Tag:=4; SpeedButton8.Tag:=9; SpeedButton9.Tag:=2; if (p mod 2=1) then (Sender as TSpeedButton).Caption:='O'; if (p mod 2=0) then (Sender as TSpeedButton).Caption:='X'; if p>=4 then //我覺的是我這裡判斷錯誤,在按第4個按鈕後,每個按鈕都會showmessage,請問要如何去判斷3個記錄為一組,就以下情況有成立才showmessage begin if ((SpeedButton1.Tag SpeedButton2.Tag SpeedButton3.Tag=15) or (SpeedButton4.Tag SpeedButton5.Tag SpeedButton6.Tag=15) or (SpeedButton7.Tag SpeedButton8.Tag SpeedButton9.Tag=15)) then //橫排 ShowMessage('O Win') if ((SpeedButton1.Tag SpeedButton4.Tag SpeedButton7.Tag=15) or (SpeedButton2.Tag SpeedButton5.Tag SpeedButton8.Tag=15) or (SpeedButton3.Tag SpeedButton6.Tag SpeedButton9.Tag=15)) then //直列 ShowMessage('O Win'); if ((SpeedButton1.Tag SpeedButton5.Tag SpeedButton9.Tag=15) or (SpeedButton3.Tag SpeedButton5.Tag SpeedButton7.Tag=15)) then //對角 ShowMessage('O Win'); else ShowMessage('X Win'); end; end;然後大大說寫一個function問題就在我不知如何去寫一個並且在procedure內呼叫他,所以我使用上面的寫法。下面是大大用紅色顯示出來我之前有問題的地方,我的想法如下: 816 對照 123←speedbutton1~9 357 對照 456 492 對照 789 for i:=1 to ComponentCount-1 do 為0→9 if (Components[i] is TSpeedButton) then begin if ((Components[i] as TSpeedButton).Caption:='O') and ( ((Components[i] as TSpeedButton).Tag (Components[i 1] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag=15) or代表三個橫排 ((Components[i] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag=15) or代表357對角 ((Components[i] as TSpeedButton).Tag (Components[i 3] as TSpeedButton).Tag (Components[i 6] as TSpeedButton).Tag=15) or代表三個直列 ((Components[i] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag (Components[i 8] as TSpeedButton).Tag=15))代表159對角 then ShowMessage('O Win') else ShowMessage('X Win'); |
seaturn99
版主 ![]() ![]() ![]() ![]() 發表:69 回覆:427 積分:214 註冊:2003-08-25 發送簡訊給我 |
引言: SouthWind大大您好: 我試過橫排、直列、對角8個狀況,我的寫法如下:north 您好: 紅色標起的來的地方,不知道這幾天您有沒有發現您的程式恆為 True?? 所以結果都是 ShowMessage('O Win')?? 想了很久,還是不要直接把 code 寫出來好,再以討論的方式交流.. 我之前提到的是屬於我自己的想法,使用 function 能讓您的程式可讀性提高,也可以抽離邏輯部分,提高程式結構性,這部分您可以好好練習,將來不管對於讀別人的程式或自行撰寫程式,都有很大的幫助 ... 回到問題本身 .. 問題已分析到 8 個狀況,所以不需要列舉 9 個點,去組合所有的狀況再去判斷是否相加為 15 ,您可以在您標示的紅色區段 code ,以 i 值逐一帶入,便可以發現,有的並不是直線,而且這種寫法依然會造成 Componet[i] 存取違規.. 所以在 if p>=4 then 這個判斷後要寫接著判斷八個狀況 .. 這邊只列舉對角狀況 ..procedure TForm1.Game(Sender: TObject); var i:integer; begin SpeedButton1.Tag:=8; SpeedButton2.Tag:=1; SpeedButton3.Tag:=6; SpeedButton4.Tag:=3; SpeedButton5.Tag:=5; SpeedButton6.Tag:=7; SpeedButton7.Tag:=4; SpeedButton8.Tag:=9; SpeedButton9.Tag:=2; if (p mod 2=1) then (Sender as TSpeedButton).Caption:='O'; if (p mod 2=0) then (Sender as TSpeedButton).Caption:='X'; if p>=4 then //我覺的是我這裡判斷錯誤,在按第4個按鈕後,每個按鈕都會showmessage,請問要如何去判斷3個記錄為一組,就以下情況有成立才showmessage begin if ((SpeedButton1.Tag SpeedButton2.Tag SpeedButton3.Tag=15) or (SpeedButton4.Tag SpeedButton5.Tag SpeedButton6.Tag=15) or (SpeedButton7.Tag SpeedButton8.Tag SpeedButton9.Tag=15)) then //橫排 ShowMessage('O Win') if ((SpeedButton1.Tag SpeedButton4.Tag SpeedButton7.Tag=15) or (SpeedButton2.Tag SpeedButton5.Tag SpeedButton8.Tag=15) or (SpeedButton3.Tag SpeedButton6.Tag SpeedButton9.Tag=15)) then //直列 ShowMessage('O Win'); if ((SpeedButton1.Tag SpeedButton5.Tag SpeedButton9.Tag=15) or (SpeedButton3.Tag SpeedButton5.Tag SpeedButton7.Tag=15)) then //對角 ShowMessage('O Win'); else ShowMessage('X Win'); end; end;然後大大說寫一個function問題就在我不知如何去寫一個並且在procedure內呼叫他,所以我使用上面的寫法。下面是大大用紅色顯示出來我之前有問題的地方,我的想法如下: 816 對照 123←speedbutton1~9 357 對照 456 492 對照 789for i:=1 to ComponentCount-1 do 為0→9 if (Components[i] is TSpeedButton) then begin if ((Components[i] as TSpeedButton).Caption:='O') and ( ((Components[i] as TSpeedButton).Tag (Components[i 1] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag=15) or代表三個橫排 ((Components[i] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag=15) or代表357對角 ((Components[i] as TSpeedButton).Tag (Components[i 3] as TSpeedButton).Tag (Components[i 6] as TSpeedButton).Tag=15) or代表三個直列 ((Components[i] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag (Components[i 8] as TSpeedButton).Tag=15))代表159對角 then ShowMessage('O Win') else ShowMessage('X Win'); if (SpeedButton1.Caption = 'O') and (SpeedButton5.Caption = 'O') and (SpeedButton9.Caption = 'O') then ShowMessage('O Win') else if (SpeedButton1.Caption = 'X') and (SpeedButton5.Caption = 'X') and (SpeedButton9.Caption = 'X') then ShowMessage('X Win') else if (SpeedButton3.Caption = 'O') and (SpeedButton5.Caption = 'O') and (SpeedButton7.Caption = 'O') then ShowMessage('O Win') else if (SpeedButton3.Caption = 'X') and (SpeedButton5.Caption = 'X') and (SpeedButton7.Caption = 'X') then ShowMessage('X Win') // .... 接續 else if 判斷其他六個狀況 12 個判斷式(O,X 各不同)這樣就可以正確判斷勝負了,這樣的邏輯比較簡單看出來,不過 Code 有點亂,也沒有用到相加為 15 的演算法 .. ---- 我只會兩件事,這也不會,那也不會 眼見不一定為真 ---- |
north
一般會員 ![]() ![]() 發表:10 回覆:17 積分:5 註冊:2004-08-06 發送簡訊給我 |
SouthWind大大您好:
你說的部分我已經處理好了,目前我煩惱的事random的問題說
目前進度如下:
procedure TForm1.Game(Sender: TObject); var j:integer; begin if ((Sender as TSpeedButton).Caption='') then begin if (p mod 2=1) then (Sender as TSpeedButton).Caption:='O'; if (p mod 2=0) then //電腦判斷,我將可能連線的判斷一個一個寫出來 begin //如果是O的判斷 if ((SpeedButton1.Caption='O') and (SpeedButton2.Caption='O')) then SpeedButton3.Caption:='X'‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 //如果是X的判斷 else if ((SpeedButton1.Caption='X') and (SpeedButton2.Caption='X')) then SpeedButton3.Caption:='X' ‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 else j:=1; Randomize; if ((Sender as TSpeedButton).Caption='') then begin Components[Random(j)] as TSpeedButton).Caption:='X'; end; end; try //判斷輸贏平手,把可能連線都寫出來 if ((Sender as TSpeedButton).Caption='O') then begin if ((SpeedButton1.Caption='O') and (SpeedButton2.Caption='O') and (SpeedButton3.Caption='O')) or ((SpeedButton4.Caption='O') and (SpeedButton5.Caption='O') and (SpeedButton6.Caption='O')) or ((SpeedButton7.Caption='O') and (SpeedButton8.Caption='O') and (SpeedButton9.Caption='O')) then ShowMessage('O win'); ‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 else if ((Sender as TSpeedButton).Caption='X') then begin if ((SpeedButton1.Caption='X') and (SpeedButton2.Caption='X') and (SpeedButton3.Caption='X')) or ((SpeedButton4.Caption='X') and (SpeedButton5.Caption='X') and (SpeedButton6.Caption='X')) then ShowMessage('X win'); ‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 except if ((Sender as TSpeedButton).Caption<>'') then begin if ((SpeedButton1.Caption<>'') and (SpeedButton2.Caption<>'') and (SpeedButton3.Caption<>'') and ...... and (SpeedButton9.Caption<>'')) then ShowMessage('平手'); end; end; end; end;我目前就紅色區域,在else上面都還沒有成立前,隨機判斷一個按鈕caption產生X,可是不知道哪裡沒用好,第一個X都在按鈕1,如果按鈕1caption為O會把它蓋掉,而且每次換手都要重複按2次後,有時才會產生一個X,請問這部份是哪邊沒有寫好呢?? 謝謝 |
seaturn99
版主 ![]() ![]() ![]() ![]() 發表:69 回覆:427 積分:214 註冊:2003-08-25 發送簡訊給我 |
引言:procedure TForm1.Game(Sender: TObject); var j:integer; begin if ((Sender as TSpeedButton).Caption='') then begin if (p mod 2=1) then (Sender as TSpeedButton).Caption:='O'; if (p mod 2=0) then //電腦判斷,我將可能連線的判斷一個一個寫出來 begin //如果是O的判斷 if ((SpeedButton1.Caption='O') and (SpeedButton2.Caption='O')) then SpeedButton3.Caption:='X'‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 //如果是X的判斷 else if ((SpeedButton1.Caption='X') and (SpeedButton2.Caption='X')) then SpeedButton3.Caption:='X' ‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 else j:=1; Randomize; if ((Sender as TSpeedButton).Caption='') then begin Components[Random(j)] as TSpeedButton).Caption:='X'; end; end; try //判斷輸贏平手,把可能連線都寫出來 if ((Sender as TSpeedButton).Caption='O') then begin if ((SpeedButton1.Caption='O') and (SpeedButton2.Caption='O') and (SpeedButton3.Caption='O')) or ((SpeedButton4.Caption='O') and (SpeedButton5.Caption='O') and (SpeedButton6.Caption='O')) or ((SpeedButton7.Caption='O') and (SpeedButton8.Caption='O') and (SpeedButton9.Caption='O')) then ShowMessage('O win'); ‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 else if ((Sender as TSpeedButton).Caption='X') then begin if ((SpeedButton1.Caption='X') and (SpeedButton2.Caption='X') and (SpeedButton3.Caption='X')) or ((SpeedButton4.Caption='X') and (SpeedButton5.Caption='X') and (SpeedButton6.Caption='X')) then ShowMessage('X win'); ‧‧‧‧‧‧‧程式佔版面,版面不足,類似的刪除 except if ((Sender as TSpeedButton).Caption<>'') then begin if ((SpeedButton1.Caption<>'') and (SpeedButton2.Caption<>'') and (SpeedButton3.Caption<>'') and ...... and (SpeedButton9.Caption<>'')) then ShowMessage('平手'); end; end; end; end;我目前就紅色區域,在else上面都還沒有成立前,隨機判斷一個按鈕caption產生X,可是不知道哪裡沒用好,第一個X都在按鈕1,如果按鈕1caption為O會把它蓋掉,而且每次換手都要重複按2次後,有時才會產生一個X,請問這部份是哪邊沒有寫好呢?? 謝謝 |
north
一般會員 ![]() ![]() 發表:10 回覆:17 積分:5 註冊:2004-08-06 發送簡訊給我 |
SouthWind大大您好:
對不起唷,這麼久才回應,拍謝拍謝
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |