initial
[fpgammix.git] / workloads / Tetris / tetris.c
blob9e1dd88c5ea96db78625e6ce017db3cef4c618f6
1 #include <stdio.h>
3 //extern void setitimer(int, long *, int), system(char *), sigvec(), srand(int), exit(int);
4 //extern int atoi(char *), rand(void), getpid(void);
6 #ifndef __mmix__
7 long h[4];
9 int t(void)
11 h[3] -= h[3] / 3000;
13 setitimer(0, h, 0);
15 #endif
17 int key, d, level;
19 int v[] = {(int)t, 0, 2};
21 int w, s, I, K = 0, i = 276, j, k, q[276], Q[276], *n = q, *m, x = 17;
23 int shape[] = {
24 7, -13, -12, 1, // 0 0 -> 7 -> 0
25 8, -11, -12, -1, // 1 1 -> 8 -> 1
26 9, -1, 1, 12, // 2 2 -> 9 -> 10 -> 11 -> 2
27 3, -13, -12, -1, // 3 Square block!
28 12, -1, 11, 1, // 4 4 -> 12 -> 13 -> 14 -> 4
29 15, -1, 13, 1, // 5 5 -> 15 -> 16 -> 17 -> 5
30 18, -1, 1, 2, // 6 6 -> 18 -> 6
31 0, -12, -1, 11, // 7
32 1, -12, 1, 13, // 8
33 10, -12, 1, 12, // 9
34 11, -12, -1, 1, // 10
35 2, -12, -1, 12, // 11
36 13, -12, 12, 13, // 12
37 14, -11, -1, 1, // 13
38 4, -13, -12, 12, // 14
39 16, -11,-12, 12, // 15
40 17, -13, 1, -1, // 16
41 5, -12, 12, 11, // 17
42 6, -12, 12, 24 // 18
45 // Lazy screen refresh
46 int update(void)
48 for (i = 11; ++i < 264;)
49 if ((k = q[i]) - Q[i]) {
50 Q[i] = k;
51 if (i - ++I || i % 12 < 1) {
52 I = i;
53 #ifdef __mmix__
54 gotoxy(i % 12 * 2 + 28, i / 12);
55 #else
56 printf("\033[%d;%dH",
57 i / 12,
58 i % 12 * 2 + 28);
59 #endif
61 #ifdef __mmix__
62 mmix_putchar("# "[!k]);
63 #else
64 printf("\033[%dm " + (K - k ? 0 : 5), k);
65 K = k;
66 #endif
68 Q[263] = key = getchar();
71 // Test position
72 int test_brick(long newpos)
74 long i;
76 for (i = 4; --i;)
77 if (q[newpos + n[i]])
78 return 0;
80 if (q[newpos])
81 return 0;
83 return 1;
86 // Move
87 int draw_brick(long color)
89 long i;
90 for (i = 4; --i;)
91 q[x + n[i]] = color;
92 q[x] = color;
95 int main(long argc, char **argv)
97 char *a;
99 h[3] = 1000000 / (level = argc > 1 ? atoi(argv[1]) : 2);
101 a = argc > 2 ? argv[2] : "jkl pq";
103 /* Draw the board */
104 for (i = 276; i; --i)
105 *n++ = i < 25 || i % 12 < 2 ? 7 : 0;
107 #ifndef __mmix__
108 srand(getpid());
109 system("stty cbreak -echo stop u");
110 sigvec(14, v, 0);
111 #endif
113 t();
115 #ifdef __mmix__
116 stdout = 1;
117 fb_clear();
118 #else
119 puts("\033[H\033[J");
120 #endif
122 for (n = shape + rand() % 7 * 4;; draw_brick(7), update(), draw_brick(0)) {
123 if (key < 0) {
124 if (test_brick(x + 12))
125 x += 12;
126 else {
127 draw_brick(7);
128 ++w;
129 for (j = 0; j < 252; j = 12 * (j / 12 + 1))
130 for (; q[++j];)
131 if (j % 12 == 10) {
132 for (; j % 12; q[j--] = 0)
134 update();
135 for (; --j; q[j + 12] = q[j])
137 update();
139 n = shape + rand() % 7 * 4;
140 test_brick(x = 17) || (key = a[5]);
144 // Move left
145 if (key == a[0])
146 test_brick(--x) || ++x;
148 // Rotate
149 if (key == a[1])
150 n = shape + 4 * *(m = n), test_brick(x) || (n = m);
152 // Move Right
153 if (key == a[2])
154 test_brick(++x) || --x;
156 // Drop
157 if (key == a[3])
158 for (; test_brick(x + 12); ++w)
159 x += 12;
161 if (key == a[4] || key == a[5]) {
162 #ifndef __mmix__
163 s = sigblock(8192);
164 printf("\033[H\033[J\033[0m%d\n", w);
165 if (key == a[5])
166 break;
167 for (j = 264; j--; Q[j] = 0);
168 while (getchar() - a[4])
170 puts("\033[H\033[J\033[7m");
171 sigsetmask(s);
172 #else
173 exit(0);
174 #endif
177 #ifndef __mmix__
178 d = popen("stty -cbreak echo stop \023;cat - HI|sort -rn|head -20>/tmp/$$;mv /tmp/$$ HI;cat HI", "w");
179 fprintf(d, "%4d on level %1d by %s\n", w, level, getlogin());
180 pclose(d);
181 #endif