Index: tetris/scores.c
===================================================================
--- tetris/scores.c	(revision 0aa024b1d503a5c3520dc1cb1aadeee8029ed501)
+++ tetris/scores.c	(revision 9996ed56130d52f2e19a6f9a37c19b831440ca91)
@@ -47,7 +47,7 @@
 /* #include <fcntl.h> */
 /* #include <pwd.h> */
-/* #include <stdio.h> */
+ #include <stdio.h>
 /* #include <stdlib.h> */
-/* #include <string.h> */
+#include <string.h>
 /* #include <time.h> */
 /* #include <term.h> */
@@ -77,4 +77,5 @@
 /* static int gotscores; */
 /* static struct highscore scores[NUMSPOTS]; */
+static struct highscore scores[NUMSPOTS];
 
 /* static int checkscores(struct highscore *, int); */
@@ -83,4 +84,87 @@
 /* static void printem(int, int, struct highscore *, int, const char *); */
 /* static char *thisuser(void); */
+
+void showscores(int firstgame)
+{
+	int i;
+	
+	clear_screen();
+	moveto(10, 0);
+	printf("\tRank \tLevel \tName\t                     points\n");
+	printf("\t========================================================\n");
+	for (i = 0; i < NUMSPOTS - 1; i++) {
+		printf("\t%6d %6d %-16s %20d\n", i+1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
+	}
+	if (!firstgame) {
+		printf("\t========================================================\n");
+		printf("\t  Last %6d %-16s %20d\n", scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
+	}
+	printf("\n\n\n\n\tPress any key to return to main menu.");
+	getchar();
+}
+
+/** Copy from hiscore table score with index src to dest
+ *
+ */
+static void copyhiscore(int dest, int src)
+{
+	strcpy(scores[dest].hs_name, scores[src].hs_name);
+	scores[dest].hs_score = scores[src].hs_score; 	
+	scores[dest].hs_level = scores[src].hs_level; 
+}
+
+void insertscore(int score, int level)
+{
+	int i,j;
+	int key;
+
+	
+	clear_screen();
+	moveto(10 , 10);
+	puts("Insert your name: ");
+	strncpy(scores[NUMSPOTS - 1].hs_name, "Player", MAXLOGNAME);
+	i = 6;
+
+	moveto(10 , 28);
+	printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
+	key = getchar();	
+	while(key != '\n') {
+		if (key == '\b') {
+			if (i > 0) 
+				scores[NUMSPOTS - 1].hs_name[--i] = '\0';	
+		} else {
+			if (i < (MAXLOGNAME - 1))
+				scores[NUMSPOTS - 1].hs_name[i++] = key;	
+				scores[NUMSPOTS - 1].hs_name[i] = '\0';	
+		}
+		
+		moveto(10 , 28);
+		printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
+	
+		key = getchar();	
+	}
+	
+	scores[NUMSPOTS - 1].hs_score = score;	
+	scores[NUMSPOTS - 1].hs_level = level;
+	
+	i = NUMSPOTS-1;	
+	while ((i > 0) && (scores[i - 1].hs_score < score))
+		i--;
+
+	for (j = NUMSPOTS - 2; j > i; j--) {
+		copyhiscore(j,j-1);
+	}
+	copyhiscore(i, NUMSPOTS - 1);	
+}
+
+void initscores(void)
+{
+	int i;
+	for(i = 0; i < NUMSPOTS; i++) {
+		strncpy(scores[i].hs_name, "HelenOS Team", MAXLOGNAME);
+		scores[i].hs_score = (NUMSPOTS - i) * 200;	
+		scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1);
+	}
+}
 
 /*
@@ -345,4 +429,5 @@
  *   before it can be shown anyway.
  */
+/*
 void
 showscores(int level)
@@ -350,5 +435,5 @@
 	return;
 }
-
+*/
 /* 	struct highscore *sp; */
 /* 	int i, n, c; */
Index: tetris/scores.h
===================================================================
--- tetris/scores.h	(revision 0aa024b1d503a5c3520dc1cb1aadeee8029ed501)
+++ tetris/scores.h	(revision 9996ed56130d52f2e19a6f9a37c19b831440ca91)
@@ -40,16 +40,18 @@
  */
 #include <sys/time.h>
-#define MAXLOGNAME 10
+#define MAXLOGNAME 16
 struct highscore {
-	char	hs_name[MAXLOGNAME];	/* login name */
+	char	hs_name[MAXLOGNAME + 1];	/* login name */
 	int	hs_score;	/* raw score */
 	int	hs_level;	/* play level */
-	time_t	hs_time;	/* time at game end */
+//	time_t	hs_time;	/* time at game end */
 };
 
-#define MAXHISCORES	80
-#define MAXSCORES	9	/* maximum high score entries per person */
-#define	EXPIRATION	(5L * 365 * 24 * 60 * 60)
+#define MAXHISCORES	10
+//#define MAXSCORES	9	/* maximum high score entries per person */
+//#define	EXPIRATION	(5L * 365 * 24 * 60 * 60)
 
 void savescore(int);
 void showscores(int);
+void insertscore(int score, int level);
+void initscores(void);
Index: tetris/screen.c
===================================================================
--- tetris/screen.c	(revision 0aa024b1d503a5c3520dc1cb1aadeee8029ed501)
+++ tetris/screen.c	(revision 9996ed56130d52f2e19a6f9a37c19b831440ca91)
@@ -94,4 +94,11 @@
 }
 
