diff options
author | TinWoodman92 <chrhodgden@gmail.com> | 2024-01-14 19:33:58 -0600 |
---|---|---|
committer | TinWoodman92 <chrhodgden@gmail.com> | 2024-01-14 19:33:58 -0600 |
commit | e4816e7eeee4f498e358a0c5e0ca5b286c63b6ba (patch) | |
tree | b44d840e3928f7363fb049547a073459b2a13f49 /app.js |
Initial commit
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -0,0 +1,38 @@ +console.log('Hello from app.js'); + +const turns = {true: 'X', false: 'O'}; + +const boardTurn = function() { + console.log('clicked', this.id, this.bitBoard); +}; + +const squareClick = function() { + console.log('clicked', this.id, this.innerText, this.position); + if (this.innerText == '') { + //Mark board + this.innerText = this.board.turn; + //Update bitBoard + let pos = 9 * (this.board.turn==turns[false]); + pos += this.position - 1 + this.board.bitBoard[pos] = 1; + //Switch player turn + this.board.turn = turns[this.board.turn==turns[false]] + }; +}; + +const initBoard = function(board) { + board.squares = board.getElementsByClassName('square'); + board.squares = Array.from(board.squares); + board.squares.forEach(square => { + square.addEventListener('click', squareClick); + square.board = board; + square.position = parseInt(square.id.match(/\d+/)[0], 10); + }); + board.addEventListener('click', boardTurn); + board.turn = turns[true]; + board.bitBoard = Array(18).fill(0); +}; + +const board = document.getElementById('board'); + +initBoard(board);
\ No newline at end of file |