hextris/js/checking.js

84 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-05-20 00:47:19 +00:00
function search(twoD,oneD){
2014-05-26 01:47:21 +00:00
// Searches a two dimensional array to see if it contains a one dimensional array. indexOf doesn't work in this case
2014-05-20 00:47:19 +00:00
for(var i=0;i<twoD.length;i++){
if(twoD[i][0] == oneD[0] && twoD[i][1] == oneD[1]) {
return true;
2014-05-19 14:36:13 +00:00
}
}
return false;
2014-05-17 16:39:49 +00:00
}
2014-05-26 01:49:03 +00:00
function floodFill(clock, side, index, deleting) {
if (clock.blocks[side] === undefined || clock.blocks[side][index] === undefined) {
2014-05-26 01:47:21 +00:00
//just makin sure stuff exists
return;
}
2014-05-26 01:47:21 +00:00
//store the color
2014-05-24 16:48:46 +00:00
var color = clock.blocks[side][index].color;
2014-05-26 01:47:21 +00:00
//nested for loops for navigating the blocks
2014-05-20 00:47:19 +00:00
for(var x =-1;x<2;x++){
for(var y =-1;y<2;y++){
2014-05-26 01:47:21 +00:00
//make sure the they aren't diagonals
2014-05-20 21:09:39 +00:00
if(Math.abs(x)==Math.abs(y)){continue;}
2014-05-26 01:47:21 +00:00
//calculate the side were exploring using mods
var curSide =(side+x+clock.sides)%clock.sides;
2014-05-26 01:47:21 +00:00
//calculate the index
2014-05-24 17:25:33 +00:00
var curIndex = index+y;
2014-05-26 01:47:21 +00:00
//making sure the block exists at this side and index
if(clock.blocks[curSide] === undefined){continue;}
if(clock.blocks[curSide][curIndex] !== undefined){
2014-05-26 01:47:21 +00:00
// checking equivalency of color, if its already been explored, and if it isn't already deleted
if(clock.blocks[curSide][curIndex].color == color && search(deleting,[curSide,curIndex]) === false && clock.blocks[curSide][curIndex].deleted == 0 ) {
2014-05-26 01:47:21 +00:00
//add this to the array of already explored
deleting.push([curSide,curIndex]);
2014-05-26 01:47:21 +00:00
//recall with next block explored
floodFill(clock,curSide,curIndex,deleting);
2014-05-19 14:36:13 +00:00
}
}
}
}
2014-05-20 00:47:19 +00:00
}
2014-05-24 19:39:27 +00:00
2014-05-20 00:47:19 +00:00
function consolidateBlocks(clock,side,index){
2014-05-26 01:47:21 +00:00
//record which sides have been changed
2014-05-20 00:47:19 +00:00
var sidesChanged =[];
2014-05-24 16:48:46 +00:00
var deleting=[];
2014-05-26 16:45:52 +00:00
var deletedBlocks = []
2014-05-26 01:47:21 +00:00
//add start case
2014-05-20 00:47:19 +00:00
deleting.push([side,index]);
2014-05-26 01:47:21 +00:00
//fill deleting
2014-05-24 16:48:46 +00:00
floodFill(clock,side,index,deleting);
2014-05-26 01:47:21 +00:00
//make sure there are more than 3 blocks to be deleted
if(deleting.length<3){return;}
var i;
2014-05-26 01:47:21 +00:00
for(var i=0; i<deleting.length;i++){
var arr = deleting[i];
//just making sure the arrays are as they should be
2014-05-20 00:47:19 +00:00
if(arr !== undefined && arr.length==2) {
2014-05-26 01:47:21 +00:00
//add to sides changed if not in there
if(sidesChanged.indexOf(arr[0])==-1){
2014-05-20 00:47:19 +00:00
sidesChanged.push(arr[0]);
2014-05-19 14:36:13 +00:00
}
2014-05-26 01:47:21 +00:00
//mark as deleted
2014-05-20 20:48:11 +00:00
clock.blocks[arr[0]][arr[1]].deleted = 1;
2014-05-26 16:45:52 +00:00
deletedBlocks.push(clock.blocks[arr[0]][arr[1]]);
2014-05-19 14:36:13 +00:00
}
}
2014-05-26 01:47:21 +00:00
// add scores
2014-05-26 18:07:07 +00:00
var now = MainClock.ct;
2014-06-23 23:21:09 +00:00
if(now - clock.lastCombo < 240 ){
2014-05-26 03:12:47 +00:00
clock.comboMultiplier += 1;
clock.lastCombo = now;
2014-05-26 17:04:23 +00:00
var coords = findCenterOfBlocks(deletedBlocks);
2014-07-03 21:51:13 +00:00
clock.texts.push(new Text(coords['x'],coords['y'],"x "+clock.comboMultiplier.toString(),"bold Q","#fff",fadeUpAndOut));
2014-05-26 03:12:47 +00:00
}
else{
clock.lastCombo = now;
clock.comboMultiplier = 1;
}
2014-05-26 16:27:30 +00:00
var adder = deleting.length * deleting.length * clock.comboMultiplier;
2014-07-03 21:51:13 +00:00
clock.texts.push(new Text(clock.x,clock.y,"+ "+adder.toString(),"bold Q ",deletedBlocks[0].color,fadeUpAndOut));
2014-06-24 14:53:26 +00:00
clock.lastColorScored = deletedBlocks[0].color;
2014-05-26 16:27:30 +00:00
score += adder;
2014-05-17 16:39:49 +00:00
}