Tick Tack Toe - Logic(CheckIfWin)

Pfergu

New Member
Licensed User
Longtime User
Hi there i am new at this, i was reading the forum and i found this thread which is very nice.

http://www.b4x.com/forum/basic4android-getting-started-tutorials/8506-tick-tack-toe-working-arrays-views.html


I downloaded the example and tried to understand the logic, so i found this sub called Checkifwin which receives the current player name and return a boolean value.


First thing check the columns , then rows and finally diagonals.

Here is checking columns

B4X:
Dim found As Boolean
   For x = 0 To 2
      found = True
      For y = 0 To 2
         found = found AND Buttons(x, y).Text = Player
      Next
      If found = True Then Return True
   Next



this line here "If found = True Then Return True" means to me the function is returning a true value, but then it keeps going to the second row and returns another value.


What i dont understand is how do you know whether (0,1)(0,2),(0,3) are true if the variable "found" is being overwrited each row.


If somebody could explain it to me i'll aprecciate it.




Thanks
 

Pfergu

New Member
Licensed User
Longtime User
B4X:
If found = True Then Return True
Return True means the function STOPS and returns True.

Best regards.


Hi that's what i thought! but then i debug the function and it's not stopping the function.

Thats why i dont understand how it works

Here is the full function

B4X:
Sub CheckIfWin (Player As String) As Boolean
   'Check columns
   Dim found As Boolean
   For x = 0 To 2
      found = True
      For y = 0 To 2
         found = found AND Buttons(x, y).Text = Player
      Next
      If found = True Then Return True
   Next
   
   'Check rows
   For y = 0 To 2
      found = True
      For x = 0 To 2
         found = found AND Buttons(x, y).Text = Player
      Next
      If found = True Then Return True
   Next
   
   'Check diagonals
   found = True
   For i = 0 To 2
      found = found AND Buttons(i, i).Text = Player
   Next
   If found = True Then Return True
   
   found = True
   
   For i = 0 To 2
      found = found AND Buttons(i, 2 - i).Text = Player
   Next
   
   If found = True Then Return True
   Return False

End Sub


Thanks!
 
Last edited:
Upvote 0
Top