Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1.  
  2. var height = 10;
  3. var width = 10;
  4. var winLenght = 5;
  5.  
  6. var player = 'X';
  7. var winCounter = 0; // pocitadlo viteznych policek po sobe jdoucich
  8.  
  9.  
  10. function start() {
  11. createGameArena();
  12.  
  13. }
  14.  
  15. // --------------- vytvoření herního pole ----------------- //
  16. function createGameArena() {
  17.  
  18. var gameDiv = document.getElementById('game');
  19. var table = document.createElement('table');
  20.  
  21. for (var i = 0; i < height; i++) {
  22. var tr = document.createElement('tr');
  23. for (var j = 0; j < width; j++) {
  24. var td = document.createElement('td');
  25. td.id = j + 'x' + i;
  26. td.X = j;
  27. td.Y = i;
  28.  
  29. td.addEventListener('click', clickToNode);
  30.  
  31. tr.appendChild(td);
  32. }
  33. table.appendChild(tr);
  34. }
  35. gameDiv.appendChild(table);
  36.  
  37.  
  38. }
  39.  
  40. function clickToNode(e) {
  41.  
  42. /**
  43. *
  44. * @type Element element
  45. */
  46. var element = e.toElement;
  47. element.textContent = player;
  48.  
  49.  
  50. // funkce na kontrlu smeru
  51.  
  52. checkLeftRight(element);
  53. checkUpBottom(element);
  54. checkDiagonalLeftUpToRightBottom(element);
  55. checkDiagonalRightUpToLeftBottom(element);
  56.  
  57. switchPlayer();
  58.  
  59. }
  60.  
  61. function checkLeftRight(rootElement) {
  62.  
  63. var rightElement = getRightElement(rootElement)
  64. while(rightElement != null && rightElement.textContent == player){
  65. rightElement = getRightElement(rightElement);
  66. winCounter++;
  67. }
  68.  
  69. var leftElement = getLeftElement(rootElement);
  70. while(leftElement != null && leftElement.textContent == player){
  71. leftElement = getLeftElement(leftElement);
  72. winCounter++;
  73. }
  74.  
  75. if(winCounter + 1 == winLenght){
  76. gameOver();
  77. }
  78.  
  79. winCounter = 0;
  80. }
  81.  
  82. function checkUpBottom(rootElement) {
  83.  
  84.  
  85. var topElement = getTopElement(rootElement)
  86. while(topElement != null && topElement.textContent == player){
  87. topElement = getTopElement(topElement);
  88. winCounter++;
  89. }
  90.  
  91. var bottomElement = getBottomElement(rootElement);
  92. while(bottomElement != null && bottomElement.textContent == player){
  93. bottomElement = getBottomElement(bottomElement);
  94. winCounter++;
  95. }
  96.  
  97. if(winCounter + 1 == winLenght){
  98. gameOver();
  99. }
  100.  
  101. winCounter = 0;
  102.  
  103.  
  104. }
  105.  
  106. function checkDiagonalLeftUpToRightBottom(rootElement) {
  107.  
  108. var topLeftElement = getTopLeftElement(rootElement)
  109. while(topLeftElement != null && topLeftElement.textContent == player){
  110. topLeftElement = getTopLeftElement(topLeftElement);
  111. winCounter++;
  112. }
  113.  
  114. var bottomRightElement = getBottomRightElement(rootElement);
  115. while(bottomRightElement != null && bottomRightElement.textContent == player){
  116. bottomRightElement = getBottomRightElement(bottomRightElement);
  117. winCounter++;
  118. }
  119.  
  120. if(winCounter + 1 == winLenght){
  121. gameOver();
  122. }
  123.  
  124. winCounter = 0;
  125. }
  126.  
  127. function checkDiagonalRightUpToLeftBottom(rootElement) {
  128.  
  129. var topRightElement = getTopRightElement(rootElement)
  130. while(topRightElement != null && topRightElement.textContent == player){
  131. topRightElement = getTopRightElement(topRightElement);
  132. winCounter++;
  133. }
  134.  
  135. var bottomLeftElement = getBottomLeftElement(rootElement);
  136. while(bottomLeftElement != null && bottomLeftElement.textContent == player){
  137. bottomLeftElement = getbottomLeftElement(bottomLeftElement);
  138. winCounter++;
  139. }
  140.  
  141. if(winCounter + 1 == winLenght){
  142. gameOver();
  143. }
  144.  
  145.  
  146. winCounter = 0;
  147. }
  148.  
  149. function gameOver() {
  150. alert("game over, vyhrál " + player);
  151. }
  152.  
  153.  
  154. // ------------------------------------------------------------------
  155.  
  156. function switchPlayer() {
  157. if (player == 'X')
  158. player = 'O';
  159. else
  160. player = 'X';
  161. }
  162.  
  163. function getTopElement(rootElement) {
  164. var topElementId = rootElement.X + 'x' + (rootElement.Y - 1);
  165. return document.getElementById(topElementId);
  166. }
  167.  
  168. function getBottomElement(rootElement) {
  169. var bottomElementId = rootElement.X + 'x' + (rootElement.Y + 1);
  170. return document.getElementById(bottomElementId);
  171. }
  172.  
  173. function getLeftElement(rootElement) {
  174. var leftElementId = (rootElement.X - 1) + 'x' + rootElement.Y;
  175. return document.getElementById(leftElementId);
  176. }
  177.  
  178. function getRightElement(rootElement) {
  179. var rightElementId = (rootElement.X + 1) + 'x' + rootElement.Y;
  180. return document.getElementById(rightElementId);
  181. }
  182.  
  183. function getTopLeftElement(rootElement) {
  184. var topLeftElementId = (rootElement.X - 1) + 'x' + (rootElement.Y - 1);
  185. return document.getElementById(topLeftElementId);
  186. }
  187.  
  188. function getTopRightElement(rootElement) {
  189. var topRitghtElementId = (rootElement.X + 1) + 'x' + (rootElement.Y - 1);
  190. return document.getElementById(topRitghtElementId);
  191. }
  192.  
  193. function getBottomLeftElement(rootElement) {
  194. var bottomLeftElementId = (rootElement.X - 1) + 'x' + (rootElement.Y + 1);
  195. return document.getElementById(bottomLeftElementId);
  196. }
  197.  
  198. function getBottomRightElement(rootElement) {
  199. var bottomRightElementId = (rootElement.X + 1) + 'x' + (rootElement.Y + 1);
  200. return document.getElementById(bottomRightElementId);
  201. }
  202.  
  203.  
  204.  
  205.  
  206. // ----------- debugovací funkce ---------------- //
  207.  
  208. function lookAround(element) {
  209. console.log('left:');
  210. console.log(getLeftElement(element));
  211. console.log('right:');
  212. console.log(getRightElement(element));
  213. console.log('top:');
  214. console.log(getTopElement(element));
  215. console.log('bottom:');
  216. console.log(getBottomElement(element));
  217. console.log('top left:');
  218. console.log(getTopLeftElement(element));
  219. console.log('top right:');
  220. console.log(getTopRightElement(element));
  221. console.log('bottom left:');
  222. console.log(getBottomLeftElement(element));
  223. console.log('bottom right:');
  224. console.log(getBottomRightElement(element));
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement