Linux环境下连连看游戏代码,《连连看》算法C语言演示(自动连连看)
时间: 2023-09-12 09:02:51 浏览: 59
很高兴为您提供代码示例,以下是在Linux环境下实现自动连连看的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROW 20
#define MAX_COL 20
int map[MAX_ROW][MAX_COL]; // 存储地图
int path[MAX_ROW][MAX_COL]; // 存储路径
struct node {
int x, y;
int step; // 步数
};
// 判断两个点是否连通
int is_link(int x1, int y1, int x2, int y2)
{
if (map[x1][y1] != map[x2][y2]) {
return 0;
}
// 横向连通
if (x1 == x2) {
int min_y = (y1 < y2 ? y1 : y2);
int max_y = (y1 > y2 ? y1 : y2);
int i;
for (i = min_y + 1; i < max_y; i++) {
if (map[x1][i] != 0) {
return 0;
}
}
return 1;
}
// 纵向连通
if (y1 == y2) {
int min_x = (x1 < x2 ? x1 : x2);
int max_x = (x1 > x2 ? x1 : x2);
int i;
for (i = min_x + 1; i < max_x; i++) {
if (map[i][y1] != 0) {
return 0;
}
}
return 1;
}
// 拐弯连通
if (map[x1][y2] == 0 && is_link(x1, y1, x1, y2) && is_link(x2, y2, x1, y2)) {
return 1;
}
if (map[x2][y1] == 0 && is_link(x1, y1, x2, y1) && is_link(x2, y2, x2, y1)) {
return 1;
}
return 0;
}
// 查找两个点之间的最短路径
int find_path(int x1, int y1, int x2, int y2)
{
int i, j;
struct node queue[MAX_ROW * MAX_COL];
int front = 0, rear = 0;
memset(path, 0, sizeof(path));
// 初始化队列
queue[rear].x = x1;
queue[rear].y = y1;
queue[rear].step = 0;
path[x1][y1] = 1;
rear++;
// 广度优先搜索
while (front < rear) {
struct node cur = queue[front++];
if (cur.x == x2 && cur.y == y2) {
return cur.step;
}
// 上
if (cur.x > 0 && path[cur.x - 1][cur.y] == 0 && (map[cur.x - 1][cur.y] == 0 || (cur.x - 1 == x2 && cur.y == y2 && map[x2][y2] == 0)) && is_link(cur.x, cur.y, cur.x - 1, cur.y)) {
queue[rear].x = cur.x - 1;
queue[rear].y = cur.y;
queue[rear].step = cur.step + 1;
path[cur.x - 1][cur.y] = 1;
rear++;
}
// 下
if (cur.x < MAX_ROW - 1 && path[cur.x + 1][cur.y] == 0 && (map[cur.x + 1][cur.y] == 0 || (cur.x + 1 == x2 && cur.y == y2 && map[x2][y2] == 0)) && is_link(cur.x, cur.y, cur.x + 1, cur.y)) {
queue[rear].x = cur.x + 1;
queue[rear].y = cur.y;
queue[rear].step = cur.step + 1;
path[cur.x + 1][cur.y] = 1;
rear++;
}
// 左
if (cur.y > 0 && path[cur.x][cur.y - 1] == 0 && (map[cur.x][cur.y - 1] == 0 || (cur.x == x2 && cur.y - 1 == y2 && map[x2][y2] == 0)) && is_link(cur.x, cur.y, cur.x, cur.y - 1)) {
queue[rear].x = cur.x;
queue[rear].y = cur.y - 1;
queue[rear].step = cur.step + 1;
path[cur.x][cur.y - 1] = 1;
rear++;
}
// 右
if (cur.y < MAX_COL - 1 && path[cur.x][cur.y + 1] == 0 && (map[cur.x][cur.y + 1] == 0 || (cur.x == x2 && cur.y + 1 == y2 && map[x2][y2] == 0)) && is_link(cur.x, cur.y, cur.x, cur.y + 1)) {
queue[rear].x = cur.x;
queue[rear].y = cur.y + 1;
queue[rear].step = cur.step + 1;
path[cur.x][cur.y + 1] = 1;
rear++;
}
}
return -1; // 没有找到路径
}
// 自动连连看
int auto_link()
{
int i, j, k, l;
int min_step = -1; // 最短路径
int x1 = -1, y1 = -1, x2 = -1, y2 = -1; // 连通的两个点
for (i = 0; i < MAX_ROW; i++) {
for (j = 0; j < MAX_COL; j++) {
if (map[i][j] != 0) {
for (k = i; k < MAX_ROW; k++) {
for (l = 0; l < MAX_COL; l++) {
if (map[k][l] != 0 && (i != k || j != l)) {
int step = find_path(i, j, k, l); // 查找路径
if (step != -1 && (min_step == -1 || step < min_step)) {
min_step = step;
x1 = i;
y1 = j;
x2 = k;
y2 = l;
}
}
}
}
}
}
}
if (min_step != -1) {
printf("(%d,%d)->(%d,%d)\n", x1, y1, x2, y2);
map[x1][y1] = 0;
map[x2][y2] = 0;
return 1; // 连通成功
}
return 0; // 连通失败
}
// 初始化地图
void init_map()
{
memset(map, 0, sizeof(map));
int i, j;
for (i = 0; i < MAX_ROW; i++) {
for (j = 0; j < MAX_COL; j++) {
map[i][j] = rand() % 3;
}
}
}
// 打印地图
void print_map()
{
int i, j;
for (i = 0; i < MAX_ROW; i++) {
for (j = 0; j < MAX_COL; j++) {
printf("%d ", map[i][j]);
}
printf("\n");
}
}
int main()
{
srand(time(NULL));
init_map();
print_map();
while (auto_link()) {
print_map();
}
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)