hextris/js/checking.js

136 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-05-17 23:26:31 +00:00
// HackExeter
2014-05-17 18:15:16 +00:00
function getIndex(list,index) {
if(index>-1) {
2014-05-17 16:39:49 +00:00
return index%list.length;
2014-05-17 18:15:16 +00:00
}
else {
2014-05-17 16:39:49 +00:00
return list.length+index;
2014-05-17 18:15:16 +00:00
}
2014-05-17 16:39:49 +00:00
}
function scoreCheckHorizontal(clock,side,index) {
clockSides = clock.blocks;
if(clockSides[getIndex(clockSides,side)][index] && clockSides[getIndex(clockSides,side-1)][index] && clockSides[getIndex(clockSides,side-2)][index]) {
2014-05-17 18:15:16 +00:00
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side-1)][index].color && clockSides[getIndex(clockSides,side-1)][index].color == clockSides[getIndex(clockSides,side-2)][index].color) {
2014-05-17 16:39:49 +00:00
return -2;
}
}
if(clockSides[getIndex(clockSides,side)][index] && clockSides[getIndex(clockSides,side+1)][index] && clockSides[getIndex(clockSides,side-1)][index]) {
2014-05-17 18:15:16 +00:00
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side+1)][index].color && clockSides[getIndex(clockSides,side+1)][index].color == clockSides[getIndex(clockSides,side-1)][index].color) {
2014-05-17 16:39:49 +00:00
return -1;
}
}
if(clockSides[getIndex(clockSides,side)][index] && clockSides[getIndex(clockSides,side+1)][index] && clockSides[getIndex(clockSides,side+2)][index]) {
2014-05-17 18:15:16 +00:00
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side+1)][index].color && clockSides[getIndex(clockSides,side+1)][index].color == clockSides[getIndex(clockSides,side+2)][index].color) {
2014-05-17 16:39:49 +00:00
return 0;
}
}
return "false";
}
function scoreCheckVertical(clock,side,index) {
curSide = clock.blocks[side];
if(curSide[index] && curSide[index-2] && curSide[index-1]) {
if(curSide[index].color == curSide[index-2].color && curSide[index-2].color ==curSide[index-1].color) {
return -2;
}
}
if(curSide[index] && curSide[index+1] && curSide[index-1]) {
if(curSide[index].color == curSide[index+1].color && curSide[index+1].color == curSide[index-1].color) {
return -1;
}
}
if(curSide[index] && curSide[index+2] && curSide[index+1]) {
if(curSide[index].color == curSide[index+2].color&& curSide[index+2].color ==curSide[index+1].color) {
return 0;
}
}
return "false";
}
function consolidateBlocks(clock,side,index) {
horizontal = scoreCheckHorizontal(clock,side,index);
vertical = scoreCheckVertical(clock,side,index);
deleted = [];
if(horizontal != "false") {
2014-05-17 21:38:11 +00:00
scoreScalar *= 2;
2014-05-17 16:39:49 +00:00
deleted.push([side,index,horizontal]);
}
else {
deleted.push([]);
}
if(vertical != "false") {
2014-05-17 21:38:11 +00:00
scoreScalar *= 2;
2014-05-17 16:39:49 +00:00
deleted.push([side,index,vertical]);
}
else {
deleted.push([]);
}
eraseBlocks(clock,deleted);
2014-05-17 23:21:19 +00:00
return;
2014-05-17 16:39:49 +00:00
}
2014-05-17 22:52:15 +00:00
2014-05-17 16:39:49 +00:00
function eraseBlocks(clock,deleted) {
if(deleted[0].length>0){
side = deleted[0][0];
index = deleted[0][1];
horizontal = deleted[0][2];
2014-05-17 23:16:55 +00:00
length =3;
flag=0;
if(clock.blocks[getIndex(clock.blocks,side+horizontal+length+1)][index]){
2014-05-17 23:23:59 +00:00
try {
flag= clock.blocks[getIndex(clock.blocks,side+horizontal+length)][index].color == clock.blocks[getIndex(clock.blocks,side+horizontal+length+1)][index].color;
}
catch (e) {
console.log(e);
}
2014-05-17 23:16:55 +00:00
}
while(flag) {
if(clock.blocks[getIndex(clock.blocks,side+horizontal+length+1)][index]){
flag= clock.blocks[getIndex(clock.blocks,side+horizontal+length)][index].color == clock.blocks[getIndex(clock.blocks,side+horizontal+length+1)][index].color;
}
length++;
}
console.log(length);
for(var i=0;i<length;i++) {
2014-05-17 18:15:16 +00:00
clock.blocks[getIndex(clock.blocks,side+horizontal+i)].splice(index,1);
2014-05-17 16:39:49 +00:00
}
2014-05-17 23:16:55 +00:00
for(var i=0;i<length;i++) {
2014-05-17 18:15:16 +00:00
if(side+horizontal+i<clock.blocks.length) {
consolidateBlocks(clock,getIndex(clock.blocks,side+horizontal+i),index);
}
2014-05-17 16:39:49 +00:00
}
}
if(deleted[1].length>0){
side = deleted[1][0];
index = deleted[1][1];
vertical = deleted[1][2];
2014-05-17 23:16:55 +00:00
vertlength=3;
while(index+vertical+vertlength<clock.blocks[side].length-1 && (clock.blocks[slide][index+vertical+length].color ==clock.blocks[slide][index+vertical+length+1].color )) {
vertlength+=1;
}
2014-05-17 18:15:16 +00:00
clock.blocks[side].splice(index+vertical,2+(1*(!deleted[0].length>0)));
for(var i=0; i<clock.blocks[side].length-(index+vertical); i++) {
consolidateBlocks(clock,side,index+vertical+i);
}
2014-05-17 16:39:49 +00:00
}
2014-05-17 22:52:15 +00:00
sidesChanged = [];
if(deleted[1].length>0){
if(deleted[1][0] != "false") {
sidesChanged.push(deleted[1][0]);
}
}
if(deleted[0].length>0){
2014-05-17 23:16:55 +00:00
for(var i=0;i<length;i++) {
2014-05-17 22:52:15 +00:00
if(deleted[0][2] != "false" ) {
sidesChanged.push(getIndex(clock.blocks,deleted[0][0]+deleted[0][2]+i));
}
}
}
sidesChanged.forEach(function(o){
MainClock.blocks[o].forEach(function(block) {
block.settled = 0;
})
});
2014-05-17 16:39:49 +00:00
}