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[] =
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 */
183 if ((orig.sg_ospeed < B9600) ||
184 ((! CM) && (! TA))) fast=0;
186 chase (&snake[i], &snake[i-1]);
191 /* Main command loop */
201 if (((c = getchar() & 0177) <= '9') && (c >= '0')) {
203 j = scanf("%d",&repeat);
204 c = getchar() & 0177;
206 if (c != '.') repeat = 1;
212 (c == *KL || c == *KR || c == *KU || c == *KD)) {
219 for (j=Klength;j>0;j--){
242 * This works if we figure it out on second character.
248 if(j != 1) c = getchar() & 0177;
260 case 0177: /* del or end of file */
282 repeat = you.col - money.col;
291 repeat = you.line - money.line;
295 repeat = ccnt - 1 - you.col;
300 repeat = money.col - you.col;
304 repeat = lcnt - 1 - you.line;
309 repeat = money.line - you.line;
313 for(k=1;k<=repeat;k++){
323 if((fast) || (k == repeat) ||
331 if (you.col < ccnt-1) {
335 if((fast) || (k == repeat) ||
348 if((fast) || (k == repeat) ||
358 if (you.line+1 < lcnt) {
362 if((fast) || (k == repeat) ||
363 (you.line == lcnt-1))
369 if (same(&you,&money))
378 } while (money.col == finish.col && money.line == finish.line ||
379 money.col < 5 && money.line == 0 ||
380 money.col == you.col && money.line == you.line);
381 pchar(&money,TREASURE);
385 if (same(&you,&finish))
390 pr("You have won with $%d.\n",cashvalue);
397 if (pushsnake())break;
411 pchar(&money,TREASURE);
413 pchar(&snake[i],SNAKETAIL);
415 pchar(&snake[0], SNAKEHEAD);
426 for (i= 0; i<ccnt; i++) {
431 for (i= -1; i<=lcnt; i++) {
436 for (i= -1; i<=lcnt; i++) {
441 for (i= 0; i<ccnt; i++) {
454 p.col = random() % ccnt;
455 p.line = random() % lcnt;
457 /* make sure it's not on top of something else */
458 if (p.line == 0 && p.col < 5)
462 if (same(&p, &money))
464 if (same(&p, &finish))
466 for (i = 0; i < 5; i++)
467 if (same(&p, &snake[i]))
479 short score = iscore;
482 short allbwho=0, allbscore=0;
486 * Neg uid, 0, and 1 cannot have scores recorded.
488 if ((uid = getuid()) <= 1) {
489 pr("No saved scores for uid %d.\n", uid);
492 if (rawscores == -1) {
493 pr("No score file %s: %s.\n", _PATH_RAWSCORES,
497 /* Figure out what happened in the past */
498 read(rawscores, &allbscore, sizeof(short));
499 read(rawscores, &allbwho, sizeof(short));
500 lseek(rawscores, ((off_t)uid)*sizeof(short), 0);
501 read(rawscores, &oldbest, sizeof(short));
503 return (score > oldbest ? 1 : 0);
505 /* Update this jokers best */
506 if (score > oldbest) {
507 lseek(rawscores, ((off_t)uid)*sizeof(short), 0);
508 write(rawscores, &score, sizeof(short));
509 pr("You bettered your previous best of $%d\n", oldbest);
511 pr("Your best to date is $%d\n", oldbest);
513 /* See if we have a new champ */
514 p = getpwuid(allbwho);
515 if (p == NULL || score > allbscore) {
516 lseek(rawscores, (off_t)0, 0);
517 write(rawscores, &score, sizeof(short));
518 write(rawscores, &uid, sizeof(short));
520 pr("You beat %s's old record of $%d!\n",
521 p->pw_name, allbscore);
523 pr("You set a new record!\n");
525 pr("The highest is %s with $%d\n", p->pw_name, allbscore);
531 * Flush typeahead to keep from buffering a bunch of chars and then
532 * overshooting. This loses horribly at 9600 baud, but works nicely
533 * if the terminal gets behind.
540 0, 1, 1, 1, 0,-1,-1,-1};
542 -1,-1, 0, 1, 1, 1, 0,-1};
544 1, 1.4, 1, 1.4, 1, 1.4, 1, 1.4
548 struct point *sp, *np;
550 /* this algorithm has bugs; otherwise the
551 snake would get too good */
554 double v1, v2, vp, max;
555 point(&d,you.col-sp->col,you.line-sp->line);
556 v1 = sqrt( (double) (d.col*d.col + d.line*d.line) );
561 vp = d.col*mx[i] + d.line*my[i];
564 vp = ((double)vp)/(v1*v2);
574 point(&d,sp->col+mx[i],sp->line+my[i]);
576 if (d.col<0 || d.col>=ccnt || d.line<0 || d.line>=lcnt)
579 * Change to allow snake to eat you if you're on the money,
580 * otherwise, you can just crouch there until the snake goes
581 * away. Not positive it's right.
583 * if (d.line == 0 && d.col < 5) continue;
585 if (same(&d,&money)) continue;
586 if (same(&d,&finish)) continue;
587 wt[i]= i==w ? loot/10 : 1;
588 if (i==oldw) wt [i] += loot/20;
601 while (wt[i]==0) i++;
604 point(np,sp->col+mx[w],sp->line+my[w]);
614 point(&p,COLUMNS/2 - 8,LINES/2 - 1);
621 loot = loot - penalty;
624 str = "SPACE WARP!!!";
625 penalty += loot/PENALTY;
642 pchar(point(&p,you.col,0),'-');
644 if(you.line > lcnt-4){
645 pchar(point(&p,you.col,lcnt-1),'_');
648 pchar(point(&p,0,you.line),'(');
650 if(you.col > ccnt-10){
651 pchar(point(&p,ccnt-1,you.line),')');
653 if (! stretch(&money)) if (! stretch(&finish)) delay(10);
658 if(you.line > lcnt-4){
659 point(&p,you.col,lcnt-1);
663 point(&p,0,you.line);
666 if(you.col > ccnt-10){
667 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\\_/");
741 int boxsize; /* actually diameter of box, not radius */
743 boxsize = fast ? 10 : 4;
744 point(&x,ps->col,ps->line);
745 for(j=1;j<boxsize;j++){
773 * My manual says times doesn't return a value. Furthermore, the
774 * snake should get his turn every time no matter if the user is
775 * on a fast terminal with typematic keys or not.
776 * So I have taken the call to times out.
779 if (same(&snake[i], &snake[5]))
782 pchar(&snake[5],' ');
784 snake[i+1]= snake[i];
785 chase(&snake[0], &snake[1]);
786 pchar(&snake[1],SNAKETAIL);
787 pchar(&snake[0],SNAKEHEAD);
790 if (same(&snake[i],&you))
793 i = (cashvalue) % 10;
794 bonus = random() % 10;
804 if ( loot >= penalty ){
805 pr("You and your $%d have been eaten\n",
808 pr("The snake ate you. You owe $%d.\n",
824 if (same(sp,&money)) {
828 if (same(sp,&finish)) {
832 if (same(sp,&snake[0])) {
837 if(same(sp,&snake[j])){
842 if ((sp->col < 4) && (sp->line == 0)){
844 if((you.line == 0) && (you.col < 4)) pchar(&you,ME);
868 signal(SIGINT,SIG_IGN);
880 kill(getpid(), SIGTSTP);
889 pr("You made %d moves.\n",num);
897 if (logfile != NULL) {
899 fprintf(logfile, "%s $%d %dx%d %s %s",
900 getlogin(), cashvalue, lcnt, ccnt, msg, ctime(&t));