-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (81 loc) · 2.01 KB
/
main.cpp
File metadata and controls
86 lines (81 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<vector>
#include<stdio.h>
#include<ctime>
#include<sys/ioctl.h>
#include<map>
#include<deque>
using namespace std;
typedef struct{
char c;
int live;
}Code;
void render(deque<map<int, Code> > &matrix);
int msleep(unsigned long milisec)
{
struct timespec req={0};
time_t sec=(int)(milisec/1000);
milisec=milisec-(sec*1000);
req.tv_sec=sec;
req.tv_nsec=milisec*1000000L;
while(nanosleep(&req,NULL)==-1)
continue;
return 1;
}
int main(void){
static const char alpha[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphaSize = sizeof(alpha)/sizeof(char);
cout<<alphaSize;
// get terminal size
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
int col_size = w.ws_col;
int row_size = w.ws_row;
//cout<< col_size<< " "<<row_size<<endl;
deque<map<int, Code> > matrix;
matrix.push_front(map<int,Code>());
for(;;){
map<int,Code> new_map = matrix[0];
int new_col = rand()%col_size+1;
int new_live = rand()%row_size+1;
Code empty_code = {'0',new_live};
new_map[new_col] = empty_code;
for(map<int,Code>::iterator it = new_map.begin();it!=new_map.end();){
int new_c = alpha[rand()%alphaSize];
it->second.c = new_c;
it->second.live--;
// clear 0 live code
if(it->second.live==0){
new_map.erase(it);
}else{
it++;
}
}
matrix.push_front(new_map);
// clear row that out of terminal
if(matrix.size()>row_size){
matrix.erase(matrix.begin()+row_size,matrix.end());
}
render(matrix);
}
msleep(1000L);
return 0;
}
void render(deque<map<int, Code> > &matrix){
// clear screen
printf("\033[2J\033[1;1H");
// render
for(int i = 0;i<matrix.size();i++){
for(map<int,Code>::iterator it = matrix[i].begin();it!=matrix[i].end();it++){
int print_col = it->first;
Code print_code = it->second;
printf("\033[%d;%dH",i,print_col);//, 0, print_col);
printf("%c",print_code.c);
}
}
fflush(stdout);
msleep(100);
}