| 792 | | <strong> function check(map, block, offsetx, offsety) {</strong> |
| 793 | | <strong> if (offsetx < 0 || offsety < 0 ||</strong> |
| 794 | | <strong> mapHeight < offsety + block.length ||</strong> |
| 795 | | <strong> mapWidth < offsetx + block[0].length) {</strong> |
| 796 | | <strong> return false;;</strong> |
| 797 | | <strong> }</strong> |
| 798 | | <strong> for (var y = 0; y < block.length; y ++) {</strong> |
| 799 | | <strong> for (var x = 0; x < block[y].length; x ++) {</strong> |
| 800 | | <strong> if (block[y][x] && map[y + offsety][x + offsetx]) { </strong> |
| 801 | | <strong> return false;</strong> |
| 802 | | <strong> }</strong> |
| 803 | | <strong> }</strong> |
| 804 | | <strong> }</strong> |
| 805 | | <strong> return true;</strong> |
| 806 | | <strong> }</strong> |
| | 792 | <strong>function check(map, block, offsetx, offsety) {</strong> |
| | 793 | <strong>if (offsetx < 0 || offsety < 0 ||</strong> |
| | 794 | <strong>mapHeight < offsety + block.length ||</strong> |
| | 795 | <strong>mapWidth < offsetx + block[0].length) {</strong> |
| | 796 | <strong>return false;;</strong> |
| | 797 | <strong>}</strong> |
| | 798 | <strong>for (var y = 0; y < block.length; y ++) {</strong> |
| | 799 | <strong>for (var x = 0; x < block[y].length; x ++) {</strong> |
| | 800 | <strong>if (block[y][x] && map[y + offsety][x + offsetx]) { </strong> |
| | 801 | <strong>return false;</strong> |
| | 802 | <strong>}</strong> |
| | 803 | <strong>}</strong> |
| | 804 | <strong>}</strong> |
| | 805 | <strong>return true;</strong> |
| | 806 | <strong>}</strong> |
| 808 | | <strong> function mergeMatrix(map, block, offsetx, offsety) {</strong> |
| 809 | | <strong> for (var y = 0; y < mapHeight; y ++) {</strong> |
| 810 | | <strong> for (var x = 0; x < mapWidth; x ++) {</strong> |
| 811 | | <strong> if (block[y - offsety] && block[y - offsety][x - offsetx]) {</strong> |
| 812 | | <strong> map[y][x]++;</strong> |
| 813 | | <strong> }</strong> |
| 814 | | <strong> }</strong> |
| 815 | | <strong> }</strong> |
| 816 | | <strong> }</strong> |
| | 808 | <strong>function mergeMatrix(map, block, offsetx, offsety) {</strong> |
| | 809 | <strong>for (var y = 0; y < mapHeight; y ++) {</strong> |
| | 810 | <strong>for (var x = 0; x < mapWidth; x ++) {</strong> |
| | 811 | <strong>if (block[y - offsety] && block[y - offsety][x - offsetx]) {</strong> |
| | 812 | <strong>map[y][x]++;</strong> |
| | 813 | <strong>}</strong> |
| | 814 | <strong>}</strong> |
| | 815 | <strong>}</strong> |
| | 816 | <strong>}</strong> |
| 925 | | <strong> function key(keyCode) {</strong> |
| 926 | | <strong> switch (keyCode) {</strong> |
| 927 | | <strong> case 39:</strong> |
| 928 | | <strong> if (!check(map, block, posx + 1, posy)) {</strong> |
| 929 | | <strong> return;</strong> |
| 930 | | <strong> }</strong> |
| 931 | | <strong> posx = posx + 1;</strong> |
| 932 | | <strong> break;</strong> |
| 933 | | <strong> case 37:</strong> |
| 934 | | <strong> if (!check(map, block, posx - 1, posy)) {</strong> |
| 935 | | <strong> return;</strong> |
| 936 | | <strong> }</strong> |
| 937 | | <strong> posx = posx - 1;</strong> |
| 938 | | <strong> break;</strong> |
| 939 | | <strong> case 40:</strong> |
| 940 | | <strong> var y = posy;</strong> |
| 941 | | <strong> while (check(map, block, posx, y)) { y++; }</strong> |
| 942 | | <strong> posy = y - 1;</strong> |
| 943 | | <strong> break;</strong> |
| 944 | | <strong> default:</strong> |
| 945 | | <strong> return;</strong> |
| 946 | | <strong> }</strong> |
| 947 | | <strong> ctx.clearRect(0, 0, 200, 400);</strong> |
| 948 | | <strong> paintMatrix(block, posx, posy, 'rgb(255, 0, 0)');</strong> |
| 949 | | <strong> paintMatrix(map, 0, 0, 'rgb(128, 128, 128)');</strong> |
| 950 | | <strong> }</strong> |
| | 925 | <strong>function key(keyCode) {</strong> |
| | 926 | <strong>switch (keyCode) {</strong> |
| | 927 | <strong>case 39:</strong> |
| | 928 | <strong>if (!check(map, block, posx + 1, posy)) {</strong> |
| | 929 | <strong>return;</strong> |
| | 930 | <strong>}</strong> |
| | 931 | <strong>posx = posx + 1;</strong> |
| | 932 | <strong>break;</strong> |
| | 933 | <strong>case 37:</strong> |
| | 934 | <strong>if (!check(map, block, posx - 1, posy)) {</strong> |
| | 935 | <strong>return;</strong> |
| | 936 | <strong>}</strong> |
| | 937 | <strong>posx = posx - 1;</strong> |
| | 938 | <strong>break;</strong> |
| | 939 | <strong>case 40:</strong> |
| | 940 | <strong>var y = posy;</strong> |
| | 941 | <strong>while (check(map, block, posx, y)) { y++; }</strong> |
| | 942 | <strong>posy = y - 1;</strong> |
| | 943 | <strong>break;</strong> |
| | 944 | <strong>default:</strong> |
| | 945 | <strong>return;</strong> |
| | 946 | <strong>}</strong> |
| | 947 | <strong>ctx.clearRect(0, 0, 200, 400);</strong> |
| | 948 | <strong>paintMatrix(block, posx, posy, 'rgb(255, 0, 0)');</strong> |
| | 949 | <strong>paintMatrix(map, 0, 0, 'rgb(128, 128, 128)');</strong> |
| | 950 | <strong>}</strong> |
| 1046 | | <strong> function rotate(block) {</strong> |
| 1047 | | <strong> var rotated = [];</strong> |
| 1048 | | <strong> for (var x = 0; x < block[0].length; x ++) {</strong> |
| 1049 | | <strong> rotated[x] = [];</strong> |
| 1050 | | <strong> for (var y = 0; y < block.length; y ++) {</strong> |
| 1051 | | <strong> rotated[x][block.length - y - 1] = block[y][x];</strong> |
| 1052 | | <strong> }</strong> |
| 1053 | | <strong> }</strong> |
| 1054 | | <strong> return rotated;</strong> |
| 1055 | | <strong> }</strong> |
| | 1046 | <strong>function rotate(block) {</strong> |
| | 1047 | <strong>var rotated = [];</strong> |
| | 1048 | <strong>for (var x = 0; x < block[0].length; x ++) {</strong> |
| | 1049 | <strong>rotated[x] = [];</strong> |
| | 1050 | <strong>for (var y = 0; y < block.length; y ++) {</strong> |
| | 1051 | <strong>rotated[x][block.length - y - 1] = block[y][x];</strong> |
| | 1052 | <strong>}</strong> |
| | 1053 | <strong>}</strong> |
| | 1054 | <strong>return rotated;</strong> |
| | 1055 | <strong>}</strong> |
| 1170 | | <strong> function clearRows(map) {</strong> |
| 1171 | | <strong> for (var y = 0; y < mapHeight; y ++) {</strong> |
| 1172 | | <strong> var full = true;</strong> |
| 1173 | | <strong> for (var x = 0; x < mapWidth; x ++) {</strong> |
| 1174 | | <strong> if (!map[y][x]) {</strong> |
| 1175 | | <strong> full = false;</strong> |
| 1176 | | <strong> }</strong> |
| 1177 | | <strong> }</strong> |
| 1178 | | <strong> if (full) {</strong> |
| 1179 | | <strong> map.splice(y, 1);</strong> |
| 1180 | | <strong> var newRow = [];</strong> |
| 1181 | | <strong> for (var i = 0; i < mapWidth; i ++) {</strong> |
| 1182 | | <strong> newRow[i] = 0;</strong> |
| 1183 | | <strong> }</strong> |
| 1184 | | <strong> map.unshift(newRow);</strong> |
| 1185 | | <strong> }</strong> |
| 1186 | | <strong> }</strong> |
| 1187 | | <strong> }</strong> |
| | 1170 | <strong>function clearRows(map) {</strong> |
| | 1171 | <strong>for (var y = 0; y < mapHeight; y ++) {</strong> |
| | 1172 | <strong>var full = true;</strong> |
| | 1173 | <strong>for (var x = 0; x < mapWidth; x ++) {</strong> |
| | 1174 | <strong>if (!map[y][x]) {</strong> |
| | 1175 | <strong>full = false;</strong> |
| | 1176 | <strong>}</strong> |
| | 1177 | <strong>}</strong> |
| | 1178 | <strong>if (full) {</strong> |
| | 1179 | <strong>map.splice(y, 1);</strong> |
| | 1180 | <strong>var newRow = [];</strong> |
| | 1181 | <strong>for (var i = 0; i < mapWidth; i ++) {</strong> |
| | 1182 | <strong>newRow[i] = 0;</strong> |
| | 1183 | <strong>}</strong> |
| | 1184 | <strong>map.unshift(newRow);</strong> |
| | 1185 | <strong>}</strong> |
| | 1186 | <strong>}</strong> |
| | 1187 | <strong>}</strong> |