Back
Close

Bitboard for Tic-Tac-Toe game

.EagleDawn.
4,866 views

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
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants