summaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
authorTinWoodman92 <chrhodgden@gmail.com>2024-01-15 17:16:32 -0600
committerTinWoodman92 <chrhodgden@gmail.com>2024-01-15 17:16:32 -0600
commit609cde41abfbae145a4ce0aa0aa8c83f6ab123ec (patch)
treed6eb4379a567e46fe6d66eb091a2710f1424e48c /app.js
parentca23fac11fea368ac2a611f25904bdd5cd26c221 (diff)
added getter methods
Diffstat (limited to 'app.js')
-rw-r--r--app.js59
1 files changed, 29 insertions, 30 deletions
diff --git a/app.js b/app.js
index 29fd993..0a21da8 100644
--- a/app.js
+++ b/app.js
@@ -2,62 +2,52 @@ console.log('Hello from app.js');
const turns = {true: 'X', false: 'O'};
-const boardTurn = function() {
- console.log('clicked', this.id, this.bitBoard);
+const getXBoard = function() {
+ return this.bitBoard.slice(0, 9);
+};
+
+const getOBoard = function() {
+ return this.bitBoard.slice(9, 18);
};
const checkVictory = function() {
let checkSum = 0;
let victory = false;
- const rows = [
- //rows
- [0, 1, 2],
- [3, 4, 5],
- [6, 7, 8],
- //columns
- [0, 3, 6],
- [1, 4, 7],
- [2, 5, 8],
- //diagonals
- [0, 4, 8],
- [2, 4, 6]
- ];
- const xBoard = this.bitBoard.slice(0, 9);
- const oBoard = this.bitBoard.slice(9, 18);
let checkRow = [];
- rows.forEach(row => {
- checkRow = xBoard.filter((_, index) => row.includes(index));
+ this.rows.forEach(row => {
+ checkRow = this.xBoard.filter((_, index) => row.includes(index));
checkSum = checkRow.reduce((acc, value) => acc + value, 0);
if (checkSum == 3) {
victory = true;
board.victory_row = row;
};
- checkRow = oBoard.filter((_, index) => row.includes(index));
+ checkRow = this.oBoard.filter((_, index) => row.includes(index));
checkSum = checkRow.reduce((acc, value) => acc + value, 0);
if (checkSum == 3) {
victory = true;
board.victory_row = row;
};
});
- board.victory = victory;
if (victory) {
board.victor = board.turn;
};
return(victory);
};
+const colorVictoryRow = function () {
+
+};
+
const squareClick = function() {
- console.log('clicked', this.id, this.innerText, this.position);
- if (this.innerText == '') {
+ if ((this.innerText == '') && !(this.board.victory)) {
//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;
+ let position = 9 * (this.board.turn==turns[false]);
+ position += this.position;
+ this.board.bitBoard[position] = 1;
//check victory
- this.board.checkVictory();
if (this.board.victory) {
console.log(`${this.board.victor} wins!!!`);
};
@@ -72,12 +62,21 @@ const initBoard = function(board) {
board.squares.forEach(square => {
square.addEventListener('click', squareClick);
square.board = board;
- square.position = parseInt(square.id.match(/\d+/)[0], 10);
+ square.position = parseInt(square.id.match(/\d+/)[0], 10)-1;
});
- board.addEventListener('click', boardTurn);
board.turn = turns[true];
board.bitBoard = Array(18).fill(0);
- board.checkVictory = checkVictory;
+ Object.defineProperty(board, 'victory', {get: checkVictory});
+ Object.defineProperty(board, 'xBoard', {get: getXBoard});
+ Object.defineProperty(board, 'oBoard', {get: getOBoard});
+ board.rows = [
+ //rows
+ [0, 1, 2], [3, 4, 5], [6, 7, 8],
+ //columns
+ [0, 3, 6], [1, 4, 7], [2, 5, 8],
+ //diagonals
+ [0, 4, 8], [2, 4, 6]
+ ];
};
const board = document.getElementById('board');