+
+void clear_screen(void)
+{
+	send_call(con_phone, CONSOLE_CLEAR, 0);
+	moveto(0,0);
+}
+
 /*
  * Clear the screen, forgetting the current contents in the process.
@@ -119,5 +126,5 @@
 }
 
-static void moveto(int r, int c)
+void moveto(int r, int c)
 {
 	send_call_2(con_phone, CONSOLE_GOTO, r, c);
@@ -129,10 +136,7 @@
 }
 
-struct winsize {
-	ipcarg_t ws_row;
-	ipcarg_t ws_col;
-};
-
-static int get_display_size(struct winsize *ws)
+winsize_t winsize;
+
+static int get_display_size(winsize_t *ws)
 {
 	return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
@@ -154,5 +158,5 @@
 scr_set(void)
 {
-	struct winsize ws;
+	winsize_t ws;
 
 	Rows = 0, Cols = 0;
Index: tetris/screen.h
===================================================================
--- tetris/screen.h	(revision 0aa024b1d503a5c3520dc1cb1aadeee8029ed501)
+++ tetris/screen.h	(revision 9996ed56130d52f2e19a6f9a37c19b831440ca91)
@@ -41,4 +41,16 @@
 #define	putpad(s)	tputs(s, 1, put)
 
+#include <async.h>
+
+typedef struct {
+	ipcarg_t ws_row;
+	ipcarg_t ws_col;
+} winsize_t;
+
+extern winsize_t winsize;
+
+void moveto(int r, int c);
+void clear_screen(void);
+
 int	put(int);			/* just calls putchar; for tputs */
 void	scr_clear(void);
Index: tetris/tetris.c
===================================================================
--- tetris/tetris.c	(revision 0aa024b1d503a5c3520dc1cb1aadeee8029ed501)
+++ tetris/tetris.c	(revision 9996ed56130d52f2e19a6f9a37c19b831440ca91)
@@ -159,4 +159,91 @@
 }
 
+static void tetris_scores(int firstgame)
+{
+}
+
+static void tetris_menu_draw(int level) 
+{
+		clear_screen();
+		moveto(5,10);
+		puts("Tetris\n\n");
+			
+		moveto(8,10);
+		printf("Level = %d (press keys 1 - 9 to change)",level);
+		moveto(9,10);
+		printf("Preview is %s (press 'p' to change)", (showpreview?"on ":"off"));
+		moveto(12,10);
+		printf("Press 'h' to show hiscore table.");
+		moveto(13,10);
+		printf("Press 's' to start game.");
+		moveto(14,10);
+		printf("Press 'q' to quit game.");
+		moveto(20,10);
+		printf("In game controls:");
+		moveto(21,0);
+		puts(key_msg);
+}
+
+static int tetris_menu(int *level) 
+{
+	static int firstgame = 1;
+	int i;
+/*	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);
+
+	showscores(level);
+	
+	printf("\nHit 's' to new game, 'q' to quit.\n");
+*/
+	tetris_menu_draw(*level);
+	while (1) {
+	
+		i = getchar();
+		
+		switch(i) {
+			case 'p':
+				showpreview = !showpreview;
+				moveto(9,21);
+				if (showpreview)
+					printf("on ");
+				else
+					printf("off");
+					
+				break;
+			case 'h':
+				showscores(firstgame);
+				tetris_menu_draw(*level);
+				break;
+			case 's':
+				firstgame = 0;
+				return 1;
+			case 'q':
+				return 0;
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':		
+			case '7':
+			case '8':
+			case '9':
+				*level = i - '0';
+				moveto(8,18);
+				printf("%d", *level);
+				break;
+		}
+	}
+	
+}
+
 int
 main(int argc, char *argv[])
@@ -175,5 +262,6 @@
 //	setegid(gid);
 
