Tic Tac Toe Program

gcorm18
10-23-2000, 10:55 AM
I've created a Tic Tac Toe program and I was informed by my VB instructor that there might be a shorter way to do this... please someone help...

Private Sub optO_Click(Index As Integer)
'code for not cheeting
If optO(Index).Value = True Then
lblDisplay(Index).Caption = "O"
optX(Index).Enabled = False
End If
'Calculate for all possible wins for O
If optO(0).Value = True And optO(1).Value = True And optO(2).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(3).Value = True And optO(4).Value = True And optO(5).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(6).Value = True And optO(7).Value = True And optO(8).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(0).Value = True And optO(3).Value = True And optO(6).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(1).Value = True And optO(4).Value = True And optO(7).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(2).Value = True And optO(5).Value = True And optO(8).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(2).Value = True And optO(4).Value = True And optO(6).Value = True Then
lblWl.Caption = "O WINS"
End If
If optO(0).Value = True And optO(4).Value = True And optO(8).Value = True Then
lblWl.Caption = "O WINS"
End If
End Sub

Private Sub optX_Click(Index As Integer)
'code for not cheeting
If optX(Index).Value = True Then
lblDisplay(Index).Caption = "X"
optO(Index).Enabled = False
End If
'Calculate for all possible wins for X
If optX(0).Value = True And optX(1).Value = True And optX(2).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(3).Value = True And optX(4).Value = True And optX(5).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(6).Value = True And optX(7).Value = True And optX(8).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(0).Value = True And optX(3).Value = True And optX(6).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(1).Value = True And optX(4).Value = True And optX(7).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(2).Value = True And optX(5).Value = True And optX(8).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(2).Value = True And optX(4).Value = True And optX(6).Value = True Then
lblWl.Caption = "X WINS"
End If
If optX(0).Value = True And optX(4).Value = True And optX(8).Value = True Then
lblWl.Caption = "X WINS"
End If

End Sub

BillSoo
10-23-2000, 01:02 PM
He probably meant that you could use a nested if structure. Or maybe a case statement.

But if it were me, I'd rewrite it as follows:

<PRE><CODE>
v%=0
for i% = 0 to 8
v% = v% * 2 + (optO(i%).value AND 1)
next i%

select case v%
case &H1C0, &H38,&H3,&H124,&H92,&H49,&H111,&H54
lblWl.caption = "O Wins"
end select
</PRE></CODE>

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

roger
10-26-2000, 03:02 PM
Bill,
Could you explain your code?
I'm very interested in understanding how you got to it!!!

Thanks
Roger

"Great Ideas need Landing Gears as well as Wings"

BillSoo
10-26-2000, 04:54 PM
Hey Roger,

Basically, the code generates a unique integer for every possible pattern. It then compares this pattern to the 8 possible patterns that result in a victory.

<PRE><CODE>
v%=0
for i% = 0 to 8
' for each square, encode it as
' square8 = 1, square7 = 2, square6=4, square5=8, square4=16
' square3=32, square2=64, square1=128 and square0=256
v% = v% * 2 + (optO(i%).value AND 1)
next i%
select case v%
'look for all 8 patterns where 0 wins
'for instance &H1C0 = top row filled (binary 111000000)
'&H38 = second row filled (binary 000111000)
'&H111 = diagonal (binary 100010001)
case &H1C0, &H38,&H3,&H124,&H92,&H49,&H111,&H54
lblWl.caption = "O Wins"
end select
</PRE></CODE>

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum