1*4105daf3Sdholland /* $NetBSD: one.c,v 1.9 2012/10/13 19:19:39 dholland Exp $ */
2101657d1Scgd
361f28255Scgd /*
4101657d1Scgd * Copyright (c) 1980, 1993
5101657d1Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
3216641b76Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34101657d1Scgd #if 0
35101657d1Scgd static char sccsid[] = "@(#)one.c 8.1 (Berkeley) 5/31/93";
36101657d1Scgd #else
37*4105daf3Sdholland __RCSID("$NetBSD: one.c,v 1.9 2012/10/13 19:19:39 dholland Exp $");
38101657d1Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #include "back.h"
4261f28255Scgd
43*4105daf3Sdholland static int checkd(struct move *, int);
449795f61aSdholland static int last(void);
459795f61aSdholland
4616641b76Slukem int
makmove(struct move * mm,int i)47*4105daf3Sdholland makmove(struct move *mm, int i)
4861f28255Scgd {
4916641b76Slukem int n, d;
5061f28255Scgd int max;
5161f28255Scgd
528733c61fSdholland d = mm->d0;
538733c61fSdholland n = abs(mm->g[i] - mm->p[i]);
5461f28255Scgd max = (*offptr < 0 ? 7 : last());
558733c61fSdholland if (board[mm->p[i]] * cturn <= 0)
56*4105daf3Sdholland return (checkd(mm, d) + 2);
578733c61fSdholland if (mm->g[i] != home && board[mm->g[i]] * cturn < -1)
58*4105daf3Sdholland return (checkd(mm, d) + 3);
598733c61fSdholland if (i || mm->D0 == mm->D1) {
608733c61fSdholland if (n == max ? mm->D1 < n : mm->D1 != n)
61*4105daf3Sdholland return (checkd(mm, d) + 1);
6261f28255Scgd } else {
638733c61fSdholland if (n == max ? mm->D0 < n && mm->D1 < n : mm->D0 != n && mm->D1 != n)
64*4105daf3Sdholland return (checkd(mm, d) + 1);
658733c61fSdholland if (n == max ? mm->D0 < n : mm->D0 != n) {
668733c61fSdholland if (mm->d0)
67*4105daf3Sdholland return (checkd(mm, d) + 1);
688733c61fSdholland mswap(mm);
6961f28255Scgd }
7061f28255Scgd }
718733c61fSdholland if (mm->g[i] == home && *offptr < 0)
72*4105daf3Sdholland return (checkd(mm, d) + 4);
738733c61fSdholland mm->h[i] = 0;
748733c61fSdholland board[mm->p[i]] -= cturn;
758733c61fSdholland if (mm->g[i] != home) {
768733c61fSdholland if (board[mm->g[i]] == -cturn) {
7761f28255Scgd board[home] -= cturn;
788733c61fSdholland board[mm->g[i]] = 0;
798733c61fSdholland mm->h[i] = 1;
808733c61fSdholland if (abs(bar - mm->g[i]) < 7) {
8161f28255Scgd (*inopp)--;
8261f28255Scgd if (*offopp >= 0)
8361f28255Scgd *offopp -= 15;
8461f28255Scgd }
8561f28255Scgd }
868733c61fSdholland board[mm->g[i]] += cturn;
878733c61fSdholland if (abs(home - mm->g[i]) < 7 && abs(home - mm->p[i]) > 6) {
8861f28255Scgd (*inptr)++;
8961f28255Scgd if (*inptr + *offptr == 0)
9061f28255Scgd *offptr += 15;
9161f28255Scgd }
9261f28255Scgd } else {
9361f28255Scgd (*offptr)++;
9461f28255Scgd (*inptr)--;
9561f28255Scgd }
9661f28255Scgd return (0);
9761f28255Scgd }
9861f28255Scgd
9916641b76Slukem void
moverr(struct move * mm,int i)100*4105daf3Sdholland moverr(struct move *mm, int i)
10161f28255Scgd {
10216641b76Slukem int j;
10361f28255Scgd
10461f28255Scgd if (tflag)
10561f28255Scgd curmove(20, 0);
10661f28255Scgd else
10761f28255Scgd writec('\n');
10861f28255Scgd writel("Error: ");
10961f28255Scgd for (j = 0; j <= i; j++) {
1108733c61fSdholland wrint(mm->p[j]);
11161f28255Scgd writec('-');
1128733c61fSdholland wrint(mm->g[j]);
11361f28255Scgd if (j < i)
11461f28255Scgd writec(',');
11561f28255Scgd }
11661f28255Scgd writel("... ");
117*4105daf3Sdholland movback(mm, i);
11861f28255Scgd }
11961f28255Scgd
12061f28255Scgd
1219795f61aSdholland static int
checkd(struct move * mm,int d)122*4105daf3Sdholland checkd(struct move *mm, int d)
12361f28255Scgd {
1248733c61fSdholland if (mm->d0 != d)
1258733c61fSdholland mswap(mm);
12661f28255Scgd return (0);
12761f28255Scgd }
12861f28255Scgd
1299795f61aSdholland static int
last(void)130fcb317eeSjmc last(void)
13116641b76Slukem {
13216641b76Slukem int i;
13361f28255Scgd
13461f28255Scgd for (i = home - 6 * cturn; i != home; i += cturn)
13561f28255Scgd if (board[i] * cturn > 0)
13661f28255Scgd return (abs(home - i));
13716641b76Slukem return (-1);
13861f28255Scgd }
13961f28255Scgd
14016641b76Slukem void
movback(struct move * mm,int i)141*4105daf3Sdholland movback(struct move *mm, int i)
14261f28255Scgd {
14316641b76Slukem int j;
14461f28255Scgd
14561f28255Scgd for (j = i - 1; j >= 0; j--)
146*4105daf3Sdholland backone(mm, j);
14761f28255Scgd }
14861f28255Scgd
14916641b76Slukem void
backone(struct move * mm,int i)150*4105daf3Sdholland backone(struct move *mm, int i)
15161f28255Scgd {
1528733c61fSdholland board[mm->p[i]] += cturn;
1538733c61fSdholland if (mm->g[i] != home) {
1548733c61fSdholland board[mm->g[i]] -= cturn;
1558733c61fSdholland if (abs(mm->g[i] - home) < 7 && abs(mm->p[i] - home) > 6) {
15661f28255Scgd (*inptr)--;
15761f28255Scgd if (*inptr + *offptr < 15 && *offptr >= 0)
15861f28255Scgd *offptr -= 15;
15961f28255Scgd }
16061f28255Scgd } else {
16161f28255Scgd (*offptr)--;
16261f28255Scgd (*inptr)++;
16361f28255Scgd }
1648733c61fSdholland if (mm->h[i]) {
16561f28255Scgd board[home] += cturn;
1668733c61fSdholland board[mm->g[i]] = -cturn;
1678733c61fSdholland if (abs(bar - mm->g[i]) < 7) {
16861f28255Scgd (*inopp)++;
16961f28255Scgd if (*inopp + *offopp == 0)
17061f28255Scgd *offopp += 15;
17161f28255Scgd }
17261f28255Scgd }
17361f28255Scgd }
174