Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

setSquare(x, y, state)

// set square to 1 / 0
function setSquare(x, y, state) {
 if (state == 0)
document.images[computeIndex(x, y)].src = off.src
 else
document.images[computeIndex(x, y)].src = on[shape].src
 // if state is one square is active, so 1 is assigned to ar[x][y]
 // otherwise square is not active so 0 is assigned to ar[x][y]
 ar[x][y] = state
}

The setSquare() function accepts three arguments: the x coordinate of a square, its y coordinate, and a state (0 or 1) assignment. If the value of state is 0, the square at the given position is cleared by assigning off.src to its src property. On the other hand, if state is 1 (or any other value), the box at the given position is assigned the block image whose index is shape, where shape is a global integer between 0 and 6. The current state of the square, at the specified position, is stored in the global array, ar. See the inline comments for additional explanations regarding this statement.

clearActive()

// clear array so no active squares exist
function clearActive() {
// scan entire array and assign 0 to all elements
// (no active squares)
 for (var i = 0; i < 10; ++i) {
for (var j = 0; j < 19; ++j) {
 ar[i][j] = 0
}
 }

// no shape is currently in screen
 shape = -1
}

The ar array, which is 10 x 19, keeps track of all current “active” blocks, i.e., those that belong to a falling shape. Moving blocks around is done simply by visiting all active ones and assigning their next locations. Whenever a block hits the bottom of the board or lies on top of another block, the falling shape is inactivated by the clearActive() function, which clears the entire board. The last statement of the function sets shape to –1, signaling that there is no falling shape on the board.

checkMoveX(step)

// check if specified move (left or right) is valid
function checkMoveX(step) {
 // scan screen (direction does not matter)
 for (var x = 0; x < 10; ++x) {
for (var y = 0; y < 19; ++y) {
 // if current square is active
 if (ar[x][y] == 1) {
// check all conditions:
// not out of range and not colliding with existing
 not active block
if (x + step < 0 || x + step > 9 || (state(x + step,
 y) && ar[x + step][y] == 0))
 // return false if move (new situation) is not
 legal
 return false
 }
}
 }

 // return true if no invalid state has been encountered
 return true
}

This function accepts one argument, either 1 or –1, and checks if it is possible to move the active shape one square to the right (if the argument is 1) or to the left (if the argument is –1). The function looks for all active squares on the board and the following condition is evaluated for each active one:

x+step<0||x+step>9||(state(x+step,y)&&ar[x+step][y]==0)

This expression yields true if the active square, after moving it step positions to the right, finds itself out of the board’s range, or in the territory of an inactive block (an active block obviously belongs to the same shape). If the specified movement is not valid, the function returns false. Otherwise, it returns true.

The efficiency of this function can be improved in two ways. First, to reduce the number of checks, the function can be spliced into CheckMoveXright() and CheckMoveXleft(). Second, instead of searching the whole board for active squares, a more localized algorithm, which takes advantage of the current shape and position information, can be devised.


Figure 25-6.  A shape map reflects the minimum rectangle necessary to include the entire shape.

   Block in shape

      Blocks in shape map

checkMoveY()

// check if specified move (down) is valid
function checkMoveY() {
 // only possible step is one to the bottom
 var step = 1
 // scan screen (direction does not matter)
 for (var x = 0; x < 10; ++x) {
for (var y = 0; y < 19; ++y) {
 // if current square is active
 if (ar[x][y] == 1) {
// check all conditions:
// not out of range and not colliding with existing
 not active block
if (y + step > 18 || (state(x, y + step) && ar[x]
 [y + step] == 0))
 // return false if move (new situation) is not
 legal
 return false
 }
}
 }
 // return true if no invalid state has been encountered
 return true
}

This function is very similar to checkMoveX(), except that, since the movement is always downward, it does not accept any argument. The step variable appears in this function for historical reasons; it could have been replaced by 1.

moveX(step)

// move all active squares step squares on the x axis
function moveX(step) {
 // if specified move is not legal
 if (!checkMoveX(step))
// terminate function (active blocks are not moved)
return

 // if left movement then scan screen from left to right
 if (step < 0) {
for (var x = 0; x < 10; ++x) {
 for (var y = 0; y < 19; ++y) {
 // if current square is active
 if (ar[x][y] == 1)
 // call function to handle movement
 smartX(x, y, step)
 }
}
 } else
 // if right movement then scan screen from right to left
 if (step > 1) {
for (var x = 9; x >= 0; --x) {
 for (var y = 0; y < 19; ++y) {
// if current square is active
if (ar[x][y] == 1)
// call function to handle movement
smartX(x, y, step)
 }
}
 }
}

The moveX() function accepts one argument, specifying the number of positions that all active squares need to be moved. A positive value means that they should be moved to the right, while a negative value means that they should be moved to the left. The scanning algorithm is coordinated with the movement direction, so it does not visit the same square again (after it has been moved). The movement itself is accomplished by the smartX() function. Refer to inline comments for statement-specific notes.


Figure 25-7.  Shape #4 in its initial position.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us