Exemplo n.º 1
0
void
process(int ch)
{
	int x,y;
	struct body *nh;

	x = head->x;
	y = head->y;
	switch(ch)
	{
#ifdef KEY_LEFT
		case KEY_LEFT:
#endif
		case 'h':
			x--; break;
#ifdef KEY_DOWN
		case KEY_DOWN:
#endif
		case 'j':
			y++; break;
#ifdef KEY_UP
		case KEY_UP:
#endif
		case 'k':
			y--; break;
#ifdef KEY_RIGHT
		case KEY_RIGHT:
#endif
		case 'l':
			x++; break;
		case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
		case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
		case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
		case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
		case '\f': setup(); return;
		case CNTRL('Z'): suspend(0); return;
		case CNTRL('C'): crash(); return;
		case CNTRL('D'): crash(); return;
		case ERR: leave(0); return;
		default: return;
	}
	lastch = ch;
	if (growing == 0)
	{
		display(tail, ' ');
		tail->next->prev = NULL;
		nh = tail->next;
		free(tail);
		tail = nh;
		visible_len--;
	}
	else growing--;
	display(head, BODY);
	wmove(tv, y, x);
	if (isdigit(ch = winch(tv)))
	{
		growing += ch-'0';
		prize();
		score += growing;
		running = 0;
		wmove(stw, 0, COLS - 12);
		wprintw(stw, "Score: %3d", score);
		wrefresh(stw);
	}
	else if(ch != ' ') crash();
	nh = newlink();
	nh->next = NULL;
	nh->prev = head;
	head->next = nh;
	nh->y = y;
	nh->x = x;
	display(nh, HEAD);
	head = nh;
	visible_len++;
	if (!(slow && running)) {
		wmove(tv, head->y, head->x);
		wrefresh(tv);
	}
}
Exemplo n.º 2
0
int
main(int argc, char **argv)
{
	int retval;
	struct timeval t, tod;
	struct timezone tz;
	fd_set rset;

	FD_ZERO(&rset);
	setbuf(stdout, outbuf);
	srandomdev();
	signal(SIGINT, leave);
	signal(SIGQUIT, leave);
	signal(SIGTSTP, suspend);	/* process control signal */
	initscr();
	cbreak();
	noecho();
	keypad(stdscr, TRUE);
	slow = (baudrate() <= 1200);
	clear();
	if (COLS < 18 || LINES < 5) {
		endwin();
		errx(1, "screen too small");
	}
	if (argc == 2)
		start_len = atoi(argv[1]);
	if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3))
		start_len = LENGTH;
	stw = newwin(1, COLS-1, 0, 0);
	tv = newwin(LINES-1, COLS-1, 1, 0);
	box(tv, '*', '*');
	scrollok(tv, FALSE);
	scrollok(stw, FALSE);
	wmove(stw, 0, 0);
	wprintw(stw, " Worm");
	refresh();
	wrefresh(stw);
	wrefresh(tv);
	life();			/* Create the worm */
	prize();		/* Put up a goal */
	while(1)
	{
		if (wantleave) {
			endwin();		/* XXX signal race */
			exit(0);
		}
		if (wantsuspend) {
			move(LINES-1, 0);
			refresh();
			endwin();
			fflush(stdout);
			kill(getpid(), SIGSTOP);
			signal(SIGTSTP, suspend);
			cbreak();
			noecho();
			setup();
			wantsuspend = 0;
		}

		if (running)
		{
			running--;
			process(lastch);
		}
		else
		{
			/* fflush(stdout); */
			/* Delay could be a command line option */
			t.tv_sec = 1;
			t.tv_usec = 0;
			(void)gettimeofday(&tod, &tz);
			FD_SET(STDIN_FILENO, &rset);
			retval = select(STDIN_FILENO + 1, &rset, NULL, NULL, &t);
			if (retval > 0)
				process(getch());
			else
				process(lastch);
		}
	}
}
Exemplo n.º 3
0
double Book::currPrize() const {
    return prize(simulation()->t());
}