home *** CD-ROM | disk | FTP | other *** search
/ Enter 2003: The Beautiful Scenery / enter-parhaat-2003.iso / files / frendz.exe / frendz.dcr / Scripts_53_Boards.ls < prev    next >
Encoding:
Text File  |  2002-12-31  |  12.7 KB  |  488 lines

  1. 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
  2.  
  3. on gopvGetDiagLength diagNum
  4.   if diagNum <= NUM_ROWS then
  5.     return diagNum
  6.   else
  7.     return NUM_ROWS + NUM_COLS - diagNum
  8.   end if
  9. end
  10.  
  11. on gopvLDiagToNum diagNum, i
  12.   if diagNum <= NUM_ROWS then
  13.     row = diagNum - i + 1
  14.     col = i
  15.   else
  16.     row = NUM_ROWS - i + 1
  17.     col = diagNum - NUM_ROWS + i
  18.   end if
  19.   n = gopvRowColToNum(row, col)
  20.   return n
  21. end
  22.  
  23. on gopvRDiagToNum diagNum, i
  24.   if diagNum <= NUM_ROWS then
  25.     row = diagNum - i + 1
  26.     col = NUM_COLS - i + 1
  27.   else
  28.     row = NUM_ROWS - i + 1
  29.     col = NUM_COLS + NUM_ROWS - diagNum - i + 1
  30.   end if
  31.   n = gopvRowColToNum(row, col)
  32.   return n
  33. end
  34.  
  35. on gopvfetchLastDiagIndex
  36.   return gpvLastDiagIndex
  37. end
  38.  
  39. on gopvRowColToLDiag row, col
  40.   diagNum = row + col - 1
  41.   if diagNum <= NUM_ROWS then
  42.     gpvLastDiagIndex = col
  43.   else
  44.     gpvLastDiagIndex = NUM_ROWS - row + 1
  45.   end if
  46.   return diagNum
  47. end
  48.  
  49. on gopvRowColToRDiag row, col
  50.   diagNum = row + NUM_COLS - col
  51.   if diagNum <= NUM_ROWS then
  52.     gpvLastDiagIndex = NUM_COLS - col + 1
  53.   else
  54.     gpvLastDiagIndex = NUM_ROWS - row + 1
  55.   end if
  56.   return diagNum
  57. end
  58.  
  59. on bumpIndices theIndices
  60.   i = 1
  61.   keepOn = 1
  62.   repeat while keepOn and (i <= NUM_ROWS)
  63.     keepOn = 0
  64.     theIndices[i] = theIndices[i] + 1
  65.     if theIndices[i] > 2 then
  66.       theIndices[i] = 0
  67.       i = i + 1
  68.       keepOn = 1
  69.     end if
  70.   end repeat
  71.   return i <= NUM_ROWS
  72. end
  73.  
  74. on gopvGetMoveOneDir theIndices, pieceType, n, Dir
  75.   otherPiece = 3 - pieceType
  76.   runLength = 0
  77.   Match = 0
  78.   repeat while 1
  79.     n = n + Dir
  80.     if (n < 1) or (n > NUM_ROWS) then
  81.       exit repeat
  82.       next repeat
  83.     end if
  84.     if theIndices[n] = otherPiece then
  85.       runLength = runLength + 1
  86.       next repeat
  87.     end if
  88.     if theIndices[n] = pieceType then
  89.       Match = runLength > 0
  90.       exit repeat
  91.       next repeat
  92.     end if
  93.     exit repeat
  94.   end repeat
  95.   return Match
  96. end
  97.  
  98. on gopvGetMoves theIndices, pieceType
  99.   multValue = 1
  100.   retcode = 0
  101.   repeat with i = 1 to NUM_ROWS
  102.     pieceVal = 0
  103.     if theIndices[i] = 0 then
  104.       if gopvGetMoveOneDir(theIndices, pieceType, i, -1) = 1 then
  105.         pieceVal = pieceType
  106.       else
  107.         if gopvGetMoveOneDir(theIndices, pieceType, i, 1) = 1 then
  108.           pieceVal = pieceType
  109.         end if
  110.       end if
  111.     end if
  112.     retcode = retcode + (multValue * pieceVal)
  113.     multValue = multValue * 3
  114.   end repeat
  115.   return retcode
  116. end
  117.  
  118. on gopvInitBoards
  119.   ROW_INDICES_INDEX = NUM_SQUARES + 1
  120.   COL_INDICES_INDEX = ROW_INDICES_INDEX + 1
  121.   LDIAG_INDICES_INDEX = COL_INDICES_INDEX + 1
  122.   RDIAG_INDICES_INDEX = LDIAG_INDICES_INDEX + 1
  123.   gpvP1Moves = []
  124.   gpvP2Moves = []
  125.   buildRow = []
  126.   theRange = 1
  127.   repeat with i = 1 to NUM_ROWS
  128.     theRange = 3 * theRange
  129.     append(buildRow, 0)
  130.   end repeat
  131.   keepOn = 1
  132.   curIndex = 1
  133.   repeat while keepOn
  134.     gpvP1Moves[curIndex] = gopvGetMoves(buildRow, 1)
  135.     gpvP2Moves[curIndex] = gopvGetMoves(buildRow, 2)
  136.     keepOn = bumpIndices(buildRow)
  137.     curIndex = curIndex + 1
  138.   end repeat
  139.   gpvDoBoardValidates = 0
  140. end
  141.  
  142. on gopvInitBoard boardList
  143.   repeat with i = 1 to NUM_SQUARES
  144.     boardList[i] = 0
  145.   end repeat
  146.   boardList[ROW_INDICES_INDEX] = []
  147.   boardList[COL_INDICES_INDEX] = []
  148.   boardList[LDIAG_INDICES_INDEX] = []
  149.   boardList[RDIAG_INDICES_INDEX] = []
  150.   theRowList = boardList[ROW_INDICES_INDEX]
  151.   repeat with i = 1 to NUM_ROWS
  152.     theRowList[i] = 0
  153.   end repeat
  154.   theColList = boardList[COL_INDICES_INDEX]
  155.   repeat with i = 1 to NUM_COLS
  156.     theColList[i] = 0
  157.   end repeat
  158.   theLDiagList = boardList[LDIAG_INDICES_INDEX]
  159.   repeat with i = 1 to NUM_DIAGS
  160.     theLDiagList[i] = 0
  161.   end repeat
  162.   theRDiagList = boardList[RDIAG_INDICES_INDEX]
  163.   repeat with i = 1 to NUM_DIAGS
  164.     theRDiagList[i] = 0
  165.   end repeat
  166. end
  167.  
  168. on gopvUpdateRowIndices boardList, r, c, pieceType
  169.   theRowList = boardList[ROW_INDICES_INDEX]
  170.   theMult = integer(power(3, c - 1))
  171.   theLeft = theMult * 3
  172.   curVal = theRowList[r]
  173.   newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
  174.   theRowList[r] = newVal
  175. end
  176.  
  177. on gopvUpdateColIndices boardList, r, c, pieceType
  178.   theColList = boardList[COL_INDICES_INDEX]
  179.   theMult = integer(power(3, r - 1))
  180.   theLeft = theMult * 3
  181.   curVal = theColList[c]
  182.   newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
  183.   theColList[c] = newVal
  184. end
  185.  
  186. on gopvUpdateLDiagIndices boardList, r, c, pieceType
  187.   theLDiagList = boardList[LDIAG_INDICES_INDEX]
  188.   diag = gopvRowColToLDiag(r, c)
  189.   n = gopvfetchLastDiagIndex()
  190.   theMult = integer(power(3, n - 1))
  191.   theLeft = theMult * 3
  192.   curVal = theLDiagList[diag]
  193.   newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
  194.   theLDiagList[diag] = newVal
  195. end
  196.  
  197. on gopvUpdateRDiagIndices boardList, r, c, pieceType
  198.   theRDiagList = boardList[RDIAG_INDICES_INDEX]
  199.   diag = gopvRowColToRDiag(r, c)
  200.   n = gopvfetchLastDiagIndex()
  201.   theMult = integer(power(3, n - 1))
  202.   theLeft = theMult * 3
  203.   curVal = theRDiagList[diag]
  204.   newVal = (curVal / theLeft * theLeft) + (theMult * pieceType) + (curVal mod theMult)
  205.   theRDiagList[diag] = newVal
  206. end
  207.  
  208. on gopvAssignBoard boardList, n, pieceType
  209.   oldPieceType = boardList[n]
  210.   boardList[n] = pieceType
  211.   rowNum = gopvNumToRow(n)
  212.   colNum = gopvNumToCol(n)
  213.   gopvUpdateRowIndices(boardList, rowNum, colNum, pieceType)
  214.   gopvUpdateColIndices(boardList, rowNum, colNum, pieceType)
  215.   gopvUpdateLDiagIndices(boardList, rowNum, colNum, pieceType)
  216.   gopvUpdateRDiagIndices(boardList, rowNum, colNum, pieceType)
  217.   gopvValidateBoard(boardList)
  218.   return oldPieceType
  219. end
  220.  
  221. on gopvCopyBoard theOldBoard, theNewBoard
  222.   repeat with i = 1 to NUM_SQUARES
  223.     theNewBoard[i] = theOldBoard[i]
  224.   end repeat
  225.   theOldRowList = theOldBoard[ROW_INDICES_INDEX]
  226.   theNewRowList = theNewBoard[ROW_INDICES_INDEX]
  227.   repeat with i = 1 to NUM_ROWS
  228.     theNewRowList[i] = theOldRowList[i]
  229.   end repeat
  230.   theOldColList = theOldBoard[COL_INDICES_INDEX]
  231.   theNewColList = theNewBoard[COL_INDICES_INDEX]
  232.   repeat with i = 1 to NUM_COLS
  233.     theNewColList[i] = theOldColList[i]
  234.   end repeat
  235.   theOldLDiagList = theOldBoard[LDIAG_INDICES_INDEX]
  236.   theNewLDiagList = theNewBoard[LDIAG_INDICES_INDEX]
  237.   repeat with i = 1 to NUM_DIAGS
  238.     theNewLDiagList[i] = theOldLDiagList[i]
  239.   end repeat
  240.   theOldRDiagList = theOldBoard[RDIAG_INDICES_INDEX]
  241.   theNewRDiagList = theNewBoard[RDIAG_INDICES_INDEX]
  242.   repeat with i = 1 to NUM_DIAGS
  243.     theNewRDiagList[i] = theOldRDiagList[i]
  244.   end repeat
  245.   gopvValidateBoard(theNewBoard)
  246. end
  247.  
  248. on gopvDrawBoard theBoard, highlightNum
  249.   gopvGetBoardValues(theBoard)
  250.   pieceStr = [".", "*", "X"]
  251.   put "     ------------------------"
  252.   repeat with row = 1 to NUM_ROWS
  253.     rowStr = "    |"
  254.     repeat with col = 1 to NUM_COLS
  255.       n = gopvRowColToNum(row, col)
  256.       if n = highlightNum then
  257.         divStr = "+"
  258.       else
  259.         divStr = " "
  260.       end if
  261.       rowStr = rowStr & divStr & pieceStr[theBoard[n] + 1] & abs(gpvBoardValues[n])
  262.     end repeat
  263.     put rowStr & "|"
  264.   end repeat
  265.   put "     ------------------------"
  266.   put 
  267. end
  268.  
  269. on gopvValidateBoard theBoard
  270.   if gpvDoBoardValidates then
  271.     numErrs = 0
  272.     theRowIndices = theBoard[ROW_INDICES_INDEX]
  273.     repeat with row = 1 to NUM_ROWS
  274.       val = theRowIndices[row]
  275.       repeat with col = 1 to NUM_COLS
  276.         thisOne = val mod 3
  277.         val = val / 3
  278.         n = gopvRowColToNum(row, col)
  279.         if thisOne <> theBoard[n] then
  280.           put "Validate Row Error on (Row, Col)" && row && col && theRowIndices
  281.           numErrs = numErrs + 1
  282.         end if
  283.       end repeat
  284.     end repeat
  285.     theColIndices = theBoard[COL_INDICES_INDEX]
  286.     repeat with col = 1 to NUM_COLS
  287.       val = theColIndices[col]
  288.       repeat with row = 1 to NUM_ROWS
  289.         thisOne = val mod 3
  290.         val = val / 3
  291.         n = gopvRowColToNum(row, col)
  292.         if thisOne <> theBoard[n] then
  293.           put "Validate Col Error on (Row, Col)" && row && col && theColIndices
  294.           numErrs = numErrs + 1
  295.         end if
  296.       end repeat
  297.     end repeat
  298.     theLDiagIndices = theBoard[LDIAG_INDICES_INDEX]
  299.     repeat with diag = 1 to NUM_DIAGS
  300.       val = theLDiagIndices[diag]
  301.       num = gopvGetDiagLength(diag)
  302.       repeat with i = 1 to num
  303.         thisOne = val mod 3
  304.         val = val / 3
  305.         n = gopvLDiagToNum(diag, i)
  306.         if thisOne <> theBoard[n] then
  307.           put "Validate LDiag Error on (Diag, i)" && diag && i && theLDiagIndices
  308.           numErrs = numErrs + 1
  309.         end if
  310.       end repeat
  311.     end repeat
  312.     theRDiagIndices = theBoard[RDIAG_INDICES_INDEX]
  313.     repeat with diag = 1 to NUM_DIAGS
  314.       val = theRDiagIndices[diag]
  315.       num = gopvGetDiagLength(diag)
  316.       repeat with i = 1 to num
  317.         thisOne = val mod 3
  318.         val = val / 3
  319.         n = gopvRDiagToNum(diag, i)
  320.         if thisOne <> theBoard[n] then
  321.           put "Validate RDiag Error on (Diag, i)" && diag && i && theLDiagIndices
  322.           numErrs = numErrs + 1
  323.         end if
  324.       end repeat
  325.     end repeat
  326.     if numErrs > 0 then
  327.       gopvDrawBoard(theBoard, -1)
  328.     end if
  329.   end if
  330. end
  331.  
  332. on gopvIndicesToStr val
  333.   theStr = "["
  334.   repeat with i = 1 to NUM_ROWS
  335.     thisOne = val mod 3
  336.     val = val / 3
  337.     if thisOne = 0 then
  338.       theStr = theStr && "."
  339.       next repeat
  340.     end if
  341.     if thisOne = 1 then
  342.       theStr = theStr && "*"
  343.       next repeat
  344.     end if
  345.     if thisOne = 2 then
  346.       theStr = theStr && "X"
  347.     end if
  348.   end repeat
  349.   theStr = theStr && "]"
  350.   return theStr
  351. end
  352.  
  353. on gopvAddColsToList row, val, movesList
  354.   repeat with i = 1 to NUM_COLS
  355.     thisOne = val mod 3
  356.     val = val / 3
  357.     if thisOne <> 0 then
  358.       n = gopvRowColToNum(row, i)
  359.       if movesList.getPos(n) = 0 then
  360.         append(movesList, n)
  361.       end if
  362.     end if
  363.   end repeat
  364. end
  365.  
  366. on gopvAddRowsToList col, val, movesList
  367.   repeat with i = 1 to NUM_ROWS
  368.     thisOne = val mod 3
  369.     val = val / 3
  370.     if thisOne <> 0 then
  371.       n = gopvRowColToNum(i, col)
  372.       if movesList.getPos(n) = 0 then
  373.         append(movesList, n)
  374.       end if
  375.     end if
  376.   end repeat
  377. end
  378.  
  379. on gopvAddLDiagToList diag, val, movesList
  380.   num = gopvGetDiagLength(diag)
  381.   repeat with i = 1 to num
  382.     thisOne = val mod 3
  383.     val = val / 3
  384.     if thisOne <> 0 then
  385.       n = gopvLDiagToNum(diag, i)
  386.       if movesList.getPos(n) = 0 then
  387.         append(movesList, n)
  388.       end if
  389.     end if
  390.   end repeat
  391. end
  392.  
  393. on gopvAddRDiagToList diag, val, movesList
  394.   num = gopvGetDiagLength(diag)
  395.   repeat with i = 1 to num
  396.     thisOne = val mod 3
  397.     val = val / 3
  398.     if thisOne <> 0 then
  399.       n = gopvRDiagToNum(diag, i)
  400.       if movesList.getPos(n) = 0 then
  401.         append(movesList, n)
  402.       end if
  403.     end if
  404.   end repeat
  405. end
  406.  
  407. on gopvNewNumPossibleMoves pieceType, boardList, movesList
  408.   theRowIndices = boardList[ROW_INDICES_INDEX]
  409.   theColIndices = boardList[COL_INDICES_INDEX]
  410.   theLDiagIndices = boardList[LDIAG_INDICES_INDEX]
  411.   theRDiagIndices = boardList[RDIAG_INDICES_INDEX]
  412.   if pieceType = 1 then
  413.     movesTable = gpvP1Moves
  414.   else
  415.     movesTable = gpvP2Moves
  416.   end if
  417.   repeat with i = 1 to NUM_ROWS
  418.     rowIndex = theRowIndices[i] + 1
  419.     rowMoves = movesTable[rowIndex]
  420.     if rowMoves <> 0 then
  421.       gopvAddColsToList(i, rowMoves, movesList)
  422.     end if
  423.     colIndex = theColIndices[i] + 1
  424.     colMoves = movesTable[colIndex]
  425.     if colMoves <> 0 then
  426.       gopvAddRowsToList(i, colMoves, movesList)
  427.     end if
  428.   end repeat
  429.   repeat with i = 1 to NUM_DIAGS
  430.     ldiagIndex = theLDiagIndices[i] + 1
  431.     lDiagMoves = movesTable[ldiagIndex]
  432.     if lDiagMoves <> 0 then
  433.       gopvAddLDiagToList(i, lDiagMoves, movesList)
  434.     end if
  435.     rdiagIndex = theRDiagIndices[i] + 1
  436.     rDiagMoves = movesTable[rdiagIndex]
  437.     if rDiagMoves <> 0 then
  438.       gopvAddRDiagToList(i, rDiagMoves, movesList)
  439.     end if
  440.   end repeat
  441.   return movesList.count()
  442. end
  443.  
  444. on gopvNumToDiag squareNum
  445. end
  446.  
  447. on gopvBuildRowIndex boardList, rowNum
  448.   multValue = 1
  449.   retcode = 0
  450.   n = gopvRowColToNum(rowNum, 1)
  451.   repeat with i = 1 to NUM_ROWS
  452.     retcode = retcode + (multValue * boardList[n])
  453.     multValue = multValue * 3
  454.     n = n + 1
  455.   end repeat
  456.   return retcode
  457. end
  458.  
  459. on gopvBuildColIndex boardList, colNum
  460.   multValue = 1
  461.   retcode = 0
  462.   n = gopvRowColToNum(1, colNum)
  463.   repeat with i = 1 to NUM_COLS
  464.     retcode = retcode + (multValue * boardList[n])
  465.     multValue = multValue * 3
  466.     n = n + NUM_COLS
  467.   end repeat
  468.   return retcode
  469. end
  470.  
  471. on gopvXCheckValidMoveEx squareNum, pieceType, boardList
  472.   retcode = 0
  473.   if boardList[squareNum] = 0 then
  474.     row = gopvNumToRow(squareNum)
  475.     col = gopvNumToCol(squareNum)
  476.     diag = gopvNumToDiag(squareNum)
  477.     rowIndex = gopvBuildRowIndex(boardList, row)
  478.     colIndex = gopvBuildColIndex(boardList, col)
  479.     repeat with Dir = 1 to 8
  480.       if gopvTrapsOpponentEx(pieceType, boardList, row, col, Dir) > 0 then
  481.         retcode = 1
  482.         exit repeat
  483.       end if
  484.     end repeat
  485.   end if
  486.   return retcode
  487. end
  488.