Advertisement
Guest User

ta

a guest
Dec 17th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var boardHeight = 12;
  2. var boardWidth = 6;
  3. var numberColors = 5;
  4. var cellSize = 25; // size of the blocks in pixels
  5.  
  6. var boardArray = new Array(); // array of the colours on the board
  7. var clearArray = new Array(); // array to check which from which direction blocks are cleared
  8. var clearX = new Array(); // keeps track of block's X value which need clearing, but only after *all* the checking!
  9. var clearY = new Array(); // ditto for the blocks corresponding Y value
  10. var dropArray = new Array(); // which columns have had items cleared/dropped (1 = something cleared, 0 = nothing in this column)
  11. var heightArray = new Array(); // height of first empty in column
  12. var colorArray = new Array('#ffcc00','#ff3300','#0066cc','#009966','#9999cc','#990099','#999999');
  13. var speedArray = new Array(3000,2900,2800,2700,2600,2500,2400,2300,2200,2000);
  14.  
  15. var gameScore = 0;
  16. var gameLevel = 0; // game level from 0 - 10, affects
  17. var levelUp = 1000; // number of marks needed to level up
  18. var upRate = speedArray[gameLevel]; // time between automatic lowering of block in milliseconds
  19.  
  20. var clearedBlocks = 0;
  21. var Score = 0; // more running score for a particular drop than successive score, but want to keep similar names!
  22. var sDrops = 0; // successive drops (to calculate a multiplier)
  23.  
  24. var stillPlaying = 1; // check if game over, then stops you from moving the last block at the top from side to side!
  25. var needDropping = 0; // flags off if there are enough in a row to call the function to drop cells down
  26. var cursorX = 3;
  27. var cursorY = 5;
  28.  
  29. var nextArray = new Array(); // next row of blocks
  30. var blockColor = null; // used to check if any blocks need clearing
  31.  
  32. // sets up the board to start the game...
  33.  
  34. function mainBoard() {
  35.     "use strict";
  36.     document.writeln('<div id="board" style="position:absolute; left:50; top:50; z-index:2; border: 1px none #000000">');
  37.     document.writeln('<div id="cursor" style="position:absolute; left:0; top:0; z-index:1; border: 1px none #000000"><img src="gfx/cursor.gif"></div>');
  38.    
  39.     document.writeln('<table border="0" cellspacing="0" cellpadding="0"><tr>');
  40.     document.writeln('<td>');
  41.     initBoard();
  42.     document.writeln('</td>');
  43.     document.writeln('<td width="30">&nbsp;</td>');
  44.     document.writeln('<td valign="top">');
  45.    
  46.     document.writeln('<table border="0" cellspacing="1" cellpadding="10" bgcolor="#666666">');
  47.     document.writeln('<tr bgcolor="#dddddd">');
  48.     document.writeln('<td>Level</td><td><input type="text" name="levelText" value="0" size="3"></td>');
  49.     document.writeln('</tr>');
  50.     document.writeln('<tr bgcolor="#dddddd"><td>Score</td><td><input type="text" name="scoreText" value="0" size="3"></td></tr>');
  51.     document.writeln('<tr bgcolor="#dddddd"><td>Blocks</td><td><input type="text" name="blocksText" value="0" size="3"></td></tr>');
  52.     document.writeln('<tr bgcolor="#dddddd"><td colspan="2"><div align="center"><input type="button" value="Restart" onClick="location.reload()"></div></td></tr>');
  53.     document.writeln('</table>');
  54.    
  55.     document.writeln('</td>');
  56.     document.writeln('</tr></table>');
  57.    
  58.     document.writeln('</div>');
  59.  
  60. // just make sure you don't start with 3 in a row!
  61.  
  62.     for (var i=0; i<2; ++i) {
  63.         nextArray[i] = colorArray[Math.floor(Math.random()*numberColors)]
  64.     }
  65.     var nextColor = colorArray[Math.floor(Math.random()*numberColors)]
  66.     for (var i=2;i<boardWidth;++i) {
  67.         while(nextColor == nextArray[i-1] && nextColor == nextArray[i-2])
  68.             nextColor = colorArray[Math.floor(Math.random()*numberColors)];
  69.               nextArray[i] = nextColor;
  70.     }
  71.     updateCursor();
  72.     updateScores();
  73.     newBlock();
  74. }
  75.  
  76.  
  77.  
  78. function initBoard() {
  79.     for (i=0;i<boardWidth;++i) {
  80.         boardArray[i] = new Array()
  81.         clearArray[i] = new Array()
  82.         dropArray[i] = 0
  83.         heightArray[i] = 0
  84.         for (j=0;j<boardHeight;++j) {
  85.             boardArray[i][j] = '#ffffff'
  86.             clearArray[i][j] = new Array(0,0)
  87.         }
  88.     }
  89.     document.writeln('<table cellspacing="1" border="0" cellpadding="0" bgcolor="#dddddd">')   
  90.     for (i=0;i<boardHeight;++i) {
  91.         document.writeln('<tr>')
  92.         for (j=0;j<boardWidth;++j) {
  93.             document.writeln('<td id="cell' + cellNum(j,boardHeight-1-i) + '" bgcolor="#ffffff"><img src="/media/null.gif" width="' + cellSize + '" height="' + cellSize + '"></td>')
  94.         }
  95.         document.writeln('</tr>')
  96.     }
  97.     document.writeln('</table>')
  98.     document.writeln('<br>Next:<br><table cellspacing="1" border="0" cellpadding="0" bgcolor="#dddddd"><tr>')
  99.     for (i=0;i<boardWidth;++i) document.writeln('<td id="next' + i + '" bgcolor="#ffffff"><img src="/media/null.gif" width="' + cellSize + '" height="' + cellSize + '"></td>')
  100.     document.writeln('</tr></table>')
  101. }
  102.  
  103. // used to calculated a 4-digit number for each cell, e.g. (2,3) becomes 0203, (4,12) becomes 0412
  104.  
  105. function cellNum(x,y) {
  106.     var nameStr = ""
  107.     if (x<10) nameStr = "0"
  108.     nameStr += x
  109.     if (y<10) nameStr += "0"
  110.     nameStr += y
  111.     return nameStr
  112. }
  113.  
  114. function updateCursor() {
  115.     document.all.cursor.style.left = (cursorX)*(cellSize+1)-1
  116.     document.all.cursor.style.top = (boardHeight-cursorY-1)*(cellSize+1)-1
  117. }  
  118.  
  119. // shuffle blocks up to make way for a new 'next' row
  120. // the new 'next' row when chosen shouldn't complete a row of 3 horizontally or vertically
  121.  
  122. function newBlock() {
  123.     for (i=0;i<boardWidth;++i) {
  124.     if (heightArray[i] == boardHeight) { endGame(); return false }
  125.         for (j=heightArray[i]-1;j>=0;--j) {
  126.             colorBelow = boardArray[i][j]
  127.             boardArray[i][j+1] = colorBelow
  128.             eval('document.all.cell' + cellNum(i,j+1) + '.style.backgroundColor = "' + colorBelow + '"')
  129.         }      
  130.         boardArray[i][0] = nextArray[i]
  131.         eval('document.all.cell' + cellNum(i,0) + '.style.backgroundColor = "' + nextArray[i] + '"')
  132.  
  133.         var nextColor = colorArray[Math.floor(Math.random()*numberColors)]
  134.         if (i > 1) {
  135.             while((nextColor == boardArray[i][0] && nextColor == boardArray[i][1]) || (nextColor == nextArray[i-1] && nextColor == nextArray[i-2])) nextColor = colorArray[Math.floor(Math.random()*numberColors)]
  136.         } else {
  137.             while (nextColor == boardArray[i][0] && nextColor == boardArray[i][1]) nextColor = colorArray[Math.floor(Math.random()*numberColors)]
  138.         }
  139.         nextArray[i] = nextColor
  140.         eval('document.all.next' + i + '.style.backgroundColor = "' + nextArray[i] + '"')
  141.         heightArray[i] += 1
  142.     }
  143.     if (cursorY < boardHeight-1) cursorY += 1
  144.     updateCursor()
  145.     for (i=0;i<boardWidth;++i) checkIndiv(i,0)
  146.     if (needDropping == 1) {
  147.         dropCells()
  148.         updateScores()
  149.     }
  150.     keepMoving = setTimeout('newBlock()', upRate)
  151. }
  152. document.onkeypress = handler;
  153. function handler(e) {
  154.     if (stillPlaying ==0) return
  155.   if (document.all) e = window.event
  156.   var key;
  157.   if (document.layers) key = e.which
  158.   if (document.all) key = e.keyCode
  159.     var chr = String.fromCharCode(key)
  160.     if (chr == "l") endGame()
  161.     if (chr == "w" && cursorY < boardHeight-1) cursorY += 1
  162.     if (chr == "s" && cursorY > 0) cursorY -= 1
  163.     if (chr == "a" && cursorX > 0) cursorX -= 1
  164.     if (chr == "d" && cursorX < boardWidth - 2) cursorX += 1
  165.  
  166.     if (chr == " ") {
  167.         needDropping = 0
  168.         var b1 = boardArray[cursorX][cursorY]
  169.         var b2 = boardArray[cursorX+1][cursorY]
  170.         if (b1 == '#ffffff' && b2 == '#ffffff') return
  171.         boardArray[cursorX][cursorY] = b2
  172.         boardArray[cursorX+1][cursorY] = b1
  173.  
  174. // ew! bit messy this bit.. if either block is a blank(white) then both columns need dropping (removeGaps()), then
  175. // depending on whether it was the coloured or the white block, you choose the correct block to use checkIndiv() on.
  176.  
  177.         if (b1 == '#ffffff' || b2 == '#ffffff') {
  178.             removeGaps(cursorX,cursorY)
  179.             removeGaps(cursorX+1,cursorY)
  180.         } else {
  181.             eval('document.all.cell' + cellNum(cursorX,cursorY) + '.style.backgroundColor = "' + b2 + '"')
  182.             eval('document.all.cell' + cellNum(cursorX+1,cursorY) + '.style.backgroundColor = "' + b1 + '"')
  183.             checkIndiv(cursorX,cursorY)
  184.             checkIndiv(cursorX+1,cursorY)
  185.         }
  186.         if (needDropping == 1) dropCells()
  187.         updateScores()
  188.     }  
  189.     updateCursor()
  190. }
  191.  
  192. function removeGaps(x,y) {
  193.     var tempPos = 0
  194.     for (j=0;j<Math.max(heightArray[x],y+1);++j) {
  195.         if (boardArray[x][j] != '#ffffff') {
  196.             boardArray[x][tempPos] = boardArray[x][j]
  197.             eval('document.all.cell' + cellNum(x,tempPos) + '.style.backgroundColor = "' + boardArray[x][tempPos] + '"')       
  198.             tempPos += 1
  199.         }
  200.     }
  201.     for (j=tempPos;j<Math.max(heightArray[x],y+1);++j) {
  202.         boardArray[x][j] = '#ffffff'
  203.         eval('document.all.cell' + cellNum(x,j) + '.style.backgroundColor = "#ffffff"')
  204.     }
  205.     heightArray[x] = tempPos
  206.     if (y < heightArray[x]) {
  207.         checkIndiv(x,y)
  208.     } else {
  209.         if (heightArray[x] != 0) checkIndiv(x,heightArray[x]-1)
  210.     }
  211. }
  212.  
  213. function updateScores() {
  214.     gameScore += 10*sScore
  215.     gameLevel = Math.floor(gameScore/levelUp)
  216.     if (gameLevel > 10) gameLevel = 10
  217.     lowerRate = speedArray[gameLevel]  
  218.     document.all.scoreText.value = gameScore
  219.     document.all.levelText.value = gameLevel
  220.     sScore = 0
  221. }
  222.  
  223. function checkIndiv(x,y) {
  224.     checkDirn(x,y,0,1,0)
  225.     checkDirn(x,y,1,0,0)
  226. }
  227.  
  228. function onBoard(x,y) {
  229.     if (x < 0 || x > boardWidth - 1 || y < 0 || y > boardHeight - 1) {
  230.         return 0
  231.     } else {
  232.         return 1
  233.     }
  234. }
  235.  
  236.  
  237. // Using the arrays clearX, clearY in the following two functions to store which blocks need clearing
  238.  
  239. // in the main boardArray after all the checking has been done.
  240.  
  241. // Rather than writing two function with basically the same mecahnics of searching, the checkDirn combines two steps.
  242.  
  243. // First use is to check if there are enough joined in a row in any direction to clear. (whichOption = 0)
  244.  
  245. // If there are enough then the same procedure to locate these blocks is used but rather than counting.
  246.  
  247. // The function itself checks if there are enough then calls itself again only with a different option (whichOption = 1)
  248.  
  249.  
  250. function checkDirn(x,y,dx,dy,whichOption) {
  251.     if (clearArray[x][y][dirn(dx,dy)] == 1) return
  252.     var colorCount = 1
  253.     var blockColor = boardArray[x][y]
  254.     if (whichOption == 1) {
  255.         var nBlocks = 1
  256.         dropArray[x] = 1
  257.         clearX[clearX.length] = x
  258.         clearY[clearY.length] = y
  259.         clearArray[x][y][dirn(dx,dy)] = 1
  260.         eval('document.all.cell' + cellNum(x,y) + '.style.backgroundColor = "#ffffff"')
  261.     }
  262.     var n = 1
  263.     while (onBoard(x+dx*n,y+dy*n) == 1) {
  264.         if (boardArray[x+dx*n][y+dy*n] == blockColor) {
  265.             if (whichOption == 0) colorCount += 1
  266.             if (whichOption == 1) {
  267.                 nBlocks += 1
  268.                 dropArray[x+dx*n] = 1
  269.                 clearX[clearX.length] = x+dx*n
  270.                 clearY[clearY.length] = y+dy*n
  271.                 clearArray[x+dx*n][y+dy*n][dirn(dx,dy)] = 1
  272.                 eval('document.all.cell' + cellNum(x+dx*n,y+dy*n) + '.style.backgroundColor = "#ffffff"')
  273.             }
  274.             n += 1
  275.         } else {
  276.             break
  277.         }
  278.     }
  279.     n = 1
  280.     while (onBoard(x-dx*n,y-dy*n) == 1) {
  281.         if (boardArray[x-dx*n][y-dy*n] == blockColor) {
  282.             if (whichOption == 0) colorCount += 1
  283.             if (whichOption == 1) {
  284.                 nBlocks += 1
  285.                 dropArray[x-dx*n] = 1
  286.                 clearX[clearX.length] = x-dx*n
  287.                 clearY[clearY.length] = y-dy*n
  288.                 clearArray[x-dx*n][y-dy*n][dirn(dx,dy)] = 1
  289.                 eval('document.all.cell' + cellNum(x-dx*n,y-dy*n) + '.style.backgroundColor = "#ffffff"')
  290.             }
  291.             n += 1
  292.         } else {
  293.             break
  294.         }
  295.     }
  296.     if (whichOption == 0 && colorCount >= 3) checkDirn(x,y,dx,dy,1)
  297.     if (whichOption == 1) {
  298.         sScore += nBlocks
  299.         needDropping = 1
  300.     }
  301. }
  302.  
  303. function dirn(dx,dy) {
  304.     if (dx == 0 && dy == 1) return 0
  305.     if (dx == 1 && dy == 0) return 1
  306. }
  307.  
  308. // clears blocks that need clearing and reset the clearX/Y arrays for next use
  309.  
  310. // update the number of blocks cleared; note the way clearX/Y have been filled, will lead to repeats, so need to be careful when counting
  311.  
  312. function updateBoardArray() {
  313.     for (i=0;i<clearX.length;++i) {
  314.         if (boardArray[clearX[i]][clearY[i]] != '#ffffff') clearedBlocks += 1
  315.         boardArray[clearX[i]][clearY[i]] = '#ffffff'
  316.         clearArray[clearX[i]][clearY[i]] = new Array()
  317.     }
  318.     document.all.blocksText.value = clearedBlocks
  319.     clearX = new Array()
  320.     clearY = new Array()
  321. }
  322.  
  323. function dropCells() {
  324.     sDrops += 1
  325.     needDropping = 0
  326.     updateBoardArray()
  327.     for (i=0;i<boardWidth;++i) {
  328.         if (dropArray[i] == 1) {
  329.             var tempPos = 0
  330.             var lowestDrop = boardHeight
  331.             for (j=0;j<heightArray[i];++j) {
  332.                 if (boardArray[i][j] == '#ffffff' && j < lowestDrop) lowestDrop = j            
  333.                 if (boardArray[i][j] != '#ffffff') {
  334.                     boardArray[i][tempPos] = boardArray[i][j]
  335.                     eval('document.all.cell' + cellNum(i,tempPos) + '.style.backgroundColor = "' + boardArray[i][tempPos] + '"')       
  336.                     tempPos += 1
  337.                 }
  338.             }
  339.             for (j=tempPos;j<heightArray[i];++j) {
  340.                 boardArray[i][j] = '#ffffff'
  341.                 eval('document.all.cell' + cellNum(i,j) + '.style.backgroundColor = "#ffffff"')
  342.             }
  343.             heightArray[i] = tempPos
  344.             dropArray[i] = 0
  345.         }
  346.     }
  347.  
  348.     for (i=0;i<boardWidth;++i) {
  349.         for (j=lowestDrop;j<heightArray[i];++j) checkIndiv(i,j)
  350.     }
  351.     if (needDropping == 1) dropCells()
  352. }
  353.  
  354. function endGame() {
  355.     stillPlaying = 0
  356.     alert('Game over!')
  357.     clearTimeout(keepMoving)
  358. }
  359.  
  360. mainBoard()
  361.  
  362. //lowerBlock()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement