summaryrefslogtreecommitdiff
path: root/app.js
blob: b8041855ceae56c4fd0d9b6a3a89eec9a23be3f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);