Linux环境下连连看游戏代码,《连连看》算法C语言演示(自动连连看)
时间: 2023-09-12 07:02:52 浏览: 54
很好,这是一个比较有趣的项目。下面是一个简单的连连看游戏的C语言代码演示:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 8
#define COL 10
#define KIND 20
int map[ROW][COL];
int score = 0;
void init_map(void)
{
int i, j;
srand((unsigned int)time(NULL));
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
map[i][j] = rand() % KIND + 1;
}
}
}
void print_map(void)
{
int i, j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%d ", map[i][j]);
}
printf("\n");
}
}
int find_path(int x1, int y1, int x2, int y2)
{
int i, j;
int flag = 1;
if (x1 == x2 && y1 == y2)
{
return 0;
}
if (x1 == x2)
{
if (y1 < y2)
{
for (j = y1 + 1; j <= y2; j++)
{
if (map[x1][j] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
return 1;
}
}
else
{
for (j = y2 + 1; j <= y1; j++)
{
if (map[x1][j] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
return 1;
}
}
}
if (y1 == y2)
{
if (x1 < x2)
{
for (i = x1 + 1; i <= x2; i++)
{
if (map[i][y1] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
return 1;
}
}
else
{
for (i = x2 + 1; i <= x1; i++)
{
if (map[i][y1] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
return 1;
}
}
}
return 0;
}
int remove(int x1, int y1, int x2, int y2)
{
int i, j;
int flag = 1;
if (map[x1][y1] != map[x2][y2])
{
return 0;
}
if (x1 == x2)
{
if (y1 < y2)
{
for (j = y1 + 1; j <= y2; j++)
{
if (map[x1][j] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
for (j = y1 + 1; j <= y2; j++)
{
map[x1][j] = 0;
}
score += 10;
return 1;
}
}
else
{
for (j = y2 + 1; j <= y1; j++)
{
if (map[x1][j] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
for (j = y2 + 1; j <= y1; j++)
{
map[x1][j] = 0;
}
score += 10;
return 1;
}
}
}
if (y1 == y2)
{
if (x1 < x2)
{
for (i = x1 + 1; i <= x2; i++)
{
if (map[i][y1] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
for (i = x1 + 1; i <= x2; i++)
{
map[i][y1] = 0;
}
score += 10;
return 1;
}
}
else
{
for (i = x2 + 1; i <= x1; i++)
{
if (map[i][y1] != 0)
{
flag = 0;
break;
}
}
if (flag)
{
for (i = x2 + 1; i <= x1; i++)
{
map[i][y1] = 0;
}
score += 10;
return 1;
}
}
}
return 0;
}
int check_map(void)
{
int i, j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if (map[i][j] != 0)
{
return 0;
}
}
}
return 1;
}
int main(void)
{
int x1, y1, x2, y2;
int i, j;
init_map();
while (1)
{
system("clear");
print_map();
printf("Score: %d\n", score);
printf("Enter two positions: ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if (x1 < 0 || x1 >= ROW || y1 < 0 || y1 >= COL || x2 < 0 || x2 >= ROW || y2 < 0 || y2 >= COL)
{
printf("Invalid position!\n");
continue;
}
if (map[x1][y1] == 0 || map[x2][y2] == 0)
{
printf("The position is empty!\n");
continue;
}
if (!find_path(x1, y1, x2, y2))
{
printf("No path found!\n");
continue;
}
if (!remove(x1, y1, x2, y2))
{
printf("Not the same kind of fruit!\n");
continue;
}
if (check_map())
{
system("clear");
print_map();
printf("Score: %d\n", score);
printf("You win!\n");
break;
}
}
return 0;
}
```
这段代码实现了一个简单的连连看游戏,包括初始化地图,打印地图,查找路径,消除相同的水果,检测是否胜利等功能。你可以将这段代码编译运行,体验一下自动连连看的乐趣。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)