2021-06-01 11:12:45 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-06-18 16:06:24 +02:00
|
|
|
var root = document.documentElement;
|
|
|
|
var ball = document.getElementById("ball");
|
|
|
|
|
2021-06-01 11:12:45 +02:00
|
|
|
var Board = function(scoreBoard) {
|
2021-06-14 16:56:35 +02:00
|
|
|
this.cups = [];
|
2021-06-18 14:53:55 +02:00
|
|
|
this.dom = document.getElementById('cont');
|
|
|
|
this.dom.addEventListener('keypress', this.mark.bind(this));
|
2021-06-01 11:12:45 +02:00
|
|
|
this.players = [];
|
|
|
|
this.currentTurn = 0;
|
|
|
|
this.ready = false;
|
|
|
|
|
|
|
|
this.scoreBoard = scoreBoard;
|
|
|
|
|
|
|
|
this.init();
|
|
|
|
};
|
|
|
|
|
2021-06-14 16:56:35 +02:00
|
|
|
Board.prototype.onMark = function(cupId){};
|
2021-06-01 11:12:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Board.prototype.init = function() {
|
2021-06-18 14:53:55 +02:00
|
|
|
for (var i = 1; i <= 10; i++) {
|
|
|
|
var cupname = "cup" + i;
|
|
|
|
var cup = document.getElementById(cupname);
|
|
|
|
cup.setAttribute('marked', 'false');
|
|
|
|
cup.setAttribute('data-intent', 'gamecup');
|
|
|
|
this.cups.push(cup);
|
2021-06-01 11:12:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param container
|
|
|
|
*/
|
2021-06-18 14:53:55 +02:00
|
|
|
|
2021-06-01 11:12:45 +02:00
|
|
|
|
|
|
|
Board.prototype.disableAll = function() {
|
2021-06-14 16:56:35 +02:00
|
|
|
this.cups.forEach(function(cup) {
|
|
|
|
//cup.classList.add('notActive');
|
2021-06-01 11:12:45 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Board.prototype.enableAll = function() {
|
2021-06-18 14:53:55 +02:00
|
|
|
alert('enableAll');
|
2021-06-14 16:56:35 +02:00
|
|
|
this.cups.forEach(function(cup) {
|
|
|
|
//cup.classList.remove('notActive');
|
|
|
|
cup.setAttribute('marked', 'false');
|
2021-06-01 11:12:45 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Board.prototype.enableTurn = function() {
|
2021-06-14 16:56:35 +02:00
|
|
|
this.cups.forEach(function(cup) {
|
|
|
|
if (cup.getAttribute('marked') === 'false') {
|
|
|
|
//cup.classList.remove('notActive');
|
|
|
|
cup.setAttribute('active', 'true');
|
2021-06-01 11:12:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-14 16:56:35 +02:00
|
|
|
Board.prototype.highlightcups = function(pos) {
|
|
|
|
var cups = this.cups;
|
2021-06-18 14:53:55 +02:00
|
|
|
alert('highlightcups');
|
2021-06-01 11:12:45 +02:00
|
|
|
pos.forEach(function(i) {
|
2021-06-14 16:56:35 +02:00
|
|
|
// cups[i].classList.add('colorRed');
|
2021-06-01 11:12:45 +02:00
|
|
|
});
|
|
|
|
|
2021-06-14 16:56:35 +02:00
|
|
|
cups.forEach(function(cup) {
|
|
|
|
cup.setAttribute('marked', 'true');
|
2021-06-01 11:12:45 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-14 16:56:35 +02:00
|
|
|
Board.prototype.lowlightcups = function() {
|
|
|
|
alert('lowlightcups');
|
|
|
|
var cups = this.cups;
|
|
|
|
cups.forEach(function(cup) {
|
|
|
|
//cup.classList.add('colorWhite');
|
2021-06-01 11:12:45 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-18 14:53:55 +02:00
|
|
|
|
|
|
|
Board.prototype.getCup = function(code) {
|
2021-06-18 16:06:24 +02:00
|
|
|
ball.classList.add("Shoot");
|
2021-06-18 14:53:55 +02:00
|
|
|
switch (code)
|
|
|
|
{
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
root.style.setProperty('--top325', "-515"+ "px");
|
|
|
|
ball.style.top = "-515px";
|
2021-06-18 14:53:55 +02:00
|
|
|
ball.style.left = "-155px";
|
|
|
|
return "cup1";
|
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
root.style.setProperty('--top325', "-515"+ "px");
|
|
|
|
ball.style.top = "-515px";
|
2021-06-18 14:53:55 +02:00
|
|
|
ball.style.left = "-50px";
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup2";
|
2021-06-20 22:32:41 +02:00
|
|
|
//removeCup("2");j
|
2021-06-18 14:53:55 +02:00
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
root.style.setProperty('--top325', "-515"+ "px");
|
|
|
|
ball.style.top = "-515px";
|
|
|
|
ball.style.left = "50px";
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup3";
|
2021-06-18 14:53:55 +02:00
|
|
|
//removeCup("3");
|
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
root.style.setProperty('--top325', "-515"+ "px");
|
|
|
|
ball.style.top = "-515px";
|
|
|
|
ball.style.left = "150px";
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup4";
|
2021-06-18 14:53:55 +02:00
|
|
|
//removeCup("4");
|
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
root.style.setProperty('--top325', "-430"+ "px");
|
|
|
|
ball.style.top = "-430px";
|
2021-06-18 14:53:55 +02:00
|
|
|
ball.style.left = "-100px";
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup5";
|
2021-06-18 14:53:55 +02:00
|
|
|
//removeCup("5");
|
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
root.style.setProperty('--top325', "-430"+ "px");
|
|
|
|
ball.style.top = "-430px";
|
2021-06-18 14:53:55 +02:00
|
|
|
ball.style.left = "0px";
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup6";
|
2021-06-18 14:53:55 +02:00
|
|
|
//removeCup("6");
|
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
root.style.setProperty('--top325', "-430"+ "px");
|
|
|
|
ball.style.top = "-430px";
|
|
|
|
ball.style.left = "100px";
|
2021-06-18 14:53:55 +02:00
|
|
|
//removeCup("7");
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup7";
|
2021-06-18 14:53:55 +02:00
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'y':
|
|
|
|
case 'Y':
|
|
|
|
root.style.setProperty('--top325', "-340"+ "px");
|
|
|
|
ball.style.top = "-340px"
|
2021-06-18 14:53:55 +02:00
|
|
|
ball.style.left = "-50px";
|
|
|
|
//removeCup("8");
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup8";
|
2021-06-18 14:53:55 +02:00
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
root.style.setProperty('--top325', "-340"+ "px");
|
|
|
|
ball.style.top = "-340px"
|
|
|
|
ball.style.left = "50px";
|
2021-06-18 14:53:55 +02:00
|
|
|
//removeCup("9");
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup9";
|
2021-06-18 14:53:55 +02:00
|
|
|
break;
|
2021-06-20 22:32:41 +02:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
root.style.setProperty('--top325', "-250"+ "px");
|
|
|
|
ball.style.top = "-250px"
|
2021-06-18 14:53:55 +02:00
|
|
|
ball.style.left = "0px";
|
|
|
|
//removeCup("10");
|
2021-06-18 16:06:24 +02:00
|
|
|
return "cup10";
|
2021-06-18 14:53:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:12:45 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
Board.prototype.mark = function(event) {
|
2021-06-18 14:53:55 +02:00
|
|
|
if (this.ready) {
|
|
|
|
var target = document.getElementById(this.getCup(event.key));
|
|
|
|
if (target.getAttribute('data-intent') === 'gamecup' && target.getAttribute('active') === 'true') {
|
|
|
|
this.onMark(this.cups.indexOf(target));
|
|
|
|
this.disableAll();
|
|
|
|
|
|
|
|
}
|
2021-06-01 11:12:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2021-06-14 16:56:35 +02:00
|
|
|
* @param cupId
|
2021-06-01 11:12:45 +02:00
|
|
|
* @param label
|
|
|
|
*/
|
2021-06-14 16:56:35 +02:00
|
|
|
Board.prototype.doMark = function(cupId, label) {
|
|
|
|
var cup = this.cups[cupId];
|
|
|
|
cup.textContent = label;
|
|
|
|
//cup.classList.add('notActive');
|
|
|
|
// removeCup
|
2021-06-18 16:06:24 +02:00
|
|
|
setTimeout(this.cups[cupId].classList.add("fadeAway") ,1000);
|
2021-06-14 16:56:35 +02:00
|
|
|
cup.setAttribute('marked', 'true');
|
2021-06-18 16:06:24 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
ball.style.top = "0px";
|
|
|
|
ball.style.left = "0px";
|
|
|
|
}, 1000);
|
2021-06-01 11:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param pos
|
|
|
|
*/
|
|
|
|
Board.prototype.doWinner = function(pos) {
|
|
|
|
this.disableAll();
|
2021-06-14 16:56:35 +02:00
|
|
|
this.highlightcups(pos);
|
2021-06-01 11:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Board.prototype.doDraw = function() {
|
2021-06-18 16:06:24 +02:00
|
|
|
alert('doDraw');
|
2021-06-14 16:56:35 +02:00
|
|
|
this.lowlightcups();
|
2021-06-01 11:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Board.prototype.highlightScoreboard = function(playerId) {
|
|
|
|
this.scoreBoard.forEach(function(score) {
|
2021-06-18 14:53:55 +02:00
|
|
|
score.classList.remove('active');
|
2021-06-01 11:12:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.scoreBoard.forEach(function(board){
|
|
|
|
if (board.getAttribute('playerId') == playerId) {
|
2021-06-18 14:53:55 +02:00
|
|
|
board.classList.add('active');
|
2021-06-01 11:12:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {{win: boolean, pos: Array}}
|
|
|
|
*/
|
|
|
|
Board.prototype.checkWinner = function() {
|
|
|
|
var winPosition = [
|
|
|
|
[0, 1, 2],
|
|
|
|
[3, 4, 5],
|
|
|
|
[6, 7, 8],
|
|
|
|
|
|
|
|
[0, 3, 6],
|
|
|
|
[1, 4, 7],
|
|
|
|
[2, 5, 8],
|
|
|
|
|
|
|
|
[0, 4, 8],
|
|
|
|
[6, 4, 2]
|
|
|
|
];
|
|
|
|
|
|
|
|
var player = this.players[this.currentTurn];
|
|
|
|
var pos = [];
|
|
|
|
|
|
|
|
var win = winPosition.some(function(win) {
|
2021-06-14 16:56:35 +02:00
|
|
|
if (this.cups[win[0]].textContent === player.label) {
|
|
|
|
var res = this.cups[win[0]].textContent === this.cups[win[1]].textContent && this.cups[win[1]].textContent === this.cups[win[2]].textContent;
|
2021-06-01 11:12:45 +02:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
pos = win;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
return {
|
|
|
|
win: win,
|
|
|
|
pos: pos
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
Board.prototype.checkDraw = function() {
|
2021-06-14 16:56:35 +02:00
|
|
|
return this.cups.every(function(cup) {
|
|
|
|
return cup.textContent === this.players[0].label || cup.textContent === this.players[1].label;
|
2021-06-01 11:12:45 +02:00
|
|
|
}, this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param player
|
|
|
|
*/
|
|
|
|
Board.prototype.addPlayer = function(player) {
|
|
|
|
if (this.players.length < 2) {
|
|
|
|
var isNew = this.players.filter(function(p) {
|
|
|
|
return p.id == player.id;
|
|
|
|
}).length === 0;
|
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
this.players.push(player);
|
|
|
|
|
|
|
|
if (this.players.length === 1) {
|
|
|
|
this.scoreBoard[this.players.length - 1].textContent = player.label + ' ' + player.name;
|
|
|
|
} else {
|
|
|
|
this.scoreBoard[this.players.length - 1].textContent = player.name + ' ' + player.label;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scoreBoard[this.players.length - 1].setAttribute('playerId', player.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|