# Quickly checking array values

## Recommended Posts

Hi all,

I am working on a Tic-Tac-Toe game as some people may know. I have started work on a function to check whether or not there is a possible win.

```Func PossibleWin()
; This is where our main algorithm comes Into play as we have To check our \$Marked Array
; Returns 0 if player p can not win on her next move; otherwise it returns the number of the square that constitutes
; a winning move. This function should enable to win and to block the opponent's win. PossibleWin operates by checking one
; at a time each of the rows, columns, and diagonals. It can see if a row or diagonal for possible win by multiplying the
; values of its squares together. If the product is 18 (3 * 3 * 2) then X could win; if the product is 50 (5 * 5 *2) then O
; could win. If there is a winning row then it determines which square is blank and return the number of that square.

; These values will be used later In multiplication To check a winning Formula
Local \$rPos, \$cPos, \$dPos

For \$i = 0 To \$Marked
MsgBox(0, "", \$Marked[\$i]); Display the marked values For now
Next

; Check rows
; First Row
If \$Marked = 3 Then
\$rPos = 3
EndIf
If \$Marked = 3 Then
\$rPos = 3
EndIf
If \$Marked = 3 Then
\$rPos = 3
EndIf
; Second Row
If \$Marked = 3 Then
\$rPos = 3
EndIf
If \$Marked = 3 Then
\$rPos = 3
EndIf
If \$Marked = 3 Then
\$rPos = 3
EndIf
; Third rows
If \$Marked = 3 Then
\$rPos = 3
EndIf
If \$Marked = 3 Then
\$rPos = 3
EndIf
If \$Marked = 3 Then
\$rPos = 3
EndIf

; Check Columns
; First Column
If \$Marked = 3 Then
\$cPos = 3
EndIf
If \$Marked = 3 Then
\$cPos = 3
EndIf
If \$Marked = 3 Then
\$cPos = 3
EndIf
; Second Column
If \$Marked = 3 Then
\$cPos = 3
EndIf
If \$Marked = 3 Then
\$cPos = 3
EndIf
If \$Marked = 3 Then
\$cPos = 3
EndIf
; Third Column
If \$Marked = 3 Then
\$cPos = 3
EndIf
If \$Marked = 3 Then
\$cPos = 3
EndIf
If \$Marked = 3 Then
\$cPos = 3
EndIf

; Check diagonals
; Right To left
If \$Marked = 3 Then
\$dPos = 3
EndIf
If \$Marked = 3 Then
\$dPos = 3
EndIf
If \$Marked = 3 Then
\$dPos = 3
EndIf
; Left To Right
If \$Marked = 3 Then
\$dPos = 3
EndIf
If \$Marked = 3 Then
\$dPos = 3
EndIf
If \$Marked = 3 Then
\$dPos = 3
EndIf
EndFunc```

That snippet alone only checks to see if X has been marked.

Here is my \$Marked array:

`Global \$Marked = [2, 2, 2, 2, 2, 2, 2, 2, 2]; 2 = UnMarked | 3 = X | 5 = O`

I need to find away of checking in each row, column and diagonal to see if X or O has been marked then after they have all been checked, the \$*Pos[*] values are then multiplied together. If X(Player) can win then the product would be 18 (3*3*2) 2 = Blank so that means that the AI has to block it with 5 (O). If O(AI) can win

then the product would be 50 (5*5*2) 2 = Blank so that now means its up to the player to block it, otherwise if it's the AI's turn then mark it with 5 and AI wins.

WOW.. confusing I know. If you need any more information, or don't understand me what so ever, just ask me and I shall try and explain again early morning here.

Edit: Just in case anyone needs it, here is the link to the algorithm I am using.

Thank you,

James

Edited by JamesB
##### Share on other sites

Nevermind,

```Func PossibleWin()
If \$intBoard * \$intBoard * \$intBoard = 50 Then
\$r1Win = _ArraySearch(\$intBoard, 2, 1, 3)
Go(\$r1Win)
ElseIf \$intBoard * \$intBoard * \$intBoard = 50 Then
\$r2Win = _ArraySearch(\$intBoard, 2, 4, 6)
Go(\$r2Win)
ElseIf \$intBoard * \$intBoard * \$intBoard = 50 Then
\$r3Win = _ArraySearch(\$intBoard, 2, 7, 9)
Go(\$r3Win)
EndIf
EndFunc```

That does the rows ## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...