]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - snake/snake/snake.c
c98920435bdbba31b7756fba0f01d40578710b24
2 * Copyright (c) 1980 Regents of the University of California.
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
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
41 /*static char sccsid[] = "from: @(#)snake.c 5.10 (Berkeley) 2/28/91";*/
42 static char rcsid
[] = "$Id: snake.c,v 1.3 1993/08/01 18:51:11 mycroft Exp $";
46 * snake - crt hack game.
48 * You move around the screen with arrow keys trying to pick up money
49 * without getting eaten by the snake. hjkl work as in vi in place of
50 * arrow keys. You can leave at the exit any time.
53 * cc -O snake.c move.c -o snake -lm -ltermlib
56 #include <sys/param.h>
63 #include "pathnames.h"
65 #define PENALTY 10 /* % penalty for invoking spacewarp */
82 struct point snake
[6];
90 char *kl
, *kr
, *ku
, *kd
;
108 while ((ch
= getopt(argc
, argv
, "l:w:")) != EOF
)
115 case 'w': /* width */
118 case 'l': /* length */
123 fputs("usage: snake [-d seed] [-w width] [-l length]\n", stderr
);
133 pr("snake: screen too small for a fair game.\n");
138 * chunk is the amount of money the user gets for each $.
139 * The formula below tries to be fair for various screen sizes.
140 * We only pay attention to the smaller of the 2 edges, since
141 * that seems to be the bottleneck.
142 * This formula is a hyperbola which includes the following points:
143 * (24, $25) (original scoring algorithm)
144 * (12, $40) (experimentally derived by the "feel")
145 * (48, $15) (a guess)
146 * This will give a 4x4 screen $99/shot. We don't allow anything
147 * smaller than 4x4 because there is a 3x3 game where you can win
148 * an infinite amount of money.
150 if (i
< 12) i
= 12; /* otherwise it isn't fair */
152 * Compensate for border. This really changes the game since
153 * the screen is two squares smaller but we want the default
154 * to be $25, and the high scores on small screens were a bit
158 chunk
= (675.0 / (i
+6)) + 2.5; /* min screen edge */
160 signal (SIGINT
, stop
);
161 putpad(TI
); /* String to begin programs that use cm */
162 putpad(KS
); /* Put terminal in keypad transmit mode */
169 if ((orig
.sg_ospeed
< B9600
) ||
170 ((! CM
) && (! TA
))) fast
=0;
172 chase (&snake
[i
], &snake
[i
-1]);
177 /* Main command loop */
187 if (((c
= getchar() & 0177) <= '9') && (c
>= '0')) {
189 j
= scanf("%d",&repeat
);
190 c
= getchar() & 0177;
192 if (c
!= '.') repeat
= 1;
198 (c
== *KL
|| c
== *KR
|| c
== *KU
|| c
== *KD
)) {
205 for (j
=Klength
;j
>0;j
--){
228 * This works if we figure it out on second character.
234 if(j
!= 1) c
= getchar() & 0177;
246 case 0177: /* del or end of file */
268 repeat
= you
.col
- money
.col
;
277 repeat
= you
.line
- money
.line
;
281 repeat
= ccnt
- 1 - you
.col
;
286 repeat
= money
.col
- you
.col
;
290 repeat
= lcnt
- 1 - you
.line
;
295 repeat
= money
.line
- you
.line
;
299 for(k
=1;k
<=repeat
;k
++){
309 if((fast
) || (k
== repeat
) ||
317 if (you
.col
< ccnt
-1) {
321 if((fast
) || (k
== repeat
) ||
334 if((fast
) || (k
== repeat
) ||
344 if (you
.line
+1 < lcnt
) {
348 if((fast
) || (k
== repeat
) ||
349 (you
.line
== lcnt
-1))
355 if (same(&you
,&money
))
364 } while (money
.col
== finish
.col
&& money
.line
== finish
.line
||
365 money
.col
< 5 && money
.line
== 0 ||
366 money
.col
== you
.col
&& money
.line
== you
.line
);
367 pchar(&money
,TREASURE
);
371 if (same(&you
,&finish
))
376 pr("You have won with $%d.\n",cashvalue
);
383 if (pushsnake())break;
397 pchar(&money
,TREASURE
);
399 pchar(&snake
[i
],SNAKETAIL
);
401 pchar(&snake
[0], SNAKEHEAD
);
412 for (i
= 0; i
<ccnt
; i
++) {
417 for (i
= -1; i
<=lcnt
; i
++) {
422 for (i
= -1; i
<=lcnt
; i
++) {
427 for (i
= 0; i
<ccnt
; i
++) {
440 p
.col
= random() % ccnt
;
441 p
.line
= random() % lcnt
;
443 /* make sure it's not on top of something else */
444 if (p
.line
== 0 && p
.col
< 5)
448 if (same(&p
, &money
))
450 if (same(&p
, &finish
))
452 for (i
= 0; i
< 5; i
++)
453 if (same(&p
, &snake
[i
]))
465 short score
= iscore
;
469 short allbwho
=0, allbscore
=0;
473 * Neg uid, 0, and 1 cannot have scores recorded.
475 if ((uid
= getuid()) <= 1) {
476 pr("No saved scores for uid %d.\n", uid
);
479 if ((rawscores
= open(_PATH_RAWSCORES
, O_RDWR
|O_CREAT
, 0644)) < 0) {
480 pr("No score file %s: %s.\n", _PATH_RAWSCORES
,
484 /* Figure out what happened in the past */
485 read(rawscores
, &allbscore
, sizeof(short));
486 read(rawscores
, &allbwho
, sizeof(short));
487 lseek(rawscores
, ((long)uid
)*sizeof(short), 0);
488 read(rawscores
, &oldbest
, sizeof(short));
490 return (score
> oldbest
? 1 : 0);
492 /* Update this jokers best */
493 if (score
> oldbest
) {
494 lseek(rawscores
, ((long)uid
)*sizeof(short), 0);
495 write(rawscores
, &score
, sizeof(short));
496 pr("You bettered your previous best of $%d\n", oldbest
);
498 pr("Your best to date is $%d\n", oldbest
);
500 /* See if we have a new champ */
501 p
= getpwuid(allbwho
);
502 if (p
== NULL
|| score
> allbscore
) {
503 lseek(rawscores
, (long)0, 0);
504 write(rawscores
, &score
, sizeof(short));
505 write(rawscores
, &uid
, sizeof(short));
507 pr("You beat %s's old record of $%d!\n",
508 p
->pw_name
, allbscore
);
510 pr("You set a new record!\n");
512 pr("The highest is %s with $%d\n", p
->pw_name
, allbscore
);
518 * Flush typeahead to keep from buffering a bunch of chars and then
519 * overshooting. This loses horribly at 9600 baud, but works nicely
520 * if the terminal gets behind.
527 0, 1, 1, 1, 0,-1,-1,-1};
529 -1,-1, 0, 1, 1, 1, 0,-1};
531 1, 1.4, 1, 1.4, 1, 1.4, 1, 1.4
535 struct point
*sp
, *np
;
537 /* this algorithm has bugs; otherwise the
538 snake would get too good */
541 double sqrt(), v1
, v2
, vp
, max
;
542 point(&d
,you
.col
-sp
->col
,you
.line
-sp
->line
);
543 v1
= sqrt( (double) (d
.col
*d
.col
+ d
.line
*d
.line
) );
548 vp
= d
.col
*mx
[i
] + d
.line
*my
[i
];
551 vp
= ((double)vp
)/(v1
*v2
);
561 point(&d
,sp
->col
+mx
[i
],sp
->line
+my
[i
]);
563 if (d
.col
<0 || d
.col
>=ccnt
|| d
.line
<0 || d
.line
>=lcnt
)
566 * Change to allow snake to eat you if you're on the money,
567 * otherwise, you can just crouch there until the snake goes
568 * away. Not positive it's right.
570 * if (d.line == 0 && d.col < 5) continue;
572 if (same(&d
,&money
)) continue;
573 if (same(&d
,&finish
)) continue;
574 wt
[i
]= i
==w
? loot
/10 : 1;
575 if (i
==oldw
) wt
[i
] += loot
/20;
579 vp
= (( rand() >> 6 ) & 01777) %w
;
588 while (wt
[i
]==0) i
++;
591 point(np
,sp
->col
+mx
[w
],sp
->line
+my
[w
]);
601 point(&p
,COLUMNS
/2 - 8,LINES
/2 - 1);
608 loot
= loot
- penalty
;
611 str
= "SPACE WARP!!!";
612 penalty
+= loot
/PENALTY
;
629 pchar(point(&p
,you
.col
,0),'-');
631 if(you
.line
> lcnt
-4){
632 pchar(point(&p
,you
.col
,lcnt
-1),'_');
635 pchar(point(&p
,0,you
.line
),'(');
637 if(you
.col
> ccnt
-10){
638 pchar(point(&p
,ccnt
-1,you
.line
),')');
640 if (! stretch(&money
)) if (! stretch(&finish
)) delay(10);
645 if(you
.line
> lcnt
-4){
646 point(&p
,you
.col
,lcnt
-1);
650 point(&p
,0,you
.line
);
653 if(you
.col
> ccnt
-10){
654 point(&p
,ccnt
-1,you
.line
);
663 point(&p
,you
.col
,you
.line
);
664 if(abs(ps
->col
-you
.col
) < 6){
665 if(you
.line
< ps
->line
){
666 for (p
.line
= you
.line
+1;p
.line
<= ps
->line
;p
.line
++)
669 for (;p
.line
> you
.line
;p
.line
--)
672 for (p
.line
= you
.line
-1;p
.line
>= ps
->line
;p
.line
--)
675 for (;p
.line
< you
.line
;p
.line
++)
679 } else if(abs(ps
->line
-you
.line
) < 3){
681 if(you
.col
< ps
->col
){
682 for (p
.col
= you
.col
+1;p
.col
<= ps
->col
;p
.col
++)
685 for (;p
.col
> you
.col
;p
.col
--)
688 for (p
.col
= you
.col
-1;p
.col
>= ps
->col
;p
.col
--)
691 for (;p
.col
< you
.col
;p
.col
++)
704 if(ps
->col
== 0)ps
->col
++;
705 if(ps
->line
== 0)ps
->line
++;
706 if(ps
->line
== LINES
-1)ps
->line
--;
707 if(ps
->col
== COLUMNS
-1)ps
->col
--;
708 apr(point(&x
,ps
->col
-1,ps
->line
-1),"/*\\\r* *\r\\*/");
715 if (post(cashvalue
,0)) {
716 apr(point(&x
,ps
->col
-1,ps
->line
-1)," \ro.o\r\\_/");
718 apr(point(&x
,ps
->col
-1,ps
->line
-1)," \ro.-\r\\_/");
721 apr(point(&x
,ps
->col
-1,ps
->line
-1)," \ro.o\r\\_/");
728 int boxsize
; /* actually diameter of box, not radius */
730 boxsize
= fast
? 10 : 4;
731 point(&x
,ps
->col
,ps
->line
);
732 for(j
=1;j
<boxsize
;j
++){
760 * My manual says times doesn't return a value. Furthermore, the
761 * snake should get his turn every time no matter if the user is
762 * on a fast terminal with typematic keys or not.
763 * So I have taken the call to times out.
766 if (same(&snake
[i
], &snake
[5]))
769 pchar(&snake
[5],' ');
771 snake
[i
+1]= snake
[i
];
772 chase(&snake
[0], &snake
[1]);
773 pchar(&snake
[1],SNAKETAIL
);
774 pchar(&snake
[0],SNAKEHEAD
);
777 if (same(&snake
[i
],&you
))
780 i
= (cashvalue
) % 10;
781 bonus
= ((rand()>>8) & 0377)% 10;
791 if ( loot
>= penalty
){
792 pr("You and your $%d have been eaten\n",
795 pr("The snake ate you. You owe $%d.\n",
811 if (same(sp
,&money
)) {
815 if (same(sp
,&finish
)) {
819 if (same(sp
,&snake
[0])) {
824 if(same(sp
,&snake
[j
])){
829 if ((sp
->col
< 4) && (sp
->line
== 0)){
831 if((you
.line
== 0) && (you
.col
< 4)) pchar(&you
,ME
);
855 signal(SIGINT
,SIG_IGN
);
867 kill(getpid(), SIGTSTP
);
876 pr("You made %d moves.\n",num
);
885 if ((logfile
=fopen(_PATH_LOGFILE
, "a")) != NULL
) {
887 fprintf(logfile
, "%s $%d %dx%d %s %s",
888 getlogin(), cashvalue
, lcnt
, ccnt
, msg
, ctime(&t
));