练手:康威生命游戏demo

主要是练习了终端光标移动的用法,虽然都是在用宏 :-)

#include 
#include 
#include 
#include 

//console display settings
#define CSI "\033["
#define CLEAR CSI"2J"
#define MOVE(x,y) CSI#x";"#y"H"
#define MOVEPRINT CSI"%d;%dH"
#define CLEARLINE CSI"K"
#define SAVE CSI"s"
#define RETREIVE CSI"u"
#define HIDE CSI"?25l"
#define SHOW CSI"?25h"
#define UP(a) CSI#a"A"
#define DOWN(a) CSI#a"B"
#define RIGHT(a) CSI#a"C"
#define LEFT(a) CSI#a"D"
#define CLOSE CSI"0m"
#define LIGHT CSI"1m"
#define UNDERSCORE CSI"4m"
#define BLINK CSI"5m"
#define REVERSE CSI"7m"
#define BLANK CSI"8m"
#define FRONTCOLOR(a) CSI"3"#a"m"
#define BACKCOLOR(a) CSI"4"#a"m"
#define COLOR(f,b) FRONTCOLOR(f) BACKCOLOR(b)

#define cycmod(a,b) ((a)>=0?(a)%(b):((a)%(b)?(a)%(b)+(b):(a)%(b)))

int init[][2] = {
  {5,5},
  {5,6},
  {5,7},
  {4,7},
  {3,6}
};

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

template 
class mat{
public:
  T * data;
  int row,col;
  mat(int a,int b){row = a; col = b; data = new T[a*b];};
  ~mat(){delete[]data;};
  T * operator[](int a){return data+a*col;}
};

int main(int argc, char* argv[]){
  int MAP_SIZE = argc>1 ? atoi(argv[1]) : 10;
  mat map(MAP_SIZE,MAP_SIZE);
  mat nmap(MAP_SIZE,MAP_SIZE);
  int i,j,alive;
  memset(map.data,0,sizeof(map));
  printf(CLEAR MOVE(0,0));
  for(i=0;i<5;++i){
	map[init[i][0]][init[i][1]] = 1;
	printf(MOVEPRINT"#",init[i][0],init[i][1]+5);
  }

  for(i=0;i

你可能感兴趣的:(练手)