資源簡介
LINUX下命令行界面的細胞游戲,純C語言實現,已在ubuntu系統上編譯運行成功,適合初學者學習LINUX C 編程
代碼片段和文件信息
#include?“cell.h“
#include?“drawer.h“
#include?“common.h“
#include?
#include?“./3rdpart/myrandom/include/myrand.h“
/********************************************************
?*??
?*?cellobject
?*
?********************************************************/
cellobject?*initCellobject(postion?pos?aliveRuleFunc?aliveRule)
{
????cellobject?*o?=?(cellobject?*)malloc(sizeof(*o));
????if?(NULL?==?o)?{
????????ERR(“malloc?error\n“);
????????goto?error;
????}
????setCellInitStatus(o);
????setCellobjectPostion(o?pos);
????o->aliveRule?=?aliveRule;
????
????return?o;
error:
????return?NULL;
}
void?uninitCellobject(cellobject?**po)
{
????free(*po);
????*po?=?NULL;
}
void?setCellInitStatus(cellobject?*o)
{
????unsigned?char?r?=?0;
????getRandom(&r?sizeof(r));
????if?(r?>?220)?{
????????o->status?=?ALIVE;
????}?else?{
????????o->status?=?DEAD;
????}
}
void?setCellobjectPostion(cellobject?*o?postion?pos)
{
????o->pos.x?=?pos.x;
????o->pos.y?=?pos.y;
}
eCellStatus?beCellobjectAlive(cellobject?*o)
{
????return?o->status;
}
void?setCellobjectAlive(cellobject?*o)
{
????o->status?=?ALIVE;
}
void?setCellobjectDead(cellobject?*o)
{
????o->status?=?DEAD;
}
/********************************************************
?*??
?*?cellWorld
?*
?********************************************************/
cellWorld?*initCellWorld(aliveRuleFunc?aliveRule)
{
????int?x?=?0?y?=?0;
????postion?pos;
????cellWorld?*w?=?(cellWorld?*)malloc(sizeof(*w));
????if?(NULL?==?w)?{
????????ERR(“malloc?error\n“);
????????goto?error;
????}
????w->running?=?STOP;
????for?(x?=?0;?x?(int)CELL_WORLD_SIZE;?x?++)?{
????????for?(y?=?0;?y?(int)CELL_WORLD_SIZE;?y?++)?{
????????????if?((x?>=?0?&&?x?(int)MARGIN_SIZE)??
????????????????||?(y?>=?0?&&?y?(int)MARGIN_SIZE)?
????????????????||?(x?>?(int)(CELL_WORLD_SIZE?-?MARGIN_SIZE?-?1)?
????????????????????&&?x?(int)CELL_WORLD_SIZE)?
????????????????||?(y?>?(int)(CELL_WORLD_SIZE?-?MARGIN_SIZE?-?1)?
????????????????????&&?y?(int)CELL_WORLD_SIZE))?{
????????????????w->cell[x][y]?=?NULL;
????????????????//PRT(“NULL?cell?[%d?%d]\n“?x?y);
????????????}?else?{
????????????????pos.x?=?x;
????????????????pos.y?=?y;
????????????????w->cell[x][y]?=?initCellobject(pos?aliveRule);
????????????????//PRT(“cell?object?[%d?%d]?%s\n“?x?y?w->cell[x][y]->status???“ALIVE“?:?“DEAD“);
????????????????if?(NULL?==?w->cell[x][y])?{
????????????????????ERR(“malloc?error\n“);
????????????????????uninitCellWorld(&w);
????????????????????goto?error;
????????????????}
????????????}
????????}
????}
????PRT(“initCellWorld?OK\n“);
????return?w;
error:
????return?NULL;
}
void?uninitCellWorld(cellWorld?**pw)
{
????int?x?=?0?y?=?0;
????for?(x?=?0;?x?(int)CELL_WORLD_SIZE;?x?++)?{
????????for?(y?=?0;?y?(int)CELL_WORLD_SIZE;?y?++)?{
????????????if?(NULL?!=?(*pw)->cell[x][y])?{
????????????????uninitCellobject(&((*pw)->cell[x][y]));
????????????}
????????}
????}
????free(*pw);
????*pw?=?NULL;
????return;
}
void?runThread(cellWorld?*w?threadobject?*o)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????665??2014-02-21?06:57??simple-cell\3rdpart\myrandom\include\myrand.h
?????文件?????103296??2014-02-21?06:57??simple-cell\3rdpart\myrandom\lib\libmyrandom.so
?????文件???????4636??2014-02-21?06:57??simple-cell\cell.c
?????文件???????1780??2014-02-21?06:57??simple-cell\cell.h
?????文件???????1504??2014-03-08?13:27??simple-cell\common.h
?????文件????????673??2014-02-21?06:57??simple-cell\drawer.c
?????文件???????1163??2014-02-21?06:57??simple-cell\drawer.h
?????文件?????196319??2014-02-21?06:57??simple-cell\main
?????文件???????2098??2014-02-21?06:57??simple-cell\main.c
?????文件????????322??2014-03-08?13:27??simple-cell\Makefile
?????目錄??????????0??2014-03-08?13:25??simple-cell\3rdpart\myrandom\include
?????目錄??????????0??2014-03-08?13:25??simple-cell\3rdpart\myrandom\lib
?????目錄??????????0??2014-03-08?13:25??simple-cell\3rdpart\myrandom
?????目錄??????????0??2014-03-08?13:25??simple-cell\3rdpart
?????目錄??????????0??2014-03-08?13:27??simple-cell
-----------?---------??----------?-----??----
???????????????312456????????????????????15
評論
共有 條評論