From e4816e7eeee4f498e358a0c5e0ca5b286c63b6ba Mon Sep 17 00:00:00 2001 From: TinWoodman92 Date: Sun, 14 Jan 2024 19:33:58 -0600 Subject: Initial commit --- app.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app.js (limited to 'app.js') diff --git a/app.js b/app.js new file mode 100644 index 0000000..b804185 --- /dev/null +++ b/app.js @@ -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 -- cgit v1.2.3