如何在C中移动打印的字符



我即将完成我的项目,该项目在环形交叉路口移动打印的汽车,但我有两个大问题:

  • 当生成一个带有rand的字符,并且该字符向前移动时(例如,它的初始位置是[5][0],现在他在[5][1]),如何在每次初始字符移动时在[5][0]中生成另一个字符?

  • 我的环形交叉路口有两种方式,就像在现实生活中一样,但当我制定并执行程序时,这两种方式的两辆车不会同时移动,我的程序会从右侧移动汽车,当他到达时,它会开始移动左侧的一辆

我的环岛在图片中:我的环形交叉路口

我的程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NB_L 43
#define NB_C 90
typedef struct array ARRAY;
struct array {
        int matrice1 [NB_L][NB_C];
        int matrice2 [NB_L][NB_C];
};
// Les prototypes des fonctions
void myDelay (float i);
void roadMap1 (ARRAY *vehicule);
void roadMap2 (ARRAY *vehicule);
void initCarsFromWest (ARRAY *vehicule);
void deplacement1 (ARRAY *vehicule);
void deplacement2 (ARRAY *vehicule);
void movingCarsFromWest (ARRAY *vehicule);
// Fais une pause de l'application durant i seconds
void myDelay (float i) {
        clock_t start, end;
        start = clock ();
        while (((end = clock ()) - start) <= i * CLOCKS_PER_SEC);
}
// Fonction qui initialise chaque case du giratoire dans un tableau
void roadMap1 (ARRAY *vehicule) {
        int i = 0, j = 0;
        // NB_L de NB_C
        for (i = 0; i < NB_L; i++) {
                for (j = 0; j < NB_C; j++) {
                        vehicule -> matrice1 [i][j] = 0;
                }
        }
        // La partie nord de notre giratoire.
        for (i = 1; i < 14; i++) {
                vehicule -> matrice1 [i][40] = 20;
                vehicule -> matrice1 [i][42] = 13;
                vehicule -> matrice1 [i][44] = 20;
                vehicule -> matrice1 [i][46] = 13;
                vehicule -> matrice1 [i][48] = 20;
        }
        //La partie est du giratoire.
        for (j = 61; j < 89; j++) {
                vehicule -> matrice1 [17][j] = 16;
                vehicule -> matrice1 [19][j] = 15;
                vehicule -> matrice1 [21][j] = 12;
                vehicule -> matrice1 [23][j] = 15;
                vehicule -> matrice1 [25][j] = 16;
        }
        // La partie sud du giratoire.
        for (i = 29; i < 42; i++) {
                vehicule -> matrice1 [i][40] = 20;
                vehicule -> matrice1 [i][42] = 13;
                vehicule -> matrice1 [i][44] = 20;
                vehicule -> matrice1 [i][46] = 13;
                vehicule -> matrice1 [i][48] = 20;
        }
        //La partie ouest du giratoire.
        for (j = 0; j < 28; j++) {
                vehicule -> matrice1 [17][j] = 16;
                vehicule -> matrice1 [19][j] = 15;
                vehicule -> matrice1 [21][j] = 12;
                vehicule -> matrice1 [23][j] = 15;
                vehicule -> matrice1 [25][j] = 16;
        }
        // Le giratoire.
        for (i = 15; i < 17; i++) {
                vehicule -> matrice1 [i][28] = 20;
        }
        for (i = 26; i < 28; i++) {
                vehicule -> matrice1 [i][28] = 20;
        }
        for (j = 29; j < 40; j++) {
                vehicule -> matrice1 [14][j] = 16;
                vehicule -> matrice1 [28][j] = 16;
        }
        for (j = 49; j < 60; j++) {
                vehicule -> matrice1 [14][j] = 16;
                vehicule -> matrice1 [28][j] = 16;
        }
        for (i = 15; i < 17; i++) {
                vehicule -> matrice1 [i][60] = 20;
        }
        for (i = 26; i < 28; i++) {
                vehicule -> matrice1 [i][60] = 20;
        }
        // Le centre de notre giratoire.
        for (j = 35; j < 54; j++) {
                vehicule -> matrice1 [17][j] = 11;
                vehicule -> matrice1 [18][j] = 11;
                vehicule -> matrice1 [19][j] = 11;
                vehicule -> matrice1 [20][j] = 11;
                vehicule -> matrice1 [21][j] = 11;
                vehicule -> matrice1 [22][j] = 11;
                vehicule -> matrice1 [23][j] = 11;
                vehicule -> matrice1 [24][j] = 11;
                vehicule -> matrice1 [25][j] = 11;
        }
        // Les "coins" du centre de notre giratoire.
        vehicule -> matrice1 [17][28] = 21;
        vehicule -> matrice1 [25][28] = 18;
        vehicule -> matrice1 [14][28] = 19;
        vehicule -> matrice1 [28][28] = 17;
        vehicule -> matrice1 [14][40] = 21;
        vehicule -> matrice1 [28][40] = 18;
        vehicule -> matrice1 [14][48] = 17;
        vehicule -> matrice1 [28][48] = 19;
        vehicule -> matrice1 [14][60] = 18;
        vehicule -> matrice1 [28][60] = 21;
        vehicule -> matrice1 [17][60] = 17;
        vehicule -> matrice1 [25][60] = 19;
}
// Fonction qui sert à afficher notre giratoire
void roadMap2 (ARRAY *vehicule) {
        int i = 0, j = 0;
        // NB_L de NB_C
        for (i = 0; i < NB_L; i++) {
                for (j = 0; j < NB_C; j++) {
                        if (vehicule -> matrice1 [i][j] == 0) {
                                printf(" ");
                        }
                        if (vehicule -> matrice1 [i][j] == 1) {
                                printf("N");
                        }
                        if (vehicule -> matrice1 [i][j] == 2) {
                                printf("E");
                        }
                        if (vehicule -> matrice1 [i][j] == 3) {
                                printf("S");
                        }
                        if (vehicule -> matrice1 [i][j] == 4) {
                                printf("W");
                        }
                        if (vehicule -> matrice1 [i][j] == 11) {
                                printf("█");
                        }
                        if (vehicule -> matrice1 [i][j] == 12) {
                                printf("■");
                        }
                        if (vehicule -> matrice1 [i][j] == 13) {
                                printf("|");
                        }
                        if (vehicule -> matrice1 [i][j] == 14) {
                                printf("╬");
                        }
                        if (vehicule -> matrice1 [i][j] == 15) {
                                printf("-");
                        }
                        if (vehicule -> matrice1 [i][j] == 16) {
                                printf("═");
                        }
                        if (vehicule -> matrice1 [i][j] == 17) {
                                printf("╚");
                        }
                        if (vehicule -> matrice1 [i][j] == 18) {
                                printf("╗");
                        }
                        if (vehicule -> matrice1 [i][j] == 19) {
                                printf("╔");
                        }
                        if (vehicule -> matrice1 [i][j] == 20) {
                                printf("║");
                        }
                        if (vehicule -> matrice1 [i][j] == 21) {
                                printf("╝");
                        }
                } printf("n");
        }
}
// Fonction qui initialise les deux voitures partant de l'ouest
void initCarsFromWest (ARRAY *vehicule) {
        // char direction [] = {'N', 'S', 'W', 'E'};
        vehicule -> matrice1 [22][0] = rand () % 5;
        vehicule -> matrice1 [24][0] = rand () % 5;
}
// Déplacement sur la ligne 24
void deplacement1 (ARRAY *vehicule) {
        int i = 0, j = 0;
        for (j = 0; j < 30; j++) {
                if (vehicule -> matrice1 [24][j] == 1 || vehicule -> matrice1 [24][j] == 2 || vehicule -> matrice1 [24][j] == 3 || vehicule -> matrice1 [24][j] == 4) {
                        vehicule -> matrice1 [24][j+1] = vehicule -> matrice1 [24][j];
                        vehicule -> matrice1 [24][j] = 0;
                }
                myDelay (0.1);
                system ("clear");
                roadMap2 (vehicule);
        }
}
// Déplacement sur la ligne 22
void deplacement2 (ARRAY *vehicule) {
        int i = 0, j = 0;
        for (j = 0; j < 32; j++) {
                if (vehicule -> matrice1 [22][j] == 1 || vehicule -> matrice1 [22][j] == 2 || vehicule -> matrice1 [22][j] == 3 || vehicule -> matrice1 [22][j] == 4) {
                        vehicule -> matrice1 [22][j+1] = vehicule -> matrice1 [22][j];
                        vehicule -> matrice1 [22][j] = 0;
                }
                myDelay (0.1);
                system ("clear");
                roadMap2 (vehicule);
        }
}
void movingCarsFromWest (ARRAY *vehicule) {
        roadMap1 (vehicule);
        initCarsFromWest (vehicule);
        int i = 0, j = 0;
        // Nord
        if (vehicule -> matrice1 [24][0] == 1) {
                deplacement1 (vehicule);
        }
        // Est
        if (vehicule -> matrice1 [24][0] == 2) {
                deplacement1 (vehicule);
        }
        // Sud
        if (vehicule -> matrice1 [24][0] == 3) {
                deplacement1 (vehicule);
        }
        // Ouest
        if (vehicule -> matrice1 [24][0] == 4) {
                deplacement1 (vehicule);
        }
// Nord
            if (vehicule -> matrice1 [22][0] == 1) {
                    deplacement2 (vehicule);
            }
            // Est
            if (vehicule -> matrice1 [22][0] == 2) {
                    deplacement2 (vehicule);
            }
            // Sud
            if (vehicule -> matrice1 [22][0] == 3) {
                    deplacement2 (vehicule);
            }
            // Ouest
            if (vehicule -> matrice1 [22][0] == 4) {
                    deplacement2 (vehicule);
            }
        roadMap2 (vehicule);
}
int main (int argc, int **argv) {
ARRAY *vehicule;
        srand (time (NULL));
        while (1) {
                movingCarsFromWest (vehicule);
                system ("clear");
        }
        return 0;
}

一旦字符打印到STDOUT,就不能删除它。可以使用ANSI控件擦除打印的字符。

您可以删除单个字符、行,也可以擦除整个屏幕并重新打印。以下是擦除一行的示例:

#include <stdio.h>
int main(){
    printf("hellon");
    fflush(stdout);
    sleep(1);
    //clear from cursor to the beginning of the line
    printf("33[1K");
    //move cursor to the beginning of the line
    printf("33[F");
    printf("world!n");
    return 0;
}

我希望这能回答你的问题。。。

问题1是您的代码立即崩溃。它崩溃是因为这个:

ARRAY *vehicule;
while (1) {
    movingCarsFromWest (vehicule);

您没有初始化vehicule,所以它指向内存中的一个随机位置,这会导致未定义的行为。您需要的是实际分配一个ARRAY,而不是指向ARRAY的指针,然后传递其地址,如下所示:

ARRAY vehicule;
while (1) {
    movingCarsFromWest (&vehicule);

进行此更改至少允许您的程序运行。我不知道它是否产生了正确的结果,因为你还没有定义正确的结果是什么,但你应该能够从这里取得进展。

最新更新