⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 llk_algorithm.c

📁 连连看
💻 C
📖 第 1 页 / 共 2 页
字号:
    case 1:/*Move Down*/           algorithm_data_change_1(p1,p2);           break;    case 2:/*Move Left*/           algorithm_data_change_2(p1,p2);           break;    case 3:/*Up Down Separate*/           algorithm_data_change_3(p1,p2);           break;    case 4:/*Left and Right Separate*/           algorithm_data_change_4(p1,p2);           break;    case 5:/*Up and Down Converge*/           algorithm_data_change_5(p1,p2);           break;    case 6:/*Left and Right Converge*/           algorithm_data_change_6(p1,p2);           break;    case 7:/*Up leftward,Down rightward*/           algorithm_data_change_7(p1,p2);           break;    case 8:/*Left downward,Right upward*/           algorithm_data_change_8(p1,p2);           break;    case 9:/*Disperse from Center*/           algorithm_data_change_9(p1,p2);           break;    case 10:/*Centralize*/           algorithm_data_change_10(p1,p2);           break;    default:    	   /*assert not reach*/           break;     }}gboolean algorithm_game_net_level(void){  if( algorithm_game.level >= 10 ) /*11 Levels,from 0 to 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;  }}/*No Change, do nothing*/void     algorithm_data_change_0(struct AlgorithmPoint p1, struct AlgorithmPoint p2){  /*do nothing*/}/*Move Down*/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;}/*Move Left*/void     algorithm_data_change_2(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ /*BE CARE: the last column do not in the for loop.*/  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;}/*Up and Down Separate*/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) /*p1 and p2 are in the same column*/  {     if(p1.x < algorithm_game.row/2 && p2.x < algorithm_game.row/2)  /*p1 and p2 are all in the up half rows*/     {       if(p1.x < p2.x) /*p1's move will change p2's positon*/       {         tmp_start--; /*start position move up*/       }     }     else if(p1.x >= algorithm_game.row/2 && p2.x >= algorithm_game.row/2) /*p1 and p2 are all in  the down half rows*/     {       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;    }  }}/*Left and Right Separate*/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;    }  }}/*Up and Down Converge*/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) /*同p1 and p2 are in the same column*/  {     if(p1.x < algorithm_game.row/2 && p2.x < algorithm_game.row/2)  /*p1 and p2 are all in the up half rows*/     {       if(p1.x > p2.x) /*p1's move will change p2's positon*/       {         tmp_start++; /*start positon move down*/       }     }     else if(p1.x >= algorithm_game.row/2 && p2.x >= algorithm_game.row/2) /*p1 and p2 are all in the down half rows*/     {       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;    }  }}/*Left and Right Converge*/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;  //this sentence can be write outside of while loop in some way.      j = j+sign;    }  }}/*Up leftward,Down rightward*/void     algorithm_data_change_7(struct AlgorithmPoint p1, struct AlgorithmPoint p2){  gint j;  if(p1.x < algorithm_game.row/2) /*leftward*/  {    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  /*rightward*/  {    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) /*leftward*/  {    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  /*rightward*/  {    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;  }}/*Left downward,Right upward*/void     algorithm_data_change_8(struct AlgorithmPoint p1, struct AlgorithmPoint p2){  gint i;  if(p1.y < algorithm_game.col/2) /*downward*/  {      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 /*upward*/  {    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)  /*downward*/  {    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  /*upward*/  {    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;  }}/*  Disperse from Center  first,left and right seperate,and then up and down seperate.*/void     algorithm_data_change_9(struct AlgorithmPoint p1, struct AlgorithmPoint p2){  struct AlgorithmPoint p3,p4;  gint sign;  algorithm_data_change_4( p1, p2 );  /*1.left and right seperate*/  /*find p1,p2 's position after 'left and right seperate',then do 'up and down seperate' on p1,p2 's new position*/  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;  }  /*modify in case of special situation*/  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 are the new position of p1,p2 after 'left and right seperate',    do 'up and down seperate' on them*/  algorithm_data_change_3(p3,p4);}/*  Centralize  first do 'Left Right Converge' and then 'Up Down Converge'*/void     algorithm_data_change_10(struct AlgorithmPoint p1, struct AlgorithmPoint p2){  struct AlgorithmPoint p3,p4;  gint sign;  algorithm_data_change_6( p1, p2 );  /*first do 'Left Right Converge'*/  /*find the new position of p1,p2 after 'Left Right Converge',then do 'Up Down Converge' on new position*/  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;  }  /*modify in case of special situation*/  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 are the new position of p1,p2 after 'Left Right Converge',    do 'Up Down Converge' on them*/  algorithm_data_change_5(p3,p4);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -