* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)snake.c 8.2 (Berkeley) %G%";
* You move around the screen with arrow keys trying to pick up money
* without getting eaten by the snake. hjkl work as in vi in place of
* arrow keys. You can leave at the exit any time.
* cc -O snake.c move.c -o snake -lm -ltermlib
#define PENALTY 10 /* % penalty for invoking spacewarp */
while ((ch
= getopt(argc
, argv
, "l:w:")) != EOF
)
fputs("usage: snake [-d seed] [-w width] [-l length]\n", stderr
);
pr("snake: screen too small for a fair game.\n");
* chunk is the amount of money the user gets for each $.
* The formula below tries to be fair for various screen sizes.
* We only pay attention to the smaller of the 2 edges, since
* that seems to be the bottleneck.
* This formula is a hyperbola which includes the following points:
* (24, $25) (original scoring algorithm)
* (12, $40) (experimentally derived by the "feel")
* This will give a 4x4 screen $99/shot. We don't allow anything
* smaller than 4x4 because there is a 3x3 game where you can win
* an infinite amount of money.
if (i
< 12) i
= 12; /* otherwise it isn't fair */
* Compensate for border. This really changes the game since
* the screen is two squares smaller but we want the default
* to be $25, and the high scores on small screens were a bit
chunk
= (675.0 / (i
+6)) + 2.5; /* min screen edge */
putpad(TI
); /* String to begin programs that use cm */
putpad(KS
); /* Put terminal in keypad transmit mode */
if ((orig
.sg_ospeed
< B9600
) ||
((! CM
) && (! TA
))) fast
=0;
chase (&snake
[i
], &snake
[i
-1]);
if (((c
= getchar() & 0177) <= '9') && (c
>= '0')) {
if (c
!= '.') repeat
= 1;
(c
== *KL
|| c
== *KR
|| c
== *KU
|| c
== *KD
)) {
* This works if we figure it out on second character.
if(j
!= 1) c
= getchar() & 0177;
case 0177: /* del or end of file */
repeat
= you
.col
- money
.col
;
repeat
= you
.line
- money
.line
;
repeat
= ccnt
- 1 - you
.col
;
repeat
= money
.col
- you
.col
;
repeat
= lcnt
- 1 - you
.line
;
repeat
= money
.line
- you
.line
;
if((fast
) || (k
== repeat
) ||
if((fast
) || (k
== repeat
) ||
if((fast
) || (k
== repeat
) ||
if((fast
) || (k
== repeat
) ||
} while (money
.col
== finish
.col
&& money
.line
== finish
.line
||
money
.col
< 5 && money
.line
== 0 ||
money
.col
== you
.col
&& money
.line
== you
.line
);
pr("You have won with $%d.\n",cashvalue
);
pchar(&snake
[i
],SNAKETAIL
);
pchar(&snake
[0], SNAKEHEAD
);
for (i
= 0; i
<ccnt
; i
++) {
for (i
= -1; i
<=lcnt
; i
++) {
for (i
= -1; i
<=lcnt
; i
++) {
for (i
= 0; i
<ccnt
; i
++) {
p
.line
= random() % lcnt
;
/* make sure it's not on top of something else */
if (p
.line
== 0 && p
.col
< 5)
short allbwho
=0, allbscore
=0;
* Neg uid, 0, and 1 cannot have scores recorded.
if ((uid
= getuid()) <= 1) {
pr("No saved scores for uid %d.\n", uid
);
if ((rawscores
= open(_PATH_RAWSCORES
, O_RDWR
|O_CREAT
, 0644)) < 0) {
pr("No score file %s: %s.\n", _PATH_RAWSCORES
,
/* Figure out what happened in the past */
read(rawscores
, &allbscore
, sizeof(short));
read(rawscores
, &allbwho
, sizeof(short));
lseek(rawscores
, ((long)uid
)*sizeof(short), 0);
read(rawscores
, &oldbest
, sizeof(short));
return (score
> oldbest
? 1 : 0);
/* Update this jokers best */
lseek(rawscores
, ((long)uid
)*sizeof(short), 0);
write(rawscores
, &score
, sizeof(short));
pr("You bettered your previous best of $%d\n", oldbest
);
pr("Your best to date is $%d\n", oldbest
);
/* See if we have a new champ */
if (p
== NULL
|| score
> allbscore
) {
lseek(rawscores
, (long)0, 0);
write(rawscores
, &score
, sizeof(short));
write(rawscores
, &uid
, sizeof(short));
pr("You beat %s's old record of $%d!\n",
pr("You set a new record!\n");
pr("The highest is %s with $%d\n", p
->pw_name
, allbscore
);
* Flush typeahead to keep from buffering a bunch of chars and then
* overshooting. This loses horribly at 9600 baud, but works nicely
* if the terminal gets behind.
-1,-1, 0, 1, 1, 1, 0,-1};
1, 1.4, 1, 1.4, 1, 1.4, 1, 1.4
/* this algorithm has bugs; otherwise the
snake would get too good */
point(&d
,you
.col
-sp
->col
,you
.line
-sp
->line
);
v1
= sqrt( (double) (d
.col
*d
.col
+ d
.line
*d
.line
) );
vp
= d
.col
*mx
[i
] + d
.line
*my
[i
];
vp
= ((double)vp
)/(v1
*v2
);
point(&d
,sp
->col
+mx
[i
],sp
->line
+my
[i
]);
if (d
.col
<0 || d
.col
>=ccnt
|| d
.line
<0 || d
.line
>=lcnt
)
* Change to allow snake to eat you if you're on the money,
* otherwise, you can just crouch there until the snake goes
* away. Not positive it's right.
* if (d.line == 0 && d.col < 5) continue;
if (same(&d
,&money
)) continue;
if (same(&d
,&finish
)) continue;
wt
[i
]= i
==w
? loot
/10 : 1;
if (i
==oldw
) wt
[i
] += loot
/20;
vp
= (( rand() >> 6 ) & 01777) %w
;
point(np
,sp
->col
+mx
[w
],sp
->line
+my
[w
]);
point(&p
,COLUMNS
/2 - 8,LINES
/2 - 1);
pchar(point(&p
,you
.col
,0),'-');
pchar(point(&p
,you
.col
,lcnt
-1),'_');
pchar(point(&p
,0,you
.line
),'(');
pchar(point(&p
,ccnt
-1,you
.line
),')');
if (! stretch(&money
)) if (! stretch(&finish
)) delay(10);
point(&p
,you
.col
,lcnt
-1);
point(&p
,ccnt
-1,you
.line
);
point(&p
,you
.col
,you
.line
);
if(abs(ps
->col
-you
.col
) < 6){
for (p
.line
= you
.line
+1;p
.line
<= ps
->line
;p
.line
++)
for (;p
.line
> you
.line
;p
.line
--)
for (p
.line
= you
.line
-1;p
.line
>= ps
->line
;p
.line
--)
for (;p
.line
< you
.line
;p
.line
++)
} else if(abs(ps
->line
-you
.line
) < 3){
for (p
.col
= you
.col
+1;p
.col
<= ps
->col
;p
.col
++)
for (;p
.col
> you
.col
;p
.col
--)
for (p
.col
= you
.col
-1;p
.col
>= ps
->col
;p
.col
--)
for (;p
.col
< you
.col
;p
.col
++)
if(ps
->col
== 0)ps
->col
++;
if(ps
->line
== 0)ps
->line
++;
if(ps
->line
== LINES
-1)ps
->line
--;
if(ps
->col
== COLUMNS
-1)ps
->col
--;
apr(point(&x
,ps
->col
-1,ps
->line
-1),"/*\\\r* *\r\\*/");
apr(point(&x
,ps
->col
-1,ps
->line
-1)," \ro.o\r\\_/");
apr(point(&x
,ps
->col
-1,ps
->line
-1)," \ro.-\r\\_/");
apr(point(&x
,ps
->col
-1,ps
->line
-1)," \ro.o\r\\_/");
int boxsize
; /* actually diameter of box, not radius */
point(&x
,ps
->col
,ps
->line
);
* My manual says times doesn't return a value. Furthermore, the
* snake should get his turn every time no matter if the user is
* on a fast terminal with typematic keys or not.
* So I have taken the call to times out.
if (same(&snake
[i
], &snake
[5]))
chase(&snake
[0], &snake
[1]);
pchar(&snake
[1],SNAKETAIL
);
pchar(&snake
[0],SNAKEHEAD
);
if (same(&snake
[i
],&you
))
bonus
= ((rand()>>8) & 0377)% 10;
pr("You and your $%d have been eaten\n",
pr("The snake ate you. You owe $%d.\n",
if (same(sp
,&snake
[0])) {
if ((sp
->col
< 4) && (sp
->line
== 0)){
if((you
.line
== 0) && (you
.col
< 4)) pchar(&you
,ME
);
pr("You made %d moves.\n",num
);
if ((logfile
=fopen(_PATH_LOGFILE
, "a")) != NULL
) {
fprintf(logfile
, "%s $%d %dx%d %s %s",
getlogin(), cashvalue
, lcnt
, ccnt
, msg
, ctime(&t
));