Subversion Repositories HelenOS-historic

Compare Revisions

Ignore whitespace Rev 1589 → Rev 1590

/uspace/trunk/tetris/scores.c
46,9 → 46,9
/* #include <err.h> */
/* #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> */
/* #include <unistd.h> */
76,6 → 76,7
/* static int nscores; */
/* static int gotscores; */
/* static struct highscore scores[NUMSPOTS]; */
static struct highscore scores[NUMSPOTS];
 
/* static int checkscores(struct highscore *, int); */
/* static int cmpscores(const void *, const void *); */
83,6 → 84,89
/* 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);
}
}
 
/*
* Read the score file. Can be called from savescore (before showscores)
* or showscores (if savescore will not be called). If the given pointer
344,12 → 428,13
* - Even if that were not the case, a new score must be recorded
* before it can be shown anyway.
*/
/*
void
showscores(int level)
{
return;
}
 
*/
/* struct highscore *sp; */
/* int i, n, c; */
/* const char *me; */
/uspace/trunk/tetris/screen.c
93,6 → 93,13
set_style(0xe0e0e0, 0);
}
 
 
void clear_screen(void)
{
send_call(con_phone, CONSOLE_CLEAR, 0);
moveto(0,0);
}
 
/*
* Clear the screen, forgetting the current contents in the process.
*/
118,7 → 125,7
scr_clear();
}
 
static void moveto(int r, int c)
void moveto(int r, int c)
{
send_call_2(con_phone, CONSOLE_GOTO, r, c);
}
128,12 → 135,9
send_call(con_phone, CONSOLE_FLUSH, 0);
}
 
struct winsize {
ipcarg_t ws_row;
ipcarg_t ws_col;
};
winsize_t winsize;
 
static int get_display_size(struct winsize *ws)
static int get_display_size(winsize_t *ws)
{
return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
}
153,7 → 157,7
void
scr_set(void)
{
struct winsize ws;
winsize_t ws;
 
Rows = 0, Cols = 0;
if (get_display_size(&ws) == 0) {
/uspace/trunk/tetris/scores.h
39,17 → 39,19
* Tetris scores.
*/
#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);
/uspace/trunk/tetris/screen.h
40,6 → 40,18
*/
#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);
void scr_end(void);
/uspace/trunk/tetris/tetris.c
158,6 → 158,93
srandom(tv.tv_sec + tv.tv_usec / 100000);
}
 
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[])
{
174,7 → 261,8
// egid = getegid();
// setegid(gid);
 
classic = showpreview = 0;
classic = 0;
showpreview = 1;
 
/* while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
/* switch(ch) { */
232,135 → 320,118
"%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
key_write[0], key_write[1], key_write[2], key_write[3],
key_write[4], key_write[5]);
newgame:
 
scr_init();
setup_board();
initscores();
while (tetris_menu(&level)) {
 
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;
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;
}
 
/*
* Put up the current shape `permanently',
* bump score, and elide any full rows.
* Handle command keys.
*/
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;
}
 
/*
* 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);
if (c == keys[4]) {
static char msg[] =
"paused - press RETURN to continue";
printf("\nHit 's' to new game, 'q' to quit.\n");
 
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];
while (i = getchar()) {
if (i == 's')
goto newgame;
if (i == 'q')
break;
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();
insertscore(score, level);
}
scr_clear();
370,6 → 441,7
if (i == EOF)
break
*/
scr_end();
exit(0);
}