c语言康威生命游戏
请教一下我的代码要怎么改才能运行呢?0,0,0 0,1,0
1,1,1如何变位 0,1,0
0,0,0 0,1,0
#include "jj.h"
#include <stdio.h>
#include <stdlib.h>
#define BOARD 3
char world =
{
0,0,0,
1,1,1,
0,0,0
};
void game_run(char world, int nLive)
{
int i = 0, j = 0;
int nAliveCnt = 0;
int nContinue = 0;
int k=0;
for (i=0; i< 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", world);
}
printf("\n");
}
printf("***********************************\n\n");
while (k<1)
{
for (i = 0; i < BOARD; i++)
{
for (j = 0; j < BOARD; j++)
{
nAliveCnt = 0;
if ((i-1>0)&&(j-1>0)&&(world) > 0)
{
nAliveCnt++;
}
if ((i-1>0)&&(world>0))
{
nAliveCnt++;
}
if ((j+1<BOARD)&&(i-1>0)&&(world >0))
{
nAliveCnt++;
}
if ((j-1>0)&&(world>0))
{
nAliveCnt++;
}
if ((j+1<BOARD)&&(world>0))
{
nAliveCnt++;
}
if ((i+1<BOARD)&&(j-1>0)&&(world>0))
{
nAliveCnt++;
}
if ((i+1<BOARD)&&(world>0))
{
nAliveCnt++;
}
if ((i+1<BOARD)&&(j+1<BOARD)&&(world> 0))
{
nAliveCnt++;
}
if ((world == 0) && (nAliveCnt == 3))
{
world = 1;
nLive++;
}
else if (world == 1)
{
if (nAliveCnt != 2)
{
world = 0;
nLive--;
}
}
}
}
for (i=0; i < 3; i++)
{
for (j =0; j < 3; j++)
{
printf("%d ", world);
}
printf("\n");
}
printf("===========================\n");
k++;
} // end of while
}
int main()
{
printf("\n");
game_run(world, 3);
return 0;
}
可以这样做#include<stdio.h>
#define N 3
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main(void)
{
int a = {1,2,3,4,5,6,7,8,9};
int (*p) = a;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
printf("%d ", p);
printf("\n");
}
printf("\n");
// 主要是这个
for(int i = 0; i < N; i++)
for(int j = i; j < N; j++)
swap( (*(p + i) + j), (*(p + j) + i));
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
printf("%d ", p);
printf("\n");
}
return 0;
} claws0n 发表于 2018-9-28 20:16
可以这样做
但是这个不是康威生命游戏啊 qpwoeiruyt 发表于 2018-9-28 21:45
但是这个不是康威生命游戏啊
有没有大神教一教
页:
[1]