one.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* $NetBSD: one.c,v 1.5 2003/08/07 09:36:57 agc Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)one.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: one.c,v 1.5 2003/08/07 09:36:57 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "back.h"
  39. int
  40. makmove(i)
  41. int i;
  42. {
  43. int n, d;
  44. int max;
  45. d = d0;
  46. n = abs(g[i] - p[i]);
  47. max = (*offptr < 0 ? 7 : last());
  48. if (board[p[i]] * cturn <= 0)
  49. return (checkd(d) + 2);
  50. if (g[i] != home && board[g[i]] * cturn < -1)
  51. return (checkd(d) + 3);
  52. if (i || D0 == D1) {
  53. if (n == max ? D1 < n : D1 != n)
  54. return (checkd(d) + 1);
  55. } else {
  56. if (n == max ? D0 < n && D1 < n : D0 != n && D1 != n)
  57. return (checkd(d) + 1);
  58. if (n == max ? D0 < n : D0 != n) {
  59. if (d0)
  60. return (checkd(d) + 1);
  61. swap;
  62. }
  63. }
  64. if (g[i] == home && *offptr < 0)
  65. return (checkd(d) + 4);
  66. h[i] = 0;
  67. board[p[i]] -= cturn;
  68. if (g[i] != home) {
  69. if (board[g[i]] == -cturn) {
  70. board[home] -= cturn;
  71. board[g[i]] = 0;
  72. h[i] = 1;
  73. if (abs(bar - g[i]) < 7) {
  74. (*inopp)--;
  75. if (*offopp >= 0)
  76. *offopp -= 15;
  77. }
  78. }
  79. board[g[i]] += cturn;
  80. if (abs(home - g[i]) < 7 && abs(home - p[i]) > 6) {
  81. (*inptr)++;
  82. if (*inptr + *offptr == 0)
  83. *offptr += 15;
  84. }
  85. } else {
  86. (*offptr)++;
  87. (*inptr)--;
  88. }
  89. return (0);
  90. }
  91. void
  92. moverr(i)
  93. int i;
  94. {
  95. int j;
  96. if (tflag)
  97. curmove(20, 0);
  98. else
  99. writec('\n');
  100. writel("Error: ");
  101. for (j = 0; j <= i; j++) {
  102. wrint(p[j]);
  103. writec('-');
  104. wrint(g[j]);
  105. if (j < i)
  106. writec(',');
  107. }
  108. writel("... ");
  109. movback(i);
  110. }
  111. int
  112. checkd(d)
  113. int d;
  114. {
  115. if (d0 != d)
  116. swap;
  117. return (0);
  118. }
  119. int
  120. last()
  121. {
  122. int i;
  123. for (i = home - 6 * cturn; i != home; i += cturn)
  124. if (board[i] * cturn > 0)
  125. return (abs(home - i));
  126. return (-1);
  127. }
  128. void
  129. movback(i)
  130. int i;
  131. {
  132. int j;
  133. for (j = i - 1; j >= 0; j--)
  134. backone(j);
  135. }
  136. void
  137. backone(i)
  138. int i;
  139. {
  140. board[p[i]] += cturn;
  141. if (g[i] != home) {
  142. board[g[i]] -= cturn;
  143. if (abs(g[i] - home) < 7 && abs(p[i] - home) > 6) {
  144. (*inptr)--;
  145. if (*inptr + *offptr < 15 && *offptr >= 0)
  146. *offptr -= 15;
  147. }
  148. } else {
  149. (*offptr)--;
  150. (*inptr)++;
  151. }
  152. if (h[i]) {
  153. board[home] += cturn;
  154. board[g[i]] = -cturn;
  155. if (abs(bar - g[i]) < 7) {
  156. (*inopp)++;
  157. if (*inopp + *offopp == 0)
  158. *offopp += 15;
  159. }
  160. }
  161. }