全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3009
推到 Plurk!
推到 Facebook!

執行的先後順序

尚未結案
north
一般會員


發表:10
回覆:17
積分:5
註冊:2004-08-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-24 00:35:15 IP:218.164.xxx.xxx 未訂閱
請問如何去定義一段程式執行的先後順序呢? 例如: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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-24 10:08:15 IP:210.68.xxx.xxx 未訂閱
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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-08-25 00:43:29 IP:218.164.xxx.xxx 未訂閱
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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-08-25 02:02:27 IP:211.76.xxx.xxx 未訂閱
north 您好:    容我在"南北"溝通當"中",插個花!    您的目的好像是要讓雙方(O,X)交互畫記,並在每一畫記後判斷是否有某一方已連成一線了。 所以您應該用一個全域變數,比方說一開始設put=0,來記錄目前共畫記了幾次,並且在每次畫記結束之後讓 put := put + 1。 於是您就可用(put mod 2)=0 或 (put mod 2)=1來判斷該哪一方下。 提供參考。
pigbaby
初階會員


發表:2
回覆:84
積分:47
註冊:2002-09-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-08-25 08:42:13 IP:210.200.xxx.xxx 未訂閱
豬寶寶的做法 在最前面宣告一個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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-08-26 00:31:35 IP:220.143.xxx.xxx 未訂閱
謝謝richtop和pigbaby兩位大大唷 我現在按鈕已經可以按順序動作了 謝謝唷 --------------------------------------- 然後我現在的問題是在判斷誰贏的時候出現錯誤說 < class="code"> procedure TForm1.Game(Sender: TObject); var i:integer; begin if (p mod 2=1) then (Sender as TSpeedButton).Caption:='O'; if (p mod 2=0) 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 //Run的時候這裡出現錯誤說,我有檢查前面式子的括號等有沒遺漏說,可是還是不知為什麼阿,還請各位大大幫我解釋一下唷,我想了半天還是想不出來說。 ShowMessage('O Win') else ShowMessage('X Win'); end; end; 謝謝各位大大的協助
seaturn99
版主


發表:69
回覆:427
積分:214
註冊:2003-08-25

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-08-26 10:19:46 IP:210.68.xxx.xxx 未訂閱
引言: 謝謝richtop和pigbaby兩位大大唷 我現在按鈕已經可以按順序動作了 謝謝唷 --------------------------------------- 然後我現在的問題是在判斷誰贏的時候出現錯誤說 < class="code"> procedure TForm1.Game(Sender: TObject); var i:integer; begin if (p mod 2=1) then (Sender as TSpeedButton).Caption:='O'; if (p mod 2=0) 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 //Run的時候這裡出現錯誤說,我有檢查前面式子的括號等有沒遺漏說,可是還是不知為什麼阿,還請各位大大幫我解釋一下唷,我想了半天還是想不出來說。 ShowMessage('O Win') else ShowMessage('X Win'); end; end; 謝謝各位大大的協助 < face="Verdana, Arial, Helvetica"> north 您好 : 先解釋 Run-Time Error 的問題,這是因為您的迴圈內從 0 -> ComponentCount-1 ,而又存取了 Components[i 1] ,試想當 i = ComponentCount-1 時, 就超過了 Components 陣列的存取範圍,當然會引發違規存取的問題 .. 接下來是邏輯的問題 : 以前以為這個 (上下左右,對角相加皆為 15) 8 1 6 3 5 7 4 9 2 這只是個益智遊戲,原來在井字遊戲可以派上用場 ... 上述紅色 mark 起來的邏輯有錯誤 .. 我想您應該是想要判斷 8 1 6 = 15 8 5 2 = 15 8 3 4 = 15 (Components[i] as TSpeedButton).Tag (Components[i 2] as TSpeedButton).Tag (Components[i 4] as TSpeedButton).Tag=15) => 這個判斷式應該是想法上有誤?? 但是,當 i 每次加 1 時,狀況都不一樣 Ex : i=0 時為上述 i=1 時為 8 1 6 = 15 1 5 9 = 15 真正的勝負情況是要判斷八條線是否已連線 .. 這八個狀況是 : 橫排 8 1 6 = 15 3 5 7 = 15 4 9 2 = 15 直列 8 3 4 = 15 1 5 9 = 15 6 7 2 = 15 對角 8 5 2 = 15 6 5 4 = 15 由於八個狀況並不複雜,所以可以紀錄超過下三個後 (三點方成立),判斷是否符合這八個狀況 .. 寫一個 function 去判斷 (SpeedButton 已紀錄 O,X), 這八條有 O ro X 已先完成加總 15 ,傳回三個狀態 O -> Win , X -> Win , 未分勝負 .. 我想剩下這最後一步,我就不寫 Sample Code ,讓您保留最後完成的快感 .. 加油囉,若有問題再討論囉 .. To : RichTop 大大,我不是東西 .. 哈哈 .. ---- 我只會兩件事,這也不會,那也不會 眼見不一定為真 ---- 發表人 -
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-08-26 14:58:18 IP:211.76.xxx.xxx 未訂閱
大家好:    純插花!    To SouthWind: 您的的確確不是個東西,不折不扣一個"不務正業"的優秀傢伙!< > 問題應該快解決了,大家加油囉!< > RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
north
一般會員


