Main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #define _XOPEN_SOURCE_EXTENDED
  2. #include <locale.h>
  3. #include <ncurses.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8. typedef enum { MOVING, STILL, EMPTY, WALL } FieldState;
  9. static FieldState** fields;
  10. static int width;
  11. static int height;
  12. static int objectX;
  13. static int objectY;
  14. static int movingObject[8];
  15. static void (*rotateFunction)(void);
  16. static uint32_t seed = 0;
  17. static void initRandom(void) {
  18. struct timespec time;
  19. timespec_get(&time, TIME_UTC);
  20. seed = (uint32_t)time.tv_nsec;
  21. }
  22. static int nextRandom(int max) {
  23. seed = seed * 27697u + 13711u;
  24. int32_t random = ((int32_t)(seed >> 1)) % max;
  25. return random;
  26. }
  27. static void initGamefield(int w, int h) {
  28. width = w;
  29. height = h;
  30. fields = (FieldState**)malloc(sizeof(FieldState*) * (size_t)height);
  31. for(int y = 0; y < height; y++) {
  32. fields[y] = (FieldState*)malloc(sizeof(FieldState) * (size_t)width);
  33. for(int x = 0; x < width; x++) {
  34. fields[y][x] = EMPTY;
  35. }
  36. }
  37. }
  38. static void printGamefield(void) {
  39. static const char* MAP[] = {"\u2592", "\u2593", " "};
  40. static const char* wall = "\u2588";
  41. for(int x = 0; x < width * 2 + 4; x++) {
  42. mvaddstr(0, x, wall);
  43. mvaddstr(height + 1, x, wall);
  44. }
  45. for(int y = 0; y < height; y++) {
  46. mvaddstr(y + 1, 0, wall);
  47. addstr(wall);
  48. for(int x = 0; x < width; x++) {
  49. addstr(MAP[fields[y][x]]);
  50. addstr(MAP[fields[y][x]]);
  51. }
  52. addstr(wall);
  53. addstr(wall);
  54. }
  55. }
  56. static void deleteGamefield(void) {
  57. for(int i = 0; i < height; i++) {
  58. free(fields[i]);
  59. }
  60. free(fields);
  61. }
  62. static void setField(int x, int y, FieldState state) {
  63. if(x >= 0 && y >= 0 && x < width && y < height) {
  64. fields[y][x] = state;
  65. }
  66. }
  67. static FieldState getField(int x, int y) {
  68. if(x >= 0 && y >= 0 && x < width && y < height) {
  69. return fields[y][x];
  70. }
  71. return WALL;
  72. }
  73. static int canMove(int x, int y) {
  74. FieldState t = getField(x, y);
  75. return t == EMPTY || t == MOVING;
  76. }
  77. static void rotate33(void) {
  78. int data[8];
  79. for(int i = 0; i < 8; i += 2) {
  80. data[i] = movingObject[i + 1] - objectY + objectX;
  81. data[i + 1] = 2 - (movingObject[i] - objectX) + objectY;
  82. if(!canMove(data[i], data[i + 1])) {
  83. return;
  84. }
  85. }
  86. for(int i = 0; i < 8; i += 2) {
  87. setField(movingObject[i], movingObject[i + 1], EMPTY);
  88. movingObject[i] = data[i];
  89. movingObject[i + 1] = data[i + 1];
  90. }
  91. for(int i = 0; i < 8; i += 2) {
  92. setField(movingObject[i], movingObject[i + 1], MOVING);
  93. }
  94. }
  95. static void rotate41(void) {
  96. int data[8];
  97. for(int i = 0; i < 8; i += 2) {
  98. data[i] = movingObject[i + 1] - objectY + objectX;
  99. data[i + 1] = movingObject[i] - objectX + objectY;
  100. if(!canMove(data[i], data[i + 1])) {
  101. return;
  102. }
  103. }
  104. for(int i = 0; i < 8; i += 2) {
  105. setField(movingObject[i], movingObject[i + 1], EMPTY);
  106. movingObject[i] = data[i];
  107. movingObject[i + 1] = data[i + 1];
  108. }
  109. for(int i = 0; i < 8; i += 2) {
  110. setField(movingObject[i], movingObject[i + 1], MOVING);
  111. }
  112. }
  113. static void rotate22(void) {
  114. // nothing to do
  115. }
  116. static void rotate(void) {
  117. rotateFunction();
  118. }
  119. static void spawnObject(int x, int y) {
  120. objectX = x;
  121. objectY = y;
  122. switch(nextRandom(7)) {
  123. // .X.
  124. // XXX
  125. // ...
  126. case 0:
  127. movingObject[0] = x + 1;
  128. movingObject[1] = y + 0;
  129. movingObject[2] = x + 0;
  130. movingObject[3] = y + 1;
  131. movingObject[4] = x + 1;
  132. movingObject[5] = y + 1;
  133. movingObject[6] = x + 2;
  134. movingObject[7] = y + 1;
  135. rotateFunction = rotate33;
  136. break;
  137. // .X.
  138. // .X.
  139. // .XX
  140. case 1:
  141. movingObject[0] = x + 1;
  142. movingObject[1] = y + 0;
  143. movingObject[2] = x + 1;
  144. movingObject[3] = y + 1;
  145. movingObject[4] = x + 1;
  146. movingObject[5] = y + 2;
  147. movingObject[6] = x + 2;
  148. movingObject[7] = y + 2;
  149. rotateFunction = rotate33;
  150. break;
  151. case 2:
  152. // .X.
  153. // .X.
  154. // XX.
  155. movingObject[0] = x + 1;
  156. movingObject[1] = y + 0;
  157. movingObject[2] = x + 1;
  158. movingObject[3] = y + 1;
  159. movingObject[4] = x + 1;
  160. movingObject[5] = y + 2;
  161. movingObject[6] = x + 0;
  162. movingObject[7] = y + 2;
  163. rotateFunction = rotate33;
  164. break;
  165. // .X..
  166. // .X..
  167. // .X..
  168. // .X..
  169. case 3:
  170. movingObject[0] = x + 0;
  171. movingObject[1] = y + 0;
  172. movingObject[2] = x + 0;
  173. movingObject[3] = y + 1;
  174. movingObject[4] = x + 0;
  175. movingObject[5] = y + 2;
  176. movingObject[6] = x + 0;
  177. movingObject[7] = y + 3;
  178. rotateFunction = rotate41;
  179. break;
  180. // X..
  181. // XX.
  182. // .X.
  183. case 4:
  184. movingObject[0] = x + 0;
  185. movingObject[1] = y + 0;
  186. movingObject[2] = x + 0;
  187. movingObject[3] = y + 1;
  188. movingObject[4] = x + 1;
  189. movingObject[5] = y + 1;
  190. movingObject[6] = x + 1;
  191. movingObject[7] = y + 2;
  192. rotateFunction = rotate33;
  193. break;
  194. // .X.
  195. // XX.
  196. // X..
  197. case 5:
  198. movingObject[0] = x + 1;
  199. movingObject[1] = y + 0;
  200. movingObject[2] = x + 0;
  201. movingObject[3] = y + 1;
  202. movingObject[4] = x + 1;
  203. movingObject[5] = y + 1;
  204. movingObject[6] = x + 0;
  205. movingObject[7] = y + 2;
  206. rotateFunction = rotate33;
  207. break;
  208. // XX
  209. // XX
  210. case 6:
  211. movingObject[0] = x + 0;
  212. movingObject[1] = y + 0;
  213. movingObject[2] = x + 1;
  214. movingObject[3] = y + 0;
  215. movingObject[4] = x + 0;
  216. movingObject[5] = y + 1;
  217. movingObject[6] = x + 1;
  218. movingObject[7] = y + 1;
  219. rotateFunction = rotate22;
  220. break;
  221. }
  222. for(int i = 0; i < 8; i += 2) {
  223. setField(movingObject[i], movingObject[i + 1], MOVING);
  224. }
  225. }
  226. static int moveObject(int x, int y) {
  227. for(int i = 0; i < 8; i += 2) {
  228. if(!canMove(movingObject[i] + x, movingObject[i + 1] + y)) {
  229. return 0;
  230. }
  231. }
  232. objectX += x;
  233. objectY += y;
  234. for(int i = 0; i < 8; i += 2) {
  235. setField(movingObject[i], movingObject[i + 1], EMPTY);
  236. movingObject[i] += x;
  237. movingObject[i + 1] += y;
  238. }
  239. for(int i = 0; i < 8; i += 2) {
  240. setField(movingObject[i], movingObject[i + 1], MOVING);
  241. }
  242. return 1;
  243. }
  244. static void removeRows(void) {
  245. for(int y = 0; y < height; y++) {
  246. int remove = 1;
  247. for(int x = 0; x < width; x++) {
  248. remove = remove && getField(x, y) == STILL;
  249. }
  250. if(remove) {
  251. for(int ry = y; ry > 0; ry--) {
  252. for(int rx = 0; rx < width; rx++) {
  253. setField(rx, ry, getField(rx, ry - 1));
  254. }
  255. }
  256. }
  257. }
  258. }
  259. static void onObjectFall(void) {
  260. if(!moveObject(0, 1)) {
  261. for(int i = 0; i < 8; i += 2) {
  262. setField(movingObject[i], movingObject[i + 1], STILL);
  263. }
  264. removeRows();
  265. spawnObject(0, 0);
  266. }
  267. }
  268. int main(void) {
  269. initRandom();
  270. setlocale(LC_ALL, "en_US.UTF-8");
  271. initscr();
  272. cbreak();
  273. keypad(stdscr, TRUE);
  274. noecho();
  275. nodelay(stdscr, TRUE);
  276. curs_set(0);
  277. initGamefield(10, 18);
  278. spawnObject(0, 0);
  279. long lag = 0;
  280. clock_t lastTime = clock();
  281. long ticks = 1000000;
  282. int running = 1;
  283. while(running) {
  284. int key = getch();
  285. if(key == KEY_LEFT) {
  286. moveObject(-1, 0);
  287. } else if(key == KEY_RIGHT) {
  288. moveObject(1, 0);
  289. } else if(key == KEY_DOWN) {
  290. onObjectFall();
  291. } else if(key == KEY_UP) {
  292. rotate();
  293. } else if(key == 'q') {
  294. running = 0;
  295. }
  296. clock_t time = clock();
  297. lag += (time - lastTime);
  298. lastTime = time;
  299. if(lag > ticks) {
  300. lag -= ticks;
  301. onObjectFall();
  302. }
  303. printGamefield();
  304. printw("%ld", lag);
  305. refresh();
  306. // keep the game from taking a bit too much cpu
  307. usleep(1000);
  308. lag += 1000;
  309. }
  310. endwin();
  311. deleteGamefield();
  312. return 0;
  313. }