C游戏编程
Programming in C-Game
我只想用 30 个随机 1 填充 B 数组,这些 1 不是彼此相邻的,也不在第一行或最后一行,也不在第一行或最后一行。我试图通过创建另一个名为 empodia 的数组来做到这一点,这样我就可以得到随机数(经过格式化以便它们位于 B 数组的边界),但我无法正确地做到这一点。我不想用指针来做。感谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 23
#define M 79
int main()
{
int i,j,a,b,epilogi,seira,stili;
char A[N][M];
int B[N][M],empodia[15][2];
srand(time(NULL));
for(a=0;a<15;a++)
{
for(b=0;b<2;b++)
{
do{
seira=2+rand()%18; //i want to get a random number from 2-20
stili=2+rand()%76; //i want to get a random number from 2-77
}while(B[seira][stili]==1 || B[seira-1][stili-1]==1 || B[seira-1][stili+1]==1 || B[seira+1][stili-1]==1 || B[seira+1][stili+1]==1 || B[seira][stili-1]==1 || B[seira][stili+1]==1 || B[seira-1][stili]==1 || B[seira+1][stili]==1); //so that i will not get numbers one next to each other
empodia[a][0]= seira;
empodia[a][b+1]=stili;
B[seira][stili]=1;
}
}
for (i=0;i<N;i++)
{
for (j=0;j<M;j++)
{
if((i==0) || (j==M))
{
B[i][j]= 1;
}
else if ((i==N) || (j==0))
{
B[i][j]= 1;
}
else if ((i==N-1)|| (j==M))
{
B[i][j]= 1;
}
else if ((i==N) || (j==M-1))
{
B[i][j]= 1;
}
else if(B[i][j]!=1)
{
B[i][j]= 0;
}
}
}
for (i=0;i<N;i++)
{
for (j=0;j<M;j++)
{
printf("%d",B[i][j]);
}
printf("\n");
}
return 0;
}
感谢您提出如此有趣的问题。这是我的代码,应该可以工作。不要忘记在每次 运行 操作之前对 col
数组进行 memset。
bool col[N+3][M+3]; ///a color array to check availability
int rrr[]= {1,1,0,-1,-1,-1,0,1}; ///8 direction
int ccc[]= {0,1,1,1,0,-1,-1,-1}; ///8 direction
for(int i=0; i<30; i++){ ///for 30 random numbers
while(1){ ///loop to detect a random available position
int xx,yy;
xx = 2+rand()%18;
yy = 2+rand()%76;
if(!col[xx][yy]){ ///checking availability
B[xx][yy]=1; ///setting 1
col[xx][yy]=true; ///making all the neighboring
for(int j=0; j<8; j++){ ///points unavailable
col[xx+rrr[j]][yy+ccc[j]]=true;
}
break; ///break when finds an available position
}
}
}
我只想用 30 个随机 1 填充 B 数组,这些 1 不是彼此相邻的,也不在第一行或最后一行,也不在第一行或最后一行。我试图通过创建另一个名为 empodia 的数组来做到这一点,这样我就可以得到随机数(经过格式化以便它们位于 B 数组的边界),但我无法正确地做到这一点。我不想用指针来做。感谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 23
#define M 79
int main()
{
int i,j,a,b,epilogi,seira,stili;
char A[N][M];
int B[N][M],empodia[15][2];
srand(time(NULL));
for(a=0;a<15;a++)
{
for(b=0;b<2;b++)
{
do{
seira=2+rand()%18; //i want to get a random number from 2-20
stili=2+rand()%76; //i want to get a random number from 2-77
}while(B[seira][stili]==1 || B[seira-1][stili-1]==1 || B[seira-1][stili+1]==1 || B[seira+1][stili-1]==1 || B[seira+1][stili+1]==1 || B[seira][stili-1]==1 || B[seira][stili+1]==1 || B[seira-1][stili]==1 || B[seira+1][stili]==1); //so that i will not get numbers one next to each other
empodia[a][0]= seira;
empodia[a][b+1]=stili;
B[seira][stili]=1;
}
}
for (i=0;i<N;i++)
{
for (j=0;j<M;j++)
{
if((i==0) || (j==M))
{
B[i][j]= 1;
}
else if ((i==N) || (j==0))
{
B[i][j]= 1;
}
else if ((i==N-1)|| (j==M))
{
B[i][j]= 1;
}
else if ((i==N) || (j==M-1))
{
B[i][j]= 1;
}
else if(B[i][j]!=1)
{
B[i][j]= 0;
}
}
}
for (i=0;i<N;i++)
{
for (j=0;j<M;j++)
{
printf("%d",B[i][j]);
}
printf("\n");
}
return 0;
}
感谢您提出如此有趣的问题。这是我的代码,应该可以工作。不要忘记在每次 运行 操作之前对 col
数组进行 memset。
bool col[N+3][M+3]; ///a color array to check availability
int rrr[]= {1,1,0,-1,-1,-1,0,1}; ///8 direction
int ccc[]= {0,1,1,1,0,-1,-1,-1}; ///8 direction
for(int i=0; i<30; i++){ ///for 30 random numbers
while(1){ ///loop to detect a random available position
int xx,yy;
xx = 2+rand()%18;
yy = 2+rand()%76;
if(!col[xx][yy]){ ///checking availability
B[xx][yy]=1; ///setting 1
col[xx][yy]=true; ///making all the neighboring
for(int j=0; j<8; j++){ ///points unavailable
col[xx+rrr[j]][yy+ccc[j]]=true;
}
break; ///break when finds an available position
}
}
}