發表:10
回覆:17
積分:5
註冊:2004-08-06

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-08-27 00:53:45 IP:218.164.xxx.xxx 未訂閱
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

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-08-31 09:56:14 IP:203.66.xxx.xxx 未訂閱
引言: 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');
north 您好: 紅色標起的來的地方,不知道這幾天您有沒有發現您的程式恆為 True?? 所以結果都是 ShowMessage('O Win')?? 想了很久,還是不要直接把 code 寫出來好,再以討論的方式交流.. 我之前提到的是屬於我自己的想法,使用 function 能讓您的程式可讀性提高,也可以抽離邏輯部分,提高程式結構性,這部分您可以好好練習,將來不管對於讀別人的程式或自行撰寫程式,都有很大的幫助 ... 回到問題本身 .. 問題已分析到 8 個狀況,所以不需要列舉 9 個點,去組合所有的狀況再去判斷是否相加為 15 ,您可以在您標示的紅色區段 code ,以 i 值逐一帶入,便可以發現,有的並不是直線,而且這種寫法依然會造成 Componet[i] 存取違規.. 所以在 if p>=4 then 這個判斷後要寫接著判斷八個狀況 .. 這邊只列舉對角狀況 ..
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

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-09-01 00:14:57 IP:218.164.xxx.xxx 未訂閱
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

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-09-01 10:46:57 IP:203.66.xxx.xxx 未訂閱
引言:
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,請問這部份是哪邊沒有寫好呢?? 謝謝 < face="Verdana, Arial, Helvetica"> north 您好 : Random 的部分傳入的參數可以查閱 Delphi Online Help ,是傳入所取得亂述的範圍,您 Set j:=1 所以 Random(j) 只會傳回 0 或 1,您這個做法,應該是要讓電腦自動下 X 吧?? 但是這個邏輯需要考慮若是按鈕已被標示成 O ,就不能再被下 X ,要另外找其他地方下 .. 所以需要寫一個判斷 (也許可以利用迴圈重複判斷),讓若是已經下的 Button 跳過,繼續取亂數,直到下到沒有下過的 Button .. 單純討論亂數落在 1~9 的範圍內,您可以這樣寫 Random(8) 1 (取 1~9 亂數) 另外, try except 區段用得有點奇怪,是否有用到一些元件需要攔截 Exception?? 否則,上述 Code 應該是攔截不到什麼 Exception 的 .. ---- 我只會兩件事,這也不會,那也不會 眼見不一定為真 ----
north
一般會員


發表:10
回覆:17
積分:5
註冊:2004-08-06

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-09-07 03:05:49 IP:218.169.xxx.xxx 未訂閱
SouthWind大大您好: 對不起唷,這麼久才回應,拍謝拍謝 我試過所說的寫一個 class="code"> Randomize; j:=Random(8) 1; for j:=0 to ComponentCount-1 do if (Components[j] is TSpeedButton) then if ((Components[j] as TSpeedButton).Caption='') then if ((Components[j] as TSpeedButton).Caption<>'O') then begin (Components[j] as TSpeedButton).Caption:='X'; break; end; 然後try except區段我是用來判斷當O或X贏的時候,使用者如果繼續按空白按鈕後,不會出現平手的訊息,可是我測試時,結果當平手時卻不會出現平手的訊息框,有時卻會出現說,我還在解決中。 麻煩了,謝謝唷
系統時間:2024-04-26 19:19:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!