-	classic = showpreview = 0;
+	classic = 0;
+	showpreview = 1; 
 
 /* 	while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
@@ -233,133 +321,116 @@
 		key_write[0], key_write[1], key_write[2], key_write[3],
 		key_write[4], key_write[5]);
-newgame:
+
 	scr_init();
-	setup_board();
-
-	srandomdev();
-	scr_set();
-
-	pos = A_FIRST*B_COLS + (B_COLS/2)-1;
-	nextshape = randshape();
-	curshape = randshape();
-
-	scr_msg(key_msg, 1);
-
-	for (;;) {
-		place(curshape, pos, 1);
-		scr_update();
-		place(curshape, pos, 0);
-		c = tgetchar();
-		if (c < 0) {
+	initscores();
+	while (tetris_menu(&level)) {
+
+
+		scr_clear();
+		setup_board();
+	
+		srandomdev();
+		scr_set();
+	
+		pos = A_FIRST*B_COLS + (B_COLS/2)-1;
+		nextshape = randshape();
+		curshape = randshape();
+	
+		scr_msg(key_msg, 1);
+	
+		for (;;) {
+			place(curshape, pos, 1);
+			scr_update();
+			place(curshape, pos, 0);
+			c = tgetchar();
+			if (c < 0) {
+				/*
+				 * Timeout.  Move down if possible.
+				 */
+				if (fits_in(curshape, pos + B_COLS)) {
+					pos += B_COLS;
+					continue;
+				}
+	
+				/*
+				 * Put up the current shape `permanently',
+				 * bump score, and elide any full rows.
+				 */
+				place(curshape, pos, 1);
+				score++;
+				elide();
+	
+				/*
+				 * Choose a new shape.  If it does not fit,
+				 * the game is over.
+				 */
+				curshape = nextshape;
+				nextshape = randshape();
+				pos = A_FIRST*B_COLS + (B_COLS/2)-1;
+				if (!fits_in(curshape, pos))
+					break;
+				continue;
+			}
+	
 			/*
-			 * Timeout.  Move down if possible.
+			 * Handle command keys.
 			 */
-			if (fits_in(curshape, pos + B_COLS)) {
-				pos += B_COLS;
-				continue;
-			}
-
-			/*
-			 * Put up the current shape `permanently',
-			 * bump score, and elide any full rows.
-			 */
-			place(curshape, pos, 1);
-			score++;
-			elide();
-
-			/*
-			 * Choose a new shape.  If it does not fit,
-			 * the game is over.
-			 */
-			curshape = nextshape;
-			nextshape = randshape();
-			pos = A_FIRST*B_COLS + (B_COLS/2)-1;
-			if (!fits_in(curshape, pos))
+			if (c == keys[5]) {
+				/* quit */
 				break;
-			continue;
+			}
+			if (c == keys[4]) {
+				static char msg[] =
+				    "paused - press RETURN to continue";
+	
+				place(curshape, pos, 1);
+				do {
+					scr_update();
+					scr_msg(key_msg, 0);
+					scr_msg(msg, 1);
+	//				(void) fflush(stdout);
+				} while (rwait((struct timeval *)NULL) == -1);
+				scr_msg(msg, 0);
+				scr_msg(key_msg, 1);
+				place(curshape, pos, 0);
+				continue;
+			}
+			if (c == keys[0]) {
+				/* move left */
+				if (fits_in(curshape, pos - 1))
+					pos--;
+				continue;
+			}
+			if (c == keys[1]) {
+				/* turn */
+				const struct shape *new = &shapes[
+				    classic? curshape->rotc : curshape->rot];
+	
+				if (fits_in(new, pos))
+					curshape = new;
+				continue;
+			}
+			if (c == keys[2]) {
+				/* move right */
+				if (fits_in(curshape, pos + 1))
+					pos++;
+				continue;
+			}
+			if (c == keys[3]) {
+				/* move to bottom */
+				while (fits_in(curshape, pos + B_COLS)) {
+					pos += B_COLS;
+					score++;
+				}
+				continue;
+			}
+			if (c == '\f') {
+				scr_clear();
+				scr_msg(key_msg, 1);
+			}
 		}
-
-		/*
-		 * Handle command keys.
-		 */
-		if (c == keys[5]) {
-			/* quit */
-			break;
-		}
-		if (c == keys[4]) {
-			static char msg[] =
-			    "paused - press RETURN to continue";
-
-			place(curshape, pos, 1);
-			do {
-				scr_update();
-				scr_msg(key_msg, 0);
-				scr_msg(msg, 1);
-//				(void) fflush(stdout);
-			} while (rwait((struct timeval *)NULL) == -1);
-			scr_msg(msg, 0);
-			scr_msg(key_msg, 1);
-			place(curshape, pos, 0);
-			continue;
-		}
-		if (c == keys[0]) {
-			/* move left */
-			if (fits_in(curshape, pos - 1))
-				pos--;
-			continue;
-		}
-		if (c == keys[1]) {
-			/* turn */
-			const struct shape *new = &shapes[
-			    classic? curshape->rotc : curshape->rot];
-
-			if (fits_in(new, pos))
-				curshape = new;
-			continue;
-		}
-		if (c == keys[2]) {
-			/* move right */
-			if (fits_in(curshape, pos + 1))
-				pos++;
-			continue;
-		}
-		if (c == keys[3]) {
-			/* move to bottom */
-			while (fits_in(curshape, pos + B_COLS)) {
-				pos += B_COLS;
-				score++;
-			}
-			continue;
-		}
-		if (c == '\f') {
-			scr_clear();
-			scr_msg(key_msg, 1);
-		}
-	}
-
-	scr_clear();
-	scr_end();
-
-	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);
-
-	showscores(level);
-	
-	printf("\nHit 's' to new game, 'q' to quit.\n");
-
-	
-	while (i = getchar()) {
-		if (i == 's')
-			goto newgame;
-		if (i == 'q')
-			break;
+		
+		scr_clear();
+		insertscore(score, level);
 	}
 	
@@ -371,4 +442,5 @@
 			break
 */
+	scr_end();
 	exit(0);
 }
