ErolKZ

Untitled

May 1st, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1.  
  2. function getAndSetElements(n) {
  3.  
  4. let divContainer = document.querySelector('.container');
  5.  
  6. let divGridElement = document.querySelector('.grid');
  7.  
  8. divGridElement.style.gridTemplateColumns = `repeat(${n}, 60px)`;
  9.  
  10. divGridElement.style.gridTemplateRows = `repeat(${n}, 60px)`;
  11.  
  12. return [divContainer, divGridElement];
  13.  
  14. }
  15.  
  16. function generateRandomNumbers(n) {
  17. let arrOfNumbers = [];
  18.  
  19. let playerOnePosX = Math.floor(Math.random() * n) + 0;
  20. let playerOnePosY = Math.floor(Math.random() * n) + 0;
  21.  
  22. let playerTwoPosX = Math.floor(Math.random() * n) + 0;
  23. let playerTwoPosY = Math.floor(Math.random() * n) + 0;
  24.  
  25. let samePosition = playerOnePosX === playerTwoPosX && playerOnePosY === playerTwoPosY;
  26.  
  27. while (samePosition) {
  28. playerOnePosX = Math.floor(Math.random() * n) + 0;
  29. playerOnePosY = Math.floor(Math.random() * n) + 0;
  30.  
  31. playerTwoPosX = Math.floor(Math.random() * n) + 0;
  32. playerTwoPosY = Math.floor(Math.random() * n) + 0;
  33.  
  34. samePosition = playerOnePosX === playerTwoPosX && playerOnePosY === playerTwoPosY;
  35. }
  36.  
  37. arrOfNumbers.push(playerOnePosX, playerOnePosY, playerTwoPosX, playerTwoPosY);
  38.  
  39. return arrOfNumbers;
  40. }
  41.  
  42.  
  43. function boardCreation(n) {
  44.  
  45. let [playerOnePosX, playerOnePosY, playerTwoPosX, playerTwoPosY] = generateRandomNumbers(n);
  46.  
  47. let [divContainer, divGridElement] = getAndSetElements(n);
  48.  
  49. for (let x = 0; x < n; x++) {
  50.  
  51. for (let y = 0; y < n; y++) {
  52.  
  53. let divElement = document.createElement('div');
  54.  
  55. if (playerOnePosX === x && playerOnePosY === y) {
  56. divElement.textContent = 1;
  57. } else if (playerTwoPosX === x && playerTwoPosY === y) {
  58. divElement.textContent = 2;
  59. }
  60. divElement.classList.add('cell');
  61. divContainer.style.border = '5px solid black';
  62. divGridElement.appendChild(divElement);
  63. }
  64.  
  65. }
  66. }
  67.  
  68. function movingPlayers(clickedCell) {
  69. let secondClick = false;
  70.  
  71. if (secondClick) {
  72. clickedCell.textContent = '*';
  73. } else {
  74. let divContainerElement = document.querySelectorAll('.cell');
  75. divContainerElement = Array.from(divContainerElement);
  76. let playerCurCell = divContainerElement.find(el => el.textContent === '1');
  77.  
  78. playerCurCell.textContent = '';
  79. clickedCell.textContent = '1';
  80. cellIndicesMatrix(divContainerElement.map(el => el.textContent), inpNum);
  81. // secondClick = true;
  82. divContainerElement.forEach(cell => cell.style.background = 'antiquewhite');
  83. }
  84. }
  85.  
  86. function cellIndicesMatrix(arrayOfCells, inpNum) {
  87. let matrixOfIndices = [];
  88. let subMatrix = [];
  89.  
  90. for (let o = 0; o < arrayOfCells.length;) {
  91.  
  92. for (let i = 0; i < inpNum; i++) {
  93.  
  94. if (arrayOfCells[o] === '') {
  95. subMatrix.push(o);
  96. } else {
  97. if (arrayOfCells[o] === '1') {
  98. subMatrix.push('one');
  99. } else {
  100. subMatrix.push('two');
  101. }
  102. }
  103.  
  104. o++;
  105.  
  106. }
  107.  
  108. matrixOfIndices.push(subMatrix);
  109.  
  110. subMatrix = [];
  111. }
  112.  
  113. findAdjacentIndices(matrixOfIndices);
  114. }
  115.  
  116. function findPlayersIndices(matrixOfIndices) {
  117. let playerOneIndices = undefined;
  118. let playerTwoIndices = undefined;
  119.  
  120. matrixOfIndices.forEach((arr, o) => {
  121. arr.forEach((el, i) => el === 'one' ? playerOneIndices = [o, i] : false);
  122. arr.forEach((el, i) => el === 'two' ? playerTwoIndices = [o, i] : false);
  123. });
  124.  
  125. return [playerOneIndices, playerTwoIndices];
  126. }
  127.  
  128. function findAdjacentIndices(matrixOfIndices) {
  129. let [playerOneIndices, playerTwoIndices] = findPlayersIndices(matrixOfIndices);
  130. let arrOfCells = document.querySelectorAll('.cell');
  131. let indexCounter = 0;
  132.  
  133. for (let o = 0; o < matrixOfIndices.length; o++) {
  134.  
  135. for (let i = 0; i < matrixOfIndices[o].length; i++) {
  136.  
  137. if (i === playerOneIndices[1] && (playerOneIndices[0] + 1 === o || playerOneIndices[0] - 1 === o || playerOneIndices[0] === o)) {
  138. if (matrixOfIndices[o][i + 1] !== undefined && matrixOfIndices[o][i + 1] !== 'two') {
  139. arrOfCells[indexCounter + 1].style.background = 'lightgreen';
  140. }
  141. if (matrixOfIndices[o][i - 1] !== undefined && matrixOfIndices[o][i - 1] !== 'two') {
  142. arrOfCells[indexCounter - 1].style.background = 'lightgreen';
  143. }
  144. if (matrixOfIndices[o][i] !== 'two' && (playerOneIndices[0] + 1 === o || playerOneIndices[0] - 1 === o)) {
  145. arrOfCells[indexCounter].style.background = 'lightgreen';
  146. }
  147. }
  148.  
  149. indexCounter++;
  150. }
  151.  
  152. }
  153.  
  154. console.log(matrixOfIndices);
  155.  
  156. }
  157.  
  158. const submitElement = document.getElementById('submitBtn');
  159.  
  160. let inpNum = undefined;
  161. let counter = 0;
  162.  
  163. submitElement.addEventListener('click', e => {
  164. e.preventDefault();
  165. let divGridElement = document.querySelector('.grid');
  166. divGridElement.textContent = '';
  167. let inputNumber = document.getElementById('number').value;
  168. if (inputNumber % 2 === 0 || inputNumber < 2 || inputNumber > 20 || inputNumber === '') {
  169. return alert('You must enter an odd number!');
  170. }
  171.  
  172. inpNum = inputNumber;
  173. boardCreation(inputNumber);
  174. let divContainerElement = document.querySelectorAll('.cell');
  175. divContainerElement = Array.from(divContainerElement);
  176. cellIndicesMatrix(divContainerElement.map(el => el.textContent), inpNum);
  177. });
  178.  
  179.  
  180. const startBtnElement = document.getElementById('startBtn');
  181.  
  182. startBtnElement.addEventListener('click', e => {
  183. e.preventDefault();
  184.  
  185. const checkForBoard = document.querySelectorAll('.cell').length;
  186. let coordElement = document.getElementById('coord');
  187. if (checkForBoard > 0) {
  188. coordElement.style.display = 'inline-block';
  189. } else {
  190. alert('You must create the board first!');
  191. }
  192. });
  193.  
  194.  
  195. const divContainerElement = document.querySelector('.container');
  196.  
  197. divContainerElement.addEventListener('click', e => {
  198. if (e.target.classList[0] === 'cell') {
  199. const clickedCell = e.target;
  200. movingPlayers(clickedCell);
  201. }
  202. });
Advertisement
Add Comment
Please, Sign In to add comment