xref: /dragonfly/games/backgammon/common_source/check.c (revision 4318c66eac379e15105fe145d406dfef81b795f6)
1 /*        @(#)check.c         8.1 (Berkeley) 5/31/93                                      */
2 /*        $NetBSD: check.c,v 1.9 2019/02/18 19:35:44 christos Exp $   */
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *        The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "back.h"
34 
35 void
getmove(struct move * mm)36 getmove(struct move *mm)
37 {
38           int     i, c;
39 
40           c = 0;
41           for (;;) {
42                     i = checkmove(mm, c);
43 
44                     switch (i) {
45                     case -1:
46                               if (movokay(mm, mm->mvlim)) {
47                                         if (tflag)
48                                                   curmove(20, 0);
49                                         else
50                                                   writec('\n');
51                                         for (i = 0; i < mm->mvlim; i++)
52                                                   if (mm->h[i])
53                                                             wrhit(mm->g[i]);
54                                         nexturn();
55                                         if (*offopp == 15)
56                                                   cturn *= -2;
57                                         if (tflag && pnum)
58                                                   bflag = pnum;
59                                         return;
60                               }
61                               /*FALLTHROUGH*/
62                     case -4:
63                     case 0:
64                               if (tflag)
65                                         refresh();
66                               if (i != 0 && i != -4)
67                                         break;
68                               if (tflag)
69                                         curmove(20, 0);
70                               else
71                                         writec('\n');
72                               writel(*Colorptr);
73                               if (i == -4)
74                                         writel(" must make ");
75                               else
76                                         writel(" can only make ");
77                               writec(mm->mvlim + '0');
78                               writel(" move");
79                               if (mm->mvlim > 1)
80                                         writec('s');
81                               writec('.');
82                               writec('\n');
83                               break;
84 
85                     case -3:
86                               if (quit(mm))
87                                         return;
88                     }
89 
90                     if (!tflag)
91                               proll(mm);
92                     else {
93                               curmove(cturn == -1 ? 18 : 19, 39);
94                               cline();
95                               c = -1;
96                     }
97           }
98 }
99 
100 int
movokay(struct move * mm,int mv)101 movokay(struct move *mm, int mv)
102 {
103           int     i, m;
104 
105           if (mm->d0)
106                     mswap(mm);
107 
108           for (i = 0; i < mv; i++) {
109                     if (mm->p[i] == mm->g[i]) {
110                               moverr(mm, i);
111                               curmove(20, 0);
112                               writel("Attempt to move to same location.\n");
113                               return (0);
114                     }
115                     if (cturn * (mm->g[i] - mm->p[i]) < 0) {
116                               moverr(mm, i);
117                               curmove(20, 0);
118                               writel("Backwards move.\n");
119                               return (0);
120                     }
121                     if (abs(board[bar]) && mm->p[i] != bar) {
122                               moverr(mm, i);
123                               curmove(20, 0);
124                               writel("Men still on bar.\n");
125                               return (0);
126                     }
127                     if ((m = makmove(mm, i))) {
128                               moverr(mm, i);
129                               switch (m) {
130 
131                               case 1:
132                                         writel("Move not rolled.\n");
133                                         break;
134 
135                               case 2:
136                                         writel("Bad starting position.\n");
137                                         break;
138 
139                               case 3:
140                                         writel("Destination occupied.\n");
141                                         break;
142 
143                               case 4:
144                                         writel("Can't remove men yet.\n");
145                               }
146                               return (0);
147                     }
148           }
149           return (1);
150 }
151