Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clean and Readable solution in Clear category for [old] Xs and Os Referee by Michael_Romanov
"use strict";
function xoReferee(data) {
var patterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
var strData = data.join('');
var result = patterns.map(function(pattern) {
return pattern.map(function(pos) {
return strData.charAt(pos);
}).join('');
});
if (result.indexOf('XXX') > -1) {
return 'X';
}
if (result.indexOf('OOO') > -1) {
return 'O';
}
return "D";
}
var assert = require('assert');
if (!global.is_checking) {
//assert.equal(xoReferee([
// "X.O",
// "XX.",
// "XOO"]), "X", "Xs wins");
assert.equal(xoReferee([
"OO.",
"XOX",
"XOX"]), "O", "Os wins");
assert.equal(xoReferee([
"OOX",
"XXO",
"OXX"]), "D", "Draw");
assert.equal(xoReferee([
"O.X",
"XX.",
"XOO"]), "X", "Xs wins again");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Jan. 2, 2017
Comments: