| 2293 | | <h3>配列が継承しているオブジェクト</h3> |
| 2294 | | |
| 2295 | | <pre><code><!DOCTYPE html> |
| 2296 | | <html> |
| 2297 | | <head> |
| 2298 | | <meta charset=utf-8> |
| 2299 | | <title>Sample</title> |
| 2300 | | <script type="text/javascript"> |
| 2301 | | |
| 2302 | | <strong>Array.prototype.each = function(fn) {</strong> |
| 2303 | | <strong>var result = [];</strong> |
| 2304 | | <strong>for (var i = 0; i < this.length; i ++) {</strong> |
| 2305 | | <strong>result[i] = fn(this[i], i);</strong> |
| 2306 | | <strong>}</strong> |
| 2307 | | <strong>return result;</strong> |
| 2308 | | <strong>};</strong> |
| 2309 | | |
| 2310 | | var ctx; |
| 2311 | | var blocks = [ |
| 2312 | | [ |
| 2313 | | [1,1], |
| 2314 | | [0,1], |
| 2315 | | [0,1] |
| 2316 | | ], |
| 2317 | | [ |
| 2318 | | [1,1], |
| 2319 | | [1,0], |
| 2320 | | [1,0] |
| 2321 | | ], |
| 2322 | | [ |
| 2323 | | [1,1], |
| 2324 | | [1,1] |
| 2325 | | ], |
| 2326 | | [ |
| 2327 | | [1,0], |
| 2328 | | [1,1], |
| 2329 | | [1,0] |
| 2330 | | ], |
| 2331 | | [ |
| 2332 | | [1,0], |
| 2333 | | [1,1], |
| 2334 | | [0,1] |
| 2335 | | ], |
| 2336 | | [ |
| 2337 | | [0,1], |
| 2338 | | [1,1], |
| 2339 | | [1,0] |
| 2340 | | ], |
| 2341 | | [ |
| 2342 | | [1], |
| 2343 | | [1], |
| 2344 | | [1], |
| 2345 | | [1] |
| 2346 | | ] |
| 2347 | | ]; |
| 2348 | | |
| 2349 | | var block = blocks[Math.floor(Math.random() * blocks.length)]; |
| 2350 | | var posx = 0, posy = 0; |
| 2351 | | var mapWidth = 10, mapHeight = 20; |
| 2352 | | |
| 2353 | | <strong>var map = new Array(mapHeight).each(function() {</strong> |
| 2354 | | <strong>return new Array(mapWidth).each(function() { return 0; });</strong> |
| 2355 | | <strong>});</strong> |
| 2356 | | |
| 2357 | | var load = function() { |
| 2358 | | var elmTarget = document.getElementById('target'); |
| 2359 | | ctx = elmTarget.getContext('2d'); |
| 2360 | | |
| 2361 | | setInterval(function() { |
| 2362 | | ctx.clearRect(0, 0, 200, 400); |
| 2363 | | paintMatrix(block, posx, posy, 'rgb(255, 0, 0)'); |
| 2364 | | paintMatrix(map, 0, 0, 'rgb(128, 128, 128)'); |
| 2365 | | |
| 2366 | | if (check(map, block, posx, posy + 1)) { |
| 2367 | | posy = posy + 1; |
| 2368 | | } |
| 2369 | | else { |
| 2370 | | mergeMatrix(map, block, posx, posy); |
| 2371 | | clearRows(map); |
| 2372 | | posx = 0; posy = 0; |
| 2373 | | block = blocks[Math.floor(Math.random() * blocks.length)]; |
| 2374 | | } |
| 2375 | | }, 200); |
| 2376 | | }; |
| 2377 | | |
| 2378 | | var paintMatrix = function(matrix, offsetx, offsety, color) { |
| 2379 | | ctx.fillStyle = color; |
| 2380 | | |
| 2381 | | <strong>matrix.each(function(row, y) {</strong> |
| 2382 | | <strong>row.each(function(val, x) {</strong> |
| 2383 | | <strong>if (val) {</strong> |
| 2384 | | <strong>ctx.fillRect((x + offsetx) * 20, (y + offsety) * 20, 20, 20);</strong> |
| 2385 | | <strong>}</strong> |
| 2386 | | <strong>});</strong> |
| 2387 | | <strong>});</strong> |
| 2388 | | }; |
| 2389 | | |
| 2390 | | var check = function(map, block, offsetx, offsety) { |
| 2391 | | if (offsetx < 0 || offsety < 0 || |
| 2392 | | mapHeight < offsety + block.length || |
| 2393 | | mapWidth < offsetx + block[0].length) { |
| 2394 | | return false; |
| 2395 | | } |
| 2396 | | |
| 2397 | | <strong>var ok = true;</strong> |
| 2398 | | <strong>block.each(function(row, y) {</strong> |
| 2399 | | <strong>row.each(function(val, x) {</strong> |
| 2400 | | <strong>if (val && map[y + offsety][x + offsetx]) {</strong> |
| 2401 | | <strong>ok = false;</strong> |
| 2402 | | <strong>}</strong> |
| 2403 | | <strong>});</strong> |
| 2404 | | <strong>});</strong> |
| 2405 | | <strong>return ok;</strong> |
| 2406 | | }; |
| 2407 | | |
| 2408 | | var mergeMatrix = function(map, block, offsetx, offsety) { |
| 2409 | | <strong>map.each(function(row, y) {</strong> |
| 2410 | | <strong>row.each(function(val, x) {</strong> |
| 2411 | | <strong>if (block[y - offsety] && block[y - offsety][x - offsetx]) {</strong> |
| 2412 | | <strong>row[x]++;</strong> |
| 2413 | | <strong>}</strong> |
| 2414 | | <strong>});</strong> |
| 2415 | | <strong>});</strong> |
| 2416 | | }; |
| 2417 | | |
| 2418 | | var clearRows = function(map) { |
| 2419 | | <strong>map.each(function(row, y) {</strong> |
| 2420 | | <strong>var full = true;</strong> |
| 2421 | | <strong>row.each(function(val, x) {</strong> |
| 2422 | | <strong>if (!val) {</strong> |
| 2423 | | <strong>full = false;</strong> |
| 2424 | | <strong>}</strong> |
| 2425 | | <strong>});</strong> |
| 2426 | | <strong>if (full) {</strong> |
| 2427 | | <strong>map.splice(y, 1);</strong> |
| 2428 | | <strong>map.unshift(new Array(mapWidth).each(function() { return 0 }));</strong> |
| 2429 | | <strong>}</strong> |
| 2430 | | <strong>});</strong> |
| 2431 | | }; |
| 2432 | | |
| 2433 | | var rotate = function(block) { |
| 2434 | | <strong>return new Array(block[0].length).each(function(_, y) {</strong> |
| 2435 | | <strong>return new Array(block.length).each(function(_, x) {</strong> |
| 2436 | | <strong>return block[block.length - x - 1][y];</strong> |
| 2437 | | <strong>});</strong> |
| 2438 | | <strong>});</strong> |
| 2439 | | }; |
| 2440 | | |
| 2441 | | var key = function(keyCode) { |
| 2442 | | switch (keyCode) { |
| 2443 | | case 38: |
| 2444 | | if (!check(map, rotate(block), posx, posy)) { |
| 2445 | | return; |
| 2446 | | } |
| 2447 | | block = rotate(block); |
| 2448 | | break; |
| 2449 | | case 39: |
| 2450 | | if (!check(map, block, posx + 1, posy)) { |
| 2451 | | return; |
| 2452 | | } |
| 2453 | | posx = posx + 1; |
| 2454 | | break; |
| 2455 | | case 37: |
| 2456 | | if (!check(map, block, posx - 1, posy)) { |
| 2457 | | return; |
| 2458 | | } |
| 2459 | | posx = posx - 1; |
| 2460 | | break; |
| 2461 | | case 40: |
| 2462 | | var y = posy; |
| 2463 | | while (check(map, block, posx, y)) { y++; } |
| 2464 | | posy = y - 1; |
| 2465 | | break; |
| 2466 | | default: |
| 2467 | | return; |
| 2468 | | } |
| 2469 | | ctx.clearRect(0, 0, 200, 400); |
| 2470 | | paintMatrix(block, posx, posy, 'rgb(255, 0, 0)'); |
| 2471 | | paintMatrix(map, 0, 0, 'rgb(128, 128, 128)'); |
| 2472 | | }; |
| 2473 | | |
| 2474 | | </script> |
| 2475 | | </head> |
| 2476 | | |
| 2477 | | <body onload="load()" onkeydown="key(event.keyCode)"> |
| 2478 | | <canvas id="target" style="border: 5px solid gray" width="200" height="400"></canvas> |
| 2479 | | </body> |
| 2480 | | </html></code></pre> |
| 2481 | | <p> |
| 2482 | | <a onclick="html(this)" href="javascript:void(0)">サンプル</a> |
| 2483 | | </p> |
| 2484 | | </div> |
| 2485 | | |
| 2486 | | <div> |