|
insertShape()This function inserts a new shape whose index is a random integer stored in the global variable shape. Take a look at the following code segment:
if (shape == 4) { if (state(4, 2) || state(5, 2) || state(3, 3) || state(4, 3)) return false setSquare(4, 2, 1) setSquare(5, 2, 1) setSquare(3, 3, 1) setSquare(4, 3, 1) }
The insertShape() function includes such a script segment for each of the seven supported shapes. The coordinates used in this script are illustrated in Figure 25-7. The conditional statement uses the function state() to check whether all four squares are empty. If one of the four is blocked, the insertShape() function returns false and terminates. Otherwise, the function proceeds by invoking the setSquare() function four times, one for each block of the new shape. complexWarp()
// warp several squares if possible // initial x1, initial y1, destination x1, destination y1, // initial x2, initial y2, destination x2, destination y2, etc. function complexWarp() { // loop through arguments checking that each warp is valid for (var i = 0; i < arguments.length; i += 4) { // if warp is not valid if (!checkWarp(arguments[i], arguments[i + 1], arguments[i + 2], arguments[i + 3])) // terminate the function -- no squares warped return } // loop through arguments again -- warp squares for (var i = 0; i < arguments.length; i += 4) { // call function to warp the current square corresponding to // argument coordinates warp(arguments[i], arguments[i + 1], arguments[i + 2], arguments[i + 3]) } }
This function moves a shape’s blocks from their current positions to their new ones. The argument list includes sets of four coordinates: x and y values of these positions. Since the number of sets depends on the shape type, the arguments are accessed via the arguments array, rather than as parameters. First, the function calls checkWarp() to check if the destination squares are populated or not. The complexWarp() function terminates immediately whenever one of the squares is occupied. The warping itself is accomplished by calling the warp() function, once for each set of four arguments. checkWarp(startX, startY, endX, endY)
// check if warp is valid (used by complexWarp function) function checkWarp(startX, startY, endX, endY) { // if a destination coordinate is invalid or destination square is off // state(endX, endY) must be last due to short-circuit evaluation if (endX < 0 || endX > 9 || endY < 0 || endY > 18 || state(endX, endY)) // return false because warp is invalid return false // return true because warp has not been proved // to be invalid (it is valid) return true }
Out of the four arguments this function accepts, it uses the last two (endX, endY) to check if the given position is occupied. rotate()First, the function assigns an instance of the shapeMap object to a local variable, curMap. Refer back to Figure 25-6 for an explanation about the enclosing rectangle concept of shapeMap. The shapeMap object is used to find out the current angle of the shape. The “L” shape, for example, has four different angles, whereas a “square” (2 x 2) shape has only one. It then calls the complexWarp() function with the coordinates of the blocks that need to be “warped” during rotation. flood(state)
// flood entire screen with given state function flood(state) { for (var x = 0; x < 10; ++x) { for (var y = 0; y < 19; ++y) { if (state == 0) document.images[computeIndex(x, y)].src = off.src else document.images[computeIndex(x, y)].src = on[3].src } } }
When its argument is 0, the flood() function clears the entire board by setting the URL of all images to off.src. If the argument is not 0, all board images are replaced with on[3].src. Note that, instead of the doubly-nested loop, you can use a single loop to fill the document.images 1-D array. Since you would not have to invoke the computeIndex() function for each position, the flooding would have been more efficient. noActive()
// return true if no active squares are found and false otherwise function noActive() { // scan board from top to bottom for (var y = 0; y < 19; ++y) { for (var x = 0; x < 10; ++ x) { if (ar[x][y] == 1) return false } } // no active square found on the board return true }
This function is self-explanatory. isLine(y)
// return true if the line with the given coordinate is completed function isLine(y) { // horizontal scan of current line for (var x = 0; x < 10; ++x) { // if a square is off the line is not completed if (!state(x, y)) return false } // no square was found off return true }
This function looks for complete lines, i.e., lines with all squares checked. warp(startX, startY, endX, endY)
// move block from one position to another function warp(startX, startY, endX, endY) { document.images[computeIndex(endX, endY)].src = document.images [computeIndex(startX, startY)].src document.images[computeIndex(startX, startY)].src = off.src // block in new position is now active ar[endX][endY] = 1 // previous position is no longer active ar[startX][startY] = 0 }
The warp() function “warps” a block from one position to another, by setting the URL of the destination image to that of the source one, and then setting the URL of the source image to that of the transparent one, off.src. The ar array is also being updated with the recent changes in square assignments.
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |