tetris.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* $NetBSD: tetris.c,v 1.17 2004/01/27 20:30:30 jsm Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Chris Torek and Darren F. Provine.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)tetris.c 8.1 (Berkeley) 5/31/93
  34. */
  35. #include <sys/cdefs.h>
  36. #ifndef lint
  37. __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
  38. The Regents of the University of California. All rights reserved.\n");
  39. #endif /* not lint */
  40. /*
  41. * Tetris (or however it is spelled).
  42. */
  43. #include <sys/time.h>
  44. #include <err.h>
  45. #include <fcntl.h>
  46. #include <signal.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <unistd.h>
  51. #include "input.h"
  52. #include "scores.h"
  53. #include "screen.h"
  54. #include "tetris.h"
  55. cell board[B_SIZE]; /* 1 => occupied, 0 => empty */
  56. int Rows, Cols; /* current screen size */
  57. const struct shape *curshape;
  58. const struct shape *nextshape;
  59. long fallrate; /* less than 1 million; smaller => faster */
  60. int score; /* the obvious thing */
  61. gid_t gid, egid;
  62. char key_msg[100];
  63. int showpreview;
  64. static void elide(void);
  65. static void setup_board(void);
  66. int main(int, char **);
  67. void onintr(int) __attribute__((__noreturn__));
  68. void usage(void) __attribute__((__noreturn__));
  69. /*
  70. * Set up the initial board. The bottom display row is completely set,
  71. * along with another (hidden) row underneath that. Also, the left and
  72. * right edges are set.
  73. */
  74. static void
  75. setup_board()
  76. {
  77. int i;
  78. cell *p;
  79. p = board;
  80. for (i = B_SIZE; i; i--)
  81. *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
  82. }
  83. /*
  84. * Elide any full active rows.
  85. */
  86. static void
  87. elide()
  88. {
  89. int i, j, base;
  90. cell *p;
  91. for (i = A_FIRST; i < A_LAST; i++) {
  92. base = i * B_COLS + 1;
  93. p = &board[base];
  94. for (j = B_COLS - 2; *p++ != 0;) {
  95. if (--j <= 0) {
  96. /* this row is to be elided */
  97. memset(&board[base], 0, B_COLS - 2);
  98. scr_update();
  99. tsleep();
  100. while (--base != 0)
  101. board[base + B_COLS] = board[base];
  102. scr_update();
  103. tsleep();
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. int
  110. main(argc, argv)
  111. int argc;
  112. char *argv[];
  113. {
  114. int pos, c;
  115. const char *keys;
  116. int level = 2;
  117. char key_write[6][10];
  118. int ch, i, j;
  119. int fd;
  120. gid = getgid();
  121. egid = getegid();
  122. setegid(gid);
  123. fd = open("/dev/null", O_RDONLY);
  124. if (fd < 3)
  125. exit(1);
  126. close(fd);
  127. keys = "jkl pq";
  128. while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
  129. switch(ch) {
  130. case 'k':
  131. if (strlen(keys = optarg) != 6)
  132. usage();
  133. break;
  134. case 'l':
  135. level = atoi(optarg);
  136. if (level < MINLEVEL || level > MAXLEVEL) {
  137. errx(1, "level must be from %d to %d",
  138. MINLEVEL, MAXLEVEL);
  139. }
  140. break;
  141. case 'p':
  142. showpreview = 1;
  143. break;
  144. case 's':
  145. showscores(0);
  146. exit(0);
  147. case '?':
  148. default:
  149. usage();
  150. }
  151. argc -= optind;
  152. argv += optind;
  153. if (argc)
  154. usage();
  155. fallrate = 1000000 / level;
  156. for (i = 0; i <= 5; i++) {
  157. for (j = i+1; j <= 5; j++) {
  158. if (keys[i] == keys[j]) {
  159. errx(1, "duplicate command keys specified.");
  160. }
  161. }
  162. if (keys[i] == ' ')
  163. strcpy(key_write[i], "<space>");
  164. else {
  165. key_write[i][0] = keys[i];
  166. key_write[i][1] = '\0';
  167. }
  168. }
  169. sprintf(key_msg,
  170. "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
  171. key_write[0], key_write[1], key_write[2], key_write[3],
  172. key_write[4], key_write[5]);
  173. (void)signal(SIGINT, onintr);
  174. scr_init();
  175. setup_board();
  176. srandom(getpid());
  177. scr_set();
  178. pos = A_FIRST*B_COLS + (B_COLS/2)-1;
  179. nextshape = randshape();
  180. curshape = randshape();
  181. scr_msg(key_msg, 1);
  182. for (;;) {
  183. place(curshape, pos, 1);
  184. scr_update();
  185. place(curshape, pos, 0);
  186. c = tgetchar();
  187. if (c < 0) {
  188. /*
  189. * Timeout. Move down if possible.
  190. */
  191. if (fits_in(curshape, pos + B_COLS)) {
  192. pos += B_COLS;
  193. continue;
  194. }
  195. /*
  196. * Put up the current shape `permanently',
  197. * bump score, and elide any full rows.
  198. */
  199. place(curshape, pos, 1);
  200. score++;
  201. elide();
  202. /*
  203. * Choose a new shape. If it does not fit,
  204. * the game is over.
  205. */
  206. curshape = nextshape;
  207. nextshape = randshape();
  208. pos = A_FIRST*B_COLS + (B_COLS/2)-1;
  209. if (!fits_in(curshape, pos))
  210. break;
  211. continue;
  212. }
  213. /*
  214. * Handle command keys.
  215. */
  216. if (c == keys[5]) {
  217. /* quit */
  218. break;
  219. }
  220. if (c == keys[4]) {
  221. static char msg[] =
  222. "paused - press RETURN to continue";
  223. place(curshape, pos, 1);
  224. do {
  225. scr_update();
  226. scr_msg(key_msg, 0);
  227. scr_msg(msg, 1);
  228. (void) fflush(stdout);
  229. } while (rwait((struct timeval *)NULL) == -1);
  230. scr_msg(msg, 0);
  231. scr_msg(key_msg, 1);
  232. place(curshape, pos, 0);
  233. continue;
  234. }
  235. if (c == keys[0]) {
  236. /* move left */
  237. if (fits_in(curshape, pos - 1))
  238. pos--;
  239. continue;
  240. }
  241. if (c == keys[1]) {
  242. /* turn */
  243. const struct shape *new = &shapes[curshape->rot];
  244. if (fits_in(new, pos))
  245. curshape = new;
  246. continue;
  247. }
  248. if (c == keys[2]) {
  249. /* move right */
  250. if (fits_in(curshape, pos + 1))
  251. pos++;
  252. continue;
  253. }
  254. if (c == keys[3]) {
  255. /* move to bottom */
  256. while (fits_in(curshape, pos + B_COLS)) {
  257. pos += B_COLS;
  258. score++;
  259. }
  260. continue;
  261. }
  262. if (c == '\f') {
  263. scr_clear();
  264. scr_msg(key_msg, 1);
  265. }
  266. }
  267. scr_clear();
  268. scr_end();
  269. (void)printf("Your score: %d point%s x level %d = %d\n",
  270. score, score == 1 ? "" : "s", level, score * level);
  271. savescore(level);
  272. printf("\nHit RETURN to see high scores, ^C to skip.\n");
  273. while ((i = getchar()) != '\n')
  274. if (i == EOF)
  275. break;
  276. showscores(level);
  277. exit(0);
  278. }
  279. void
  280. onintr(signo)
  281. int signo __attribute__((__unused__));
  282. {
  283. scr_clear();
  284. scr_end();
  285. exit(0);
  286. }
  287. void
  288. usage()
  289. {
  290. (void)fprintf(stderr, "usage: tetris-bsd [-ps] [-k keys] [-l level]\n");
  291. exit(1);
  292. }