2019-07-26 17:17:23 +00:00
|
|
|
/*----- constants -----*/
|
2019-07-26 18:02:30 +00:00
|
|
|
const COLORS = {
|
|
|
|
'0': 'white',
|
|
|
|
'1': 'purple',
|
|
|
|
'-1': 'lime'
|
|
|
|
};
|
2019-07-26 17:17:23 +00:00
|
|
|
|
|
|
|
/*----- app's state (variables) -----*/
|
2019-07-26 18:02:30 +00:00
|
|
|
let board, turn, winner;
|
2019-07-26 17:17:23 +00:00
|
|
|
|
|
|
|
/*----- cached element references -----*/
|
|
|
|
|
|
|
|
|
|
|
|
/*----- event listeners -----*/
|
2019-07-26 20:58:18 +00:00
|
|
|
document.querySelector('section.markers')
|
|
|
|
.addEventListener('click', handleClick);
|
2019-07-26 17:17:23 +00:00
|
|
|
|
2019-07-26 18:02:30 +00:00
|
|
|
/*----- functions -----*/
|
2019-07-26 20:58:18 +00:00
|
|
|
init();
|
2019-07-26 18:02:30 +00:00
|
|
|
|
|
|
|
function init() {
|
|
|
|
board = [
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 1 (index 0)
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 2 (index 1)
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 3 (index 2)
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 4 (index 3)
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 5 (index 4)
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 6 (index 5)
|
|
|
|
[0, 0, 0, 0, 0, 0], // column 7 (index 6)
|
|
|
|
];
|
|
|
|
turn = 1;
|
|
|
|
winner = null; // 1, -1, null (no winner), 'T' (tie)
|
2019-07-26 20:58:18 +00:00
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
// Render the board
|
|
|
|
board.forEach(function(colArr, colIdx) {
|
|
|
|
colArr.forEach(function(cell, rowIdx) {
|
|
|
|
let div = document.getElementById(`c${colIdx}r${rowIdx}`);
|
|
|
|
div.style.backgroundColor = COLORS[cell];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleClick(evt) {
|
2019-07-26 21:34:20 +00:00
|
|
|
// get index of column's marker clicked
|
2019-07-26 20:58:18 +00:00
|
|
|
let idx = parseInt(evt.target.id.replace('col', ''));
|
2019-07-26 21:34:20 +00:00
|
|
|
// make sure the MARKER was clicked
|
2019-07-26 20:58:18 +00:00
|
|
|
if (isNaN(idx) || winner) return;
|
2019-07-26 21:34:20 +00:00
|
|
|
// obtain the actual column array in board array
|
|
|
|
let colArr = board[idx];
|
|
|
|
// get the index of the first 0 in the col array
|
|
|
|
let rowIdx = colArr.indexOf(0);
|
|
|
|
// if the col is full, there are no zeroes, therefore
|
|
|
|
// indexOf returns -1.
|
|
|
|
// Do nothing if no zeroes available (col full)
|
|
|
|
if (rowIdx === -1) return;
|
|
|
|
// update the col array (within the board) with
|
|
|
|
// the player whose turn it is
|
|
|
|
colArr[rowIdx] = turn;
|
|
|
|
// flip turns from 1 to -1; -1 to 1
|
|
|
|
turn *= -1;
|
|
|
|
render();
|
2019-07-26 18:02:30 +00:00
|
|
|
}
|