示例#1
0
/*
 * mvnum is number of move (rel zero)
 * see if swapped also tested
 */
static void
trymove(int mvnum, int swapped)
{
    int pos;		/* position on board */
    int rval;		/* value of roll */

    /* if recursed through all dice values, compare move */
    if (mvnum == mvlim) {
        binsert(bsave());
        return;
    }

    /* make sure dice in always same order */
    if (d0 == swapped)
        swap;
    /* choose value for this move */
    rval = dice[mvnum != 0];

    /* find all legitimate moves */
    for (pos = bar; pos != home; pos += cturn) {
        /* fix order of dice */
        if (d0 == swapped)
            swap;
        /* break if stuck on bar */
        if (board[bar] != 0 && pos != bar)
            break;
        /* on to next if not occupied */
        if (board[pos] * cturn <= 0)
            continue;
        /* set up arrays for move */
        p[mvnum] = pos;
        g[mvnum] = pos + rval * cturn;
        if (g[mvnum] * cturn >= home) {
            if (*offptr < 0)
                break;
            g[mvnum] = home;
        }
        /* try to move */
        if (makmove(mvnum))
            continue;
        else
            trymove(mvnum + 1, 2);
        /* undo move to try another */
        backone(mvnum);
    }

    /* swap dice and try again */
    if ((!swapped) && D0 != D1)
        trymove(0, 1);
}
示例#2
0
int
movokay(int mv)
{
	int     i, m;

	if (d0)
		swap;

	for (i = 0; i < mv; i++) {
		if (p[i] == g[i]) {
			moverr(i);
			mvaddstr(20, 0, "Attempt to move to same location.\n");
			return(0);
		}
		if (cturn * (g[i] - p[i]) < 0) {
			moverr(i);
			mvaddstr(20, 0, "Backwards move.\n");
			return(0);
		}
		if (abs(board[bar]) && p[i] != bar) {
			moverr(i);
			mvaddstr(20, 0, "Men still on bar.\n");
			return(0);
		}
		if ((m = makmove(i))) {
			moverr(i);
			switch (m) {
			case 1:
				addstr("Move not rolled.\n");
				break;

			case 2:
				addstr("Bad starting position.\n");
				break;

			case 3:
				addstr("Destination occupied.\n");
				break;

			case 4:
				addstr("Can't remove men yet.\n");
			}
			return(0);
		}
	}
	return(1);
}
示例#3
0
tutor ()  {
	register int	i, j;

	i = 0;
	begscr = 18;
	cturn = -1;
	home = 0;
	bar = 25;
	inptr = &in[0];
	inopp = &in[1];
	offptr = &off[0];
	offopp = &off[1];
	Colorptr = &color[0];
	colorptr = &color[2];
	colen = 5;
	wrboard();

	while (1)  {
		if (! brdeq(test[i].brd,board))  {
			if (tflag && curr == 23)
				curmove (18,0);
			writel (better);
			nexturn();
			movback (mvlim);
			if (tflag)  {
				refresh();
				clrest ();
			}
			if ((! tflag) || curr == 19)  {
				proll();
				writec ('\t');
			}
			else
				curmove (curr > 19? curr-2: curr+4,25);
			getmove();
			if (cturn == 0)
				leave();
			continue;
		}
		if (tflag)
			curmove (18,0);
		text (*test[i].com);
		if (! tflag)
			writec ('\n');
		if (i == maxmoves)
			break;
		D0 = test[i].roll1;
		D1 = test[i].roll2;
		d0 = 0;
		mvlim = 0;
		for (j = 0; j < 4; j++)  {
			if (test[i].mp[j] == test[i].mg[j])
				break;
			p[j] = test[i].mp[j];
			g[j] = test[i].mg[j];
			mvlim++;
		}
		if (mvlim)
			for (j = 0; j < mvlim; j++)
				if (makmove(j))
					writel ("AARGH!!!\n");
		if (tflag)
			refresh();
		nexturn();
		D0 = test[i].new1;
		D1 = test[i].new2;
		d0 = 0;
		i++;
		mvlim = movallow();
		if (mvlim)  {
			if (tflag)
				clrest();
			proll();
			writec('\t');
			getmove();
			if (tflag)
				refresh();
			if (cturn == 0)
				leave();
		}
	}
	leave();
}
示例#4
0
/* zero if first move */
void
move(int okay)
{
    int i;			/* index */
    int l;			/* last man */

    l = 0;
    if (okay) {
        /* see if comp should double */
        if (gvalue < 64 && dlast != cturn && dblgood()) {
            writel(*Colorptr);
            dble();		/* double */
            /* return if declined */
            if (cturn != 1 && cturn != -1)
                return;
        }
        roll();
    }

    race = 0;
    for (i = 0; i < 26; i++) {
        if (board[i] < 0)
            l = i;
    }
    for (i = 0; i < l; i++) {
        if (board[i] > 0)
            break;
    }
    if (i == l)
        race = 1;

    /* print roll */
    if (tflag)
        curmove(cturn == -1 ? 18 : 19, 0);
    writel(*Colorptr);
    writel(" rolls ");
    writec(D0 + '0');
    writec(' ');
    writec(D1 + '0');
    /* make tty interruptable while thinking */
    if (tflag)
        cline();
    fixtty(noech);

    /* find out how many moves */
    mvlim = movallow();
    if (mvlim == 0) {
        writel(" but cannot use it.\n");
        nexturn();
        fixtty(raw);
        return;
    }

    /* initialize */
    for (i = 0; i < 4; i++)
        cp[i] = cg[i] = 0;

    /* strategize */
    trymove(0, 0);
    pickmove();

    /* print move */
    writel(" and moves ");
    for (i = 0; i < mvlim; i++) {
        if (i > 0)
            writec(',');
        wrint(p[i] = cp[i]);
        writec('-');
        wrint(g[i] = cg[i]);
        makmove(i);
    }
    writec('.');

    /* print blots hit */
    if (tflag)
        curmove(20, 0);
    else
        writec('\n');
    for (i = 0; i < mvlim; i++)
        if (h[i])
            wrhit(g[i]);
    /* get ready for next move */
    nexturn();
    if (!okay) {
        buflush();
        sleep(3);
    }
    fixtty(raw);		/* no more tty interrupt */
}
示例#5
0
int
movallow()
{
	int     i, m, iold, r;

	if (d0)
		swap;
	m = (D0 == D1 ? 4 : 2);
	for (i = 0; i < 4; i++)
		p[i] = bar;
	i = iold = 0;
	while (i < m) {
		if (*offptr == 15)
			break;
		h[i] = 0;
		if (board[bar]) {
			if (i == 1 || m == 4)
				g[i] = bar + cturn * D1;
			else
				g[i] = bar + cturn * D0;
			if ((r = makmove(i))) {
				if (d0 || m == 4)
					break;
				swap;
				movback(i);
				if (i > iold)
					iold = i;
				for (i = 0; i < 4; i++)
					p[i] = bar;
				i = 0;
			} else
				i++;
			continue;
		}
		if ((p[i] += cturn) == home) {
			if (i > iold)
				iold = i;
			if (m == 2 && i) {
				movback(i);
				p[i--] = bar;
				if (p[i] != bar)
					continue;
				else
					break;
			}
			if (d0 || m == 4)
				break;
			swap;
			movback(i);
			for (i = 0; i < 4; i++)
				p[i] = bar;
			i = 0;
			continue;
		}
		if (i == 1 || m == 4)
			g[i] = p[i] + cturn * D1;
		else
			g[i] = p[i] + cturn * D0;
		if (g[i] * cturn > home) {
			if (*offptr >= 0)
				g[i] = home;
			else
				continue;
		}
		if (board[p[i]] * cturn > 0 && (r = makmove(i)) == 0)
			i++;
	}
	movback(i);
	return (iold > i ? iold : i);
}
示例#6
0
文件: move.c 项目: radixo/openbsd-src
void
domove(int okay)
{
	int     i;		/* index */
	int     l = 0;		/* last man */

	bestmove = -9999999.;
	if (okay) {
	    	/* see if comp should double */
		if (dflag && gvalue < 64 && dlast != cturn && dblgood()) {
			addstr(*Colorptr);
			dble();	/* double */
			/* return if declined */
			if (cturn != 1 && cturn != -1)
				return;
		}
		roll();
	}
	race = 0;
	for (i = 0; i < 26; i++) {
		if (board[i] < 0)
			l = i;
	}
	for (i = 0; i < l; i++) {
		if (board[i] > 0)
			break;
	}
	if (i == l)
		race = 1;

	/* print roll */
	move(cturn == -1 ? 18 : 19, 0);
	printw("%s rolls %d %d", *Colorptr, D0, D1);
	clrtoeol();

	/* find out how many moves */
	mvlim = movallow();
	if (mvlim == 0) {
		addstr(" but cannot use it.\n");
		nexturn();
		return;
	}

	/* initialize */
	for (i = 0; i < 4; i++)
		cp[i] = cg[i] = 0;

	/* strategize */
	trymove(0, 0);
	pickmove();

	/* print move */
	addstr(" and moves ");
	for (i = 0; i < mvlim; i++) {
		if (i > 0)
			addch(',');
		printw("%d-%d", p[i] = cp[i], g[i] = cg[i]);
		makmove(i);
	}
	addch('.');

	/* print blots hit */
	move(20, 0);
	for (i = 0; i < mvlim; i++)
		if (h[i])
			wrhit(g[i]);
	/* get ready for next move */
	nexturn();
	if (!okay) {
		refresh();
		sleep(3);
	}
}
示例#7
0
文件: tutor.c 项目: aalm/obsd-src
void
tutor(void)
{
	int     i, j, k;
	int     wrongans;

	wrongans = 0;
	i = 0;
	begscr = 18;
	cturn = -1;
	home = 0;
	bar = 25;
	inptr = &in[0];
	inopp = &in[1];
	offptr = &off[0];
	offopp = &off[1];
	Colorptr = &color[0];
	colorptr = &color[2];
	colen = 5;
	wrboard();

	while (1) {
		if (!brdeq(test[i].brd, board)) {
			wrongans++;
			move(18, 0);
			if (wrongans >= 3) {
				wrongans = 0;
				text(*test[i].ans);
				memcpy(board,test[i].brd,26*sizeof(int));
				/* and have to fix *inptr, *offptr; player is red (+ve) */
				k = 0;
				for (j = 19; j < 26; j++)
					k += (board[j] > 0 ? board[j] : 0);
				*inopp = k;
				for (j = 0; j < 19; j++)
					k += (board[j] > 0 ? board[j] : 0);
				*offopp = k - 30;  /* -15 at start */
				moveplayers();
				clrest();
			} else {
				addstr(better);
				nexturn();
				movback(mvlim);
				moveplayers();
				clrest();
				getyx(stdscr, j, k);
				if (j == 19) {
					proll();
					addch('\t');
				} else
					move(j > 19 ? j - 2 : j + 4, 25);
				getmove();
				if (cturn == 0)
					leave();
				continue;
			}
		} else
			wrongans = 0;
		move(18, 0);
		text(*test[i].com);
		move(19, 0);
		if (i == maxmoves)
			break;
		D0 = test[i].roll1;
		D1 = test[i].roll2;
		d0 = 0;
		mvlim = 0;
		for (j = 0; j < 4; j++) {
			if (test[i].mp[j] == test[i].mg[j])
				break;
			p[j] = test[i].mp[j];
			g[j] = test[i].mg[j];
			mvlim++;
		}
		if (mvlim)
			for (j = 0; j < mvlim; j++)
				if (makmove(j))
					addstr("AARGH!!!\n");
		moveplayers();
		nexturn();
		D0 = test[i].new1;
		D1 = test[i].new2;
		d0 = 0;
		i++;
		mvlim = movallow();
		if (mvlim) {
			clrest();
			proll();
			addch('\t');
			getmove();
			moveplayers();
			if (cturn == 0)
				leave();
		}
	}
	leave();
}
示例#8
0
/* zero if first move */
void
move(struct move *mm, int okay)
{
	int     i;		/* index */
	int     l;		/* last man */

	l = 0;
	if (okay) {
		/* see if comp should double */
		if (gvalue < 64 && dlast != cturn && dblgood()) {
			writel(*Colorptr);
			dble();	/* double */
			/* return if declined */
			if (cturn != 1 && cturn != -1)
				return;
		}
		roll(mm);
	}
	race = 0;
	for (i = 0; i < 26; i++) {
		if (board[i] < 0)
			l = i;
	}
	for (i = 0; i < l; i++) {
		if (board[i] > 0)
			break;
	}
	if (i == l)
		race = 1;

	/* print roll */
	if (tflag)
		curmove(cturn == -1 ? 18 : 19, 0);
	writel(*Colorptr);
	writel(" rolls ");
	writec(mm->D0 + '0');
	writec(' ');
	writec(mm->D1 + '0');
	/* make tty interruptable while thinking */
	if (tflag)
		cline();
	fixtty(&noech);

	/* find out how many moves */
	mm->mvlim = movallow(mm);
	if (mm->mvlim == 0) {
		writel(" but cannot use it.\n");
		nexturn();
		fixtty(&raw);
		return;
	}
	/* initialize */
	for (i = 0; i < 4; i++)
		cp[i] = cg[i] = 0;

	/* strategize */
	trymove(mm, 0, 0);
	pickmove(mm);

	/* print move */
	writel(" and moves ");
	for (i = 0; i < mm->mvlim; i++) {
		if (i > 0)
			writec(',');
		wrint(mm->p[i] = cp[i]);
		writec('-');
		wrint(mm->g[i] = cg[i]);
		makmove(mm, i);

		/*
		 * This assertion persuades gcc 4.5 that the loop
		 * doesn't result in signed overflow of i. mvlim
		 * isn't, or at least shouldn't be, changed by makmove
		 * at all.
		 */
		assert(mm->mvlim >= 0 && mm->mvlim <= 5);
	}
	writec('.');

	/* print blots hit */
	if (tflag)
		curmove(20, 0);
	else
		writec('\n');
	for (i = 0; i < mm->mvlim; i++)
		if (mm->h[i])
			wrhit(mm->g[i]);
	/* get ready for next move */
	nexturn();
	if (!okay) {
		buflush();
		sleep(3);
	}
	fixtty(&raw);		/* no more tty interrupt */
}