JSFiddle - React, Tailwind, and code Playground
by dashk
HTML
<pre id="whatever">
6
XXXT
....
OO..
....
XOXT
XXOO
OXOX
XXOO
XOX.
OX..
....
....
OOXX
OXXX
OX.T
O..O
XXXO
..O.
.O..
T...
OXXX
XO..
..O.
...O
</pre>
JavaScript
var SIZE = 4,
GameStatus = GS = {
X_WON: 0,
O_WON: 1,
INCOMPLETE: 2,
NOTHING: 3
};
function processInput(input) {
var lines = input.split('\n');
// Parse out count
var count = parseInt(lines[0]),
partialLines = [],
output = [];
for (var i = 0; i < count; ++i) {
partialLines = [];
for (var j = 0; j < SIZE; ++j) {
partialLines.push(lines[i * 4 + 1 + i + j]);
}
output.push(partialLines);
}
return { count: count, boards: output };
}
function solve(input) {
var data = processInput(input);
for (var i = 0; i < data.count; ++i) {
console.log('Result: ' + solvePuzzle(data.boards[i]));
}
}
function solvePuzzle(board) {
// Go through first row
var xFactor = 1, yFactor = 0;
// Go through first column
xFactor = 0, yFactor = 1;
// Go through 0,0 diagonal
// Go through 3,3 diagonal
}
function solveDimension(board, xIncre, yIncre) {
// Per factor, go through each row
// NOTE We actually want this to go differently from what the original have
var xFactor = yIncre, yFactor = xIncre,
x = 0, y = 0, hasIncomplete = false;
do {
// Run through list
var status = solveList(board, x, y, xFactor, yFactor);
if (status == GS.X_WON || status == GS.O_WON) {
return status;
}
else if (status == GS.INCOMPLETE) {
hasIncomplete = true;
}
x += xIncre, y += yIncre;
} while (x < SIZE && y < SIZE);
return (hasIncomplete ? GS.INCOMPLETE : GS.NOTHING);
}
function solveList(board, xStart, yStart, xIncre, yIncre) {
// Grab list string
var x = xStart, y = yStart, strArr = [];
do {
strArr.push(board[x][y]);
} while ((x += xIncre) < SIZE && (y += yIncre) < SIZE);
var str = strArr.join('');
if (str.indexOf('.') >= 0) {
return GS.INCOMPLETE;
}
var xStr = str.replace('T', 'X');
if (xStr === 'XXXX') {
return GS.X_WON;
}
var yStr = str.replace('T', 'O');
if (yStr === 'OOOO') {
return GS.O_WON;
}
return...