Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Board.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. *
  3. * @constructor
  4. */
  5. var Board = function(scoreBoard) {
  6. this.cells = [];
  7. this.dom = document.createElement('table');
  8. this.dom.addEventListener('click', this.mark.bind(this));
  9. this.players = [];
  10. this.currentTurn = 0;
  11. this.ready = false;
  12. this.scoreBoard = scoreBoard;
  13. this.init();
  14. };
  15. Board.prototype.onMark = function(cellId){};
  16. /**
  17. *
  18. */
  19. Board.prototype.init = function() {
  20. var row = document.createElement('tr');
  21. var rowCol = document.createElement('td');
  22. var col = document.createElement('td');
  23. var cell = document.createElement('td');
  24. rowCol.classList.add('boardRow');
  25. rowCol.classList.add('bgWhite');
  26. rowCol.setAttribute('colspan', 5);
  27. row.appendChild(rowCol);
  28. col.classList.add('boardCol');
  29. col.classList.add('bgWhite');
  30. cell.classList.add('gameCell');
  31. cell.classList.add('notActive');
  32. cell.setAttribute('marked', 'false');
  33. cell.setAttribute('data-intent', 'gameCell');
  34. this.dom.classList.add('gameBoard');
  35. for (var i = 0; i < 9; i++) {
  36. this.cells.push(cell.cloneNode(true));
  37. }
  38. for (var r, i = 0; i < 9; i += 3) {
  39. r = row.cloneNode(false);
  40. r.appendChild(this.cells[i]);
  41. r.appendChild(col.cloneNode(false));
  42. r.appendChild(this.cells[i + 1]);
  43. r.appendChild(col.cloneNode(false));
  44. r.appendChild(this.cells[i + 2]);
  45. this.dom.appendChild(r);
  46. if (i < 6) {
  47. this.dom.appendChild(row.cloneNode(true));
  48. }
  49. }
  50. };
  51. /**
  52. *
  53. * @param container
  54. */
  55. Board.prototype.bindTo = function(container) {
  56. container.appendChild(this.dom);
  57. };
  58. Board.prototype.disableAll = function() {
  59. this.cells.forEach(function(cell) {
  60. cell.classList.add('notActive');
  61. });
  62. };
  63. Board.prototype.enableAll = function() {
  64. this.cells.forEach(function(cell) {
  65. cell.classList.remove('notActive');
  66. cell.setAttribute('marked', 'false');
  67. });
  68. };
  69. Board.prototype.enableTurn = function() {
  70. this.cells.forEach(function(cell) {
  71. if (cell.getAttribute('marked') === 'false') {
  72. cell.classList.remove('notActive');
  73. cell.setAttribute('active', 'true');
  74. }
  75. });
  76. };
  77. Board.prototype.highlightCells = function(pos) {
  78. var cells = this.cells;
  79. pos.forEach(function(i) {
  80. cells[i].classList.add('colorRed');
  81. });
  82. cells.forEach(function(cell) {
  83. cell.setAttribute('marked', 'true');
  84. });
  85. };
  86. Board.prototype.lowlightCells = function() {
  87. var cells = this.cells;
  88. cells.forEach(function(cell) {
  89. cell.classList.add('colorWhite');
  90. });
  91. };
  92. /**
  93. *
  94. * @param event
  95. */
  96. Board.prototype.mark = function(event) {
  97. var target = event.target;
  98. if (this.ready && target.getAttribute('data-intent') === 'gameCell' && target.getAttribute('active') === 'true') {
  99. this.onMark(this.cells.indexOf(target));
  100. this.disableAll();
  101. }
  102. };
  103. /**
  104. *
  105. * @param cellId
  106. * @param label
  107. */
  108. Board.prototype.doMark = function(cellId, label) {
  109. var cell = this.cells[cellId];
  110. cell.textContent = label;
  111. cell.classList.add('notActive');
  112. cell.setAttribute('marked', 'true');
  113. };
  114. /**
  115. *
  116. * @param pos
  117. */
  118. Board.prototype.doWinner = function(pos) {
  119. this.disableAll();
  120. this.highlightCells(pos);
  121. };
  122. /**
  123. *
  124. */
  125. Board.prototype.doDraw = function() {
  126. this.lowlightCells();
  127. };
  128. /**
  129. *
  130. */
  131. Board.prototype.highlightScoreboard = function(playerId) {
  132. this.scoreBoard.forEach(function(score) {
  133. score.classList.remove('active');
  134. });
  135. this.scoreBoard.forEach(function(board){
  136. if (board.getAttribute('playerId') == playerId) {
  137. board.classList.add('active');
  138. }
  139. });
  140. };
  141. /**
  142. *
  143. * @returns {{win: boolean, pos: Array}}
  144. */
  145. Board.prototype.checkWinner = function() {
  146. var winPosition = [
  147. [0, 1, 2],
  148. [3, 4, 5],
  149. [6, 7, 8],
  150. [0, 3, 6],
  151. [1, 4, 7],
  152. [2, 5, 8],
  153. [0, 4, 8],
  154. [6, 4, 2]
  155. ];
  156. var player = this.players[this.currentTurn];
  157. var pos = [];
  158. var win = winPosition.some(function(win) {
  159. if (this.cells[win[0]].textContent === player.label) {
  160. var res = this.cells[win[0]].textContent === this.cells[win[1]].textContent && this.cells[win[1]].textContent === this.cells[win[2]].textContent;
  161. if (res) {
  162. pos = win;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }, this);
  168. return {
  169. win: win,
  170. pos: pos
  171. };
  172. };
  173. /**
  174. *
  175. * @returns {boolean}
  176. */
  177. Board.prototype.checkDraw = function() {
  178. return this.cells.every(function(cell) {
  179. return cell.textContent === this.players[0].label || cell.textContent === this.players[1].label;
  180. }, this);
  181. };
  182. /**
  183. *
  184. * @param player
  185. */
  186. Board.prototype.addPlayer = function(player) {
  187. if (this.players.length < 2) {
  188. var isNew = this.players.filter(function(p) {
  189. return p.id == player.id;
  190. }).length === 0;
  191. if (isNew) {
  192. this.players.push(player);
  193. if (this.players.length === 1) {
  194. this.scoreBoard[this.players.length - 1].textContent = player.label + ' ' + player.name;
  195. } else {
  196. this.scoreBoard[this.players.length - 1].textContent = player.name + ' ' + player.label;
  197. }
  198. this.scoreBoard[this.players.length - 1].setAttribute('playerId', player.id);
  199. }
  200. }
  201. };