summaryrefslogtreecommitdiff
path: root/games
diff options
context:
space:
mode:
authorPaul Janzen <pjanzen@cvs.openbsd.org>1999-03-22 07:38:31 +0000
committerPaul Janzen <pjanzen@cvs.openbsd.org>1999-03-22 07:38:31 +0000
commit655f46886f41552a538bb4b82804425dff27d156 (patch)
tree2dba4c01ca96282830ec3ec47fd4f316526a3014 /games
parenta7b23dec87ab158c2011a2fa6cb22790cca1fb30 (diff)
Many fixes, most either from or based on NetBSD changes:
get rid of unused function improve output format allow previewing of next piece randomize initial rotation angle of falling blocks.
Diffstat (limited to 'games')
-rw-r--r--games/tetris/input.c16
-rw-r--r--games/tetris/input.h3
-rw-r--r--games/tetris/screen.c49
-rw-r--r--games/tetris/tetris.69
-rw-r--r--games/tetris/tetris.c45
-rw-r--r--games/tetris/tetris.h11
6 files changed, 99 insertions, 34 deletions
diff --git a/games/tetris/input.c b/games/tetris/input.c
index 63f15ec92aa..3ce2f21c5cd 100644
--- a/games/tetris/input.c
+++ b/games/tetris/input.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: input.c,v 1.3 1998/09/24 06:45:07 pjanzen Exp $ */
+/* $OpenBSD: input.c,v 1.4 1999/03/22 07:38:24 pjanzen Exp $ */
/* $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $ */
/*-
@@ -138,20 +138,6 @@ tsleep()
}
/*
- * Eat up any input (used at end of game).
- */
-void
-eat_input()
-{
- struct timeval tv;
- char c;
-
- do {
- tv.tv_sec = tv.tv_usec = 0;
- } while (rwait(&tv) && read(0, &c, 1) == 1);
-}
-
-/*
* getchar with timeout.
*/
int
diff --git a/games/tetris/input.h b/games/tetris/input.h
index 25836409d44..fddfe1e9b9d 100644
--- a/games/tetris/input.h
+++ b/games/tetris/input.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: input.h,v 1.2 1998/09/24 06:45:07 pjanzen Exp $ */
+/* $OpenBSD: input.h,v 1.3 1999/03/22 07:38:26 pjanzen Exp $ */
/* $NetBSD: input.h,v 1.2 1995/04/22 07:42:36 cgd Exp $ */
/*-
@@ -39,7 +39,6 @@
* @(#)input.h 8.1 (Berkeley) 5/31/93
*/
-void eat_input __P((void));
int rwait __P((struct timeval *));
int tgetchar __P((void));
void tsleep __P((void));
diff --git a/games/tetris/screen.c b/games/tetris/screen.c
index 43716816bb0..c336ea94bf1 100644
--- a/games/tetris/screen.c
+++ b/games/tetris/screen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: screen.c,v 1.3 1999/01/08 04:11:50 pjanzen Exp $ */
+/* $OpenBSD: screen.c,v 1.4 1999/03/22 07:38:27 pjanzen Exp $ */
/* $NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft Exp $ */
/*-
@@ -279,10 +279,12 @@ scr_set()
if (Cols == 0)
Cols = COnum;
if (Rows < MINROWS || Cols < MINCOLS) {
- (void) fprintf(stderr,
- "the screen is too small: must be at least %d x %d",
+ char smallscr[55];
+
+ (void) snprintf(smallscr, 55,
+ "the screen is too small (must be at least %dx%d)",
MINROWS, MINCOLS);
- stop(""); /* stop() supplies \n */
+ stop(smallscr);
}
if (tcgetattr(0, &oldtt) < 0)
stop("tcgetattr() fails");
@@ -376,6 +378,7 @@ scr_update()
register regcell so, cur_so = 0;
register int i, ccol, j;
sigset_t sigset, osigset;
+ static struct shape *lastshape;
sigemptyset(&sigset);
sigaddset(&sigset, SIGTSTP);
@@ -389,10 +392,46 @@ scr_update()
putpad(HOstr);
else
moveto(0, 0);
- (void) printf("%d", score);
+ (void) printf("Score: %d", score);
curscore = score;
}
+ /* draw preview of next pattern */
+ if (showpreview && (nextshape != lastshape)) {
+ int i;
+ static int r=5, c=2;
+ int tr, tc, t;
+
+ lastshape = nextshape;
+
+ /* clean */
+ putpad(SEstr);
+ moveto(r-1, c-1); putstr(" ");
+ moveto(r, c-1); putstr(" ");
+ moveto(r+1, c-1); putstr(" ");
+ moveto(r+2, c-1); putstr(" ");
+
+ moveto(r-3, c-2);
+ putstr("Next shape:");
+
+ /* draw */
+ if (SOstr)
+ putpad(SOstr);
+ moveto(r, 2 * c);
+ putstr(SOstr ? " " : "XX");
+ for (i = 0; i < 3; i++) {
+ t = c + r * B_COLS;
+ t += nextshape->off[i];
+
+ tr = t / B_COLS;
+ tc = t % B_COLS;
+
+ moveto(tr, 2*tc);
+ putstr(SOstr ? " " : "XX");
+ }
+ putpad(SEstr);
+ }
+
bp = &board[D_FIRST * B_COLS];
sp = &curscreen[D_FIRST * B_COLS];
for (j = D_FIRST; j < D_LAST; j++) {
diff --git a/games/tetris/tetris.6 b/games/tetris/tetris.6
index 9cad42baa28..5aecddeba97 100644
--- a/games/tetris/tetris.6
+++ b/games/tetris/tetris.6
@@ -1,4 +1,4 @@
-.\" $OpenBSD: tetris.6,v 1.5 1998/12/15 19:18:55 pjanzen Exp $
+.\" $OpenBSD: tetris.6,v 1.6 1999/03/22 07:38:28 pjanzen Exp $
.\"
.\" Copyright (c) 1992, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -44,7 +44,7 @@
.Nd the game of tetris
.Sh SYNOPSIS
.Nm tetris
-.Op Fl s
+.Op Fl ps
.Op Fl k Ar keys
.Op Fl l Ar level
.Sh DESCRIPTION
@@ -95,6 +95,9 @@ The current key settings are displayed at the bottom of the screen
during play.
.It Fl l Ar level
Select a level of play.
+.It Fl p
+Switch on previewing of the shape that will appear next. This penalizes
+your score.
.It Fl s
Display the top scores.
.El
@@ -155,3 +158,5 @@ Chris Torek and Darren F. Provine.
.Pp
Manual adapted from the original entry written by Nancy L. Tinkham and
Darren F. Provine.
+.Pp
+Shape previewing code adapted from code by Hubert Feyrer.
diff --git a/games/tetris/tetris.c b/games/tetris/tetris.c
index 48ee14fb6ca..46308ddf42e 100644
--- a/games/tetris/tetris.c
+++ b/games/tetris/tetris.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tetris.c,v 1.6 1999/03/22 05:20:03 pjanzen Exp $ */
+/* $OpenBSD: tetris.c,v 1.7 1999/03/22 07:38:28 pjanzen Exp $ */
/* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */
/*-
@@ -66,6 +66,7 @@ static char copyright[] =
static void elide __P((void));
static void setup_board __P((void));
+struct shape *randshape __P((void));
void onintr __P((int));
void usage __P((void));
@@ -113,13 +114,26 @@ elide()
}
}
+struct shape *
+randshape()
+{
+ struct shape *tmp;
+ int i, j;
+
+ tmp = &shapes[random() % 7];
+ j = random() % 4;
+ for (i = 0; i < j; i++)
+ tmp = &shapes[tmp->rot];
+ return (tmp);
+}
+
+
int
main(argc, argv)
int argc;
char *argv[];
{
register int pos, c;
- register struct shape *curshape;
register char *keys;
register int level = 2;
char key_write[6][10];
@@ -131,7 +145,8 @@ main(argc, argv)
egid = getegid();
setegid(gid);
- while ((ch = getopt(argc, argv, "hk:l:s")) != -1)
+ showpreview = 0;
+ while ((ch = getopt(argc, argv, "hk:l:ps")) != -1)
switch(ch) {
case 'k':
if (strlen(keys = optarg) != 6)
@@ -143,6 +158,9 @@ main(argc, argv)
errx(1, "level must be from %d to %d",
MINLEVEL, MAXLEVEL);
break;
+ case 'p':
+ showpreview = 1;
+ break;
case 's':
showscores(0);
exit(0);
@@ -186,6 +204,7 @@ main(argc, argv)
scr_set();
pos = A_FIRST*B_COLS + (B_COLS/2)-1;
+ nextshape = randshape();
curshape = randshape();
scr_msg(key_msg, 1);
@@ -216,7 +235,8 @@ main(argc, argv)
* Choose a new shape. If it does not fit,
* the game is over.
*/
- curshape = randshape();
+ curshape = nextshape;
+ nextshape = randshape();
pos = A_FIRST*B_COLS + (B_COLS/2)-1;
if (!fits_in(curshape, pos))
break;
@@ -274,15 +294,24 @@ main(argc, argv)
}
continue;
}
- if (c == '\f')
+ if (c == '\f') {
scr_clear();
+ scr_msg(key_msg, 1);
+ }
}
scr_clear();
scr_end();
- (void)printf("Your score: %d point%s x level %d = %d\n",
- score, score == 1 ? "" : "s", level, score * level);
+ if (showpreview == 0)
+ (void)printf("Your score: %d point%s x level %d = %d\n",
+ score, score == 1 ? "" : "s", level, score * level);
+ else {
+ (void)printf("Your score: %d point%s x level %d x preview penalty %0.3f = %d\n",
+ score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
+ (int)(score * level * PRE_PENALTY));
+ score = score * PRE_PENALTY;
+ }
savescore(level);
printf("\nHit RETURN to see high scores, ^C to skip.\n");
@@ -308,6 +337,6 @@ onintr(signo)
void
usage()
{
- (void)fprintf(stderr, "usage: tetris [-s] [-k keys] [-l level]\n");
+ (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
exit(1);
}
diff --git a/games/tetris/tetris.h b/games/tetris/tetris.h
index 15523f0c67f..c0210123bb0 100644
--- a/games/tetris/tetris.h
+++ b/games/tetris/tetris.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tetris.h,v 1.3 1998/09/24 06:45:08 pjanzen Exp $ */
+/* $OpenBSD: tetris.h,v 1.4 1999/03/22 07:38:30 pjanzen Exp $ */
/* $NetBSD: tetris.h,v 1.2 1995/04/22 07:42:48 cgd Exp $ */
/*-
@@ -131,7 +131,9 @@ struct shape {
};
extern struct shape shapes[];
-#define randshape() (&shapes[random() % 7])
+
+struct shape *curshape;
+struct shape *nextshape;
/*
* Shapes fall at a rate faster than once per second.
@@ -162,11 +164,16 @@ long fallrate; /* less than 1 million; smaller => faster */
* and we score a point for each row it falls (plus one more as soon as
* we find that it is at rest and integrate it---until then, it can
* still be moved or rotated).
+ *
+ * If previewing has been turned on, the score is multiplied by PRE_PENALTY.
*/
+#define PRE_PENALTY 0.75
+
int score; /* the obvious thing */
gid_t gid, egid;
char key_msg[100];
+int showpreview;
int fits_in __P((struct shape *, int));
void place __P((struct shape *, int, int));