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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. *
  3. * @constructor
  4. */
  5. var Board = function(scoreBoard) {
  6. this.cups = [];
  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(cupId){};
  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 cup = 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. cup.classList.add('gamecup');
  31. cup.classList.add('notActive');
  32. cup.setAttribute('marked', 'false');
  33. cup.setAttribute('data-intent', 'gamecup');
  34. this.dom.classList.add('gameBoard');
  35. for (var i = 0; i < 9; i++) {
  36. this.cups.push(cup.cloneNode(true));
  37. }
  38. for (var r, i = 0; i < 9; i += 3) {
  39. r = row.cloneNode(false);
  40. r.appendChild(this.cups[i]);
  41. r.appendChild(col.cloneNode(false));
  42. r.appendChild(this.cups[i + 1]);
  43. r.appendChild(col.cloneNode(false));
  44. r.appendChild(this.cups[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.cups.forEach(function(cup) {
  60. //cup.classList.add('notActive');
  61. });
  62. };
  63. Board.prototype.enableAll = function() {
  64. this.cups.forEach(function(cup) {
  65. //cup.classList.remove('notActive');
  66. cup.setAttribute('marked', 'false');
  67. });
  68. };
  69. Board.prototype.enableTurn = function() {
  70. this.cups.forEach(function(cup) {
  71. if (cup.getAttribute('marked') === 'false') {
  72. //cup.classList.remove('notActive');
  73. cup.setAttribute('active', 'true');
  74. }
  75. });
  76. };
  77. Board.prototype.highlightcups = function(pos) {
  78. var cups = this.cups;
  79. pos.forEach(function(i) {
  80. // cups[i].classList.add('colorRed');
  81. });
  82. cups.forEach(function(cup) {
  83. cup.setAttribute('marked', 'true');
  84. });
  85. };
  86. Board.prototype.lowlightcups = function() {
  87. alert('lowlightcups');
  88. var cups = this.cups;
  89. cups.forEach(function(cup) {
  90. //cup.classList.add('colorWhite');
  91. });
  92. };
  93. /**
  94. *
  95. * @param event
  96. */
  97. Board.prototype.mark = function(event) {
  98. var target = event.target;
  99. if (this.ready && target.getAttribute('data-intent') === 'gamecup' && target.getAttribute('active') === 'true') {
  100. this.onMark(this.cups.indexOf(target));
  101. this.disableAll();
  102. }
  103. };
  104. /**
  105. *
  106. * @param cupId
  107. * @param label
  108. */
  109. Board.prototype.doMark = function(cupId, label) {
  110. var cup = this.cups[cupId];
  111. cup.textContent = label;
  112. //cup.classList.add('notActive');
  113. // removeCup
  114. cup.setAttribute('marked', 'true');
  115. };
  116. /**
  117. *
  118. * @param pos
  119. */
  120. Board.prototype.doWinner = function(pos) {
  121. this.disableAll();
  122. this.highlightcups(pos);
  123. };
  124. /**
  125. *
  126. */
  127. Board.prototype.doDraw = function() {
  128. this.lowlightcups();
  129. };
  130. /**
  131. *
  132. */
  133. Board.prototype.highlightScoreboard = function(playerId) {
  134. this.scoreBoard.forEach(function(score) {
  135. // score.classList.remove('active');
  136. });
  137. this.scoreBoard.forEach(function(board){
  138. if (board.getAttribute('playerId') == playerId) {
  139. // board.classList.add('active');
  140. }
  141. });
  142. };
  143. /**
  144. *
  145. * @returns {{win: boolean, pos: Array}}
  146. */
  147. Board.prototype.checkWinner = function() {
  148. var winPosition = [
  149. [0, 1, 2],
  150. [3, 4, 5],
  151. [6, 7, 8],
  152. [0, 3, 6],
  153. [1, 4, 7],
  154. [2, 5, 8],
  155. [0, 4, 8],
  156. [6, 4, 2]
  157. ];
  158. var player = this.players[this.currentTurn];
  159. var pos = [];
  160. var win = winPosition.some(function(win) {
  161. if (this.cups[win[0]].textContent === player.label) {
  162. var res = this.cups[win[0]].textContent === this.cups[win[1]].textContent && this.cups[win[1]].textContent === this.cups[win[2]].textContent;
  163. if (res) {
  164. pos = win;
  165. return true;
  166. }
  167. }
  168. return false;
  169. }, this);
  170. return {
  171. win: win,
  172. pos: pos
  173. };
  174. };
  175. /**
  176. *
  177. * @returns {boolean}
  178. */
  179. Board.prototype.checkDraw = function() {
  180. return this.cups.every(function(cup) {
  181. return cup.textContent === this.players[0].label || cup.textContent === this.players[1].label;
  182. }, this);
  183. };
  184. /**
  185. *
  186. * @param player
  187. */
  188. Board.prototype.addPlayer = function(player) {
  189. if (this.players.length < 2) {
  190. var isNew = this.players.filter(function(p) {
  191. return p.id == player.id;
  192. }).length === 0;
  193. if (isNew) {
  194. this.players.push(player);
  195. if (this.players.length === 1) {
  196. this.scoreBoard[this.players.length - 1].textContent = player.label + ' ' + player.name;
  197. } else {
  198. this.scoreBoard[this.players.length - 1].textContent = player.name + ' ' + player.label;
  199. }
  200. this.scoreBoard[this.players.length - 1].setAttribute('playerId', player.id);
  201. }
  202. }
  203. };