Main.c 8.9 KB

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