Re: [VB6 ] 井字遊戲的勝負判定
講到基本寫法,由於遊戲是事件觸發,因此可以考慮哪種事件只做哪些事情.
Button1_Click只考慮一條橫列,一條縱行,一條斜線,
Private Sub Button1_Click(ByVal ...
x = x + 1
Button1.Enabled = False
If x Mod 2 = 1 Then
Button1.Text = "o"
Else
Button1.Text = "x"
End If
If (Button1.Text = Button2.Text AndAlso _
Button1.Text = Button3.Text) _
OrElse (Button1.Text = Button4.Text AndAlso _
Button1.Text = Button7.Text) _
OrElse (Button1.Text = Button5.Text AndAlso _
Button1.Text = Button9.Text _
Then
Label10.Text = String.Format("勝方 {0} !", Button1.Text)
End If
End If
同理,Button2,Button4,Button6,Button8都只考慮一橫列與一縱行,
Button5要考慮一橫列,一縱行,左斜線,右斜線,
Button1,Button3,Button7,Botton9則是另一組同類型的程式寫法.
一般的回合制戰略遊戲應該也是這樣寫,對每個移動的兵力只考慮其攻擊範圍的
可能情況.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.160.109.218
※ 編輯: yauhh 來自: 218.160.109.218 (05/17 12:02)
推
05/17 14:12, , 1F
05/17 14:12, 1F
→
05/17 14:13, , 2F
05/17 14:13, 2F
→
05/17 15:55, , 3F
05/17 15:55, 3F
→
05/17 16:33, , 4F
05/17 16:33, 4F
→
05/17 16:34, , 5F
05/17 16:34, 5F
→
05/17 16:35, , 6F
05/17 16:35, 6F
→
05/17 16:46, , 7F
05/17 16:46, 7F
推
05/18 08:30, , 8F
05/18 08:30, 8F
→
05/18 09:59, , 9F
05/18 09:59, 9F
小知識:
Short-cut Circuit Evaluation是指邏輯式中通常在前一部份已能決定
答案時,就不執行邏輯式後面部份,避免浪費計算動作.
A And B 情況,若A為False,此式答案很明顯是False,因此B不必評估.
A Or B 情況,若A為True,此式答案就是True,因此B不必評估.
C/C++ 的And: && 與Or: || 都有Short-cut Circuit Evaluation效果,
但VB6及以前版本的 And 與 Or 很明顯沒有 SCE 效果.
而VB.Net是將這效果搬上檯面,區分為基本的And Or與有捷徑求值的AndAlso
與OrElse. 從字面提示很清楚它帶有依情況評估的功能.
知道這個效果的人,有時會利用此效果寫出比較不一樣的程式.
要注意的是,擺在右邊的子算式可能不執行. 如果一行判斷式永遠必須執行到,
不要把此判斷式擺在AndAlso右邊,或OrElse右邊.
※ 編輯: yauhh 來自: 59.112.224.135 (05/18 10:19)
→
05/18 13:20, , 10F
05/18 13:20, 10F
推
05/18 16:05, , 11F
05/18 16:05, 11F
→
05/18 16:09, , 12F
05/18 16:09, 12F
→
05/18 16:12, , 13F
05/18 16:12, 13F
→
05/20 17:57, , 14F
05/20 17:57, 14F
→
05/20 17:58, , 15F
05/20 17:58, 15F
→
05/20 17:58, , 16F
05/20 17:58, 16F
→
05/20 17:59, , 17F
05/20 17:59, 17F
討論串 (同標題文章)
完整討論串 (本文為第 6 之 6 篇):