假设我有九个标签,范围从label1-label9,排列为3x3网格。所以看起来应该是
label1 label2 label3
label4 label5 label6
label7 label8 label9
tic tac toe的胜利条件是条件r如下图所示:
基于这些条件我可以写:
Public Sub winCondition()
If Label1.Text = "x" And Label2.Text = "x" And Label3.Text = "x" Then
Call xWin()
ElseIf Label4.Text = "x" And Label5.Text = "x" And Label6.Text = "x" Then
Call xWin()
ElseIf Label7.Text = "x" And Label8.Text = "x" And Label9.Text = "x" Then
Call xWin()
ElseIf Label1.Text = "x" And Label4.Text = "x" And Label7.Text = "x" Then
Call xWin()
ElseIf Label2.Text = "x" And Label5.Text = "x" And Label8.Text = "x" Then
Call xWin()
ElseIf Label3.Text = "x" And Label6.Text = "x" And Label9.Text = "x" Then
Call xWin()
ElseIf Label1.Text = "x" And Label5.Text = "x" And Label9.Text = "x" Then
Call xWin()
ElseIf Label3.Text = "x" And Label5.Text = "x" And Label7.Text = "x" Then
Call xWin()
ElseIf Label1.Text = "o" And Label2.Text = "o" And Label3.Text = "o" Then
Call oWin()
ElseIf Label4.Text = "o" And Label5.Text = "o" And Label6.Text = "o" Then
Call oWin()
ElseIf Label7.Text = "o" And Label8.Text = "o" And Label9.Text = "o" Then
Call oWin()
ElseIf Label1.Text = "o" And Label4.Text = "o" And Label7.Text = "o" Then
Call oWin()
ElseIf Label2.Text = "o" And Label5.Text = "o" And Label8.Text = "o" Then
Call oWin()
ElseIf Label3.Text = "o" And Label6.Text = "o" And Label9.Text = "o" Then
Call oWin()
ElseIf Label1.Text = "o" And Label5.Text = "o" And Label9.Text = "o" Then
Call oWin()
ElseIf Label3.Text = "o" And Label5.Text = "o" And Label7.Text = "o" Then
Call oWin()
End If
If Form1.Label3.Text <> "0" And Form1.Label4.Text <> "0" Then
imbang()
End If
End Sub
xWin和oWin程序只显示messageBox。 我还在模块中有另外的代码。但基本逻辑就是这样。
现在对于绘制条件,基本上如果所有的块都用x和o填充但是不符合上面的获胜条件那么它将被绘制(它将显示messagebpox,表示它是绘制的)。
我该怎么做?
答案 0 :(得分:0)
在每场比赛开始时将计数器设置为零。每次有人移动时,都要向柜台添加一个。在wincondition检查结束时,添加一个最终的elsif以查看计数器是否已达到9并且如果有则调用抽奖。