直接传入地图位置和落下的位置,适用双色消除和单色消除,将算法和整体逻辑剥离,也可以优化其它算法
/**
* 双色消除算法
* @param arr 地图数据
* @param tagrnum 所落下的位置
* @returns
*/
public shuangse(arr: Array<Array<number>>, tagrnum: string): boolean {
let county: number = 0;
let countx: number = 0;
let x: number = Number(tagrnum[0]);
let y: number = Number(tagrnum[1]);
let curritem = arr[x][y]; //当前物体
let rigthy: number = y; //右边
let lefty: number = y;//做边
let upx: number = x;//上边
let downx: number = x;//下边
let arry: Array<string> = new Array<string>();//收集横向需要消除的方块
let arrx: Array<string> = new Array<string>();//收集纵向需要消除的方块
let curxy: number = arr[x][rigthy];//所在位置的临时值
while (rigthy < 7) //右边
{
rigthy = rigthy + 1;
if (x >= 0 && rigthy >= 0 && x < 7 && rigthy < 7)//一直找到最后
{
curxy = arr[x][rigthy];
if (curxy > 0) {
if (curritem >= 10) //放的是双色
{
let str: string = curritem.toString();
let str1: string = str[0];
if (arr[x][rigthy] >= 10) //旁边是否双色
{
let arrstr: string = (arr[x][rigthy]).toString();
let arrstr1: string = arrstr[0];
if (str1 == arrstr1) {
arry.push(x.toString() + rigthy.toString());
county++;
}
else {
break;
}
} else {
if (str1 == (arr[x][rigthy]).toString()) //符合要求,不是双色
{
arry.push(x.toString() + rigthy.toString());
county++;
}
else {
break;
}
}
}
else { //放的不是双色
if (arr[x][rigthy] >= 10) //旁边是双色
{
let arrstr: string = (arr[x][rigthy]).toString();
let arrstr1: string = arrstr[0];
if (curritem == Number(arrstr1)) {
arry.push(x.toString() + rigthy.toString());
county++;
}
else {
break;
}
}
else { //不是双色
if (curritem == arr[x][rigthy]) //下一个目标是双色
{
arry.push(x.toString() + rigthy.toString());
county++;
}
else {
break;
}
}
}
}
else {
break;
}
}
else {
break;
}
}
while (lefty >= 0) //左边
{
lefty = lefty - 1;
if (lefty >= 0 && x >= 0 && lefty < 7 && x < 7)//一直找到最后
{
curxy = arr[x][lefty];
if (curxy > 0) {
if (curritem >= 10) //放的是双色
{
let str: string = curritem.toString();
let str1: string = str[0];
if (arr[x][lefty] >= 10) //旁边是否双色
{
let arrstr: string = (arr[x][lefty]).toString();
let arrstr1: string = arrstr[0];
if (str1 == arrstr1) {
arry.push(x.toString() + lefty.toString());
county++;
}
else {
break;
}
}
else {
if (str1 == (arr[x][lefty]).toString()) //符合要求,不是双色
{
arry.push(x.toString() + lefty.toString());
county++;
}
else {
break;
}
}
}
else { //放的不是双色
if (arr[x][lefty] >= 10) //旁边是双色
{
let arrstr: string = (arr[x][lefty]).toString();
let arrstr1: string = arrstr[0];
if (curritem == Number(arrstr1)) {
arry.push(x.toString() + lefty.toString());
county++;
}
else {
break;
}
}
else { //不是双色
if (curritem == arr[x][lefty]) //下一个目标是双色
{
arry.push(x.toString() + lefty.toString());
county++;
}
else {
break;
}
}
}
}
else {
break;
}
}
else {
break;
}
}
while (upx >= 0) //向上
{
upx = upx - 1;
if (y >= 0 && upx >= 0 && upx < 7 && y < 7)//一直找到最后
{
curxy = arr[upx][y] ;
if (curxy>0) {
if (curritem >= 10) //放的是双色
{
let str: string = curritem.toString();
let str1: string = str[0];
if (arr[upx][y] >= 10) //旁边是否双色
{
let arrstr: string = (arr[upx][y]).toString();
let arrstr1: string = arrstr[0];
if (str1 == arrstr1) {
arrx.push(upx.toString() + y.toString());
countx++;
}
else {
break;
}
} else {
if (str1 == (arr[upx][y]).toString()) //符合要求,不是双色
{
arrx.push(upx.toString() + y.toString());
countx++;
}
else {
break;
}
}
}
else { //放的不是双色
if (arr[upx][y] >= 10) //旁边是双色
{
let arrstr: string = (arr[upx][y]).toString();
let arrstr1: string = arrstr[0];
if (curritem == Number(arrstr1)) {
arrx.push(upx.toString() + y.toString());
countx++;
}
else {
break;
}
}
else { //不是双色
if (curritem == arr[upx][y]) //下一个目标是双色
{
arrx.push(upx.toString() + y.toString());
countx++;
}
else {
break;
}
}
}
}
else {
break;
}
}
}
while (downx < 7) //向下
{
downx = downx + 1;
if (y >= 0 && downx >= 0 && downx < 7 && y < 7)//一直找到最后
{
curxy =arr[downx][y] ;
if (curxy>0) {
if (curritem >= 10) //放的是双色
{
let str: string = curritem.toString();
let str1: string = str[0];
if (arr[downx][y] >= 10) //旁边是否双色
{
let arrstr: string = (arr[downx][y]).toString();
let arrstr1: string = arrstr[0];
if (str1 == arrstr1) {
arrx.push(downx.toString() + y.toString());
countx++;
}
else {
break;
}
} else {
if (str1 == (arr[downx][y]).toString()) //符合要求,不是双色
{
arrx.push(downx.toString() + y.toString());
countx++;
}
else {
break;
}
}
}
else { //放的不是双色
if (arr[downx][y] >= 10) //旁边是双色
{
let arrstr: string = (arr[downx][y]).toString();
let arrstr1: string = arrstr[0];
if (curritem == Number(arrstr1)) {
arrx.push(downx.toString() + y.toString());
countx++;
}
else {
break;
}
}
else { //不是双色
if (curritem == arr[downx][y]) //下一个目标是双色
{
arrx.push(downx.toString() + y.toString());
countx++;
}
else {
break;
}
}
}
}
else {
break;
}
}
else {
break;
}
}
let arrxy: Array<string> = new Array<string>();
if (countx >= 2 && county >= 2) {
arrx.push(tagrnum);
arrxy = arrx.concat(arry);
}
else if (countx >= 2) {
arrx.push(tagrnum);
arrxy = arrx;
}
else if (county >= 2) {
arry.push(tagrnum);
arrxy = arry;
}
else {
arrxy = null;
}
return arrxy;
}