| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1428 人关注过本帖
标题:为什么最后运行不出来,程序终止
只看楼主 加入收藏
小徐爱编程
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2019-3-24
结帖率:50%
收藏
 问题点数:0 回复次数:1 
为什么最后运行不出来,程序终止
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#define N 1
#define M 100
int map[M + 1][M + 1] = { 0 };
char str[2 * (M + 1)*(M + 1)] = { '\0' };
int my_x, my_y;
int count;
void ine(int count);
void print();
void find();
int move();
int Up();
int Down();
int Right();
int Left();
void menu();
int Neb(int x_index, int y_index);
void creat(int x_index, int y_index);

void main()
{
    int flag = 0;
    int    q=1;
    srand((unsigned)time(NULL));
    system("mode con cols=220 lines=100");
    system("color 0B");
    while (q)
    {
        menu();
        ine(count);
        flag = move();
        if (flag == 1)
        {
            printf("again?1->yes/0->no");
            scanf("%d", &q);
        }

    }
}

void menu()
{
    printf("control by w a s d\n");
    printf("Select map size\n");
    scanf("%d", &count);
}

void ine(int count)
{
    int i, j;
    if (count % 2 == 0)
    {
        count++;
    }
    for (i = 0; i<count; i++)
        for (j = 0; j<count; j++)
        {
            map[i][j] = 0;
            if (i != 0 && j != 0 && i != (count - 1) && j != (count - 1))
            {
                if (i % 2 != 0)
                    if (j % 2 == 1)
                        map[i][j] = 1;
            }
    }
    for (i = 0; i <= M + 1; i++)
        for (j = 0; j <= M + 1; j++)
        {
            if (i >= count&&j >= count)
            {
                map[i][j] = -1;
            }
        }
    creat(1, 1);
    for (i = 0;i < count;i++)
    {
        for (j = 0;j < count;j++)
            if (map[i][j] == 5)
                map[i][j] = 1;
    }
    map[1][1] = 3;
    map[count - 2][count - 2] = 4;
}
void creat(int x_index, int y_index)
{
    int i, j;
    int rand_position, x, y;
    int flag = 0;
    x=x_index;
    y=y_index;
    while (1)
    {

        flag = Neb(x_index ,y_index);
        if (flag == 0)
        {
            return;
        }
        else if (flag==1)
        {
            map[x_index][y_index]=5;
            x = x_index;
            y = y_index;
            while (1)
            {
                rand_position = rand() % 4;
                if (rand_position == 0 && x_index >= 3 && map[x_index - 2][y_index] == 1)
                    x_index = x_index - 2;
                else if (rand_position == 1 && x_index<count - 3 && map[x_index + 2][y_index] == 1)
                    x_index = x_index + 2;
                else if (rand_position == 2 && y_index >= 3 && map[x_index][y_index - 2] == 1)
                    y_index = y_index - 2;
                else if (rand_position == 3 && y_index<count - 3 && map[x_index][y_index + 2] == 1)
                    y_index = y_index + 2;
                map[(x + x_index) / 2][(y + y_index) / 2] = 5;
                map[x_index][y_index] = 5;
                creat(x_index, y_index);
                break;
            }
        }
    }


}
int Neb(int x_index, int y_index)
{
    if ((x_index >= 3 && map[x_index - 2][y_index] == 1) || (x_index < count - 3 && map[x_index + 2][y_index] == 1) || (y_index >= 3 && map[x_index][y_index - 2] == 1) || (y_index < count - 3 && map[x_index][y_index + 2]))
    {
        return 1;
    }
    return 0;
}
void print()
{
    int i, j;
    str[0] = '\0';
    for (i = 0; i<M; i++)
    {
        for (j = 0; j<M; j++)
        {
            if (map[i][j] == -1)
                break;
            else if (map[i][j] == 0)
                strcat(str, "■");
            else if (map[i][j] == 1)
                strcat(str, " ");
            else if (map[i][j] == 3)
                strcat(str, "⊙");
            else if (map[i][j] == 4)
                strcat(str, "☆");
        }
        if (map[i][0] != -1)
            strcat(str, "\n");
    }
    printf("%s", str);
}
int move()
{
    int flag = 0;
    print();
    while (1)
    {
        find();
        switch (getchar())
        {
        case 'w':flag = Up(); break;
        case 's':flag = Down(); break;
        case 'a':flag = Left(); break;
        case 'd':flag = Right(); break;
        }
        if (flag == 2)
            print();
        else if (flag == 1)
            printf("恭喜通过");
        return 1;
    }
}
int Up()
{
    if (my_x != 0)
    {
        if (map[my_x - 1][my_y] == 1)
        {
            map[my_x - 1][my_y] = 3;
            map[my_x][my_y] = 1;
            return 2;
        }
        else if (map[my_x][my_y] == 4)
        {
            return 1;
        }

    }
    return 0;
}
int Down()
{
    if (my_x != 0)
        if (map[my_x + 1][my_y] == 1)
        {
            map[my_x + 1][my_y] = 3;
            map[my_x][my_y] = 1;
            return 2;
        }
    else if (map[my_x][my_y] == 4)
    {
        return 1;
    }
    return 0;

}
int Right()
{
    if (my_y != count - 1)
        if (map[my_x][my_y + 1] == 1)
        {
            map[my_x][my_y + 1] = 3;
            map[my_x][my_y] = 1;
        }
    else if (map[my_x][my_y] == 4)
    {
        return 1;
    }
    return 0;

}
int Left()
{
    if (my_y != 0)
        if (map[my_x][my_y - 1] == 1)
        {
            map[my_x][my_y - 1] = 3;
            map[my_x][my_y] = 1;
        }
    else if (map[my_x][my_y] == 4)
    {
        return 1;
    }
    return 0;
}
void find()
{
    int i, j;
    for (i = 0; i<count; i++)
        for (j = 0; j<count; j++)
        {
            if (map[i][j] == 3)
            {
                my_x = i;
                my_y = j;
            }
        }
}
搜索更多相关主题的帖子: int map count flag return 
2019-06-16 17:25
aCprogrammer
Rank: 2
等 级:论坛游民
威 望:3
帖 子:38
专家分:43
注 册:2019-6-25
收藏
得分:0 
光是看完我都要疯了
2019-06-25 22:09
快速回复:为什么最后运行不出来,程序终止
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020523 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved