mapping a tictactoe board in javascript
Not the board, store the moves

A TicTacToe board can be mapped to two 9bit numbers that represent each player's moves then the test for winning moves is just a simple bitwise test against the 8 possible combinations of move that are a win.


  My question is if there's a better way of achieving the same results without having to use two for loops to get the columns and second diagonals.


The following may be what you want

// top left is low bit, bottom right is high bit
// coord to bit pos is x + y * 3
// Bit positions on board
// 0,1,2
// 3,4,5
// 6,7,8


const board = [['x','o','x'], ['o','o','o'], ['o','o','x']];
const players = ["x","o"];
const winMoves = [448,56,7,292,146,73,273,84];

const getMoves = (board, player) =&gt; {
  var moves = 0;
  for(var i = 0; i &lt; 9; i ++){
     moves += board[i / 3 | 0][i % 3] === player ? 1 &lt;&lt; i : 0;
  }
  return moves;
}
const checkWin = (board, player) =&gt;{
  const moves = getMoves(board, player);
  return winMoves.some(win =&gt; (win &amp; moves) === win);
}
if(!players.some(player =&gt; {
    if(checkWin(board, player)) { 
        console.log("Player " + player + " wins");
        return true;
    }
}){
  console.log("No winner");
}


Complete game state in 18bits

If you look into the problem further you can reverse the board/moves relationship so that the board is created from two sets of moves. This means you can store the complete game state with only 2 9bit numbers and you would not have to examine the board to find the win

const move = (player, x, y) =&gt; player |= 1 &lt;&lt; (x + y * 3);
const checkWin = moves =&gt; winMoves.some(win =&gt; (win &amp; moves) === win);
const winMoves = [448,56,7,292,146,73,273,84];
var movesX = 0, movesO = 0; // moves for each player game start

// or from your example the moves would be
// x o x
// o o o
// o o x
movesX = 0b100000101;
movesO = 0b011111010;

// with
console.log(checkWin(movesX)); // &gt;&gt; false
console.log(checkWin(movesO)); // &gt;&gt; True