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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. *
  3. * @constructor
  4. */
  5. var Board = function(scoreBoard) {
  6. this.cups = [];
  7. this.dom = document.getElementById('cont');
  8. this.dom.addEventListener('keypress', 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. for (var i = 1; i <= 10; i++) {
  21. var cupname = "cup" + i;
  22. var cup = document.getElementById(cupname);
  23. cup.setAttribute('marked', 'false');
  24. cup.setAttribute('data-intent', 'gamecup');
  25. this.cups.push(cup);
  26. }
  27. };
  28. /**
  29. *
  30. * @param container
  31. */
  32. Board.prototype.disableAll = function() {
  33. alert('disableAll');
  34. this.cups.forEach(function(cup) {
  35. //cup.classList.add('notActive');
  36. });
  37. };
  38. Board.prototype.enableAll = function() {
  39. alert('enableAll');
  40. this.cups.forEach(function(cup) {
  41. //cup.classList.remove('notActive');
  42. cup.setAttribute('marked', 'false');
  43. });
  44. };
  45. Board.prototype.enableTurn = function() {
  46. alert('enableTurn');
  47. this.cups.forEach(function(cup) {
  48. if (cup.getAttribute('marked') === 'false') {
  49. //cup.classList.remove('notActive');
  50. cup.setAttribute('active', 'true');
  51. }
  52. });
  53. };
  54. Board.prototype.highlightcups = function(pos) {
  55. var cups = this.cups;
  56. alert('highlightcups');
  57. pos.forEach(function(i) {
  58. // cups[i].classList.add('colorRed');
  59. });
  60. cups.forEach(function(cup) {
  61. cup.setAttribute('marked', 'true');
  62. });
  63. };
  64. Board.prototype.lowlightcups = function() {
  65. alert('lowlightcups');
  66. var cups = this.cups;
  67. cups.forEach(function(cup) {
  68. //cup.classList.add('colorWhite');
  69. });
  70. };
  71. Board.prototype.getCup = function(code) {
  72. alert(code);
  73. switch (code)
  74. {
  75. case 'e':
  76. case 'E':
  77. //root.style.setProperty('--top325', "-350"+ "px");
  78. ball.style.top = "-350px"
  79. ball.style.left = "-155px";
  80. return "cup1";
  81. break;
  82. case 'r':
  83. case 'R':
  84. //root.style.setProperty('--top325', "-350"+ "px");
  85. ball.style.top = "-350px"
  86. ball.style.left = "-50px";
  87. //removeCup("2");
  88. break;
  89. case 't':
  90. case 'T':
  91. //root.style.setProperty('--top325', "-350"+ "px");
  92. ball.style.top = "-350px"
  93. ball.style.left = "55px";
  94. //removeCup("3");
  95. break;
  96. case 'z':
  97. case 'Z':
  98. //root.style.setProperty('--top325', "-350"+ "px");
  99. ball.style.top = "-350px"
  100. ball.style.left = "160px";
  101. //removeCup("4");
  102. break;
  103. case 'd':
  104. case 'D':
  105. //root.style.setProperty('--top325', "-290"+ "px");
  106. ball.style.top = "-290px"
  107. ball.style.left = "-100px";
  108. //removeCup("5");
  109. break;
  110. case 'f':
  111. case 'F':
  112. //root.style.setProperty('--top325', "-290"+ "px");
  113. ball.style.top = "-290px"
  114. ball.style.left = "0px";
  115. //removeCup("6");
  116. break;
  117. case 'g':
  118. case 'G':
  119. //root.style.setProperty('--top325', "-290"+ "px");
  120. ball.style.top = "-290px"
  121. ball.style.left = "110px";
  122. //removeCup("7");
  123. break;
  124. case 'c':
  125. case 'C':
  126. //root.style.setProperty('--top325', "-220"+ "px");
  127. ball.style.top = "-220px"
  128. ball.style.left = "-50px";
  129. //removeCup("8");
  130. break;
  131. case 'v':
  132. case 'V':
  133. //root.style.setProperty('--top325', "-220"+ "px");
  134. ball.style.top = "-220px"
  135. ball.style.left = "55px";
  136. //removeCup("9");
  137. break;
  138. case 'b':
  139. case 'B':
  140. //root.style.setProperty('--top325', "-160"+ "px");
  141. ball.style.top = "-160px"
  142. ball.style.left = "0px";
  143. //removeCup("10");
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. /**
  150. *
  151. * @param event
  152. */
  153. Board.prototype.mark = function(event) {
  154. alert(this.ready);
  155. if (this.ready) {
  156. var target = document.getElementById(this.getCup(event.key));
  157. alert(target);
  158. if (target.getAttribute('data-intent') === 'gamecup' && target.getAttribute('active') === 'true') {
  159. this.onMark(this.cups.indexOf(target));
  160. this.disableAll();
  161. }
  162. }
  163. };
  164. /**
  165. *
  166. * @param cupId
  167. * @param label
  168. */
  169. Board.prototype.doMark = function(cupId, label) {
  170. var cup = this.cups[cupId];
  171. cup.textContent = label;
  172. //cup.classList.add('notActive');
  173. // removeCup
  174. cup.setAttribute('marked', 'true');
  175. };
  176. /**
  177. *
  178. * @param pos
  179. */
  180. Board.prototype.doWinner = function(pos) {
  181. this.disableAll();
  182. this.highlightcups(pos);
  183. };
  184. /**
  185. *
  186. */
  187. Board.prototype.doDraw = function() {
  188. this.lowlightcups();
  189. };
  190. /**
  191. *
  192. */
  193. Board.prototype.highlightScoreboard = function(playerId) {
  194. this.scoreBoard.forEach(function(score) {
  195. score.classList.remove('active');
  196. });
  197. this.scoreBoard.forEach(function(board){
  198. if (board.getAttribute('playerId') == playerId) {
  199. board.classList.add('active');
  200. }
  201. });
  202. };
  203. /**
  204. *
  205. * @returns {{win: boolean, pos: Array}}
  206. */
  207. Board.prototype.checkWinner = function() {
  208. var winPosition = [
  209. [0, 1, 2],
  210. [3, 4, 5],
  211. [6, 7, 8],
  212. [0, 3, 6],
  213. [1, 4, 7],
  214. [2, 5, 8],
  215. [0, 4, 8],
  216. [6, 4, 2]
  217. ];
  218. var player = this.players[this.currentTurn];
  219. var pos = [];
  220. var win = winPosition.some(function(win) {
  221. if (this.cups[win[0]].textContent === player.label) {
  222. var res = this.cups[win[0]].textContent === this.cups[win[1]].textContent && this.cups[win[1]].textContent === this.cups[win[2]].textContent;
  223. if (res) {
  224. pos = win;
  225. return true;
  226. }
  227. }
  228. return false;
  229. }, this);
  230. return {
  231. win: win,
  232. pos: pos
  233. };
  234. };
  235. /**
  236. *
  237. * @returns {boolean}
  238. */
  239. Board.prototype.checkDraw = function() {
  240. return this.cups.every(function(cup) {
  241. return cup.textContent === this.players[0].label || cup.textContent === this.players[1].label;
  242. }, this);
  243. };
  244. /**
  245. *
  246. * @param player
  247. */
  248. Board.prototype.addPlayer = function(player) {
  249. if (this.players.length < 2) {
  250. var isNew = this.players.filter(function(p) {
  251. return p.id == player.id;
  252. }).length === 0;
  253. if (isNew) {
  254. this.players.push(player);
  255. if (this.players.length === 1) {
  256. this.scoreBoard[this.players.length - 1].textContent = player.label + ' ' + player.name;
  257. } else {
  258. this.scoreBoard[this.players.length - 1].textContent = player.name + ' ' + player.label;
  259. }
  260. this.scoreBoard[this.players.length - 1].setAttribute('playerId', player.id);
  261. }
  262. }
  263. };