home *** CD-ROM | disk | FTP | other *** search
- global NUM_ROWS, NUM_COLS, NUM_DIAGS, NUM_SQUARES, gpvBoardValues, ROW_INDICES_INDEX, COL_INDICES_INDEX, LDIAG_INDICES_INDEX, RDIAG_INDICES_INDEX, gpvMainBoard, gpvDoBoardValidates, gpvP1Moves, gpvP2Moves, gpvLastDiagIndex
-
- on gopvGetDiagLength diagNum
- if diagNum <= NUM_ROWS then
- return diagNum
- else
- return NUM_ROWS + NUM_COLS - diagNum
- end if
- end
-
- on gopvLDiagToNum diagNum, i
- if diagNum <= NUM_ROWS then
- row = diagNum - i + 1
- col = i
- else
- row = NUM_ROWS - i + 1
- col = diagNum - NUM_ROWS + i
- end if
- n = gopvRowColToNum(row, col)
- return n
- end
-
- on gopvRDiagToNum diagNum, i
- if diagNum <= NUM_ROWS then
- row = diagNum - i + 1
- col = NUM_COLS - i + 1
- else
- row = NUM_ROWS - i + 1
- col = NUM_COLS + NUM_ROWS - diagNum - i + 1
- end if
- n = gopvRowColToNum(row, col)
- return n
- end
-
- on gopvfetchLastDiagIndex
- return gpvLastDiagIndex
- end
-
- on gopvRowColToLDiag row, col
- diagNum = row + col - 1
- if diagNum <= NUM_ROWS then
- gpvLastDiagIndex = col
- else
- gpvLastDiagIndex = NUM_ROWS - row + 1
- end if
- return diagNum
- end
-
- on gopvRowColToRDiag row, col
- diagNum = row + NUM_COLS - col
- if diagNum <= NUM_ROWS then
- gpvLastDiagIndex = NUM_COLS - col + 1
- else
- gpvLastDiagIndex = NUM_ROWS - row + 1
- end if
- return diagNum
- end
-
- on bumpIndices theIndices
- i = 1
- keepOn = 1
- repeat while keepOn and (i <= NUM_ROWS)
- keepOn = 0
- theIndices[i] = theIndices[i] + 1
- if theIndices[i] > 2 then
- theIndices[i] = 0
- i = i + 1
- keepOn = 1
- end if
- end repeat
- return i <= NUM_ROWS
- end
-
- on gopvGetMoveOneDir theIndices, pieceType, n, Dir
- otherPiece = 3 - pieceType
- runLength = 0
- Match = 0
- repeat while 1
- n = n + Dir
- if (n < 1) or (n > NUM_ROWS) then
- exit repeat
- next repeat
- end if
- if theIndices[n] = otherPiece then
- runLength = runLength + 1
- next repeat
- end if
- if theIndices[n] = pieceType then
- Match = runLength > 0
- exit repeat
- next repeat
- end if
- exit repeat
- end repeat
- return Match
- end
-
- on gopvGetMoves theIndices, pieceType
- multValue = 1
- retcode = 0
- repeat with i = 1 to NUM_ROWS
- pieceVal = 0
- if theIndices[i] = 0 then
- if gopvGetMoveOneDir(theIndices, pieceType, i, -1) = 1 then
- pieceVal = pieceType
- else
- if gopvGetMoveOneDir(theIndices, pieceType, i, 1) = 1 then
- pieceVal = pieceType
- end if
- end if
- end if
- retcode = retcode + (multValue * pieceVal)
- multValue = multValue * 3
- end repeat
- return retcode
- end
-
- on gopvInitBoards
- ROW_INDICES_INDEX = NUM_SQUARES + 1
- COL_INDICES_INDEX = ROW_INDICES_INDEX + 1
- LDIAG_INDICES_INDEX = COL_INDICES_INDEX + 1
- RDIAG_INDICES_INDEX = LDIAG_INDICES_INDEX + 1
- gpvP1Moves = []
- gpvP2Moves = []
- buildRow = []
- theRange = 1
- repeat with i = 1 to NUM_ROWS
- theRange = 3 * theRange
- append(buildRow, 0)
- end repeat
- keepOn = 1
- curIndex = 1
- repeat while keepOn
- gpvP1Moves[curIndex] = gopvGetMoves(buildRow, 1)
- gpvP2Moves[curIndex] = gopvGetMoves(buildRow, 2)
- keepOn = bumpIndices(buildRow)
- curIndex = curIndex + 1
- end repeat
- gpvDoBoardValidates = 0
- end
-
- on gopvInitBoard boardList
- repeat with i = 1 to NUM_SQUARES
- boardList[i] = 0
- end repeat
- boardList[ROW_INDICES_INDEX] = []
- boardList[COL_INDICES_INDEX] = []
- boardList[LDIAG_INDICES_INDEX] = []
- boardList[RDIAG_INDICES_INDEX] = []
- theRowList = boardList[ROW_INDICES_INDEX]
- repeat with i = 1 to NUM_ROWS
- theRowList[i] = 0
- end repeat
- theColList = boardList[COL_INDICES_INDEX]
- repeat with i = 1 to NUM_COLS
- theColList[i] = 0
- end repeat
- theLDiagList = boardList[LDIAG_INDICES_INDEX]
- repeat with i = 1 to NUM_DIAGS
- theLDiagList[i] = 0
- end repeat
- theRDiagList = boardList[RDIAG_INDICES_INDEX]
- repeat with i = 1 to NUM_DIAGS
- theRDiagList[i] = 0
- end repeat
- end
-
- on gopvUpdateRowIndices boardList, r, c, pieceType
- theRowList = boardList[ROW_INDICES_INDEX]
- theMult = integer(power(3, c - 1))
- theLeft = theMult * 3
- curVal = theRowList[r]
- newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
- theRowList[r] = newVal
- end
-
- on gopvUpdateColIndices boardList, r, c, pieceType
- theColList = boardList[COL_INDICES_INDEX]
- theMult = integer(power(3, r - 1))
- theLeft = theMult * 3
- curVal = theColList[c]
- newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
- theColList[c] = newVal
- end
-
- on gopvUpdateLDiagIndices boardList, r, c, pieceType
- theLDiagList = boardList[LDIAG_INDICES_INDEX]
- diag = gopvRowColToLDiag(r, c)
- n = gopvfetchLastDiagIndex()
- theMult = integer(power(3, n - 1))
- theLeft = theMult * 3
- curVal = theLDiagList[diag]
- newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
- theLDiagList[diag] = newVal
- end
-
- on gopvUpdateRDiagIndices boardList, r, c, pieceType
- theRDiagList = boardList[RDIAG_INDICES_INDEX]
- diag = gopvRowColToRDiag(r, c)
- n = gopvfetchLastDiagIndex()
- theMult = integer(power(3, n - 1))
- theLeft = theMult * 3
- curVal = theRDiagList[diag]
- newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
- theRDiagList[diag] = newVal
- end
-
- on gopvAssignBoard boardList, n, pieceType
- oldPieceType = boardList[n]
- boardList[n] = pieceType
- rowNum = gopvNumToRow(n)
- colNum = gopvNumToCol(n)
- gopvUpdateRowIndices(boardList, rowNum, colNum, pieceType)
- gopvUpdateColIndices(boardList, rowNum, colNum, pieceType)
- gopvUpdateLDiagIndices(boardList, rowNum, colNum, pieceType)
- gopvUpdateRDiagIndices(boardList, rowNum, colNum, pieceType)
- gopvValidateBoard(boardList)
- return oldPieceType
- end
-
- on gopvCopyBoard theOldBoard, theNewBoard
- repeat with i = 1 to NUM_SQUARES
- theNewBoard[i] = theOldBoard[i]
- end repeat
- theOldRowList = theOldBoard[ROW_INDICES_INDEX]
- theNewRowList = theNewBoard[ROW_INDICES_INDEX]
- repeat with i = 1 to NUM_ROWS
- theNewRowList[i] = theOldRowList[i]
- end repeat
- theOldColList = theOldBoard[COL_INDICES_INDEX]
- theNewColList = theNewBoard[COL_INDICES_INDEX]
- repeat with i = 1 to NUM_COLS
- theNewColList[i] = theOldColList[i]
- end repeat
- theOldLDiagList = theOldBoard[LDIAG_INDICES_INDEX]
- theNewLDiagList = theNewBoard[LDIAG_INDICES_INDEX]
- repeat with i = 1 to NUM_DIAGS
- theNewLDiagList[i] = theOldLDiagList[i]
- end repeat
- theOldRDiagList = theOldBoard[RDIAG_INDICES_INDEX]
- theNewRDiagList = theNewBoard[RDIAG_INDICES_INDEX]
- repeat with i = 1 to NUM_DIAGS
- theNewRDiagList[i] = theOldRDiagList[i]
- end repeat
- gopvValidateBoard(theNewBoard)
- end
-
- on gopvDrawBoard theBoard, highlightNum
- gopvGetBoardValues(theBoard)
- pieceStr = [".", "*", "X"]
- put " ------------------------"
- repeat with row = 1 to NUM_ROWS
- rowStr = " |"
- repeat with col = 1 to NUM_COLS
- n = gopvRowColToNum(row, col)
- if n = highlightNum then
- divStr = "+"
- else
- divStr = " "
- end if
- rowStr = rowStr & divStr & pieceStr[theBoard[n] + 1] & abs(gpvBoardValues[n])
- end repeat
- put rowStr & "|"
- end repeat
- put " ------------------------"
- put
- end
-
- on gopvValidateBoard theBoard
- if gpvDoBoardValidates then
- numErrs = 0
- theRowIndices = theBoard[ROW_INDICES_INDEX]
- repeat with row = 1 to NUM_ROWS
- val = theRowIndices[row]
- repeat with col = 1 to NUM_COLS
- thisOne = val mod 3
- val = val / 3
- n = gopvRowColToNum(row, col)
- if thisOne <> theBoard[n] then
- put "Validate Row Error on (Row, Col)" && row && col && theRowIndices
- numErrs = numErrs + 1
- end if
- end repeat
- end repeat
- theColIndices = theBoard[COL_INDICES_INDEX]
- repeat with col = 1 to NUM_COLS
- val = theColIndices[col]
- repeat with row = 1 to NUM_ROWS
- thisOne = val mod 3
- val = val / 3
- n = gopvRowColToNum(row, col)
- if thisOne <> theBoard[n] then
- put "Validate Col Error on (Row, Col)" && row && col && theColIndices
- numErrs = numErrs + 1
- end if
- end repeat
- end repeat
- theLDiagIndices = theBoard[LDIAG_INDICES_INDEX]
- repeat with diag = 1 to NUM_DIAGS
- val = theLDiagIndices[diag]
- num = gopvGetDiagLength(diag)
- repeat with i = 1 to num
- thisOne = val mod 3
- val = val / 3
- n = gopvLDiagToNum(diag, i)
- if thisOne <> theBoard[n] then
- put "Validate LDiag Error on (Diag, i)" && diag && i && theLDiagIndices
- numErrs = numErrs + 1
- end if
- end repeat
- end repeat
- theRDiagIndices = theBoard[RDIAG_INDICES_INDEX]
- repeat with diag = 1 to NUM_DIAGS
- val = theRDiagIndices[diag]
- num = gopvGetDiagLength(diag)
- repeat with i = 1 to num
- thisOne = val mod 3
- val = val / 3
- n = gopvRDiagToNum(diag, i)
- if thisOne <> theBoard[n] then
- put "Validate RDiag Error on (Diag, i)" && diag && i && theLDiagIndices
- numErrs = numErrs + 1
- end if
- end repeat
- end repeat
- if numErrs > 0 then
- gopvDrawBoard(theBoard, -1)
- end if
- end if
- end
-
- on gopvIndicesToStr val
- theStr = "["
- repeat with i = 1 to NUM_ROWS
- thisOne = val mod 3
- val = val / 3
- if thisOne = 0 then
- theStr = theStr && "."
- next repeat
- end if
- if thisOne = 1 then
- theStr = theStr && "*"
- next repeat
- end if
- if thisOne = 2 then
- theStr = theStr && "X"
- end if
- end repeat
- theStr = theStr && "]"
- return theStr
- end
-
- on gopvAddColsToList row, val, movesList
- repeat with i = 1 to NUM_COLS
- thisOne = val mod 3
- val = val / 3
- if thisOne <> 0 then
- n = gopvRowColToNum(row, i)
- if movesList.getPos(n) = 0 then
- append(movesList, n)
- end if
- end if
- end repeat
- end
-
- on gopvAddRowsToList col, val, movesList
- repeat with i = 1 to NUM_ROWS
- thisOne = val mod 3
- val = val / 3
- if thisOne <> 0 then
- n = gopvRowColToNum(i, col)
- if movesList.getPos(n) = 0 then
- append(movesList, n)
- end if
- end if
- end repeat
- end
-
- on gopvAddLDiagToList diag, val, movesList
- num = gopvGetDiagLength(diag)
- repeat with i = 1 to num
- thisOne = val mod 3
- val = val / 3
- if thisOne <> 0 then
- n = gopvLDiagToNum(diag, i)
- if movesList.getPos(n) = 0 then
- append(movesList, n)
- end if
- end if
- end repeat
- end
-
- on gopvAddRDiagToList diag, val, movesList
- num = gopvGetDiagLength(diag)
- repeat with i = 1 to num
- thisOne = val mod 3
- val = val / 3
- if thisOne <> 0 then
- n = gopvRDiagToNum(diag, i)
- if movesList.getPos(n) = 0 then
- append(movesList, n)
- end if
- end if
- end repeat
- end
-
- on gopvNewNumPossibleMoves pieceType, boardList, movesList
- theRowIndices = boardList[ROW_INDICES_INDEX]
- theColIndices = boardList[COL_INDICES_INDEX]
- theLDiagIndices = boardList[LDIAG_INDICES_INDEX]
- theRDiagIndices = boardList[RDIAG_INDICES_INDEX]
- if pieceType = 1 then
- movesTable = gpvP1Moves
- else
- movesTable = gpvP2Moves
- end if
- repeat with i = 1 to NUM_ROWS
- rowIndex = theRowIndices[i] + 1
- rowMoves = movesTable[rowIndex]
- if rowMoves <> 0 then
- gopvAddColsToList(i, rowMoves, movesList)
- end if
- colIndex = theColIndices[i] + 1
- colMoves = movesTable[colIndex]
- if colMoves <> 0 then
- gopvAddRowsToList(i, colMoves, movesList)
- end if
- end repeat
- repeat with i = 1 to NUM_DIAGS
- ldiagIndex = theLDiagIndices[i] + 1
- lDiagMoves = movesTable[ldiagIndex]
- if lDiagMoves <> 0 then
- gopvAddLDiagToList(i, lDiagMoves, movesList)
- end if
- rdiagIndex = theRDiagIndices[i] + 1
- rDiagMoves = movesTable[rdiagIndex]
- if rDiagMoves <> 0 then
- gopvAddRDiagToList(i, rDiagMoves, movesList)
- end if
- end repeat
- return movesList.count()
- end
-
- on gopvNumToDiag squareNum
- end
-
- on gopvBuildRowIndex boardList, rowNum
- multValue = 1
- retcode = 0
- n = gopvRowColToNum(rowNum, 1)
- repeat with i = 1 to NUM_ROWS
- retcode = retcode + (multValue * boardList[n])
- multValue = multValue * 3
- n = n + 1
- end repeat
- return retcode
- end
-
- on gopvBuildColIndex boardList, colNum
- multValue = 1
- retcode = 0
- n = gopvRowColToNum(1, colNum)
- repeat with i = 1 to NUM_COLS
- retcode = retcode + (multValue * boardList[n])
- multValue = multValue * 3
- n = n + NUM_COLS
- end repeat
- return retcode
- end
-
- on gopvXCheckValidMoveEx squareNum, pieceType, boardList
- retcode = 0
- if boardList[squareNum] = 0 then
- row = gopvNumToRow(squareNum)
- col = gopvNumToCol(squareNum)
- diag = gopvNumToDiag(squareNum)
- rowIndex = gopvBuildRowIndex(boardList, row)
- colIndex = gopvBuildColIndex(boardList, col)
- repeat with Dir = 1 to 8
- if gopvTrapsOpponentEx(pieceType, boardList, row, col, Dir) > 0 then
- retcode = 1
- exit repeat
- end if
- end repeat
- end if
- return retcode
- end
-