2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)snake.c 8.2 (Berkeley) 1/7/94";
44 static const char rcsid[] =
45 "$FreeBSD: src/games/snake/snake/snake.c,v 1.11.2.1 2000/08/17 06:21:44 jhb Exp $";
49 * snake - crt hack game.
51 * You move around the screen with arrow keys trying to pick up money
52 * without getting eaten by the snake. hjkl work as in vi in place of
53 * arrow keys. You can leave at the exit any time.
56 * cc -O snake.c move.c -o snake -lm -ltermlib
59 #include <sys/param.h>
69 #include "pathnames.h"
71 #define PENALTY 10 /* % penalty for invoking spacewarp */
88 struct point snake[6];
96 char *kl, *kr, *ku, *kd;
114 rawscores = open(_PATH_RAWSCORES, O_RDWR|O_CREAT, 0664);
115 logfile = fopen(_PATH_LOGFILE, "a");
122 while ((ch = getopt(argc, argv, "l:w:")) != -1)
129 case 'w': /* width */
132 case 'l': /* length */
137 fputs("usage: snake [-d seed] [-w width] [-l length]\n", stderr);
147 pr("snake: screen too small for a fair game.\n");
152 * chunk is the amount of money the user gets for each $.
153 * The formula below tries to be fair for various screen sizes.
154 * We only pay attention to the smaller of the 2 edges, since
155 * that seems to be the bottleneck.
156 * This formula is a hyperbola which includes the following points:
157 * (24, $25) (original scoring algorithm)
158 * (12, $40) (experimentally derived by the "feel")
159 * (48, $15) (a guess)
160 * This will give a 4x4 screen $99/shot. We don't allow anything
161 * smaller than 4x4 because there is a 3x3 game where you can win
162 * an infinite amount of money.
164 if (i < 12) i = 12; /* otherwise it isn't fair */
166 * Compensate for border. This really changes the game since
167 * the screen is two squares smaller but we want the default
168 * to be $25, and the high scores on small screens were a bit
172 chunk = (675.0 / (i+6)) + 2.5; /* min screen edge */
174 signal (SIGINT, stop);
175 putpad(TI); /* String to begin programs that use cm */
176 putpad(KS); /* Put terminal in keypad transmit mode */
177 putpad(VI); /* Hide cursor */
184 if ((orig.sg_ospeed < B9600) ||
185 ((! CM) && (! TA))) fast=0;
187 chase (&snake[i], &snake[i-1]);
192 /* Main command loop */
202 if (((c = getchar() & 0177) <= '9') && (c >= '0')) {
204 j = scanf("%d",&repeat);
205 c = getchar() & 0177;
207 if (c != '.') repeat = 1;
213 (c == *KL || c == *KR || c == *KU || c == *KD)) {
220 for (j=Klength;j>0;j--){
243 * This works if we figure it out on second character.
249 if(j != 1) c = getchar() & 0177;
261 case 0177: /* del or end of file */
283 repeat = you.col - money.col;
292 repeat = you.line - money.line;
296 repeat = ccnt - 1 - you.col;
301 repeat = money.col - you.col;
305 repeat = lcnt - 1 - you.line;
310 repeat = money.line - you.line;
314 for(k=1;k<=repeat;k++){
324 if((fast) || (k == repeat) ||
332 if (you.col < ccnt-1) {
336 if((fast) || (k == repeat) ||
349 if((fast) || (k == repeat) ||
359 if (you.line+1 < lcnt) {
363 if((fast) || (k == repeat) ||
364 (you.line == lcnt-1))
370 if (same(&you,&money))
377 } while (money.col == finish.col && money.line == finish.line ||
378 money.col < 5 && money.line == 0 ||
379 money.col == you.col && money.line == you.line);
380 pchar(&money,TREASURE);
384 if (same(&you,&finish))
389 pr("You have won with $%d.\n",cashvalue);
396 if (pushsnake())break;
410 pchar(&money,TREASURE);
412 pchar(&snake[i],SNAKETAIL);
414 pchar(&snake[0], SNAKEHEAD);
425 for (i= 0; i<ccnt; i++) {
430 for (i= -1; i<=lcnt; i++) {
435 for (i= -1; i<=lcnt; i++) {
440 for (i= 0; i<ccnt; i++) {
453 p.col = random() % ccnt;
454 p.line = random() % lcnt;
456 /* make sure it's not on top of something else */
457 if (p.line == 0 && p.col < 5)
461 if (same(&p, &money))
463 if (same(&p, &finish))
465 for (i = 0; i < 5; i++)
466 if (same(&p, &snake[i]))
478 short score = iscore;
481 short allbwho=0, allbscore=0;
485 * Neg uid, 0, and 1 cannot have scores recorded.
487 if ((uid = getuid()) <= 1) {
488 pr("No saved scores for uid %d.\n", uid);
491 if (rawscores == -1) {
492 pr("No score file %s: %s.\n", _PATH_RAWSCORES,
496 /* Figure out what happened in the past */
497 read(rawscores, &allbscore, sizeof(short));
498 read(rawscores, &allbwho, sizeof(short));
499 lseek(rawscores, ((off_t)uid)*sizeof(short), 0);
500 read(rawscores, &oldbest, sizeof(short));
502 return (score > oldbest ? 1 : 0);
504 /* Update this jokers best */
505 if (score > oldbest) {
506 lseek(rawscores, ((off_t)uid)*sizeof(short), 0);
507 write(rawscores, &score, sizeof(short));
508 pr("You bettered your previous best of $%d\n", oldbest);
510 pr("Your best to date is $%d\n", oldbest);
512 /* See if we have a new champ */
513 p = getpwuid(allbwho);
514 if (p == NULL || score > allbscore) {
515 lseek(rawscores, (off_t)0, 0);
516 write(rawscores, &score, sizeof(short));
517 write(rawscores, &uid, sizeof(short));
519 pr("You beat %s's old record of $%d!\n",
520 p->pw_name, allbscore);
522 pr("You set a new record!\n");
524 pr("The highest is %s with $%d\n", p->pw_name, allbscore);
530 * Flush typeahead to keep from buffering a bunch of chars and then
531 * overshooting. This loses horribly at 9600 baud, but works nicely
532 * if the terminal gets behind.
539 0, 1, 1, 1, 0,-1,-1,-1};
541 -1,-1, 0, 1, 1, 1, 0,-1};
543 1, 1.4, 1, 1.4, 1, 1.4, 1, 1.4
547 struct point *sp, *np;
549 /* this algorithm has bugs; otherwise the
550 snake would get too good */
553 double v1, v2, vp, max;
554 point(&d,you.col-sp->col,you.line-sp->line);
555 v1 = sqrt( (double) (d.col*d.col + d.line*d.line) );
560 vp = d.col*mx[i] + d.line*my[i];
563 vp = ((double)vp)/(v1*v2);
573 point(&d,sp->col+mx[i],sp->line+my[i]);
575 if (d.col<0 || d.col>=ccnt || d.line<0 || d.line>=lcnt)
578 * Change to allow snake to eat you if you're on the money,
579 * otherwise, you can just crouch there until the snake goes
580 * away. Not positive it's right.
582 * if (d.line == 0 && d.col < 5) continue;
584 if (same(&d,&money)) continue;
585 if (same(&d,&finish)) continue;
586 wt[i]= i==w ? loot/10 : 1;
587 if (i==oldw) wt [i] += loot/20;
600 while (wt[i]==0) i++;
603 point(np,sp->col+mx[w],sp->line+my[w]);
613 point(&p,COLUMNS/2 - 8,LINES/2 - 1);
620 loot = loot - penalty;
623 str = "SPACE WARP!!!";
624 penalty += loot/PENALTY;
641 pchar(point(&p,you.col,0),'-');
643 if(you.line > lcnt-4){
644 pchar(point(&p,you.col,lcnt-1),'_');
647 pchar(point(&p,0,you.line),'(');
649 if(you.col > ccnt-10){
650 pchar(point(&p,ccnt-1,you.line),')');
652 if (! stretch(&money)) if (! stretch(&finish)) delay(10);
657 if(you.line > lcnt-4){
658 point(&p,you.col,lcnt-1);
662 point(&p,0,you.line);
665 if(you.col > ccnt-10){
666 point(&p,ccnt-1,you.line);
676 point(&p,you.col,you.line);
677 if(abs(ps->col-you.col) < 6){
678 if(you.line < ps->line){
679 for (p.line = you.line+1;p.line <= ps->line;p.line++)
682 for (;p.line > you.line;p.line--)
685 for (p.line = you.line-1;p.line >= ps->line;p.line--)
688 for (;p.line < you.line;p.line++)
692 } else if(abs(ps->line-you.line) < 3){
694 if(you.col < ps->col){
695 for (p.col = you.col+1;p.col <= ps->col;p.col++)
698 for (;p.col > you.col;p.col--)
701 for (p.col = you.col-1;p.col >= ps->col;p.col--)
704 for (;p.col < you.col;p.col++)
717 if(ps->col == 0)ps->col++;
718 if(ps->line == 0)ps->line++;
719 if(ps->line == LINES -1)ps->line--;
720 if(ps->col == COLUMNS -1)ps->col--;
721 apr(point(&x,ps->col-1,ps->line-1),"/*\\\r* *\r\\*/");
728 if (post(cashvalue,0)) {
729 apr(point(&x,ps->col-1,ps->line-1)," \ro.o\r\\_/");
731 apr(point(&x,ps->col-1,ps->line-1)," \ro.-\r\\_/");
734 apr(point(&x,ps->col-1,ps->line-1)," \ro.o\r\\_/");
742 int boxsize; /* actually diameter of box, not radius */
744 boxsize = fast ? 10 : 4;
745 point(&x,ps->col,ps->line);
746 for(j=1;j<boxsize;j++){
774 * My manual says times doesn't return a value. Furthermore, the
775 * snake should get his turn every time no matter if the user is
776 * on a fast terminal with typematic keys or not.
777 * So I have taken the call to times out.
780 if (same(&snake[i], &snake[5]))
783 pchar(&snake[5],' ');
785 snake[i+1]= snake[i];
786 chase(&snake[0], &snake[1]);
787 pchar(&snake[1],SNAKETAIL);
788 pchar(&snake[0],SNAKEHEAD);
791 if (same(&snake[i],&you))
794 i = (cashvalue) % 10;
795 bonus = random() % 10;
805 if ( loot >= penalty ){
806 pr("You and your $%d have been eaten\n",
809 pr("The snake ate you. You owe $%d.\n",
825 if (same(sp,&money)) {
829 if (same(sp,&finish)) {
833 if (same(sp,&snake[0])) {
838 if(same(sp,&snake[j])){
843 if ((sp->col < 4) && (sp->line == 0)){
845 if((you.line == 0) && (you.col < 4)) pchar(&you,ME);
869 signal(SIGINT,SIG_IGN);
879 kill(getpid(), SIGTSTP);
888 pr("You made %d moves.\n",num);
896 if (logfile != NULL) {
898 fprintf(logfile, "%s $%d %dx%d %s %s",
899 getlogin(), cashvalue, lcnt, ccnt, msg, ctime(&t));