📄 llk_algorithm.c
字号:
break; case 6:/*左右集中*/ algorithm_data_change_6(p1,p2); break; case 7:/*上左下右*/ algorithm_data_change_7(p1,p2); break; case 8:/*左下右上*/ algorithm_data_change_8(p1,p2); break; case 9:/*向外扩散*/ algorithm_data_change_9(p1,p2); break; case 10:/*向内集中*/ algorithm_data_change_10(p1,p2); break; default: /*assert not reach*/ break; }}gboolean algorithm_game_net_level(void){ if( algorithm_game.level >= 10 ) /*共有11关,编号从0到10*/ { return FALSE; } else { algorithm_game.level ++; algorithm_game.life += 1; algorithm_game.hint += 1; algorithm_game.score += algorithm_game.level > 5 ? 400*algorithm_game.level : 200*algorithm_game.level; algorithm_game.score += algorithm_game.life*100 + algorithm_game.hint*50; algorithm_init_data(); return TRUE; }}/*不变化,什么也不做*/void algorithm_data_change_0(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ /*do nothing*/}/*向下*/void algorithm_data_change_1(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint i; for(i=p1.x;i>0;i--) { algorithm_game.data[i][p1.y] = algorithm_game.data[i-1][p1.y]; } algorithm_game.data[0][p1.y] = 0; i=p2.x; if(p1.y == p2.y && p1.x > p2.x)i++; for( ;i>0;i--) { algorithm_game.data[i][p2.y] = algorithm_game.data[i-1][p2.y]; } algorithm_game.data[0][p2.y] = 0;}/*向左*/void algorithm_data_change_2(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ /*注意:最后一列不在循环范围*/ gint j; for(j=p1.y;j < algorithm_game.col-1; j++) { algorithm_game.data[p1.x][j] = algorithm_game.data[p1.x][j+1]; } algorithm_game.data[p1.x][algorithm_game.col-1] = 0; j=p2.y; if(p1.x == p2.x && p1.y < p2.y)j--; for( ;j<algorithm_game.col-1;j++) { algorithm_game.data[p2.x][j] = algorithm_game.data[p2.x][j+1]; } algorithm_game.data[p2.x][algorithm_game.col-1] = 0;}/*上下分离*/void algorithm_data_change_3(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint i; gint tmp_start,tmp_end; tmp_start = p1.x; tmp_end = algorithm_game.row/2; if(p1.x < algorithm_game.row/2)tmp_end-- ; i=tmp_start; if(tmp_start != tmp_end) { while(i != tmp_end) { gint sign=(tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[i][p1.y] = algorithm_game.data[i+sign][p1.y]; algorithm_game.data[i+sign][p1.y] =0; i=i+sign; } } tmp_start = p2.x; tmp_end = algorithm_game.row/2; if(p2.x < algorithm_game.row/2)tmp_end--; if(p1.y == p2.y) /*同一列*/ { if(p1.x < algorithm_game.row/2 && p2.x < algorithm_game.row/2) /*都在上半部分*/ { if(p1.x < p2.x) /*p1会对p2有影响*/ { tmp_start--; /*起始点上移*/ } } else if(p1.x >= algorithm_game.row/2 && p2.x >= algorithm_game.row/2) /*都在下半部分*/ { if(p1.x > p2.x) { tmp_start++; } } } i = tmp_start; if(tmp_start != tmp_end) { while(i != tmp_end) { gint sign = (tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[i][p2.y] = algorithm_game.data[i+sign][p2.y]; algorithm_game.data[i+sign][p2.y] =0; i=i+sign; } }}/*左右分离*/void algorithm_data_change_4(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint j; gint tmp_start,tmp_end; tmp_start = p1.y; tmp_end = algorithm_game.col/2; if(p1.y < algorithm_game.col/2) tmp_end--; j = tmp_start; if(tmp_start != tmp_end) { while( j != tmp_end ) { gint sign=(tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[p1.x][j] = algorithm_game.data[p1.x][j+sign]; algorithm_game.data[p1.x][j+sign] = 0; j=j+sign; } } tmp_start = p2.y; tmp_end = algorithm_game.col/2; if(p2.y < algorithm_game.col/2)tmp_end--; if(p1.x == p2.x) { if(p1.y < algorithm_game.col/2 && p2.y < algorithm_game.col/2) { if(p1.y < p2.y) { tmp_start--; } } else if(p1.y >= algorithm_game.col/2 && p2.y >= algorithm_game.col/2) { if(p1.y > p2.y) { tmp_start++; } } } j=tmp_start; if(tmp_start != tmp_end) { while(j != tmp_end) { gint sign = (tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[p2.x][j] = algorithm_game.data[p2.x][j+sign]; algorithm_game.data[p2.x][j+sign] = 0; j = j+sign; } }}/*上下集中*/void algorithm_data_change_5(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint i; gint tmp_start,tmp_end; tmp_start = p1.x; if(p1.x < algorithm_game.row/2) /* <= row/2 -1 */ { tmp_end = 0; } else { tmp_end = algorithm_game.row-1; } i=tmp_start; if(tmp_start != tmp_end) { while(i != tmp_end) { gint sign=(tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[i][p1.y] = algorithm_game.data[i+sign][p1.y]; algorithm_game.data[i+sign][p1.y] =0; i=i+sign; } } tmp_start = p2.x; if(p2.x < algorithm_game.row/2) { tmp_end = 0; } else { tmp_end = algorithm_game.row - 1; } if(p1.y == p2.y) /*ͬ同一列*/ { if(p1.x < algorithm_game.row/2 && p2.x < algorithm_game.row/2) /*都在上半部分*/ { if(p1.x > p2.x) /*p1会对p2有影响*/ { tmp_start++; /*起始点下移*/ } } else if(p1.x >= algorithm_game.row/2 && p2.x >= algorithm_game.row/2) /*都在下半部分*/ { if(p1.x < p2.x) { tmp_start--; } } } i = tmp_start; if(tmp_start != tmp_end) { while(i != tmp_end) { gint sign = (tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[i][p2.y] = algorithm_game.data[i+sign][p2.y]; algorithm_game.data[i+sign][p2.y] =0; i=i+sign; } }}/*左右集中*/void algorithm_data_change_6(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint j; gint tmp_start,tmp_end; tmp_start = p1.y; if(p1.y < algorithm_game.col/2) { tmp_end = 0; } else { tmp_end = algorithm_game.col - 1; } j = tmp_start; if(tmp_start != tmp_end) { while( j != tmp_end ) { gint sign=(tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[p1.x][j] = algorithm_game.data[p1.x][j+sign]; algorithm_game.data[p1.x][j+sign] = 0; j=j+sign; } } tmp_start = p2.y; if(p2.y < algorithm_game.col/2) { tmp_end = 0; } else { tmp_end = algorithm_game.col - 1; } if(p1.x == p2.x) { if(p1.y < algorithm_game.col/2 && p2.y < algorithm_game.col/2) { if(p1.y > p2.y) { tmp_start++; } } else if(p1.y >= algorithm_game.col/2 && p2.y >= algorithm_game.col/2) { if(p1.y < p2.y) { tmp_start--; } } } j=tmp_start; if(tmp_start != tmp_end) { while(j != tmp_end) { gint sign = (tmp_end - tmp_start)/abs(tmp_end - tmp_start); algorithm_game.data[p2.x][j] = algorithm_game.data[p2.x][j+sign]; algorithm_game.data[p2.x][j+sign] = 0; //此句可以适当形式提出while循环 j = j+sign; } }}/*上左下右*/void algorithm_data_change_7(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint j; if(p1.x < algorithm_game.row/2) /*向左*/ { for(j=p1.y;j < algorithm_game.col-1; j++) { algorithm_game.data[p1.x][j] = algorithm_game.data[p1.x][j+1]; } algorithm_game.data[p1.x][algorithm_game.col-1] = 0; } else /*向右*/ { for(j=p1.y;j>0;j--) { algorithm_game.data[p1.x][j] = algorithm_game.data[p1.x][j-1]; } algorithm_game.data[p1.x][0] = 0; } j=p2.y; if(p2.x < algorithm_game.row/2) /*向左*/ { if(p1.x == p2.x && p1.y < p2.y)j--; for( ;j<algorithm_game.col-1;j++) { algorithm_game.data[p2.x][j] = algorithm_game.data[p2.x][j+1]; } algorithm_game.data[p2.x][algorithm_game.col-1] = 0; } else /*向右*/ { if(p1.x == p2.x && p1.y > p2.y)j++; for(;j>0;j--) { algorithm_game.data[p2.x][j] = algorithm_game.data[p2.x][j-1]; } algorithm_game.data[p2.x][0] = 0; }}/*左下右上*/void algorithm_data_change_8(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ gint i; if(p1.y < algorithm_game.col/2) /*向下*/ { for(i=p1.x;i>0;i--) { algorithm_game.data[i][p1.y] = algorithm_game.data[i-1][p1.y]; } algorithm_game.data[0][p1.y] = 0; } else /*向上*/ { for(i=p1.x;i<algorithm_game.row-1;i++) { algorithm_game.data[i][p1.y] = algorithm_game.data[i+1][p1.y]; } algorithm_game.data[algorithm_game.row-1][p1.y]=0; } i=p2.x; if(p2.y < algorithm_game.col/2) /*向下*/ { if(p1.y == p2.y && p1.x > p2.x)i++; for( ;i>0;i--) { algorithm_game.data[i][p2.y] = algorithm_game.data[i-1][p2.y]; } algorithm_game.data[0][p2.y] = 0; } else /*向上*/ { if(p1.y == p2.y && p1.x < p2.x)i--; for(; i < algorithm_game.row-1; i++) { algorithm_game.data[i][p2.y] = algorithm_game.data[i+1][p2.y]; } algorithm_game.data[algorithm_game.row-1][p2.y]=0; }}/* 向外扩散 首先进行做于哦分离,然后进行上下分离*/void algorithm_data_change_9(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ struct AlgorithmPoint p3,p4; gint sign; algorithm_data_change_4( p1, p2 ); /*先做左右分离*/ /*寻找p1,p2移动之后的位置,然后再对新位置上下分离*/ p3.x=p1.x; p3.y = (p1.y < algorithm_game.col/2) ? 0 : (algorithm_game.col-1) ; sign= p3.y ? -1:1; while(algorithm_game.data[p3.x][p3.y] != 0) { p3.y = p3.y + sign; } p4.x = p2.x; p4.y = (p2.y < algorithm_game.col/2) ? 0 : (algorithm_game.col-1); sign = p4.y ? -1:1; while(algorithm_game.data[p4.x][p4.y] != 0) { p4.y = p4.y + sign; } /*对于特殊情况进行修正*/ if(p1.x == p2.x) { if(p1.y < algorithm_game.col/2 && p2.y < algorithm_game.col/2) { p4.y = p3.y + 1; } else if(p1.y >= algorithm_game.col/2 && p2.y >= algorithm_game.col/2) { p4.y = p3.y - 1; } } /*p3,p4为p1,p2经过左右分离之后的位置,再对他们进行上下分离即可*/ algorithm_data_change_3(p3,p4);}/* 向内集中 首先进行左右集中,然后进行上下集中*/void algorithm_data_change_10(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ struct AlgorithmPoint p3,p4; gint sign; algorithm_data_change_6( p1, p2 ); /*先左右集中*/ /*寻找p1,p2移动之后的位置,然后再对新位置上下集中*/ p3.x=p1.x; p3.y = (p1.y < algorithm_game.col/2) ? (algorithm_game.col/2 - 1) : (algorithm_game.col/2) ; sign= (p1.y < algorithm_game.col/2) ? -1:1; while(algorithm_game.data[p3.x][p3.y] != 0) { p3.y = p3.y + sign; } p4.x = p2.x; p4.y = (p2.y < algorithm_game.col/2) ? (algorithm_game.col/2 - 1) : (algorithm_game.col/2); sign = (p2.y < algorithm_game.col/2) ? -1:1; while(algorithm_game.data[p4.x][p4.y] != 0) { p4.y = p4.y + sign; } /*对于特殊情况进行修正*/ if(p1.x == p2.x) { if(p1.y < algorithm_game.col/2 && p2.y < algorithm_game.col/2) { p4.y = p3.y - 1; } else if(p1.y >= algorithm_game.col/2 && p2.y >= algorithm_game.col/2) { p4.y = p3.y + 1; } } /*p3,p4为p1,p2经过左右集中之后的位置,再对他们进行上下集中即可*/ algorithm_data_change_5(p3,p4);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -