| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 546 人关注过本帖
标题:感觉良好,运行糟糕,生命游戏,求助高手
只看楼主 加入收藏
uppermore
Rank: 2
等 级:论坛游民
帖 子:33
专家分:26
注 册:2010-7-20
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:1 
感觉良好,运行糟糕,生命游戏,求助高手
自学C,试着写了一个关于生命游戏的小程序,运行的时候显示得太离谱了。所谓旁观者清,望高手帮忙看看
/*
**  生命游戏,结构字符版
**  本世纪70年代,人们曾疯魔一种被称作“生命游戏”的小游戏,这种游戏相当简单。
**  假设有一个像棋盘一样的方格网,每个方格中放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。
**  游戏规则如下:
**  1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),
**      则该细胞为生,即该细胞若原先为死,则转为生,若原先为生,则保持不变;
**  2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
**  3. 在其它情况下,该细胞为死,即该细胞若原先为生,则转为死,若原先为死,则保持不变。
**  依此规则进行迭代变化,使细胞生生死死,会得到一些有趣的结果。该游戏之所以被称为“生命游戏”,
**  是因为其简单的游戏规则,反映了自然界中的生存规律:如果一个生命,其周围的同类生命太少的话,
**  会因为得不到帮助而死亡;如果太多,则会因为得不到足够的资源而死亡。
*/
#include "stdio.h"
#include "conio.h"
/*
**   定义活细胞,死细胞的符号
*/
#define CELL_ALIVE  1
#define CELL_DEAD  '#'
/*
**      细胞个数 M * N
*/
#define M 50
#define N 50
/*
**  方向常量
*/
int const cell_direct[8][2] = {{-1,-1},{ 0,-1},{ 1,-1},
                               {-1, 0},        { 1, 0},
                               {-1, 1},{ 0, 1},{ 1, 1}};
/*
**  细胞数组,活细胞的值为1,死细胞为0
*/
int cell_map[M][N];

void  cell_d_or_al(int,int);
int   cell_isoffside(int,int,int,int);
void  cell_nexttime();
void  cell_init();
void  cell_print();
/*
** 判断细胞生死,x和y是细胞的坐标,也就是中间的那个细胞的坐标
*/
void cell_d_or_al(int x,int y)
{
    /*
    **  i用来查询细胞周围的八个细胞,count记录八个细胞中活细胞的个数
    */
    int i,count;
    count = 0;
    for(i = 0;i < 8;i++)
    {
        /*
        **  数活细胞个数,越界检查
        */
        count += cell_isoffside(x,y,cell_direct[i][0],cell_direct[i][1]);
    }
    /*
    **  如果周边细胞有三个活着,无论如何中间的细胞都是活的
    **  如果周边细胞有三个以上活的,细胞饿死,或者周边活细胞数小于2,细胞就孤独而死
    **  如果周边细胞等于二,该细胞死活状态不变
    */
    if(count == 3) cell_map[x][y] = 1;
    if(count > 3 || count < 2) cell_map[x][y] = 0;
}
/*
**  判断给定细胞周边的细胞时候越界(细胞数组)
*/
int cell_isoffside(int x,int y,int cell_dir_x,int cell_dir_y)
{
    x += cell_dir_x;
    y += cell_dir_y;
    /*
    **  细胞坐标加上方向常量后,越界等同于存在一个死细胞,返回0
    **  否则返回细胞的状态。
    */
    if(x < 0 || x > N-1 || y < 0 || y > M-1) return 0;
    return cell_map[x][y];
}
/*
**  计算下一刻所有细胞的状态
*/
void cell_nexttime()
{
    int i,j;
    for(i = 0;i < M;i++)
    {
        for(j = 0;j < N;j++)
        {
             cell_d_or_al(i,j);
        }
    }
}
/*
**  假设刚开始所有的细胞都是活的
*/
void cell_init()
{
    int i,j;
    for(i = 0;i < M;i++)
    {
        for(j = 0;j < N;j++)
        {
            cell_map[i][j] = 1;
        }
    }
}
main()
{
    int i;
    cell_init();
    cell_print();
    getch();
    for(i = 0;i < 30;i++)
    {
         cell_nexttime();
         cell_print();
         getch();
    }
}
/*
**  在屏幕上显示细胞
*/
void cell_print()
{
    int i,j;
    gotoxy(1,1);
    for(i = 0;i < M;i++)
    {
        for(j = 0;j < N;j++)
        {
            if(cell_map[i][j] == 0) putchar(CELL_DEAD);
            putchar(CELL_ALIVE);
        }
        putchar('\n');
    }
}
搜索更多相关主题的帖子: 生命游戏 感觉 运行 
2010-08-02 14:25
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:20 
我以前也写过一个,你看看:
程序代码:
#include <stdio.h>
#include <string.h>
char b1[20][21] = {

 {"**.................."},

 {"**.................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"........***........."},

 {"........*..........."},

 {".........*.........."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."},

 {"...................."}
};
char b2[20][21];
void display(char b1[][21]){

 int i;

 for(i=0;i<20;i++){
  printf("%s \n", b1[i]);

 }
} 


int neighbours(int p, int q){

 int count=0;

 int temp[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};

 int k;

 for(k=0;k<8;k++)

 {
  int x=p+temp[k][0];
  int y=q+temp[k][1];
  if(!(x<0 || y<0 || x>19 || y>19))
   if (b1[x][y]=='*')
    count++;

 }

 return count;
}


void copyback(char b1[][21], char b2[][21]) {

 int i;

 for (i=0; i<20; i++) {
  strcpy(b1[i], b2[i]);

 }
}
void newboard(char b2[20][21]) {

 int i,j,p=0,q=0;

 for (i=0; i<20; i++) {
  for (j=0; j<20; j++) {
   int d=neighbours(i,j);
   if (d!=2&&d!=3) {
    b2[i][j]='.';
   }
   if (d==3) {
    b2[i][j]='*';
   }
   if (d==2) {
    b2[i][j]=b1[i][j];
   }
  }

 }
} 





int main(){

 char c;

 display(b1);

 printf("would you like to play? (y or n)");

 c=getc(stdin);

 while (c=='y') {
  newboard(b2);
  copyback(b1, b2);
  display(b1);
  printf("would you like to play? (y or n)");
  fflush(stdin);
  c=getc(stdin);

 }
}


欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-08-02 15:35
快速回复:感觉良好,运行糟糕,生命游戏,求助高手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018886 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved