1
2
3
4 def main [
5 local-scope
6 open-console
7 clear-screen null/screen
8
9
10
11
12
13
14
15
16 chessboard null/screen, null/console
17
18 close-console
19 ]
20
21
22
23 scenario print-board-and-read-move [
24 local-scope
25 trace-until 100/app
26
27 assume-screen 120/width, 20/height
28
29 assume-console [
30 type [a2-a4
31 ]
32 ]
33 run [
34 screen, console <- chessboard screen, console
35
36 cursor-icon:char <- copy 9251/␣
37 screen <- print screen, cursor-icon
38 ]
39 screen-should-contain [
40
41
42 .Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves. .
43 . .
44 .8 | r n b q k b n r .
45 .7 | p p p p p p p p .
46 .6 | .
47 .5 | .
48 .4 | P .
49 .3 | .
50 .2 | P P P P P P P .
51 .1 | R N B Q K B N R .
52 . +---------------- .
53 . a b c d e f g h .
54 . .
55 .Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>. .
56 . .
57 .Hit 'q' to exit. .
58 . .
59 .move: ␣ .
60 . .
61 . .
62 ]
63 ]
64
65
66
67 type board = &:@:&:@:char
68
69 def chessboard screen:&:screen, console:&:console -> screen:&:screen, console:&:console [
70 local-scope
71 load-inputs
72 board:board <- initial-position
73
74 stdin-in:&:source:char, stdin-out:&:sink:char <- new-channel 10/capacity
75 start-running send-keys-to-channel, console, stdin-out, screen
76
77 buffered-stdin-in:&:source:char, buffered-stdin-out:&:sink:char <- new-channel 10/capacity
78 start-running buffer-lines, stdin-in, buffered-stdin-out
79 {
80 print screen, [Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves.
81 ]
82 cursor-to-next-line screen
83 print screen, board
84 cursor-to-next-line screen
85 print screen, [Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>.
86 ]
87 cursor-to-next-line screen
88 print screen [Hit 'q' to exit.
89 ]
90 {
91 cursor-to-next-line screen
92 screen <- print screen, [move: ]
93 m:&:move, quit:bool, error:bool <- read-move buffered-stdin-in, screen
94 break-if quit, +quit
95 buffered-stdin-in <- clear buffered-stdin-in
96 loop-if error
97 }
98 board <- make-move board, m
99 screen <- clear-screen screen
100 loop
101 }
102 +quit
103 ]
104
105
106
107 def new-board initial-position:&:@:char -> board:board [
108 local-scope
109 load-inputs
110
111 len:num <- length *initial-position
112 correct-length?:bool <- equal len, 64
113 assert correct-length?, [chessboard had incorrect size]
114
115 board <- new {(address array character): type}, 8
116 col:num <- copy 0
117 {
118 done?:bool <- equal col, 8
119 break-if done?
120 file:&:@:char <- new-file initial-position, col
121 *board <- put-index *board, col, file
122 col <- add col, 1
123 loop
124 }
125 ]
126
127 def new-file position:&:@:char, index:num -> result:&:@:char [
128 local-scope
129 load-inputs
130 index <- multiply index, 8
131 result <- new character:type, 8
132 row:num <- copy 0
133 {
134 done?:bool <- equal row, 8
135 break-if done?
136 square:char <- index *position, index
137 *result <- put-index *result, row, square
138 row <- add row, 1
139 index <- add index, 1
140 loop
141 }
142 ]
143
144 def print screen:&:screen, board:board -> screen:&:screen [
145 local-scope
146 load-inputs
147 row:num <- copy 7
148 space:char <- copy 32/space
149
150 {
151 done?:bool <- lesser-than row, 0
152 break-if done?
153
154 rank:num <- add row, 1
155 print screen, rank
156 print screen, [ | ]
157
158 col:num <- copy 0
159 {
160 done?:bool <- equal col:num, 8
161 break-if done?
162 f:&:@:char <- index *board, col
163 c:char <- index *f, row
164 bg:num <- square-color row, col
165 print screen, c, 7/white, bg
166 print screen, space
167 col <- add col, 1
168 loop
169 }
170 row <- subtract row, 1
171 cursor-to-next-line screen
172 loop
173 }
174
175 print screen, [ +----------------]
176 cursor-to-next-line screen
177 print screen, [ a b c d e f g h]
178 cursor-to-next-line screen
179 ]
180
181 def square-color row:num, col:num -> result:num [
182 local-scope
183 load-inputs
184 result <- copy 0/black
185 x:num <- add row, col
186 _, rem:num <- divide-with-remainder x, 2
187 return-if rem, 238
188 ]
189
190 def initial-position -> board:board [
191 local-scope
192
193
194
195
196
197
198
199
200
201 initial-position:&:@:char <- new-array 82/R, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 114/r, 78/N, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 110/n, 66/B, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 98/b, 81/Q, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 113/q, 75/K, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 107/k, 66/B, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 98/b, 78/N, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 110/n, 82/R, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 114/r
202