Avoid doing OpenDevice on a handler.
[AROS-Contrib.git] / rexx / progs / awari.r
blob8c7e5d84c5dd391477888f476c70c4ddca71a089
1 /*
2 A W A R I o o
3 ~~~~~~~~~~~~~~~~~ ____oo_
4 1989 /|| |\
5 My Side by BNV || |
6 `.___.'
7 | 6 | 5 | 4 | 3 | 2 | 1 | MARMITA
8 |ooo|ooo|ooo|ooo|ooo|ooo|
9 My home | | | | | | | Your home
10 |ooo|ooo|ooo|ooo|ooo|ooo|
11 | 1 | 2 | 3 | 4 | 5 | 6 |
13 Your Side
15 Awari is an African game played with seven sticks and
16 thirty-six stones or beans laid out as shown above. The
17 board is divided into six compartments or pits on each side.
18 In addition, there are two special HOME pits at the end.
20 A move is made by taking all the beans from any
21 (non-empty) pit on your own side. Starting from the pit to
22 the right of this one, these beans are "sown" one in each
23 pit working around the board anticlockwise.
25 A turn consists of one or two moves. If the last bean of
26 your move is sown in your own home you may take a second
27 move.
29 If the last bean is sown in a move lands in an empty pit,
30 provided that the opposite pit is no empty, all the beans in
31 the opposite pit, together with the last bean sown are
32 "captured" and moved to the players home.
34 When either side is empty, the game is finished. The
35 player with the most beans in his home has won.
37 In the computer version, the board is printed as 14
38 numbers representing the 14 pits.
40 3 3 3 3 3 3
41 0 0
42 3 3 3 3 3 3
44 The pits on your (lower) side are numbered 1-6 from left
45 to right. The pits on my side (computer) are numbered from
46 my left (your right).
48 To make a move you type in the number of the pit.
50 If the last bean lands in your home the computer types
51 "Again?" and then you type in the second move.
53 If you want to terminate the game just type "END" in your
54 move.
56 The computer move is typed followed by a diagram of the
57 board in its new state. The computer allways offers you the
58 first move. This consider to be a slight advantage.
60 There is a learning mechanism in the program that causes
61 the play of the computer to improve as it plays more games.
63 This version of Awari is adopted from one originally
64 written by Geoff Wyvill of Bradford, Yorksire, England and
65 translated to IBM REXX by Bill N. Vlachoudis.
66 E-mail addr: V.Vlachoudis@cern.ch
67 bnv@nisyros.physics.auth.gr
69 Arg Args .
70 if Args = '?' then Call Help
72 Say
73 Say Center('A W A R I',79)
74 Say Center('~~~~~~~~~~~~~~~~~',79)
75 Say ; Say Center('by V.Vlachoudis@cern.ch',79)
76 Say ;
77 Say Center('For instructions about the game enter "awari ?" in cmd line.',79)
78 Say Center('or simply enter "?" in your move.',80)
80 n = 0
81 awarif = open("awari.var","r")
82 If awarif ~= -1 then Do
83 Do until eof(awarif)
84 interpret read(awarif)
85 End
86 Call close awarif
87 End
88 /* // 'GLOBALV SELECT AWARI STACK N'
89 Pull N
90 if N = '';then N = 0
91 ;else Do i = 0 to N-1
92 'GLOBALV SELECT AWARI GET F.'i
93 End
95 Begin:
96 E = 0 ; B. = 3 ; C = 0 ; F.n = 0 ;
97 Do i = 0 to 13
98 b.i = 3
99 End
100 B.6 = 0 ; B.13 = 0
102 Do forever /* Main Loop */
103 Call Print
104 Call Input "Your move?"
105 if ~E then Leave
106 if M = H then Call Input "Again?"
107 if ~E then Leave
108 Move = "My move is"
109 Call CPU_move
110 if ~E then Leave
111 if M = H then Do
112 Move = Move || ","
113 Call Cpu_move
114 if ~E then Leave
116 Say; Say Move
119 /* Game Over */
120 if H = 13 then Do
121 Say; Say Move
122 Call Print
124 Say ; Say "Game Over"
125 D = B.6 - B.13
126 if D < 0 ;then Sa = "I win by "0-D" point"
127 else
128 if D = 0 ;then Do
129 N = N + 1
130 Sa = "Drawn game"
132 else Do
133 N = N + 1
134 Sa = "You win by "D" point"
136 if abs(D)=1 | D = 0 ;then Say Sa'.'
137 ;else Say Sa's.'
138 Signal Begin
140 /* Get input */
141 Input:
142 Parse Arg Text
143 Valid = 0
144 Do until Valid /* Test if input is Valid */
145 Call write ,Text' '
146 Parse UPPER External M .
147 Select
148 When M = '?' then Do
149 Call Help
150 Call Print
152 When M = 'END' then Do
153 Say "Thanks for the game."
154 Say "Have a nice day."
155 /* Save everything we done till now */
156 awarif = Open("awari.var","w")
157 If awarif ~= -1 then Do
158 Call write awarif,"N =" N
159 Call write awarif
160 Call write awarif, vardump('F.')
161 Call close awarif
163 /*// 'GLOBALV SELECT AWARI SETP N 'N
164 Do i = 0 to N-1
165 'GLOBALV SELECT AWARI SETP F.'I F.I
166 End */
167 /* O.k. now Exit from the program */
168 Exit 0
170 When ~Datatype(M,'NUM') then Say "Not valid move '"M"'"
171 Otherwise Do
172 M = Trunc(M) ; M = M - 1
173 if M<0 | M>5 | B.M=0 ;then Say "Illegal move '"M+1"'"
174 ;else Valid = 1
176 End /* Of select */
179 H = 6 /* O.k. it is valid */
180 Call MakeMove /* Make the move he asked */
181 Call Print /* Print the board */
182 Return
184 MakeMove:
185 K = M
186 Call Moveit M
187 E = 0
188 if k > 6 then k = k - 7
189 C = C + 1
190 if C < 9 then F.n = F.n * 6 + K /* Learn dummit */
191 /* Game Over ? */
192 Do i = 7 to 12
193 if B.i ~= 0 then Signal Cpufin
195 Return
196 CpuFin:
197 Do i = 0 to 5
198 if B.i ~= 0 then Do
199 E = 1
200 Return
203 Return
205 Print: /* Procedure for printing the board */
207 CC = copies(' ',22)
208 P = CC ' '
209 Do I = 12 to 7 by -1
210 P = P || format(B.i,4)
212 Say P
213 Say CC Format(B.13,2) ' ' Format(B.6,2)
214 P = CC ' '
215 Do I = 0 to 5
216 P = P || Format(B.i,4)
218 Say P
219 Return
221 Moveit: Procedure Expose B. H M /* Move the 'M' pit */
222 Arg M
223 P = B.m ; B.m = 0
224 Do P
225 M = M + 1
226 if M > 13 then M = M - 14
227 B.M = B.M + 1
229 P = 12 - M
230 if B.M = 1 then /* Last bit was empty */
231 if M ~= 6 then /* Not your home */
232 if M ~= 13 then /* Not my home */
233 if B.P~= 0 then Do /* Opposite pit was empty too */
234 B.H = B.H + B.P + 1
235 B.M = 0
236 B.P = 0
238 Return
240 CPU_Move:
241 D = -99 ; H = 13 ; Q = -99
242 Do I = 0 to 13
243 G.i = B.i /* Save the board */
246 Do J = 7 to 12 /* Calc for all the possible */
247 if B.J = 0 then ITERATE /* moves */
248 G = 0
249 Call Moveit J
251 Do i = 0 to 5
252 if B.i = 0 then ITERATE
253 L = B.i + i
254 R = (L>13)
255 L = L // 14
256 P = 12 - L
257 if B.L = 0 then /* Again the same story */
258 if L ~= 6 then /* Life goes on... */
259 if L ~= 13 then R = B.P + R
260 if R > Q then Q = R
263 Q = B.13 - B.6 - Q
264 if C < 9 then Do
265 K = J
266 if k > 6 then k = k - 7
267 Do i = 0 to n-1
268 if F.n*6 + k = Trunc(F.i/6**(7-C) + 0.1) then Q = Q - 2
272 Do i = 0 to 13
273 B.i = G.i /* Restore the board */
276 if Q >= D then Do /* if the current move is better */
277 A = J /* then Save it */
278 D = Q
282 M = A
283 Move = Move M-6
284 Call MakeMove
285 Return
287 /* Help me if you can, I'm feeling down ... */
288 Help:
289 Do Line = 2 by 1
290 if left(Sourceline(Line),2) = '*/' then return
291 Say Sourceline(Line)
292 if line // 23 = 0 then Do
293 Say 'Press <enter> to continue...'
294 Pull .