Main.c 8.9 KB

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