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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)snake.c 8.2 (Berkeley) 1/7/94
35 * $FreeBSD: src/games/snake/snake/snake.c,v 1.11.2.1 2000/08/17 06:21:44 jhb Exp $
36 * $DragonFly: src/games/snake/snake/snake.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
40 * snake - crt hack game.
42 * You move around the screen with arrow keys trying to pick up money
43 * without getting eaten by the snake. hjkl work as in vi in place of
44 * arrow keys. You can leave at the exit any time.
47 * cc -O snake.c move.c -o snake -lm -ltermlib
50 #include <sys/param.h>
60 #include "pathnames.h"
62 #define PENALTY 10 /* % penalty for invoking spacewarp */
79 struct point snake[6];
87 char *kl, *kr, *ku, *kd;
105 rawscores = open(_PATH_RAWSCORES, O_RDWR|O_CREAT, 0664);
106 logfile = fopen(_PATH_LOGFILE, "a");
113 while ((ch = getopt(argc, argv, "l:w:")) != -1)
120 case 'w': /* width */
123 case 'l': /* length */
128 fputs("usage: snake [-d seed] [-w width] [-l length]\n", stderr);
138 pr("snake: screen too small for a fair game.\n");
143 * chunk is the amount of money the user gets for each $.
144 * The formula below tries to be fair for various screen sizes.
145 * We only pay attention to the smaller of the 2 edges, since
146 * that seems to be the bottleneck.
147 * This formula is a hyperbola which includes the following points:
148 * (24, $25) (original scoring algorithm)
149 * (12, $40) (experimentally derived by the "feel")
150 * (48, $15) (a guess)
151 * This will give a 4x4 screen $99/shot. We don't allow anything
152 * smaller than 4x4 because there is a 3x3 game where you can win
153 * an infinite amount of money.
155 if (i < 12) i = 12; /* otherwise it isn't fair */
157 * Compensate for border. This really changes the game since
158 * the screen is two squares smaller but we want the default
159 * to be $25, and the high scores on small screens were a bit
163 chunk = (675.0 / (i+6)) + 2.5; /* min screen edge */
165 signal (SIGINT, stop);
166 putpad(TI); /* String to begin programs that use cm */
167 putpad(KS); /* Put terminal in keypad transmit mode */
168 putpad(VI); /* Hide cursor */
175 if ((orig.sg_ospeed < B9600) ||
176 ((! CM) && (! TA))) fast=0;
178 chase (&snake[i], &snake[i-1]);
183 /* Main command loop */
193 if (((c = getchar() & 0177) <= '9') && (c >= '0')) {
195 j = scanf("%d",&repeat);
196 c = getchar() & 0177;
198 if (c != '.') repeat = 1;
204 (c == *KL || c == *KR || c == *KU || c == *KD)) {
211 for (j=Klength;j>0;j--){
234 * This works if we figure it out on second character.
240 if(j != 1) c = getchar() & 0177;
252 case 0177: /* del or end of file */
274 repeat = you.col - money.col;
283 repeat = you.line - money.line;
287 repeat = ccnt - 1 - you.col;
292 repeat = money.col - you.col;
296 repeat = lcnt - 1 - you.line;
301 repeat = money.line - you.line;
305 for(k=1;k<=repeat;k++){
315 if((fast) || (k == repeat) ||
323 if (you.col < ccnt-1) {
327 if((fast) || (k == repeat) ||
340 if((fast) || (k == repeat) ||
350 if (you.line+1 < lcnt) {
354 if((fast) || (k == repeat) ||
355 (you.line == lcnt-1))
361 if (same(&you,&money))
368 } while (money.col == finish.col && money.line == finish.line ||
369 money.col < 5 && money.line == 0 ||
370 money.col == you.col && money.line == you.line);
371 pchar(&money,TREASURE);
375 if (same(&you,&finish))
380 pr("You have won with $%d.\n",cashvalue);
387 if (pushsnake())break;
401 pchar(&money,TREASURE);
403 pchar(&snake[i],SNAKETAIL);
405 pchar(&snake[0], SNAKEHEAD);
416 for (i= 0; i<ccnt; i++) {
421 for (i= -1; i<=lcnt; i++) {
426 for (i= -1; i<=lcnt; i++) {
431 for (i= 0; i<ccnt; i++) {
444 p.col = random() % ccnt;
445 p.line = random() % lcnt;
447 /* make sure it's not on top of something else */
448 if (p.line == 0 && p.col < 5)
452 if (same(&p, &money))
454 if (same(&p, &finish))
456 for (i = 0; i < 5; i++)
457 if (same(&p, &snake[i]))
469 short score = iscore;
472 short allbwho=0, allbscore=0;
476 * Neg uid, 0, and 1 cannot have scores recorded.
478 if ((uid = getuid()) <= 1) {
479 pr("No saved scores for uid %d.\n", uid);
482 if (rawscores == -1) {
483 pr("No score file %s: %s.\n", _PATH_RAWSCORES,
487 /* Figure out what happened in the past */
488 read(rawscores, &allbscore, sizeof(short));
489 read(rawscores, &allbwho, sizeof(short));
490 lseek(rawscores, ((off_t)uid)*sizeof(short), 0);
491 read(rawscores, &oldbest, sizeof(short));
493 return (score > oldbest ? 1 : 0);
495 /* Update this jokers best */
496 if (score > oldbest) {
497 lseek(rawscores, ((off_t)uid)*sizeof(short), 0);
498 write(rawscores, &score, sizeof(short));
499 pr("You bettered your previous best of $%d\n", oldbest);
501 pr("Your best to date is $%d\n", oldbest);
503 /* See if we have a new champ */
504 p = getpwuid(allbwho);
505 if (p == NULL || score > allbscore) {
506 lseek(rawscores, (off_t)0, 0);
507 write(rawscores, &score, sizeof(short));
508 write(rawscores, &uid, sizeof(short));
510 pr("You beat %s's old record of $%d!\n",
511 p->pw_name, allbscore);
513 pr("You set a new record!\n");
515 pr("The highest is %s with $%d\n", p->pw_name, allbscore);
521 * Flush typeahead to keep from buffering a bunch of chars and then
522 * overshooting. This loses horribly at 9600 baud, but works nicely
523 * if the terminal gets behind.
530 0, 1, 1, 1, 0,-1,-1,-1};
532 -1,-1, 0, 1, 1, 1, 0,-1};
534 1, 1.4, 1, 1.4, 1, 1.4, 1, 1.4
538 struct point *sp, *np;
540 /* this algorithm has bugs; otherwise the
541 snake would get too good */
544 double v1, v2, vp, max;
545 point(&d,you.col-sp->col,you.line-sp->line);
546 v1 = sqrt( (double) (d.col*d.col + d.line*d.line) );
551 vp = d.col*mx[i] + d.line*my[i];
554 vp = ((double)vp)/(v1*v2);
564 point(&d,sp->col+mx[i],sp->line+my[i]);
566 if (d.col<0 || d.col>=ccnt || d.line<0 || d.line>=lcnt)
569 * Change to allow snake to eat you if you're on the money,
570 * otherwise, you can just crouch there until the snake goes
571 * away. Not positive it's right.
573 * if (d.line == 0 && d.col < 5) continue;
575 if (same(&d,&money)) continue;
576 if (same(&d,&finish)) continue;
577 wt[i]= i==w ? loot/10 : 1;
578 if (i==oldw) wt [i] += loot/20;
591 while (wt[i]==0) i++;
594 point(np,sp->col+mx[w],sp->line+my[w]);
604 point(&p,COLUMNS/2 - 8,LINES/2 - 1);
611 loot = loot - penalty;
614 str = "SPACE WARP!!!";
615 penalty += loot/PENALTY;
632 pchar(point(&p,you.col,0),'-');
634 if(you.line > lcnt-4){
635 pchar(point(&p,you.col,lcnt-1),'_');
638 pchar(point(&p,0,you.line),'(');
640 if(you.col > ccnt-10){
641 pchar(point(&p,ccnt-1,you.line),')');
643 if (! stretch(&money)) if (! stretch(&finish)) delay(10);
648 if(you.line > lcnt-4){
649 point(&p,you.col,lcnt-1);
653 point(&p,0,you.line);
656 if(you.col > ccnt-10){
657 point(&p,ccnt-1,you.line);
667 point(&p,you.col,you.line);
668 if(abs(ps->col-you.col) < 6){
669 if(you.line < ps->line){
670 for (p.line = you.line+1;p.line <= ps->line;p.line++)
673 for (;p.line > you.line;p.line--)
676 for (p.line = you.line-1;p.line >= ps->line;p.line--)
679 for (;p.line < you.line;p.line++)
683 } else if(abs(ps->line-you.line) < 3){
685 if(you.col < ps->col){
686 for (p.col = you.col+1;p.col <= ps->col;p.col++)
689 for (;p.col > you.col;p.col--)
692 for (p.col = you.col-1;p.col >= ps->col;p.col--)
695 for (;p.col < you.col;p.col++)
708 if(ps->col == 0)ps->col++;
709 if(ps->line == 0)ps->line++;
710 if(ps->line == LINES -1)ps->line--;
711 if(ps->col == COLUMNS -1)ps->col--;
712 apr(point(&x,ps->col-1,ps->line-1),"/*\\\r* *\r\\*/");
719 if (post(cashvalue,0)) {
720 apr(point(&x,ps->col-1,ps->line-1)," \ro.o\r\\_/");
722 apr(point(&x,ps->col-1,ps->line-1)," \ro.-\r\\_/");
725 apr(point(&x,ps->col-1,ps->line-1)," \ro.o\r\\_/");
733 int boxsize; /* actually diameter of box, not radius */
735 boxsize = fast ? 10 : 4;
736 point(&x,ps->col,ps->line);
737 for(j=1;j<boxsize;j++){
765 * My manual says times doesn't return a value. Furthermore, the
766 * snake should get his turn every time no matter if the user is
767 * on a fast terminal with typematic keys or not.
768 * So I have taken the call to times out.
771 if (same(&snake[i], &snake[5]))
774 pchar(&snake[5],' ');
776 snake[i+1]= snake[i];
777 chase(&snake[0], &snake[1]);
778 pchar(&snake[1],SNAKETAIL);
779 pchar(&snake[0],SNAKEHEAD);
782 if (same(&snake[i],&you))
785 i = (cashvalue) % 10;
786 bonus = random() % 10;
796 if ( loot >= penalty ){
797 pr("You and your $%d have been eaten\n",
800 pr("The snake ate you. You owe $%d.\n",
816 if (same(sp,&money)) {
820 if (same(sp,&finish)) {
824 if (same(sp,&snake[0])) {
829 if(same(sp,&snake[j])){
834 if ((sp->col < 4) && (sp->line == 0)){
836 if((you.line == 0) && (you.col < 4)) pchar(&you,ME);
860 signal(SIGINT,SIG_IGN);
870 kill(getpid(), SIGTSTP);
879 pr("You made %d moves.\n",num);
887 if (logfile != NULL) {
889 fprintf(logfile, "%s $%d %dx%d %s %s",
890 getlogin(), cashvalue, lcnt, ccnt, msg, ctime(&t));