Bitboard for Tic-Tac-Toe game

.EagleDawn.
4,940 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Bitboard for Tic-Tac-Toe game

This is a simple bitboard representation for the Tic-Tac-Toe game.

class Board {
constructor() {
// dimensions
this.rows = 3;
this.cols = 3;
// boards
this.x_player = 0b000000000;
this.o_player = 0b000000000;
this.full_board = 0b111111111;
// win conditions
this.win_conditions = [
0b111000000,
0b000111000,
0b000000111,
0b100100100,
0b010010010,
0b001001001,
0b100010001,
0b001010100
];
}
show() {
let idx = 0;
for (let y = 0; y < 3; y++) {
let row = "|";
for (let x = 0; x < 3; x++, idx++) {
// compute mask
let mask = this.maskOf(idx);
// values for player x
if (this.x_player & mask) {
row += " X |";
continue;
}
// values for player o
if (this.o_player & mask) {
row += " O |";
continue;
}
// empty
row += " |";
}
console.log("|---|---|---|");
console.log(row);
}
console.log("|---|---|---|");
console.log();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content