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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. *
  3. * @constructor
  4. */
  5. var root = document.documentElement;
  6. var ball = document.getElementById("ball");
  7. var Board = function(scoreBoard) {
  8. this.cups = [];
  9. this.dom = document.getElementById('cont');
  10. this.dom.addEventListener('keypress', this.mark.bind(this));
  11. this.players = [];
  12. this.currentTurn = 0;
  13. this.ready = false;
  14. this.scoreBoard = scoreBoard;
  15. this.init();
  16. };
  17. Board.prototype.onMark = function(cupId){};
  18. /**
  19. *
  20. */
  21. Board.prototype.init = function() {
  22. for (var i = 1; i <= 10; i++) {
  23. var cupname = "cup" + i;
  24. var cup = document.getElementById(cupname);
  25. cup.setAttribute('marked', 'false');
  26. cup.setAttribute('data-intent', 'gamecup');
  27. this.cups.push(cup);
  28. }
  29. };
  30. /**
  31. *
  32. * @param container
  33. */
  34. Board.prototype.disableAll = function() {
  35. this.cups.forEach(function(cup) {
  36. //cup.classList.add('notActive');
  37. });
  38. };
  39. Board.prototype.enableAll = function() {
  40. alert('enableAll');
  41. this.cups.forEach(function(cup) {
  42. //cup.classList.remove('notActive');
  43. cup.setAttribute('marked', 'false');
  44. });
  45. };
  46. Board.prototype.enableTurn = function() {
  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. ball.classList.add("Shoot");
  73. switch (code)
  74. {
  75. case 'q':
  76. case 'Q':
  77. root.style.setProperty('--top325', "-515"+ "px");
  78. ball.style.top = "-515px";
  79. ball.style.left = "-155px";
  80. return "cup1";
  81. break;
  82. case 'w':
  83. case 'W':
  84. root.style.setProperty('--top325', "-515"+ "px");
  85. ball.style.top = "-515px";
  86. ball.style.left = "-50px";
  87. return "cup2";
  88. //removeCup("2");j
  89. break;
  90. case 'e':
  91. case 'E':
  92. root.style.setProperty('--top325', "-515"+ "px");
  93. ball.style.top = "-515px";
  94. ball.style.left = "50px";
  95. return "cup3";
  96. //removeCup("3");
  97. break;
  98. case 'r':
  99. case 'R':
  100. root.style.setProperty('--top325', "-515"+ "px");
  101. ball.style.top = "-515px";
  102. ball.style.left = "150px";
  103. return "cup4";
  104. //removeCup("4");
  105. break;
  106. case 'a':
  107. case 'A':
  108. root.style.setProperty('--top325', "-430"+ "px");
  109. ball.style.top = "-430px";
  110. ball.style.left = "-100px";
  111. return "cup5";
  112. //removeCup("5");
  113. break;
  114. case 's':
  115. case 'S':
  116. root.style.setProperty('--top325', "-430"+ "px");
  117. ball.style.top = "-430px";
  118. ball.style.left = "0px";
  119. return "cup6";
  120. //removeCup("6");
  121. break;
  122. case 'd':
  123. case 'D':
  124. root.style.setProperty('--top325', "-430"+ "px");
  125. ball.style.top = "-430px";
  126. ball.style.left = "100px";
  127. //removeCup("7");
  128. return "cup7";
  129. break;
  130. case 'y':
  131. case 'Y':
  132. root.style.setProperty('--top325', "-340"+ "px");
  133. ball.style.top = "-340px"
  134. ball.style.left = "-50px";
  135. //removeCup("8");
  136. return "cup8";
  137. break;
  138. case 'x':
  139. case 'X':
  140. root.style.setProperty('--top325', "-340"+ "px");
  141. ball.style.top = "-340px"
  142. ball.style.left = "50px";
  143. //removeCup("9");
  144. return "cup9";
  145. break;
  146. case 'c':
  147. case 'C':
  148. root.style.setProperty('--top325', "-250"+ "px");
  149. ball.style.top = "-250px"
  150. ball.style.left = "0px";
  151. //removeCup("10");
  152. return "cup10";
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. /**
  159. *
  160. * @param event
  161. */
  162. Board.prototype.mark = function(event) {
  163. if (this.ready) {
  164. var target = document.getElementById(this.getCup(event.key));
  165. if (target.getAttribute('data-intent') === 'gamecup' && target.getAttribute('active') === 'true') {
  166. this.onMark(this.cups.indexOf(target));
  167. this.disableAll();
  168. }
  169. }
  170. };
  171. /**
  172. *
  173. * @param cupId
  174. * @param label
  175. */
  176. Board.prototype.doMark = function(cupId, label) {
  177. var cup = this.cups[cupId];
  178. cup.textContent = label;
  179. //cup.classList.add('notActive');
  180. // removeCup
  181. setTimeout(this.cups[cupId].classList.add("fadeAway") ,1000);
  182. cup.setAttribute('marked', 'true');
  183. setTimeout(function() {
  184. ball.style.top = "0px";
  185. ball.style.left = "0px";
  186. }, 1000);
  187. };
  188. /**
  189. *
  190. * @param pos
  191. */
  192. Board.prototype.doWinner = function(pos) {
  193. this.disableAll();
  194. this.highlightcups(pos);
  195. };
  196. /**
  197. *
  198. */
  199. Board.prototype.doDraw = function() {
  200. alert('doDraw');
  201. this.lowlightcups();
  202. };
  203. /**
  204. *
  205. */
  206. Board.prototype.highlightScoreboard = function(playerId) {
  207. this.scoreBoard.forEach(function(score) {
  208. score.classList.remove('active');
  209. });
  210. this.scoreBoard.forEach(function(board){
  211. if (board.getAttribute('playerId') == playerId) {
  212. board.classList.add('active');
  213. }
  214. });
  215. };
  216. /**
  217. *
  218. * @returns {{win: boolean, pos: Array}}
  219. */
  220. Board.prototype.checkWinner = function() {
  221. var winPosition = [
  222. [0, 1, 2],
  223. [3, 4, 5],
  224. [6, 7, 8],
  225. [0, 3, 6],
  226. [1, 4, 7],
  227. [2, 5, 8],
  228. [0, 4, 8],
  229. [6, 4, 2]
  230. ];
  231. var player = this.players[this.currentTurn];
  232. var pos = [];
  233. var win = winPosition.some(function(win) {
  234. if (this.cups[win[0]].textContent === player.label) {
  235. var res = this.cups[win[0]].textContent === this.cups[win[1]].textContent && this.cups[win[1]].textContent === this.cups[win[2]].textContent;
  236. if (res) {
  237. pos = win;
  238. return true;
  239. }
  240. }
  241. return false;
  242. }, this);
  243. return {
  244. win: win,
  245. pos: pos
  246. };
  247. };
  248. /**
  249. *
  250. * @returns {boolean}
  251. */
  252. Board.prototype.checkDraw = function() {
  253. return this.cups.every(function(cup) {
  254. return cup.textContent === this.players[0].label || cup.textContent === this.players[1].label;
  255. }, this);
  256. };
  257. /**
  258. *
  259. * @param player
  260. */
  261. Board.prototype.addPlayer = function(player) {
  262. if (this.players.length < 2) {
  263. var isNew = this.players.filter(function(p) {
  264. return p.id == player.id;
  265. }).length === 0;
  266. if (isNew) {
  267. this.players.push(player);
  268. if (this.players.length === 1) {
  269. this.scoreBoard[this.players.length - 1].textContent = player.label + ' ' + player.name;
  270. } else {
  271. this.scoreBoard[this.players.length - 1].textContent = player.name + ' ' + player.label;
  272. }
  273. this.scoreBoard[this.players.length - 1].setAttribute('playerId', player.id);
  274. }
  275. }
  276. };