summaryrefslogtreecommitdiffstats
path: root/backgammon/common_source
diff options
context:
space:
mode:
authordholland <dholland@NetBSD.org>2012-10-13 19:19:38 +0000
committerdholland <dholland@NetBSD.org>2012-10-13 19:19:38 +0000
commit83f2ef7227030c4b88ec1006e49d9304aa6f7be0 (patch)
tree0f69865c3aa850090b15e95a6ae7a6d599c95e11 /backgammon/common_source
parentf70c1c7e831fd972c1f452e87709f5fe158e6f4d (diff)
downloadbsdgames-darwin-83f2ef7227030c4b88ec1006e49d9304aa6f7be0.tar.gz
bsdgames-darwin-83f2ef7227030c4b88ec1006e49d9304aa6f7be0.tar.zst
bsdgames-darwin-83f2ef7227030c4b88ec1006e49d9304aa6f7be0.zip
Pass the move info around instead of using it as a global.
Diffstat (limited to 'backgammon/common_source')
-rw-r--r--backgammon/common_source/allow.c19
-rw-r--r--backgammon/common_source/back.h37
-rw-r--r--backgammon/common_source/check.c28
-rw-r--r--backgammon/common_source/init.c16
-rw-r--r--backgammon/common_source/one.c38
-rw-r--r--backgammon/common_source/save.c10
-rw-r--r--backgammon/common_source/subs.c21
-rw-r--r--backgammon/common_source/table.c47
8 files changed, 105 insertions, 111 deletions
diff --git a/backgammon/common_source/allow.c b/backgammon/common_source/allow.c
index 2c0428d4..0cd24da2 100644
--- a/backgammon/common_source/allow.c
+++ b/backgammon/common_source/allow.c
@@ -1,4 +1,4 @@
-/* $NetBSD: allow.c,v 1.7 2012/10/13 18:44:14 dholland Exp $ */
+/* $NetBSD: allow.c,v 1.8 2012/10/13 19:19:38 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,18 +34,17 @@
#if 0
static char sccsid[] = "@(#)allow.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: allow.c,v 1.7 2012/10/13 18:44:14 dholland Exp $");
+__RCSID("$NetBSD: allow.c,v 1.8 2012/10/13 19:19:38 dholland Exp $");
#endif
#endif /* not lint */
#include "back.h"
int
-movallow(void)
+movallow(struct move *mm)
{
int i, m, iold;
int r;
- struct move *mm = &gm;
if (mm->d0)
mswap(mm);
@@ -62,11 +61,11 @@ movallow(void)
mm->g[i] = bar + cturn * mm->D1;
else
mm->g[i] = bar + cturn * mm->D0;
- if ((r = makmove(i)) != 0) {
+ if ((r = makmove(mm, i)) != 0) {
if (mm->d0 || m == 4)
break;
mswap(mm);
- movback(i);
+ movback(mm, i);
if (i > iold)
iold = i;
for (i = 0; i < 4; i++)
@@ -80,7 +79,7 @@ movallow(void)
if (i > iold)
iold = i;
if (m == 2 && i) {
- movback(i);
+ movback(mm, i);
mm->p[i--] = bar;
if (mm->p[i] != bar)
continue;
@@ -90,7 +89,7 @@ movallow(void)
if (mm->d0 || m == 4)
break;
mswap(mm);
- movback(i);
+ movback(mm, i);
for (i = 0; i < 4; i++)
mm->p[i] = bar;
i = 0;
@@ -106,9 +105,9 @@ movallow(void)
else
continue;
}
- if (board[mm->p[i]] * cturn > 0 && (r = makmove(i)) == 0)
+ if (board[mm->p[i]] * cturn > 0 && (r = makmove(mm, i)) == 0)
i++;
}
- movback(i);
+ movback(mm, i);
return (iold > i ? iold : i);
}
diff --git a/backgammon/common_source/back.h b/backgammon/common_source/back.h
index e008a6f9..7c333365 100644
--- a/backgammon/common_source/back.h
+++ b/backgammon/common_source/back.h
@@ -1,4 +1,4 @@
-/* $NetBSD: back.h,v 1.19 2012/10/13 18:44:14 dholland Exp $ */
+/* $NetBSD: back.h,v 1.20 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -46,8 +46,7 @@
#define rnum(r) (random()%r)
#define D0 dice[0]
#define D1 dice[1]
-#define mswap(m) {(m)->D0 ^= (m)->D1; (m)->D1 ^= (m)->D0; (m)->D0 ^= (m)->D1; (m)->d0 = 1-(m)->d0;}
-#define swap suck it
+#define mswap(m) {(m)->D0 ^= (m)->D1; (m)->D1 ^= (m)->D0; (m)->D0 ^= (m)->D1; (m)->d0 = 1-(m)->d0;}
struct move {
int dice[2]; /* value of dice */
@@ -59,8 +58,6 @@ struct move {
original position */
};
-extern struct move gm;
-
/*
*
* Some numerical conventions:
@@ -135,10 +132,10 @@ extern int begscr; /* 'beginning' of screen
(not including board) */
int addbuf(int);
-void backone(int);
+void backone(struct move *, int);
void buflush(void);
int canhit(int, int);
-int checkmove(int);
+int checkmove(struct move *, int);
void clear(void);
void clend(void);
void cline(void);
@@ -148,28 +145,28 @@ void errexit(const char *) __dead;
void fancyc(int);
void fboard(void);
void fixtty(struct termios *);
-void getarg(char ***);
+void getarg(struct move *, char ***);
int getcaps(const char *);
-void getmove(void);
+void getmove(struct move *);
void getout(int) __dead;
void gwrite(void);
void init(void);
int main(int, char *[]);
-int makmove(int);
-int movallow(void);
-void movback(int);
-void moverr(int);
-int movokay(int);
+int makmove(struct move *, int);
+int movallow(struct move *);
+void movback(struct move *, int);
+void moverr(struct move *, int);
+int movokay(struct move *, int);
void newpos(void);
void nexturn(void);
void odds(int, int, int);
-void proll(void);
-int quit(void);
+void proll(struct move *);
+int quit(struct move *);
int readc(void);
-void recover(const char *);
+void recover(struct move *, const char *);
void refresh(void);
-void roll(void);
-void save(int);
+void roll(struct move *);
+void save(struct move *, int);
int wrtext(const char *const *);
void wrboard(void);
void wrhit(int);
@@ -178,3 +175,5 @@ void writec(int);
void writel(const char *);
void wrscore(void);
int yorn(int);
+
+void move_init(struct move *);
diff --git a/backgammon/common_source/check.c b/backgammon/common_source/check.c
index 19f1c56d..c3f85251 100644
--- a/backgammon/common_source/check.c
+++ b/backgammon/common_source/check.c
@@ -1,4 +1,4 @@
-/* $NetBSD: check.c,v 1.7 2012/10/13 18:44:15 dholland Exp $ */
+/* $NetBSD: check.c,v 1.8 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,25 +34,24 @@
#if 0
static char sccsid[] = "@(#)check.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: check.c,v 1.7 2012/10/13 18:44:15 dholland Exp $");
+__RCSID("$NetBSD: check.c,v 1.8 2012/10/13 19:19:39 dholland Exp $");
#endif
#endif /* not lint */
#include "back.h"
void
-getmove(void)
+getmove(struct move *mm)
{
int i, c;
- struct move *mm = &gm;
c = 0;
for (;;) {
- i = checkmove(c);
+ i = checkmove(mm, c);
switch (i) {
case -1:
- if (movokay(mm->mvlim)) {
+ if (movokay(mm, mm->mvlim)) {
if (tflag)
curmove(20, 0);
else
@@ -91,12 +90,12 @@ getmove(void)
break;
case -3:
- if (quit())
+ if (quit(mm))
return;
}
if (!tflag)
- proll();
+ proll(mm);
else {
curmove(cturn == -1 ? 18 : 19, 39);
cline();
@@ -106,35 +105,34 @@ getmove(void)
}
int
-movokay(int mv)
+movokay(struct move *mm, int mv)
{
int i, m;
- struct move *mm = &gm;
if (mm->d0)
mswap(mm);
for (i = 0; i < mv; i++) {
if (mm->p[i] == mm->g[i]) {
- moverr(i);
+ moverr(mm, i);
curmove(20, 0);
writel("Attempt to move to same location.\n");
return (0);
}
if (cturn * (mm->g[i] - mm->p[i]) < 0) {
- moverr(i);
+ moverr(mm, i);
curmove(20, 0);
writel("Backwards move.\n");
return (0);
}
if (abs(board[bar]) && mm->p[i] != bar) {
- moverr(i);
+ moverr(mm, i);
curmove(20, 0);
writel("Men still on bar.\n");
return (0);
}
- if ((m = makmove(i))) {
- moverr(i);
+ if ((m = makmove(mm, i))) {
+ moverr(mm, i);
switch (m) {
case 1:
diff --git a/backgammon/common_source/init.c b/backgammon/common_source/init.c
index 5cdbfb36..648515f2 100644
--- a/backgammon/common_source/init.c
+++ b/backgammon/common_source/init.c
@@ -1,4 +1,4 @@
-/* $NetBSD: init.c,v 1.9 2012/10/13 18:44:15 dholland Exp $ */
+/* $NetBSD: init.c,v 1.10 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: init.c,v 1.9 2012/10/13 18:44:15 dholland Exp $");
+__RCSID("$NetBSD: init.c,v 1.10 2012/10/13 19:19:39 dholland Exp $");
#endif
#endif /* not lint */
@@ -93,3 +93,15 @@ int rscore;
int table[6][6];
int wscore;
struct termios old, noech, raw;
+
+void
+move_init(struct move *mm)
+{
+ mm->D0 = 0;
+ mm->D1 = 0;
+ mm->mvlim = 0;
+ mm->p[0] = mm->p[1] = mm->p[2] = mm->p[3] = mm->p[4] = 0;
+ mm->g[0] = mm->g[1] = mm->g[2] = mm->g[3] = mm->g[4] = 0;
+ mm->h[0] = mm->h[1] = mm->h[2] = mm->h[3] = 0;
+ mm->d0 = 0;
+}
diff --git a/backgammon/common_source/one.c b/backgammon/common_source/one.c
index 507af18f..3e55dd83 100644
--- a/backgammon/common_source/one.c
+++ b/backgammon/common_source/one.c
@@ -1,4 +1,4 @@
-/* $NetBSD: one.c,v 1.8 2012/10/13 18:44:15 dholland Exp $ */
+/* $NetBSD: one.c,v 1.9 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,43 +34,42 @@
#if 0
static char sccsid[] = "@(#)one.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: one.c,v 1.8 2012/10/13 18:44:15 dholland Exp $");
+__RCSID("$NetBSD: one.c,v 1.9 2012/10/13 19:19:39 dholland Exp $");
#endif
#endif /* not lint */
#include "back.h"
-static int checkd(int);
+static int checkd(struct move *, int);
static int last(void);
int
-makmove(int i)
+makmove(struct move *mm, int i)
{
int n, d;
int max;
- struct move *mm = &gm;
d = mm->d0;
n = abs(mm->g[i] - mm->p[i]);
max = (*offptr < 0 ? 7 : last());
if (board[mm->p[i]] * cturn <= 0)
- return (checkd(d) + 2);
+ return (checkd(mm, d) + 2);
if (mm->g[i] != home && board[mm->g[i]] * cturn < -1)
- return (checkd(d) + 3);
+ return (checkd(mm, d) + 3);
if (i || mm->D0 == mm->D1) {
if (n == max ? mm->D1 < n : mm->D1 != n)
- return (checkd(d) + 1);
+ return (checkd(mm, d) + 1);
} else {
if (n == max ? mm->D0 < n && mm->D1 < n : mm->D0 != n && mm->D1 != n)
- return (checkd(d) + 1);
+ return (checkd(mm, d) + 1);
if (n == max ? mm->D0 < n : mm->D0 != n) {
if (mm->d0)
- return (checkd(d) + 1);
+ return (checkd(mm, d) + 1);
mswap(mm);
}
}
if (mm->g[i] == home && *offptr < 0)
- return (checkd(d) + 4);
+ return (checkd(mm, d) + 4);
mm->h[i] = 0;
board[mm->p[i]] -= cturn;
if (mm->g[i] != home) {
@@ -98,10 +97,9 @@ makmove(int i)
}
void
-moverr(int i)
+moverr(struct move *mm, int i)
{
int j;
- struct move *mm = &gm;
if (tflag)
curmove(20, 0);
@@ -116,15 +114,13 @@ moverr(int i)
writec(',');
}
writel("... ");
- movback(i);
+ movback(mm, i);
}
static int
-checkd(int d)
+checkd(struct move *mm, int d)
{
- struct move *mm = &gm;
-
if (mm->d0 != d)
mswap(mm);
return (0);
@@ -142,19 +138,17 @@ last(void)
}
void
-movback(int i)
+movback(struct move *mm, int i)
{
int j;
for (j = i - 1; j >= 0; j--)
- backone(j);
+ backone(mm, j);
}
void
-backone(int i)
+backone(struct move *mm, int i)
{
- struct move *mm = &gm;
-
board[mm->p[i]] += cturn;
if (mm->g[i] != home) {
board[mm->g[i]] -= cturn;
diff --git a/backgammon/common_source/save.c b/backgammon/common_source/save.c
index 5cadf51c..462050af 100644
--- a/backgammon/common_source/save.c
+++ b/backgammon/common_source/save.c
@@ -1,4 +1,4 @@
-/* $NetBSD: save.c,v 1.15 2012/10/13 18:44:15 dholland Exp $ */
+/* $NetBSD: save.c,v 1.16 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: save.c,v 1.15 2012/10/13 18:44:15 dholland Exp $");
+__RCSID("$NetBSD: save.c,v 1.16 2012/10/13 19:19:39 dholland Exp $");
#endif
#endif /* not lint */
@@ -56,12 +56,11 @@ static const char cantrec[] = "Can't recover file: ";
static void norec(const char *) __dead;
void
-save(int n)
+save(struct move *mm, int n)
{
int fdesc;
char *fs;
char fname[50];
- struct move *mm = &gm;
if (n) {
if (tflag) {
@@ -146,10 +145,9 @@ save(int n)
}
void
-recover(const char *s)
+recover(struct move *mm, const char *s)
{
int fdesc;
- struct move *mm = &gm;
if ((fdesc = open(s, O_RDONLY)) == -1)
norec(s);
diff --git a/backgammon/common_source/subs.c b/backgammon/common_source/subs.c
index e73f5339..9a0a3723 100644
--- a/backgammon/common_source/subs.c
+++ b/backgammon/common_source/subs.c
@@ -1,4 +1,4 @@
-/* $NetBSD: subs.c,v 1.18 2012/10/13 18:44:15 dholland Exp $ */
+/* $NetBSD: subs.c,v 1.19 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)subs.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: subs.c,v 1.18 2012/10/13 18:44:15 dholland Exp $");
+__RCSID("$NetBSD: subs.c,v 1.19 2012/10/13 19:19:39 dholland Exp $");
#endif
#endif /* not lint */
@@ -153,10 +153,8 @@ writel(const char *l)
}
void
-proll(void)
+proll(struct move *mm)
{
- struct move *mm = &gm;
-
if (mm->d0)
mswap(mm);
if (cturn == 1)
@@ -229,7 +227,7 @@ gwrite(void)
}
int
-quit(void)
+quit(struct move *mm)
{
if (tflag) {
@@ -242,7 +240,7 @@ quit(void)
if (rfl) {
writel("Would you like to save this game?");
if (yorn(0))
- save(0);
+ save(mm, 0);
}
cturn = 0;
return (1);
@@ -307,7 +305,7 @@ nexturn(void)
}
void
-getarg(char ***arg)
+getarg(struct move *mm, char ***arg)
{
char **s;
@@ -383,13 +381,13 @@ getarg(char ***arg)
writel("No save file named\n");
getout(0);
} else
- recover(s[0]);
+ recover(mm, s[0]);
break;
}
s++;
}
if (s[0] != 0)
- recover(s[0]);
+ recover(mm, s[0]);
}
void
@@ -450,12 +448,11 @@ getout(int dummy __unused)
}
void
-roll(void)
+roll(struct move *mm)
{
char c;
int row;
int col;
- struct move *mm = &gm;
row = col = 0;
if (iroll) {
diff --git a/backgammon/common_source/table.c b/backgammon/common_source/table.c
index dfd382b2..c083b316 100644
--- a/backgammon/common_source/table.c
+++ b/backgammon/common_source/table.c
@@ -1,4 +1,4 @@
-/* $NetBSD: table.c,v 1.12 2012/10/13 18:44:15 dholland Exp $ */
+/* $NetBSD: table.c,v 1.13 2012/10/13 19:19:39 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: table.c,v 1.12 2012/10/13 18:44:15 dholland Exp $");
+__RCSID("$NetBSD: table.c,v 1.13 2012/10/13 19:19:39 dholland Exp $");
#endif
#endif /* not lint */
@@ -79,15 +79,14 @@ static const struct state atmata[] = {
{'\n', 0, -1}, {'.', 0, 0}
};
-static int dotable(int, int);
-static int rsetbrd(void);
+static int dotable(struct move *, int, int);
+static int rsetbrd(struct move *);
int
-checkmove(int ist)
+checkmove(struct move *mm, int ist)
{
int j, n;
char c;
- struct move *mm = &gm;
domove:
if (ist == 0) {
@@ -106,13 +105,13 @@ dochar:
if (c == 'S') {
raflag = 0;
- save(1);
+ save(mm, 1);
if (tflag) {
curmove(cturn == -1 ? 18 : 19, 39);
ist = -1;
goto domove;
} else {
- proll();
+ proll(mm);
ist = 0;
goto domove;
}
@@ -127,7 +126,7 @@ dochar:
writec(cin[ncin - 1]);
}
ncin--;
- n = rsetbrd();
+ n = rsetbrd(mm);
if (n == 0) {
n = -1;
if (tflag)
@@ -152,12 +151,12 @@ dochar:
} else {
writec('\\');
writec('\n');
- proll();
+ proll(mm);
ist = 0;
goto domove;
}
}
- n = dotable(c, ist);
+ n = dotable(mm, c, ist);
if (n >= 0) {
cin[ncin++] = c;
if (n > 2)
@@ -176,22 +175,22 @@ dochar:
if (n == -6) {
if (!tflag) {
- if (movokay(mvl + 1)) {
+ if (movokay(mm, mvl + 1)) {
wrboard();
- movback(mvl + 1);
+ movback(mm, mvl + 1);
}
- proll();
+ proll(mm);
writel("\t\tMove: ");
for (j = 0; j < ncin;)
writec(cin[j++]);
} else {
- if (movokay(mvl + 1)) {
+ if (movokay(mm, mvl + 1)) {
refresh();
- movback(mvl + 1);
+ movback(mm, mvl + 1);
} else
curmove(cturn == -1 ? 18 : 19, ncin + 39);
}
- ist = n = rsetbrd();
+ ist = n = rsetbrd(mm);
goto dochar;
}
if (n != -5)
@@ -201,11 +200,10 @@ dochar:
}
static int
-dotable(int c, int i)
+dotable(struct move *mm, int c, int i)
{
int a;
int test;
- struct move *mm = &gm;
test = (c == 'R');
@@ -217,10 +215,10 @@ dotable(int c, int i)
wrboard();
if (tflag) {
curmove(cturn == -1 ? 18 : 19, 0);
- proll();
+ proll(mm);
writel("\t\t");
} else
- proll();
+ proll(mm);
break;
case 2:
@@ -271,7 +269,7 @@ dotable(int c, int i)
curmove(cturn == -1 ? 18 : 19, 39);
} else {
writec('\n');
- proll();
+ proll(mm);
writel("\t\tMove: ");
}
break;
@@ -296,17 +294,16 @@ dotable(int c, int i)
}
static int
-rsetbrd(void)
+rsetbrd(struct move *mm)
{
int i, j, n;
- struct move *mm = &gm;
n = 0;
mvl = 0;
for (i = 0; i < 4; i++)
mm->p[i] = mm->g[i] = -1;
for (j = 0; j < ncin; j++)
- if ((n = dotable(cin[j], n)) < 0)
+ if ((n = dotable(mm, cin[j], n)) < 0)
return n;
return (n);
}