JSFiddle - React, Tailwind, and code Playground

by Turi S

JavaScript

function findMoves(board) {
	var moves = [];
	for(let i = 0; i < 9; i++) {
  	if(board[i] == null)
    	moves.push(i);
  }
  
  if(moves.length) 
  	return moves;
  else 
  	return null;
}

function evalBoard(board, player) {
	var lines = [
  	[0,1,2],
    [3,4,5],
    [6,7,8],
    [0,3,6],
    [1,4,7],
    [2,5,8],
    [0,4,8],
    [2,4,6]
  ];
  
  for(let i = 0; i < 9; i++) {
  	let line = lines[i];
    let a = line[0], b = line[1], c = line[2];
    if(a != null && a == b && b == c)
    	return a;
  }
  return 0;
}

var board = [null, null, null, null, null, null, null, null, null];