Previous article Portal: C language simply simulates Gaussian ants: random walk problem (graphical)
1. Update content
Note: The code shared in the previous article will not be repeated here.
1. Fixed coordinate system
In C language, we can use printf to change the position of the cursor, thereby changing the starting position of printing. When the starting position is the same every time printing is performed, this output overwrites the previous output, thus realizing the coordinate axis Or the fixation of the coordinate system.
For the up, down, left and right movement of the cursor, we can add the following code:
printf("\033[A");//Move the cursor up one line printf("\033[B");//Move the cursor down one line printf("\033[C");//Move the cursor one line to the right printf("\033[D");//Move the cursor one line to the left printf("\033[H");//Move the cursor to the upper left corner of the interface printf("\033[f");//Move the cursor to the beginning of this line
So if we want to move to a certain position on the screen, we can:
printf("\033[%d;%dH",2,6); //Move the cursor to the second row and sixth column
2. New interface layout
Pages have been reformatted to accommodate the fixed nature of overlay printing.
3. Improve compatibility
Changed all printf_s and scanf_s to printf and scanf, and added _CRT_SECURE_NO_WARNINGS statement. Enhanced code compatibility in different compilation software.
2. Live operation
1. One-dimensional simulation (100 ants)
2. Two-dimensional simulation (100 ants)
3. Complete code
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <Windows.h> #include <time.h> #include "unistd.h" #define NumberOfAnts 100//Constant that defines the number of ants ///If you want to try other numbers of ants, you can modify the numbers here yourself. char Map[21][21];//Storage map data int Num[21][21];//Storage how many ants there are in each position void Print(char chMap[21][21],int Dime); /*Output the results to the screen*/ void Calculate_1(int X[NumberOfAnts]); void Calculate_2(int X[NumberOfAnts],int Y[NumberOfAnts]); int main(void) { int Ant_X[NumberOfAnts]; int Ant_Y[NumberOfAnts]; /*Define the x and y variables of the ant and place them in the center of the coordinate axis*/ int Dimension = 0;//The dimension that the user needs to input for (int i = 0; i < NumberOfAnts; i + + ) Ant_X[i] = Ant_Y[i] = 10; for (int i = 0; i < 21; i + + ) for (int j = 0; j < 21; j + + ) { Map[i][j] = ' ';//Initialization Num[i][j] = 0; } WrongInputToBack://If the input is wrong, jump back here printf("\033[35m>\033[32mPlease input the dimension you want to imitate:\033[0m"); scanf("%d", & amp;Dimension);//Prompts the user to enter the dimensions to be simulated switch (dimension) { case 1: {//One-bit coordinate axis simulation do { Calculate_1(Ant_X); Sleep(1000); } while (1); break; } case 2: {//Two-digit coordinate system simulation do { Calculate_2(Ant_X, Ant_Y); Sleep(1000); } while (1); break; } default: printf("\033[35m>\033[31mYour input is not allowed! Please input 1 or 2.\033[0m\\ "); goto WrongInputToBack; } return 0; } void Print(char chMap[21][21], int Dime) {/*Output the results to the screen*/ switch (Dime) { case 1: printf("\033[36m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\\ \033[0m\ "); printf("\033[36m┃\t\t\t \033[0m"); for (int i = 0; i < 21; i + + ) { if ((Map[0][i] == '-') || (Map[0][i] == '>') || (Map[0][i] == ' + ')) printf("%c", Map[0][i]); if (Map[0][i]=='*') switch (Num[0][i]) { case 1://An ant is normally white printf("\033[0m%c\033[0m", Map[0][i]); break; case 2://Two ants stacked are represented by yellow printf("\033[33m%c\033[0m", Map[0][i]); break; case 3://Three ants stacked in green printf("\033[32m%c\033[0m", Map[0][i]); break; case 4://Four ants stacked in purple printf("\033[35m%c\033[0m", Map[0][i]); break; default://Stacks of five or more ants are shown in red printf("\033[31m%c\033[0m", Map[0][i]); break; } } printf("\t\t\t\t\033[36m ┃\\ "); printf("\033[36m┃ \033[0m*\033[36m:1 ant \t\033[33m*\033[36m:2 ants\t\033[32m* \033[36m:3 ants\t\033[35m*\033[36m:4 ants\t\033[31m*\033[36m:5 + ants ┃\\ ") ; printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\\ \033[0m"); //One-dimensional output ends here printf("\033[%d;%dH", 2, 1); //Move the cursor to achieve print coverage and maintain image stability break; case 2: printf("\033[36m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━ ━━━━━━━━━━━━━━━┓\\ \033[0m"); printf("\033[36m┃\033[36m\t\t\t\t\t\t\t┃\\ \033[36m┃ \033[ 0m"); for (int i = 0; i < 21; i + + ) { for (int j = 0; j < 21; j + + ) { if ((Map[i][j] == '-') || (Map[i][j] == '>') || (Map[i][j] == ' + ') || (Map[i][j] == '|') || (Map[i][j] == ' ') || (Map[i][j] = = '^')) printf("%c", Map[i][j]); if (Map[i][j] == '*') switch (Num[i][j]) { case 1://An ant is normally white printf("\033[0m%c\033[0m", Map[i][j]); break; case 2://Two ants stacked are represented by yellow printf("\033[33m%c\033[0m", Map[i][j]); break; case 3://Three ants stacked in green printf("\033[32m%c\033[0m", Map[i][j]); break; case 4://Four ants stacked in purple printf("\033[35m%c\033[0m", Map[i][j]); break; default://Stacks of five or more ants are shown in red printf("\033[31m%c\033[0m", Map[i][j]); break; } } if (i >= 0 & amp; & amp; i <= 4) { switch(i) { case 0: printf("\t\t\033[0m*\033[36m:1 ant \033[36m\t\t┃\\ \033[36m┃ \033[0m "); break; case 1: printf("\t\t\033[33m*\033[36m:2 ants\033[36m\t\t┃\\ \033[36m┃ \033[0m "); break; case 2: printf("\t\t\033[32m*\033[36m:3 ants\033[36m\t\t┃\\ \033[36m┃ \033[0m "); break; case 3: printf("\t\t\033[35m*\033[36m:4 ants\033[36m\t\t┃\\ \033[36m┃ \033[0m "); break; case 4: printf("\t\t\033[31m*\033[36m:5 + ants\033[36m\t\t┃\\ \033[36m┃ \033[ 0m"); break; } } else printf("\t\t\t\t\t\033[36m┃\\ \033[36m┃ \033[0m"); } printf("\033[36m\t\t\t\t\t\t\t┃\\ "); printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━ ━━━━━━━━━━┛\\ \033[0m"); printf("\033[%d;%dH", 2, 1); //Move the cursor to achieve print coverage and maintain image stability break; } for (int i=0;i<21;i + + ) for (int j = 0; j < 21; j + + ) { Map[i][j] = ' '; Num[i][j] = 0; }//Initialize every time you run, otherwise problems will occur return; } void Calculate_1(int X[NumberOfAnts]) {/*Operation for one-dimensional simulation*/ srand(time(NULL)); // Initialize random number seed int random_num = rand(); // Generate random numbers for (int i = 0; i < NumberOfAnts; i + + ) { random_num = rand(); random_num %= 2; // Take modulo 2 to get 0 or 1 if (random_num == 1)//1 then go right { if (X[i]==10) Map[0][X[i]] = ' + '; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Map[0][X[i]] = '-'; X[i] + + ; if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Map[0][X[i]] = '*'; } else {//0 then go left if (X[i] == 10) Map[0][X[i]] = ' + '; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20))Map[0][X[i]] = '-'; X[i]--; if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Map[0][X[i]] = '*'; } }//The above completes the simulation of 5 ants //Next, record statistics for each of the one-dimensional coordinate axes for (int i = 0; i < NumberOfAnts; i + + ) if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Num[0][X[i]] + + ; for (int i = 0; i < 21; i + + ) if (Map[0][i] == ' ') Map[0][i] = '-'; Map[0][20] = '>'; if (Map[0][10] != '*') Map[0][10] = ' + '; Print(Map, 1);//Call function for output return; } void Calculate_2(int X[NumberOfAnts], int Y[NumberOfAnts]) {/*Operations for two-dimensional simulation*/ srand(time(NULL)); // Initialize random number seed int random_num = rand(); // Generate random numbers for (int i = 0; i < NumberOfAnts; i + + ) { random_num = rand(); random_num %= 2;//Get 0 or 1 switch (random_num) { case 0://If you get 0, move in the y-axis direction { random_num = rand(); // Generate random numbers random_num %= 2;//Get 0 or 1 /*Generate random numbers again to determine whether up or down*/ switch (random_num) { case 1://1 means upward movement { if ((Y[i] == 10) & amp; & amp; (X[i] == 10)) { Map[Y[i]][X[i]] = ' + '; }//Note that the first dimension is the y-axis and the second dimension is the x-axis else if (X[i]==10 & amp; & amp; Y[i]>=0 & amp; & amp; Y[i]<=10) Map[Y[i]][X[i]] = '|'; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y [i] <= 20)) Map[Y[i]][X[i]] = ' '; Y[i] + + ; if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y[ i] <= 20)) Map[Y[i]][X[i]] = '*'; break; } case 0://0 means downward movement { if ((Y[i] == 10) & amp; & amp; (X[i] == 10)) { Map[Y[i]][X[i]] = ' + '; }//Note that the first dimension is the y-axis and the second dimension is the x-axis else if (X[i] == 10 & amp; & amp; Y[i] >= 0 & amp; & amp; Y[i] <= 10) Map[Y[i]][X[i]] = '|'; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y [i] <= 20)) Map[Y[i]][X[i]] = ' '; Y[i]--; if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y[ i] <= 20)) Map[Y[i]][X[i]] = '*'; break; } } break; } case 1://If you get 1, move in the x-axis direction { random_num = rand(); // Generate random numbers random_num %= 2;//Get 0 or 1 /*Generate random numbers again to determine whether to go right or left*/ switch(random_num) { case 1://1 moves to the right { if ((Y[i] == 10) & amp; & amp; (X[i] == 10)) { Map[Y[i]][X[i]] = ' + '; }//Note that the first dimension is the y-axis and the second dimension is the x-axis else if (Y[i] == 10 & amp; & amp; X[i] >= 0 & amp; & amp; X[i] <= 10) Map[Y[i]][X[i]] = '-'; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y [i] <= 20)) Map[Y[i]][X[i]] = ' '; X[i] + + ; if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y[ i] <= 20)) Map[Y[i]][X[i]] = '*'; break; } case 0://0 moves left { if ((Y[i] == 10) & amp; & amp; (X[i] == 10)) { Map[Y[i]][X[i]] = ' + '; }//Note that the first dimension is the y-axis and the second dimension is the x-axis else if (Y[i] == 10 & amp; & amp; X[i] >= 0 & amp; & amp; X[i] <= 10) Map[Y[i]][X[i]] = '-'; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y [i] <= 20)) Map[Y[i]][X[i]] = ' '; X[i]--; if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y[ i] <= 20)) Map[Y[i]][X[i]] = '*'; break; } } break; } } } //The above completes the movement simulation of 5 ants //Next statistics and writing for (int i = 0; i < NumberOfAnts; i + + ) if ((X[i] >= 0) & amp; & amp; (X[i] <= 20) & amp; & amp; (Y[i] >= 0) & amp; & amp; (Y[ i] <= 20)) Num[Y[i]][X[i]] + + ; for (int i = 0; i < 21; i + + ) { if (Map[10][i] == ' ') Map[10][i] = '-'; if (Map[i][10] == ' ') Map[i][10] = '|'; } Map[10][20] = '>'; Map[0][10] = '^'; if (Map[10][10] != '*') Map[10][10] = ' + '; Print(Map, 2);//Call function for output return; }
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57317 people are learning the system