24小时热门版块排行榜    

查看: 2003  |  回复: 3
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

MajorMei

金虫 (小有名气)

[求助] 修改一个C语言生命游戏程序

学校的project,一个生命游戏的程序:
------------------------------------------------------------------------------------------------------
#include
#include

#define ARRAYSIZE1 3       /* array size, predefined by the user */
#define ARRAYSIZE2 7
#define NSTEPS 2    /* number of steps take */

int main(int argc, char *argv[]) {
   
    int i, j, n, im, ip, jm, jp, ni, nj, nsum, isum;
    int **old, **new;
    float x;
    FILE *outFile;
        
    ni = ARRAYSIZE1 + 2;  /* add 2 for left and right ghost cells */
  //  nj = ARRAYSIZE2 + 2;
    old = malloc(ni*sizeof(int*));
    new = malloc(ni*sizeof(int*));
   
    for(i=0; i         old = malloc(nj*sizeof(int));
        new = malloc(nj*sizeof(int));
    }
   
    /*  initialize elements of old to 0 or 1 */
   
    for(i=1; i<=ARRAYSIZE1; i++){
        for(j=1; j<=ARRAYSIZE2; j++){
          x = rand()/((float)RAND_MAX + 1);
            if(x<0.5){
                old[j] = 0;
            } else {
                old[j] = 1;
            }
        }
    }
   
    /*  time steps */
    for(n=0; n         
        /* corner boundary conditions */
        old[0][0] = old[ARRAYSIZE1][ARRAYSIZE2];
        old[0][ARRAYSIZE2+1] = old[ARRAYSIZE1][1];
        old[ARRAYSIZE1+1][ARRAYSIZE2+1] = old[1][1];
        old[ARRAYSIZE1+1][0] = old[1][ARRAYSIZE2];
        
        /* left-right boundary conditions */
        
        for(i=1; i<=ARRAYSIZE1; i++){
            old[0] = old[ARRAYSIZE2];
            old[ARRAYSIZE2+1] = old[1];
        }
        
        /* top-bottom boundary conditions */
        
        for(j=1; j<=ARRAYSIZE2; j++){
            old[0][j] = old[ARRAYSIZE1][j];
            old[ARRAYSIZE1+1][j] = old[1][j];
        }
        
        for(i=1; i<=ARRAYSIZE1; i++){
            for(j=1; j<=ARRAYSIZE2; j++){
                im = i-1;
                ip = i+1;
                jm = j-1;
                jp = j+1;
               
                nsum =  old[im][jp] + old[jp] + old[ip][jp]
                + old[im][j ]              + old[ip][j ]
                + old[im][jm] + old[jm] + old[ip][jm];
               
                switch(nsum){
                        
                    case 3:
                        new[j] = 1;
                        break;
                        
                    case 2:
                        new[j] = old[j];
                        break;
                        
                    default:
                        new[j] = 0;
                }
            }
        }
        
        
        for(i=1; i<=ARRAYSIZE1; i++){
            for(j=1; j<=ARRAYSIZE2; j++){
                old[j] = new[j];
            }
        }
    }
   
    /* sum the number of living cells */
    isum = 0;
    for(i=1; i<=ARRAYSIZE1; i++){
        for(j=1; j<=ARRAYSIZE2; j++){
            isum = isum + new[j];
        }
    }
    printf("\nNumber of living cells = %d\n", isum);
   
    return 0;
}
------------------------------------------------------------------------------------------------------




现在的程序直接定义了生命游戏中的初始条件,如下
#define ARRAYSIZE1 3       /* array size, predefined by the user */
#define ARRAYSIZE2 7
#define NSTEPS 2    /* number of steps take */
直接运行可以输出存活细胞数,如下
printf("\nNumber of living cells = %d\n", isum);
   
return 0;




想把程序改成满足以下条件的新程序:

运行程序的时候接受输入,格式为:
[first argument: 演化次数steps] [second argument:调用的文件名]

其中调用的文件包含了初始的grid,格式为
m 行数
n 列数
举例:
5
6
01100
01110
01000
00000
00000
00000

输出格式不变,输出存活细胞数。
请问这个程序要怎么修改呢?请熟悉c语言的前辈多多指教,在线等回复。拜谢!

[ Last edited by MajorMei on 2012-11-28 at 12:06 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

MajorMei

金虫 (小有名气)

引用回帖:
2楼: Originally posted by 缘不可解 at 2012-11-28 13:38:25
还需要读文件??请麻烦说的具体点,输入数据的具体格式是什么、

想把程序改成用户自定义某些参数运行的模式,输入的数据分为两部分,比如输入

5    grid.txt

就说明生命游戏要执行五步,初始矩阵是grid.txt这个文件里面的样子

grid.txt由三部分构成:m,n和矩阵

m 为行数
n 为列数

举例:
5
6
01100
01110
01000
00000
00000
00000

例子中是grid.txt这个文件的内容

通过命令行中的grid.txt调用此文件中的信息,然后运行程序,输出存活的细胞数。

谢谢您的回复,如果需要更详细的信息我会尽快回复您。请指点!
3楼2012-11-28 13:57:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 MajorMei 的主题更新
信息提示
请填处理意见