Lllen

Untitled

Jul 31st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.12 KB | None | 0 0
  1. const WINSCORE = 2048;
  2. const FIELDSIZE = 4;
  3. const CELLSIZE = 100;
  4. const VICTORY = 2048;
  5. var gameField = CreateMatrix(FIELDSIZE, FIELDSIZE);
  6. var flying = [];
  7. var notflying = [];
  8. var SCORE = 0;
  9. var EmptyX;
  10. var EmptyY;
  11. var keyboard = false;
  12. var moves = [];
  13. var dir = [-1, 0, 1, 0, 0, -1, 0, 1];
  14. var canvas = document.getElementById("matrix");
  15. var bufer = document.getElementById("buf");
  16. var context = canvas.getContext("2d");
  17. var buf = bufer.getContext("2d");
  18. canvas.width = FIELDSIZE * CELLSIZE;
  19. canvas.height = FIELDSIZE * CELLSIZE;
  20. bufer.width = FIELDSIZE * CELLSIZE;
  21. bufer.height = FIELDSIZE * CELLSIZE;
  22. var YouWin = false;
  23. //document.getElementById('buf').style.display = 'none';
  24. function ShowScore() {
  25. var score = document.getElementById("scoreId");
  26. score.innerHTML = "Score: " + SCORE;
  27. }
  28.  
  29. function show(state) {
  30. document.getElementById('window').style.display = state;
  31. document.getElementById('wrap1').style.display = state;
  32. }
  33.  
  34. function showWindowEnd(dis) {
  35. document.getElementById('GameOverWindow').style.display = dis;
  36. document.getElementById('wrap2').style.display = dis;
  37. }
  38.  
  39. function showWindowWin(win) {
  40. document.getElementById('YouWinWindow').style.display = win;
  41. document.getElementById('wrap3').style.display = win;
  42. }
  43.  
  44. function delay() {
  45. document.location.reload();
  46. }
  47.  
  48. function RandomDegree() {
  49. return Math.floor(Math.random() * 2) + 1;
  50. }
  51.  
  52. function Random(min, max) {
  53. return Math.floor(Math.random() * (max - min + 1)) + min;
  54. }
  55.  
  56. function DrawNumber(number, posY, posX) {
  57. if ((number == VICTORY) && (YouWin == false)) {
  58. YouWin = true;
  59. showWindowWin('block');
  60. }
  61. context.fillStyle = "#FAFAD2";
  62. buf.fillStyle = "#FAFAD2";
  63. context.textAlign = 'center';
  64. buf.textAlign = 'center';
  65. buf.strokeText(number, (posX + 1) * CELLSIZE - CELLSIZE / 2, (posY + 1) * CELLSIZE - CELLSIZE / 2);
  66. context.strokeText(number, (posX + 1) * CELLSIZE - CELLSIZE / 2, (posY + 1) * CELLSIZE - CELLSIZE / 2);
  67.  
  68. }
  69.  
  70. function DrawBufBackground() {
  71. buf.fillStyle = "#666";
  72. buf.fillRect(0, 0, bufer.width, bufer.height);
  73.  
  74. for (let x = CELLSIZE; x < FIELDSIZE * CELLSIZE; x += CELLSIZE) {
  75. buf.moveTo(x, 0);
  76. buf.lineTo(x, 800);
  77. }
  78. for (let y = CELLSIZE; y < FIELDSIZE * CELLSIZE; y += CELLSIZE) {
  79. buf.moveTo(0, y);
  80. buf.lineTo(FIELDSIZE * CELLSIZE, y);
  81. }
  82. buf.strokeStyle = "#808080";
  83. buf.stroke();
  84. buf.strokeStyle = "#f00";
  85. buf.font = "30px AR DELANEY";
  86. for (let i = 0; i < notflying.length; i++) {
  87. if (gameField[notflying[i][1]][notflying[i][2]] !== 0) {
  88. buf.fillRect(notflying[i][2] * CELLSIZE + 1, notflying[i][1] * CELLSIZE + 1, CELLSIZE - 2, CELLSIZE - 2);
  89. }
  90. DrawNumber(notflying[i][0], notflying[i][1], notflying[i][2]);
  91. }
  92. for (let i = 0; i < flying.length; i++) {
  93. if (gameField[moves[i][2]][moves[i][3]] !== 0) {
  94. buf.fillRect(flying[i] * CELLSIZE + 1, flying[i] * CELLSIZE + 1, CELLSIZE - 2, CELLSIZE - 2);
  95. }
  96. DrawNumber(flying[i], moves[i][0], moves[i][1]);
  97. }
  98. }
  99.  
  100. function DrawBackground() {
  101. DrawBufBackground();
  102. document.getElementById('matrix').style.display = 'none';
  103. document.getElementById('buf').style.display = 'block';
  104. context.fillStyle = "#FAFAD2";
  105. context.fillRect(0, 0, canvas.width, canvas.height);
  106.  
  107. for (let x = CELLSIZE; x < FIELDSIZE * CELLSIZE; x += CELLSIZE) {
  108. context.moveTo(x, 0);
  109. context.lineTo(x, 800);
  110. }
  111. for (let y = CELLSIZE; y < FIELDSIZE * CELLSIZE; y += CELLSIZE) {
  112. context.moveTo(0, y);
  113. context.lineTo(FIELDSIZE * CELLSIZE, y);
  114. }
  115. context.strokeStyle = "#808080";
  116. context.stroke();
  117. context.strokeStyle = "#F00";
  118. context.font = "30px AR DELANEY";
  119. for (let i = 0; i < notflying.length; i++) {
  120. if (gameField[notflying[i][1]][notflying[i][2]] !== 0) {
  121. context.fillRect(notflying[i][2] * CELLSIZE + 1, notflying[i][1] * CELLSIZE + 1, CELLSIZE - 2, CELLSIZE - 2);
  122. }
  123. DrawNumber(notflying[i][0], notflying[i][1], notflying[i][2]);
  124. }
  125. //document.getElementById('buf').style.display = 'none';
  126. //document.getElementById('matrix').style.display = 'block';
  127.  
  128.  
  129. }
  130.  
  131. function ShowMatrix(rows) {
  132. for (let i = 0; i < rows; i++) {
  133. console.log(gameField[i]);
  134. }
  135. console.log('');
  136. }
  137.  
  138.  
  139. function IsEmpty(Y, X) {
  140. return (gameField[Y][X] === 0);
  141. }
  142.  
  143. function CheckEmptyCells() {
  144. let flag = false;
  145. for (let i = 0; i < FIELDSIZE; i++) {
  146. for (let j = 0; j < FIELDSIZE; j++) {
  147. if (IsEmpty(i, j)) {
  148. EmptyX = j;
  149. EmptyY = i;
  150. flag = true;
  151. }
  152. }
  153. }
  154. return flag;
  155. }
  156.  
  157. function AddNumber(Y, X) {
  158. gameField[Y][X] = Math.pow(2, RandomDegree());
  159. DrawNumber(gameField[Y][X], Y, X);
  160. notflying.push([gameField[Y][X], Y, X])
  161. }
  162.  
  163. function AddNewCell() {
  164. EndGame();
  165. ShowScore();
  166. var X = Random(0, FIELDSIZE - 1);
  167. var Y = Random(0, FIELDSIZE - 1);
  168. if (IsEmpty(Y, X)) {
  169. AddNumber(Y, X);
  170. } else {
  171. AddNumber(EmptyY, EmptyX);
  172. }
  173. }
  174.  
  175. function CheckEdge(i, j, v, h) {
  176. if (i - v >= 0 && i - v < FIELDSIZE && j - h >= 0 && j - h < FIELDSIZE) {
  177. return true
  178. } else
  179. return false;
  180. }
  181.  
  182. function IsEqual(vertic, horizon) {
  183. for (let i = 0; i < FIELDSIZE; i++)
  184. for (let j = 0; j < FIELDSIZE; j++) {
  185. if (CheckEdge(i, j, vertic, horizon) && gameField[i - vertic][j - horizon] === gameField[i][j]) {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191.  
  192. function EndGame() {
  193. if (!CheckEmptyCells()) {
  194. for (let i = 0; i < dir.length; i += 2) {
  195. if (IsEqual(dir[i], dir[i + 1])) {
  196. return;
  197. }
  198. }
  199. showWindowEnd('block');
  200. }
  201. }
  202.  
  203. function AddFirstCell() {
  204. AddNewCell();
  205. AddNewCell();
  206. }
  207.  
  208. function LetItMoves(i, j, Curr_i, Curr_j, Curr, offset_i, offset_j) {
  209. if (gameField[Curr_i][Curr_j] === 0 || gameField[Curr_i][Curr_j] === gameField[i][j]) {
  210. if (gameField[Curr_i][Curr_j] != 0)
  211. SCORE += gameField[Curr_i][Curr_j];
  212. gameField[Curr_i][Curr_j] += gameField[i][j];
  213. moves.push([i, j, Curr_i, Curr_j]);
  214. flying.push(gameField[i][j]);
  215. } else {
  216. if (Curr === 1) {
  217. notflying.push([gameField[i][j], i, j]);
  218. return;
  219. }
  220. gameField[Curr_i + offset_i][Curr_j + offset_j] += gameField[i][j];
  221. moves.push([i, j, Curr_i + offset_i, Curr_j + offset_j]);
  222. flying.push(gameField[i][j]);
  223. }
  224. gameField[i][j] = 0;
  225. }
  226.  
  227. function Move(direction) {
  228. console.log('Move');
  229. moves.splice(0, moves.length);
  230. notflying.splice(0, notflying.length);
  231. clearInterval(timer);
  232. let dx = 0, dy = 0;
  233. const delta = 0.09;
  234. switch (direction) {
  235. case "left":
  236. for (var i = 0; i < FIELDSIZE; i++) {
  237. for (var j = 0; j < FIELDSIZE; j++) {
  238. if (gameField[i][j] === 0) continue;
  239. let Curr = 1;
  240. if (j - Curr < 0) {
  241. notflying.push([gameField[i][j], i, j]);
  242. continue;
  243. }
  244. while (j - Curr > 0 && gameField[i][j - Curr] === 0) {
  245. Curr++;
  246. }
  247. LetItMoves(i, j, i, j - Curr, Curr, 0, 1);
  248. }
  249. }
  250. dx = -delta;
  251. break;
  252. case "up":
  253. for (var i = 0; i < FIELDSIZE; i++) {
  254. for (var j = 0; j < FIELDSIZE; j++) {
  255. if (gameField[i][j] === 0) continue;
  256. let Curr = 1;
  257. if (i - Curr < 0) {
  258. notflying.push([gameField[i][j], i, j]);
  259. continue;
  260. }
  261. while (i - Curr > 0 && gameField[i - Curr][j] === 0) {
  262. Curr++;
  263. }
  264. LetItMoves(i, j, i - Curr, j, Curr, 1, 0);
  265. }
  266. }
  267. dy = -delta;
  268. break;
  269. case "right":
  270. for (var i = 0; i < FIELDSIZE; i++) {
  271. for (var j = FIELDSIZE - 1; j >= 0; j--) {
  272. if (gameField[i][j] === 0) continue;
  273. let Curr = 1;
  274. if (j + Curr > FIELDSIZE - 1) {
  275. notflying.push([gameField[i][j], i, j]);
  276. continue;
  277. }
  278. while (j + Curr < FIELDSIZE - 1 && gameField[i][j + Curr] === 0) {
  279. Curr++;
  280. }
  281. LetItMoves(i, j, i, j + Curr, Curr, 0, -1);
  282. }
  283. }
  284. dx = delta;
  285. break;
  286. case "down":
  287. for (var i = FIELDSIZE - 1; i >= 0; i--) {
  288. for (var j = 0; j < FIELDSIZE; j++) {
  289. if (gameField[i][j] === 0) continue;
  290. let Curr = 1;
  291. if (i + Curr > FIELDSIZE - 1) {
  292. notflying.push([gameField[i][j], i, j]);
  293. continue;
  294. }
  295. while (i + Curr < FIELDSIZE - 1 && gameField[i + Curr][j] === 0) {
  296. Curr++;
  297. }
  298.  
  299. LetItMoves(i, j, i + Curr, j, Curr, -1, 0);
  300. }
  301. }
  302. dy = delta;
  303. break;
  304. }
  305. if (flying.length == 0) {
  306. var addnewcell = false;
  307. } else {
  308. var addnewcell = true;
  309. }
  310. EndGame();
  311.  
  312. var timer = setInterval(function () {
  313. context.clearRect(0, 0, canvas.width, canvas.height);
  314. DrawBackground(); // нарисуем все ячейки которые не двигались
  315. if (moves.length === 0 || flying.length == 0) {
  316. clearInterval(timer);
  317. DrawBackground();
  318. if (addnewcell)
  319. AddNewCell();
  320. acceptKeys = true;
  321. }
  322. for (let j = 0; j < moves.length; j++) {
  323. let x1 = moves[j][0], y1 = moves[j][1], x2 = moves[j][2], y2 = moves[j][3];
  324. if (Math.abs(x1 - x2) < delta && Math.abs(y1 - y2) < delta) {
  325. notflying.push([gameField[x2][y2], x2, y2])
  326. moves.splice(j, 1);
  327. flying.splice(j, 1);
  328. continue;
  329. }
  330. DrawNumber(flying[j], x1, y1);
  331. moves[j][0] += dy;
  332. moves[j][1] += dx;
  333. DrawNumber(flying[j], x1, y1);
  334. document.getElementById('buf').style.display = 'none';
  335. document.getElementById('matrix').style.display = 'block';
  336. }
  337. speed++;
  338. }, 50);
  339. speed = 0;
  340. console.log('out');
  341. // ShowMatrix(FIELDSIZE);
  342. }
  343.  
  344. function CreateMatrix(rows, columns) {
  345. gameField = new Array();
  346. for (let i = 0; i < rows; i++) {
  347. gameField[i] = new Array();
  348. for (let j = 0; j < columns; j++) {
  349. gameField[i][j] = 0;
  350. }
  351. }
  352. return gameField;
  353. }
  354.  
  355. var acceptKeys = true;
  356.  
  357. function Direction(key) {
  358. if (!acceptKeys) return;
  359. var direction;
  360. switch (key.keyCode) {
  361. case 37: // если нажата клавиша влево
  362. direction = 'left';
  363. break;
  364. case 38: // если нажата клавиша вверх
  365. direction = 'up';
  366. break;
  367. case 39: // если нажата клавиша вправо
  368. direction = 'right';
  369. break;
  370. case 40: // если нажата клавиша вниз
  371. direction = 'down';
  372. break;
  373. default:
  374. direction = 'none';
  375. break;
  376. }
  377. if (direction === 'none' || moves.length != 0) return;
  378. acceptKeys = false;
  379. Move(direction);
  380. }
  381.  
  382. function init() {
  383. DrawBackground();
  384. AddFirstCell();
  385. addEventListener("keydown", Direction);
  386. }
  387.  
  388. init();
Advertisement
Add Comment
Please, Sign In to add comment