Advertisement
ostyleo

P6JavaScript

May 11th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.00 KB | None | 0 0
  1. let gameBoard = [];
  2. let empty;
  3.  
  4. function generateGameBoard() {
  5.     let $tds = $("#table").find("td"), number = 0;
  6.     while (number < 9) {
  7.         for (let i = 0; i < 3; i++) {
  8.             var aux = [];
  9.             for (let j = 0; j < 3; j++) {
  10.                 $($tds[number]).attr("i", i).attr("j", j);
  11.                 aux.push($($tds[number]));
  12.                 number++;
  13.             }
  14.             gameBoard.push(aux);
  15.         }
  16.     }
  17. }
  18.  
  19. function generateRandom() {
  20.     let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8];
  21.     let randi, randj, dimension = numbers.length, aux;
  22.     while (dimension > 0) {
  23.         randi = Math.floor(Math.random() * numbers.length);
  24.         randj = Math.floor(Math.random() * numbers.length);
  25.         aux = numbers[randi];
  26.         numbers[randi] = numbers[randj];
  27.         numbers[randj] = aux;
  28.         dimension--;
  29.     }
  30.     count = 0;
  31.     for (let i = 0; i < 3; i++) {
  32.         for (let j = 0; j < 3; j++) {
  33.             if (numbers[count] !== 0)
  34.                 gameBoard[i][j].html(numbers[count]);
  35.             else
  36.                 empty = gameBoard[i][j];
  37.             gameBoard[i][j].click(function () {
  38.                 console.log($(this).html() + " i=" + $(this).attr("i") + " j=" + $(this).attr("j"));
  39.             });
  40.             count++;
  41.         }
  42.     }
  43. }
  44.  
  45. function move(keyCode) {
  46.     let i = parseInt(empty.attr("i")), j = parseInt(empty.attr("j"));
  47.     switch (keyCode) {
  48.         case 37:
  49.             console.log("Left");
  50.             moveLeft(i, j);
  51.             break;
  52.         case 39:
  53.             console.log("Right");
  54.             moveRight(i, j);
  55.             break;
  56.         case 38:
  57.             console.log("Up");
  58.             moveUp(i, j);
  59.             break;
  60.         case 40:
  61.             console.log("Down");
  62.             moveDown(i, j);
  63.             break;
  64.     }
  65.     if (checkVictory()) {
  66.         alert("Felicitari!");
  67.         location.reload();
  68.     }
  69. }
  70.  
  71. function startGame() {
  72.     generateGameBoard();
  73.     generateRandom();
  74. }
  75.  
  76. function moveLeft(i, j) {
  77.     if (j !== 2) {
  78.         switchTwo(gameBoard[i][j + 1], gameBoard[i][j]);
  79.     }
  80. }
  81.  
  82. function moveRight(i, j) {
  83.     if (j !== 0) {
  84.         switchTwo(gameBoard[i][j - 1], gameBoard[i][j]);
  85.     }
  86. }
  87.  
  88. function moveUp(i, j) {
  89.     if (i !== 2) {
  90.         switchTwo(gameBoard[i + 1][j], gameBoard[i][j]);
  91.     }
  92. }
  93.  
  94. function moveDown(i, j) {
  95.     if (i !== 0) {
  96.         switchTwo(gameBoard[i - 1][j], gameBoard[i][j]);
  97.     }
  98. }
  99.  
  100. //p2 mereu empty
  101. function switchTwo(p1, p2) {
  102.     let aux = p1.html();
  103.     p1.html(p2.html());
  104.     p2.html(aux);
  105.     empty = p1;
  106. }
  107.  
  108. function checkVictory() {
  109.     if (gameBoard[2][2].html() === "") {
  110.         let against = ["1", "2", "3", "4", "5", "6", "7", "8", ""], count = 0;
  111.         for (let i = 0; i < 3; i++) {
  112.             for (let j = 0; j < 3; j++) {
  113.                 if (against[count] !== gameBoard[i][j].html())
  114.                     return false;
  115.                 count++;
  116.             }
  117.         }
  118.         return true;
  119.     }
  120.     return false;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement