shifting/masking stuff; remove shell escape code, suspend always works;
add getopt(3); minor cleanups; bug report 4.2BSD/games/16
SCCS-vsn: games/snake/snake/snake.c 5.5
#endif /* not lint */
#ifndef lint
#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)snake.c 5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)snake.c 5.5 (Berkeley) %G%";
* cc -O snake.c move.c -o snake -lm -ltermlib
*/
* cc -O snake.c move.c -o snake -lm -ltermlib
*/
-
- /*
- * If CHECKBUSY is defined, the file BUSY must be executable
- * and must return a value which is used to determine the priority
- * a which snake runs. A zero value means no nice.
- * If BUSY does not exist, snake won't play.
- */
-#ifndef BUSY
-#define BUSY "/usr/games/lib/busy"
-#endif
/*
* This is the data file for scorekeeping.
/*
* This is the data file for scorekeeping.
int loot, penalty;
int long tl, tm=0L;
int loot, penalty;
int long tl, tm=0L;
-int argcount;
-char **argval;
char stri[BSIZE];
char *p;
char ch, savec;
char stri[BSIZE];
char *p;
char ch, savec;
- int i,k;
- int j;
- long time();
+ extern char *optarg;
+ extern int optind;
+ int ch, i, j, k;
+ time_t time();
+ long atol();
- argcount = argc;
- argval = argv;
- penalty = loot = 0;
- getcap();
- ccnt -= 2; lcnt -= 2; /* compensate for border */
- busy();
- time(&tv);
+ (void)time(&tv);
+ srandom((int)tv);
- for (i=1; i<argc; i++) {
- switch(argv[i][1]) {
+ while ((ch = getopt(argc, argv, "l:w:")) != EOF)
+ switch((char)ch) {
+#ifdef notdef
- sscanf(argv[1], "-d%ld", &tv);
- case 'c': /* columns */
- ccnt = atoi(argv[i]+2);
break;
case 'l': /* length */
break;
case 'l': /* length */
- case 'h': /* height */
- case 'r': /* rows */
- lcnt = atoi(argv[i]+2);
- printf("bad option %s\n", argv[1]);
+ fputs("usage: snake [-d seed] [-w width] [-l length]\n", stderr);
+ exit(1);
- srand((int)tv);
- setbuf(stdout,_sobuf);
- i = ((lcnt < ccnt) ? lcnt : ccnt); /* min screen edge */
+ penalty = loot = 0;
+ getcap();
+
+ i = MIN(lcnt, ccnt);
- printf("Screen too small for a fair game\n");
- done();
+ cook();
+ printf("snake: screen too small for a fair game.\n");
+ exit(1);
/*
* chunk is the amount of money the user gets for each $.
* The formula below tries to be fair for various screen sizes.
/*
* chunk is the amount of money the user gets for each $.
* The formula below tries to be fair for various screen sizes.
putpad(TI); /* String to begin programs that use cm */
putpad(KS); /* Put terminal in keypad transmit mode */
putpad(TI); /* String to begin programs that use cm */
putpad(KS); /* Put terminal in keypad transmit mode */
- random(&finish);
- random(&you);
- random(&money);
- random(&snake[0]);
+ snrand(&finish);
+ snrand(&you);
+ snrand(&money);
+ snrand(&snake[0]);
if ((orig.sg_ospeed < B9600) ||
((! CM) && (! TA))) fast=0;
if ((orig.sg_ospeed < B9600) ||
((! CM) && (! TA))) fast=0;
lastc = c;
switch (c){
case CTRL('z'):
lastc = c;
switch (c){
case CTRL('z'):
suspend();
continue;
case EOT:
suspend();
continue;
case EOT:
length(moves);
logit("quit");
done();
length(moves);
logit("quit");
done();
- case '!':
- cook();
- putchar('\n');
- putchar(c);
- fflush(stdout);
- j = read(0,stri,BSIZE);
- stri[j] = 0;
- if (fork() == 0) {
- setuid(getuid());
- system(stri);
- } else
- wait(0);
- printf("READY?\n");
- fflush(stdout);
- raw();
- c = getchar();
- ungetc(c,stdin);
- putpad(KS);
- putpad(TI);
- point(&cursor,0,lcnt-1);
case CTRL('l'):
setup();
winnings(cashvalue);
case CTRL('l'):
setup();
winnings(cashvalue);
if(k < repeat)
pchar(&you,' ');
do {
if(k < repeat)
pchar(&you,' ');
do {
} while (money.col == finish.col && money.line == finish.line ||
money.col < 5 && money.line == 0 ||
money.col == you.col && money.line == you.line);
} while (money.col == finish.col && money.line == finish.line ||
money.col < 5 && money.line == 0 ||
money.col == you.col && money.line == you.line);
logit("won");
post(cashvalue,0);
length(moves);
logit("won");
post(cashvalue,0);
length(moves);
}
if (pushsnake())break;
}
}
if (pushsnake())break;
}
struct point p;
register int i;
struct point p;
register int i;
- sp->col = sp->line = -1; /* impossible */
- do {
- issame = 0;
- p.col = ((rand()>>8) & 0377)% ccnt;
- p.line = ((rand()>>8) & 0377)% lcnt;
+ for (;;) {
+ p.col = random() % ccnt;
+ p.line = random() % lcnt;
/* make sure it's not on top of something else */
/* make sure it's not on top of something else */
- if (p.line == 0 && p.col <5) issame++;
- if(same(&p, &you)) issame++;
- if(same(&p, &money)) issame++;
- if(same(&p, &finish)) issame++;
- for (i=0; i<5; i++)
- if(same(&p, &snake[i])) issame++;
-
- } while (issame);
+ if (p.line == 0 && p.col < 5)
+ continue;
+ if (same(&p, &you))
+ continue;
+ if (same(&p, &money))
+ continue;
+ if (same(&p, &finish))
+ continue;
+ for (i = 0; i < 5; i++)
+ if (same(&p, &snake[i]))
+ break;
+ if (i < 5)
+ continue;
+ break;
+ }
-busy()
-{
- FILE *pip, *popen();
- char c;
- int b,r;
- float a;
-
-#ifdef CHECKBUSY
- if (! strcmp (argval[0], "test")) return;
- if ((access(BUSY,1) != 0) || (pip = popen(BUSY,"r")) == NULL){
- printf("Sorry, no snake just now.\n");
- done();
- }
- fscanf(pip,"%d",&b);
- pclose(pip);
- if (b > 20) {
- printf("Sorry, the system is too heavily loaded right now.\n");
- done();
- }
- nice(b);
-#endif
-}
-
post(iscore, flag)
int iscore, flag;
{
post(iscore, flag)
int iscore, flag;
{
wt[i]=0;
if (d.col<0 || d.col>=ccnt || d.line<0 || d.line>=lcnt)
continue;
wt[i]=0;
if (d.col<0 || d.col>=ccnt || d.line<0 || d.line>=lcnt)
continue;
- if (d.line == 0 && d.col < 5) continue;
+ /*
+ * 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 (same(&d,&money)) continue;
if (same(&d,&finish)) continue;
wt[i]= i==w ? loot/10 : 1;
int w;{
struct point p;
int j;
int w;{
struct point p;
int j;
+ char *str;
+
+ snrand(&you);
point(&p,COLUMNS/2 - 8,LINES/2 - 1);
if (p.col < 0)
p.col = 0;
if (p.line < 0)
p.line = 0;
if (w) {
point(&p,COLUMNS/2 - 8,LINES/2 - 1);
if (p.col < 0)
p.col = 0;
if (p.line < 0)
p.line = 0;
if (w) {
- strcpy(str,"BONUS!!!");
loot = loot - penalty;
penalty = 0;
} else {
loot = loot - penalty;
penalty = 0;
} else {
- strcpy(str,"SPACE WARP!!!");
penalty += loot/PENALTY;
}
for(j=0;j<3;j++){
penalty += loot/PENALTY;
}
for(j=0;j<3;j++){
stretch(ps)
struct point *ps;{
struct point p;
stretch(ps)
struct point *ps;{
struct point p;
point(&p,you.col,you.line);
if(abs(ps->col-you.col) < 6){
if(you.line < ps->line){
point(&p,you.col,you.line);
if(abs(ps->col-you.col) < 6){
if(you.line < ps->line){
-#else
- sh = getenv("SHELL");
- if (sh == NULL)
- sh = "/bin/sh";
- system(sh);
-#endif
raw();
setup();
winnings(cashvalue);
raw();
setup();
winnings(cashvalue);