At the request of some people, the number of lines of code has been violently compressed. If it is not easy to read, please see [Update] C language to simply simulate Gaussian ants: random walk problem
#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 number here by 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 result 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] = ' ';Num[i][j] = 0;}//Initialization 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:do {Calculate_1(Ant_X);Sleep(1000);} while (1);break;//One-bit coordinate axis simulation case 2:do {Calculate_2(Ant_X, Ant_Y);Sleep(1000);} while (1);break;//Two-digit coordinate system simulation 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 result to the screen*/ switch (Dime) { case 1: printf("\033[36m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\\ \033[0m\ \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:printf("\033[0m%c\033[0m", Map[0][i]);break; case 2:printf("\033[33m%c\033[0m", Map[0][i]);break; case 3:printf("\033[32m%c\033[0m", Map[0][i]);break; case 4:printf("\033[35m%c\033[0m", Map[0][i]);break; default:printf("\033[31m%c\033[0m", Map[0][i]);break; } } printf("\t\t\t\t\033[36m ┃\\ \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 ┃\\ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━┛\\ \ \033[0m\033[%d;%dH", 2, 1);//The one-dimensional output ends here break; case 2: printf("\033[36m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━ ━━━━━━━━━━━━━━━┓\\ \033[0m\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:printf("\033[0m%c\033[0m", Map[i][j]);break; case 2:printf("\033[33m%c\033[0m", Map[i][j]);break; case 3:printf("\033[32m%c\033[0m", Map[i][j]);break; case 4:printf("\033[35m%c\033[0m", Map[i][j]);break; default: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┃\\ ┗━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ \\ \033[0m\ \033[%d;%dH", 2, 1); break; } for (int i=0;i<21;i + + ) for (int j = 0; j < 21; j + + ) {Map[i][j] = ' ';Num[i][j] = 0;}//Initialize every run, otherwise something will go wrong 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() % 2;//Modulo 2 to get 0 or 1 if (X[i]==10) Map[0][X[i]] = ' + '; else if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Map[0][X[i]] = '-'; X[i] + = (2 * random_num - 1);//y=2*x-1 realizes the conversion of random_num=1 into + 1, and random_num=0 into -1 if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Map[0][X[i]] = '*'; }//The above completes the simulation of 5 ants for (int i = 0; i < NumberOfAnts; i + + ) if ((X[i] >= 0) & amp; & amp; (X[i] <= 20)) Num[0][X[i ]] + + ;//Record statistics for each digit of the one-dimensional coordinate axis 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() % 2;//get 0 or 1 switch (random_num) { case 0: {//If you get 0, move in the y-axis direction random_num = rand() % 2; // Generate random number, get 0 or 1 /* Generate random number again to determine up or down */ if ((Y[i] == 10) & amp; & amp; (X[i] == 10))Map[Y[i]][X[i]] = ' + ';//Note 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] + = (2 * random_num - 1);//y=2*x-1 realizes the conversion of random_num=1 into + 1, and random_num=0 into -1 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 1: {//If you get 1, move in the x-axis direction random_num = rand() % 2; // Generate a random number and get 0 or 1 /* Generate a random number again to determine whether to go right or left */ if ((Y[i] == 10) & amp; & amp; (X[i] == 10)) Map[Y[i]][X[i]] = ' + ';//Note 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] + =(2 * random_num - 1);//y=2*x-1 realizes the conversion of random_num=1 into + 1, and random_num=0 into -1 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; } } }//The above completes the movement simulation of 5 ants 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]] + + ;//Statistics and writing 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; }
New content: Use the one-time function y=2*x-1 to directly realize the conversion of 0->-1 and 1-> + 1, reducing the use of 3 switches.
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194645 people are learning the system