Jump to content

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[9], $cPos[9], $dPos[6]
    
    For $i = 0 To $Marked[8]
        MsgBox(0, "", $Marked[$i]); Display the marked values For now
    Next
    
  ; Check rows
  ; First Row
    If $Marked[1] = 3 Then
        $rPos[1] = 3
    EndIf
    If $Marked[2] = 3 Then
        $rPos[2] = 3
    EndIf
    If $Marked[3] = 3 Then
        $rPos[3] = 3
    EndIf
  ; Second Row
    If $Marked[4] = 3 Then
        $rPos[4] = 3
    EndIf
    If $Marked[5] = 3 Then
        $rPos[5] = 3
    EndIf
    If $Marked[6] = 3 Then
        $rPos[6] = 3
    EndIf
  ; Third rows
    If $Marked[7] = 3 Then
        $rPos[7] = 3
    EndIf
    If $Marked[8] = 3 Then
        $rPos[8] = 3
    EndIf
    If $Marked[9] = 3 Then
        $rPos[9] = 3
    EndIf
    
  ; Check Columns
  ; First Column
    If $Marked[1] = 3 Then
        $cPos[1] = 3
    EndIf
    If $Marked[4] = 3 Then
        $cPos[2] = 3
    EndIf
    If $Marked[7] = 3 Then
        $cPos[3] = 3 
    EndIf
  ; Second Column
    If $Marked[2] = 3 Then
        $cPos[4] = 3
    EndIf
    If $Marked[5] = 3 Then
        $cPos[5] = 3
    EndIf
    If $Marked[8] = 3 Then
        $cPos[6] = 3 
    EndIf
  ; Third Column
    If $Marked[3] = 3 Then
        $cPos[7] = 3
    EndIf
    If $Marked[5] = 3 Then
        $cPos[8] = 3
    EndIf
    If $Marked[9] = 3 Then
        $cPos[9] = 3 
    EndIf
    
  ; Check diagonals
  ; Right To left
    If $Marked[1] = 3 Then
        $dPos[1] = 3
    EndIf
    If $Marked[5] = 3 Then
        $dPos[2] = 3
    EndIf
    If $Marked[9] = 3 Then
        $dPos[3] = 3
    EndIf
  ; Left To Right
    If $Marked[3] = 3 Then
        $dPos[4] = 3
    EndIf
    If $Marked[5] = 3 Then
        $dPos[5] = 3
    EndIf
    If $Marked[7] = 3 Then
        $dPos[6] = 3
    EndIf
EndFunc

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

Here is my $Marked array:

Global $Marked[9] = [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
Link to comment
Share on other sites

Nevermind,

Func PossibleWin()
    If $intBoard[0] * $intBoard[1] * $intBoard[2] = 50 Then
        $r1Win = _ArraySearch($intBoard, 2, 1, 3)
        Go($r1Win)
    ElseIf $intBoard[3] * $intBoard[4] * $intBoard[5] = 50 Then
        $r2Win = _ArraySearch($intBoard, 2, 4, 6)
        Go($r2Win)
    ElseIf $intBoard[6] * $intBoard[7] * $intBoard[8] = 50 Then
        $r3Win = _ArraySearch($intBoard, 2, 7, 9)
        Go($r3Win)
    EndIf
EndFunc

That does the rows :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...