Skip to content

Instantly share code, notes, and snippets.

@soardex
Created June 23, 2014 17:08
Show Gist options
  • Save soardex/b736e531764a708f694e to your computer and use it in GitHub Desktop.
Save soardex/b736e531764a708f694e to your computer and use it in GitHub Desktop.
Simple reel winline check
"use strict";
var ERRR = 1;
var WARN = 2;
var LOG0 = 3;
var LOG1 = 4;
var LOG2 = 5;
var DEBUG = LOG0;
var PLOOKUP = [
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[0, 1, 2, 1, 0],
[2, 1, 0, 1, 2],
[0, 1, 1, 1, 1],
[2, 1, 1, 1, 1]
];
var AP = [
'p', 'b', 'r', 'n',
'a', 'h', 'q', 'k',
'w'
];
var SP = [
[7, 5, 7, 4, 4, 1, 1, 1, 5, 1, 3, 7, 6, 0, 2, 1, 0, 0, 3, 3, 3, 2, 6, 5, 7, 6, 6, 0, 3, 2, 1, 2, 1, 0, 7, 1, 0, 3, 3, 0, 4, 1, 6, 3, 1, 6, 7, 1, 7, 3, 3, 4, 2, 6, 6],
[3, 2, 7, 0, 8, 5, 0, 4, 7, 4, 2, 5, 6, 7, 6, 3, 4, 2, 6, 0, 6, 4, 6, 0, 6, 3, 1, 0, 2, 5, 7, 3, 6, 3, 4, 5, 0, 4, 1, 6, 2],
[0, 6, 1, 0, 8, 0, 7, 5, 5, 5, 4, 0, 4, 5, 2, 6, 0, 3, 4, 3, 5, 4, 5, 4, 6, 1, 4, 6, 6, 3, 0, 6, 2, 4, 3, 4, 6, 1, 0, 2, 7, 2, 3, 1, 6, 3, 5, 7, 7, 0, 6],
[1, 5, 4, 4, 2, 5, 6, 1, 3, 2, 6, 4, 7, 1, 7, 8, 5, 7, 1, 2, 7, 4, 0, 1, 7, 2, 5, 0, 7, 4, 5, 5, 5, 7, 0, 1, 2, 3, 2, 4, 2, 1, 2, 4, 2, 0, 0, 7, 0, 1, 2, 4, 6, 2, 5, 1],
[5, 6, 0, 7, 7, 5, 1, 1, 8, 2, 5, 1, 5, 3, 4, 6, 5, 3, 2, 7, 7, 2, 5, 1, 0, 5, 1, 0, 0, 3, 4, 5, 4, 6, 1, 5, 6, 1, 0, 6, 3, 2, 2, 4, 4, 4, 2, 1, 4, 2, 2, 1, 6, 4, 1, 5, 1],
[7, 3, 0, 0, 0, 5, 2, 4, 7, 1, 5, 3, 2, 3, 4, 7, 0, 6, 5, 3, 7, 6, 7, 2, 0, 7, 4, 5, 7, 5, 5, 5, 6, 7, 5, 4, 5, 2, 5, 3, 1, 6, 7]
];
function generateLookupTable() {
var table = [];
var i, j;
if (DEBUG >= LOG0) {
if (DEBUG >= LOG2) {
console.log('generating ' + PLOOKUP.length + ' lookup tables');
}
}
for (i = 0; i < PLOOKUP.length; ++i) {
var set = 0x8000;
for (j = 0; j < PLOOKUP[i].length; ++j) {
if (PLOOKUP[i][j] == 0) {
set ^= (1 << (j));
} else if (PLOOKUP[i][j] == 1) {
set ^= (32 << (j));
} else if (PLOOKUP[i][j] == 2) {
set ^= (1024 << (j));
}
}
table.push(set);
}
/// DOTH: full pattern.
table.push(0xFFFF);
if (DEBUG >= LOG0) {
if (DEBUG >= LOG2) {
console.log('printing lookup tables');
for (i = 0; i < table.length; ++i) {
console.log(table[i].toString(2));
}
}
}
return table;
}
function printTable(store) {
if (DEBUG >= LOG0) {
var i, a, b, c, d, e, u;
for (i = 0; i < store.length; i += 5) {
a = store[i + 0];
b = store[i + 1];
c = store[i + 2];
d = store[i + 3];
e = store[i + 4];
console.log(AP[a] + ' ' + AP[b] + ' ' +
AP[c] + ' ' + AP[d] + ' ' +
AP[e]);
}
console.log('');
for (i = 0; i < store.length; i += 5) {
a = store[i + 0];
b = store[i + 1];
c = store[i + 2];
d = store[i + 3];
e = store[i + 4];
console.log(a + ' ' + b + ' ' +
c + ' ' + d + ' ' +
e);
}
}
}
var mt = new MersenneTwister19937();
mt.init_genrand(new Date().getTime() * 10000);
function generateRandomNumber() {
return mt.genrand_int31();
}
function generateRandomNumberLimit(v) {
return generateRandomNumber() % v;
}
function generateRandomNumberInRange(min, max) {
var range = Math.abs(max - (min + 1));
if (range) {
return (min + generateRandomNumberLimit(range));
}
return 0;
}
function generateStore() {
var store = [];
var i;
var nlength = generateRandomNumberInRange(40, 60);
for (i = 0; i < nlength; ++i) {
var v = generateRandomNumberInRange(0, 9);
store.push(v);
}
return store;
}
function generateSequence() {
var store = [];
var i;
for (i = 0; i < SP.length - 1; ++i) {
var count = SP[i].length;
var v = generateRandomNumberInRange(0, count);
store.push(v);
}
return store;
}
function generateFieldsLeapt(leap) {
leap = (leap === undefined) ? 1 : leap;
var store = [];
var sequence = generateSequence();
var i, j;
var tmp = [], d, u, o, alpha = leap;
for (i = 0; i < sequence.length; ++i) {
d = sequence[i];
/// DOTH: definition for underflow and overflow in array.
u = ((d - alpha) < 0) ? SP[i].length + (d - alpha) : d - alpha;
o = ((d + alpha) >= SP[i].length) ? (SP[i].length - (d + alpha)) : d + alpha;
tmp.push([SP[i][u], SP[i][d], SP[i][o]]);
}
/// DOTH: flatten the array.
for (i = 0; i < 3; ++i) {
for (j = 0; j < sequence.length; ++j) {
store.push(tmp[j][i]);
}
}
return store;
}
function generateFields() {
return generateFieldsLeapt(1);
}
function checkSequence() {
var n, c, i;
var cbit = [
0x8000,
0x8000,
0x8000,
0x8000,
];
/// DOTH: generate pattern lookup table.
var lookup = generateLookupTable();
//var looked0 = generateFields();
/// DOTH: static table.
var looked2 = [3, 3, 3, 1, 1, 1, 3, 3, 1, 2, 1, 2, 2, 3, 2];
var looked1 = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2, 3, 2];
var looked0 = [3, 8, 3, 8, 3, 3, 3, 8, 3, 3, 1, 8, 2, 3, 2];
printTable(looked0);
if (DEBUG >= LOG0) {
if (DEBUG >= LOG2) {
console.log('what will be looked: ' +
looked0[0] + ', ' +
looked0[5] + ', ' +
looked0[10]);
}
}
for (c = 0; c < looked0.length; ++c) {
if (looked0[c] == looked0[0]) {
cbit[0] ^= (1 << c);
}
if (looked0[c] == looked0[5]) {
cbit[1] ^= (1 << c);
}
if (looked0[c] == looked0[10]) {
cbit[2] ^= (1 << c);
}
if (looked0[c] == 8) {
cbit[3] ^= (1 << c);
}
}
/// DOTH: make sure to remove duplicates
/// to avoid double checking of board.
if (looked0[0] == looked0[5]) {
cbit[1] = 0x8000;
}
if (looked0[0] == looked0[10]) {
cbit[2] = 0x8000;
}
if (looked0[5] == looked0[10]) {
cbit[2] = 0x8000;
}
var puzzle = [];
var count, a, b;
for (n = 0; n < cbit.length - 1; ++n) {
if (DEBUG >= LOG0) {
if (DEBUG >= LOG1) {
console.log('');
console.log('');
if (n == 0) {
console.log('-- check upper-bit --');
} else if (n == 1) {
console.log('-- check center-bit --');
} else {
console.log('-- check bottom-bit --');
}
}
}
for (i = 0; i < lookup.length - 1; ++i) {
count = 0;
if (DEBUG >= LOG0) {
if (DEBUG >= LOG1) {
console.log('-- lookup ' + i + ' --');
console.log('L1: ' + lookup[i].toString(2));
console.log('L2: ' + cbit[n].toString(2));
}
}
for (c = 0; c < looked0.length; ++c) {
a = (0x1 & (cbit[n] >> c));
b = (0x1 & (lookup[i] >> c));
if (b == 1) {
if (a == 0) {
break;
}
}
if (a == b && a == 1) {
count++;
}
}
if (count >= 3) {
if (DEBUG >= LOG0) {
if (DEBUG >= LOG1) {
console.log('-- checkmate --');
}
}
puzzle.push(i);
}
if (DEBUG >= LOG0) {
if (DEBUG >= LOG1) {
console.log('-- break --');
}
}
}
}
count = 0;
for (c = 0; c < looked0.length; ++c) {
a = (0x1 & (cbit[cbit.length - 1] >> c));
b = (0x1 & (lookup[lookup.length - 1] >> c));
if (a == b && a == 1) {
count++;
}
}
if (count >= 3) {
if (DEBUG >= LOG1) {
console.log('-- checkmate --');
}
puzzle.push(lookup.length - 1);
}
return puzzle;
}
(function() {
var a = checkSequence()
console.log('');
console.log(a);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment