Update state and call render in handleClick

This commit is contained in:
jim-clark 2019-07-26 14:34:20 -07:00
parent 26002d06c3
commit d43d48362b

View file

@ -44,8 +44,22 @@ function render() {
}
function handleClick(evt) {
// get index of column's marker clicked
let idx = parseInt(evt.target.id.replace('col', ''));
// make sure the MARKER was clicked
if (isNaN(idx) || winner) return;
// 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();
}