2015-05-10 20:03:21 +00:00
# Chessboard program: you type in moves in algebraic notation, and it'll
# display the position after each move.
2015-05-08 01:35:33 +00:00
2015-05-11 07:51:28 +00:00
# recipes are mu's names for functions
2015-05-10 20:01:58 +00:00
recipe main [
2015-06-22 06:53:17 +00:00
open-console # take control of screen, keyboard and mouse
2015-05-10 20:01:58 +00:00
# The chessboard recipe takes keyboard and screen objects as 'ingredients'.
#
# In mu it is good form (though not required) to explicitly show the
# hardware you rely on.
#
# The chessboard also returns the same keyboard and screen objects. In mu it
# is good form to not modify ingredients of a recipe unless they are also
# results. Here we clearly modify both keyboard and screen, so we return
# both.
2015-05-11 07:51:28 +00:00
#
2015-06-22 06:53:17 +00:00
# Here the console and screen are both 0, which usually indicates real
2015-05-11 07:51:28 +00:00
# hardware rather than a fake for testing as you'll see below.
2015-11-19 05:36:36 +00:00
chessboard 0/screen, 0/console
2015-05-10 20:01:58 +00:00
2015-06-22 06:53:17 +00:00
close-console # cleanup screen, keyboard and mouse
2015-05-10 20:01:58 +00:00
]
2015-05-20 01:23:20 +00:00
## But enough about mu. Here's what it looks like to run the chessboard program.
2015-05-14 19:06:16 +00:00
scenario print-board-and-read-move [
2015-10-07 06:38:28 +00:00
trace-until 100/app
2015-05-14 23:04:26 +00:00
# we'll make the screen really wide because the program currently prints out a long line
2015-07-28 21:33:22 +00:00
assume-screen 120/width, 20/height
2015-05-14 22:40:31 +00:00
# initialize keyboard to type in a move
2015-06-23 04:16:31 +00:00
assume-console [
type [a2-a4
2015-05-14 19:39:12 +00:00
]
2015-06-23 04:16:31 +00:00
]
2015-05-14 19:06:16 +00:00
run [
2016-01-20 07:18:03 +00:00
screen:address:shared:screen, console:address:shared:console <- chessboard screen:address:shared:screen, console:address:shared:console
2015-05-18 22:27:33 +00:00
# icon for the cursor
2015-11-21 18:19:34 +00:00
screen <- print screen, 9251/␣
2015-05-14 19:06:16 +00:00
]
screen-should-contain [
2015-05-14 23:04:26 +00:00
# 1 2 3 4 5 6 7 8 9 10 11
2015-05-14 22:40:31 +00:00
# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
.Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves. .
. .
.8 | r n b q k b n r .
.7 | p p p p p p p p .
.6 | .
.5 | .
.4 | P .
.3 | .
.2 | P P P P P P P .
.1 | R N B Q K B N R .
. +---------------- .
. a b c d e f g h .
. .
.Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>. .
. .
.Hit 'q' to exit. .
. .
2015-05-18 22:27:33 +00:00
.move: ␣ .
2015-05-14 22:40:31 +00:00
. .
. .
2015-05-14 19:06:16 +00:00
]
]
2015-05-20 01:23:20 +00:00
## Here's how 'chessboard' is implemented.
2016-01-20 07:18:03 +00:00
recipe chessboard screen:address:shared:screen, console:address:shared:console -> screen:address:shared:screen, console:address:shared:console [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2016-01-20 07:18:03 +00:00
board:address:shared:array:address:shared:array:character <- initial-position
2015-05-10 20:09:53 +00:00
# hook up stdin
2016-01-20 07:18:03 +00:00
stdin:address:shared:channel <- new-channel 10/capacity
2016-01-18 07:15:03 +00:00
start-running send-keys-to-channel, console, stdin, screen
2015-05-10 20:09:53 +00:00
# buffer lines in stdin
2016-01-20 07:18:03 +00:00
buffered-stdin:address:shared:channel <- new-channel 10/capacity
2016-01-18 07:15:03 +00:00
start-running buffer-lines, stdin, buffered-stdin
2015-05-10 20:09:53 +00:00
{
2016-01-20 07:18:03 +00:00
msg:address:shared:array:character <- new [Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves.
2015-05-10 20:09:53 +00:00
]
2015-11-21 18:01:12 +00:00
print screen, msg
2015-07-29 08:23:22 +00:00
cursor-to-next-line screen
print-board screen, board
cursor-to-next-line screen
msg <- new [Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>.
2015-05-10 20:09:53 +00:00
]
2015-11-21 18:01:12 +00:00
print screen, msg
2015-07-29 08:23:22 +00:00
cursor-to-next-line screen
msg <- new [Hit 'q' to exit.
2015-05-10 20:09:53 +00:00
]
2015-11-21 18:01:12 +00:00
print screen, msg
2015-05-10 20:09:53 +00:00
{
2015-07-29 08:23:22 +00:00
cursor-to-next-line screen
msg <- new [move: ]
2015-11-21 18:01:12 +00:00
screen <- print screen, msg
2016-01-20 07:18:03 +00:00
m:address:shared:move, quit:boolean, error:boolean <- read-move buffered-stdin, screen
2015-07-29 08:23:22 +00:00
break-if quit, +quit:label
buffered-stdin <- clear-channel buffered-stdin # cleanup after error. todo: test this?
loop-if error
2015-05-10 20:09:53 +00:00
}
2015-07-29 08:23:22 +00:00
board <- make-move board, m
2015-11-19 05:36:36 +00:00
screen <- clear-screen screen
2015-05-10 20:09:53 +00:00
loop
}
2016-01-20 07:18:03 +00:00
msg <- copy 0
2015-05-10 20:09:53 +00:00
+quit
]
2015-05-08 01:35:33 +00:00
## a board is an array of files, a file is an array of characters (squares)
2015-05-10 20:02:33 +00:00
2016-01-20 07:18:03 +00:00
recipe new-board initial-position:address:shared:array:character -> board:address:shared:array:address:shared:array:character [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-05-06 19:13:04 +00:00
# assert(length(initial-position) == 64)
2015-07-29 08:23:22 +00:00
len:number <- length *initial-position
correct-length?:boolean <- equal len, 64
assert correct-length?, [chessboard had incorrect size]
2015-05-06 19:13:04 +00:00
# board is an array of pointers to files; file is an array of characters
2016-01-20 07:18:03 +00:00
board <- new {(address shared array character): type}, 8
2015-07-28 21:33:22 +00:00
col:number <- copy 0
2015-05-06 19:13:04 +00:00
{
2015-07-29 08:23:22 +00:00
done?:boolean <- equal col, 8
break-if done?
2016-01-20 07:18:03 +00:00
file:address:address:shared:array:character <- index-address *board, col
2015-07-29 08:23:22 +00:00
*file <- new-file initial-position, col
col <- add col, 1
2015-05-06 19:13:04 +00:00
loop
}
]
2016-01-20 07:18:03 +00:00
recipe new-file position:address:shared:array:character, index:number -> result:address:shared:array:character [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-29 08:23:22 +00:00
index <- multiply index, 8
2015-11-19 05:36:36 +00:00
result <- new character:type, 8
2015-07-28 21:33:22 +00:00
row:number <- copy 0
2015-05-06 19:13:04 +00:00
{
2015-07-29 08:23:22 +00:00
done?:boolean <- equal row, 8
break-if done?
dest:address:character <- index-address *result, row
*dest <- index *position, index
row <- add row, 1
index <- add index, 1
2015-05-06 19:13:04 +00:00
loop
}
]
2016-01-20 07:18:03 +00:00
recipe print-board screen:address:shared:screen, board:address:shared:array:address:shared:array:character -> screen:address:shared:screen [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-28 21:33:22 +00:00
row:number <- copy 7 # start printing from the top of the board
2015-12-28 16:44:36 +00:00
space:character <- copy 32/space
2015-05-06 19:13:04 +00:00
# print each row
{
2015-07-29 08:23:22 +00:00
done?:boolean <- lesser-than row, 0
break-if done?
2015-05-06 19:13:04 +00:00
# print rank number as a legend
2015-07-29 08:23:22 +00:00
rank:number <- add row, 1
print-integer screen, rank
2016-01-20 07:18:03 +00:00
s:address:shared:array:character <- new [ | ]
2015-11-21 18:01:12 +00:00
print screen, s
2015-05-06 19:13:04 +00:00
# print each square in the row
2015-07-28 21:33:22 +00:00
col:number <- copy 0
2015-05-06 19:13:04 +00:00
{
2015-07-28 21:33:22 +00:00
done?:boolean <- equal col:number, 8
2015-05-06 19:13:04 +00:00
break-if done?:boolean
2016-01-20 07:18:03 +00:00
f:address:shared:array:character <- index *board, col
2015-07-29 08:23:22 +00:00
c:character <- index *f, row
2015-11-21 18:19:34 +00:00
print screen, c
2015-12-28 16:44:36 +00:00
print screen, space
2015-07-29 08:23:22 +00:00
col <- add col, 1
2015-05-06 19:13:04 +00:00
loop
}
2015-07-29 08:23:22 +00:00
row <- subtract row, 1
cursor-to-next-line screen
2015-05-06 19:13:04 +00:00
loop
}
# print file letters as legend
2015-07-29 08:23:22 +00:00
s <- new [ +----------------]
2015-11-21 18:01:12 +00:00
print screen, s
2015-07-29 08:23:22 +00:00
screen <- cursor-to-next-line screen
s <- new [ a b c d e f g h]
2015-11-21 18:01:12 +00:00
screen <- print screen, s
2015-07-29 08:23:22 +00:00
screen <- cursor-to-next-line screen
2015-05-06 19:13:04 +00:00
]
2016-01-20 07:18:03 +00:00
recipe initial-position -> board:address:shared:array:address:shared:array:character [
2015-07-14 05:43:16 +00:00
local-scope
2015-05-08 16:11:12 +00:00
# layout in memory (in raster order):
# R P _ _ _ _ p r
# N P _ _ _ _ p n
# B P _ _ _ _ p b
# Q P _ _ _ _ p q
# K P _ _ _ _ p k
# B P _ _ _ _ p B
# N P _ _ _ _ p n
# R P _ _ _ _ p r
2016-01-20 07:18:03 +00:00
initial-position:address:shared:array:character <- 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
2015-07-28 21:33:22 +00:00
#? 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
2015-11-19 05:36:36 +00:00
board <- new-board initial-position
2015-05-08 16:11:12 +00:00
]
scenario printing-the-board [
2015-07-28 21:33:22 +00:00
assume-screen 30/width, 12/height
2015-05-08 16:11:12 +00:00
run [
2016-01-20 07:18:03 +00:00
1:address:shared:array:address:shared:array:character/board <- initial-position
screen:address:shared:screen <- print-board screen:address:shared:screen, 1:address:shared:array:address:shared:array:character/board
2015-05-06 19:13:04 +00:00
]
screen-should-contain [
# 012345678901234567890123456789
.8 | r n b q k b n r .
.7 | p p p p p p p p .
.6 | .
.5 | .
.4 | .
.3 | .
.2 | P P P P P P P P .
.1 | R N B Q K B N R .
. +---------------- .
. a b c d e f g h .
. .
. .
]
]
2015-05-08 01:35:33 +00:00
## data structure: move
2015-05-10 20:02:33 +00:00
2015-05-08 01:35:33 +00:00
container move [
# valid range: 0-7
2015-05-13 17:03:26 +00:00
from-file:number
from-rank:number
to-file:number
to-rank:number
2015-05-08 01:35:33 +00:00
]
2015-05-10 18:38:18 +00:00
# prints only error messages to screen
2016-01-20 07:18:03 +00:00
recipe read-move stdin:address:shared:channel, screen:address:shared:screen -> result:address:shared:move, quit?:boolean, error?:boolean, stdin:address:shared:channel, screen:address:shared:screen [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-29 08:23:22 +00:00
from-file:number, quit?:boolean, error?:boolean <- read-file stdin, screen
reply-if quit?, 0/dummy, quit?, error?
reply-if error?, 0/dummy, quit?, error?
2015-05-08 01:35:33 +00:00
# construct the move object
2016-01-20 07:18:03 +00:00
result:address:shared:move <- new move:type
2015-07-29 08:23:22 +00:00
x:address:number <- get-address *result, from-file:offset
*x <- copy from-file
x <- get-address *result, from-rank:offset
*x, quit?, error? <- read-rank stdin, screen
reply-if quit?, 0/dummy, quit?, error?
reply-if error?, 0/dummy, quit?, error?
error? <- expect-from-channel stdin, 45/dash, screen
reply-if error?, 0/dummy, 0/quit, error?
x <- get-address *result, to-file:offset
*x, quit?, error? <- read-file stdin, screen
2015-07-28 21:33:22 +00:00
reply-if quit?:boolean, 0/dummy, quit?:boolean, error?:boolean
reply-if error?:boolean, 0/dummy, quit?:boolean, error?:boolean
2015-07-29 08:23:22 +00:00
x:address:number <- get-address *result, to-rank:offset
*x, quit?, error? <- read-rank stdin, screen
reply-if quit?, 0/dummy, quit?, error?
reply-if error?, 0/dummy, quit?, error?
error? <- expect-from-channel stdin, 10/newline, screen
reply-if error?, 0/dummy, 0/quit, error?
reply result, quit?, error?
2015-05-08 01:35:33 +00:00
]
2015-05-10 18:38:18 +00:00
# valid values for file: 0-7
2016-01-20 07:18:03 +00:00
recipe read-file stdin:address:shared:channel, screen:address:shared:screen -> file:number, quit:boolean, error:boolean, stdin:address:shared:channel, screen:address:shared:screen [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-29 08:23:22 +00:00
c:character, stdin <- read stdin
2015-05-08 01:35:33 +00:00
{
2015-07-29 08:23:22 +00:00
q-pressed?:boolean <- equal c, 81/Q
break-unless q-pressed?
2015-07-28 21:33:22 +00:00
reply 0/dummy, 1/quit, 0/error
2015-05-08 05:27:30 +00:00
}
{
2015-07-29 08:23:22 +00:00
q-pressed? <- equal c, 113/q
break-unless q-pressed?
2015-07-28 21:33:22 +00:00
reply 0/dummy, 1/quit, 0/error
2015-05-08 01:35:33 +00:00
}
2015-05-14 19:39:12 +00:00
{
2015-07-29 08:23:22 +00:00
empty-fake-keyboard?:boolean <- equal c, 0/eof
break-unless empty-fake-keyboard?
2015-07-28 21:33:22 +00:00
reply 0/dummy, 1/quit, 0/error
2015-05-14 19:39:12 +00:00
}
2015-05-10 18:44:33 +00:00
{
2015-07-29 08:23:22 +00:00
newline?:boolean <- equal c, 10/newline
break-unless newline?
2016-01-20 07:18:03 +00:00
error-message:address:shared:array:character <- new [that's not enough]
2015-11-21 18:01:12 +00:00
print screen, error-message
2015-07-28 21:33:22 +00:00
reply 0/dummy, 0/quit, 1/error
2015-05-10 18:44:33 +00:00
}
2015-07-29 08:23:22 +00:00
file:number <- subtract c, 97/a
2015-05-08 01:35:33 +00:00
# 'a' <= file <= 'h'
2015-05-10 18:38:18 +00:00
{
2015-07-29 08:23:22 +00:00
above-min:boolean <- greater-or-equal file, 0
break-if above-min
2016-01-20 07:18:03 +00:00
error-message:address:shared:array:character <- new [file too low: ]
2015-11-21 18:01:12 +00:00
print screen, error-message
2015-11-21 18:19:34 +00:00
print screen, c
2015-07-29 08:23:22 +00:00
cursor-to-next-line screen
2015-07-28 21:33:22 +00:00
reply 0/dummy, 0/quit, 1/error
2015-05-10 18:38:18 +00:00
}
{
2015-07-29 08:23:22 +00:00
below-max:boolean <- lesser-than file, 8
break-if below-max
error-message <- new [file too high: ]
2015-11-21 18:01:12 +00:00
print screen, error-message
2015-11-21 18:19:34 +00:00
print screen, c
2015-07-28 21:33:22 +00:00
reply 0/dummy, 0/quit, 1/error
2015-05-10 18:38:18 +00:00
}
2015-07-29 08:23:22 +00:00
reply file, 0/quit, 0/error
2015-05-08 01:35:33 +00:00
]
2015-05-10 18:38:18 +00:00
# valid values: 0-7, -1 (quit), -2 (error)
2016-01-20 07:18:03 +00:00
recipe read-rank stdin:address:shared:channel, screen:address:shared:screen -> rank:number, quit?:boolean, error?:boolean, stdin:address:shared:channel, screen:address:shared:screen [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-29 08:23:22 +00:00
c:character, stdin <- read stdin
2015-05-08 01:35:33 +00:00
{
2015-07-29 08:23:22 +00:00
q-pressed?:boolean <- equal c, 8/Q
break-unless q-pressed?
2015-07-28 21:33:22 +00:00
reply 0/dummy, 1/quit, 0/error
2015-05-08 05:27:30 +00:00
}
{
2015-07-29 08:23:22 +00:00
q-pressed? <- equal c, 113/q
break-unless q-pressed?
2015-07-28 21:33:22 +00:00
reply 0/dummy, 1/quit, 0/error
2015-05-08 01:35:33 +00:00
}
2015-05-10 18:44:33 +00:00
{
2015-07-29 08:23:22 +00:00
newline?:boolean <- equal c, 10 # newline
break-unless newline?
2016-01-20 07:18:03 +00:00
error-message:address:shared:array:character <- new [that's not enough]
2015-11-21 18:01:12 +00:00
print screen, error-message
2015-07-28 21:33:22 +00:00
reply 0/dummy, 0/quit, 1/error
2015-05-10 18:44:33 +00:00
}
2015-07-29 08:23:22 +00:00
rank:number <- subtract c, 49/'1'
2015-05-08 01:35:33 +00:00
# assert'1' <= rank <= '8'
2015-05-10 18:38:18 +00:00
{
2015-07-29 08:23:22 +00:00
above-min:boolean <- greater-or-equal rank, 0
break-if above-min
error-message <- new [rank too low: ]
2015-11-21 18:01:12 +00:00
print screen, error-message
2015-11-21 18:19:34 +00:00
print screen, c
2015-07-28 21:33:22 +00:00
reply 0/dummy, 0/quit, 1/error
2015-05-10 18:38:18 +00:00
}
{
2015-07-29 08:23:22 +00:00
below-max:boolean <- lesser-or-equal rank, 7
break-if below-max
error-message <- new [rank too high: ]
2015-11-21 18:01:12 +00:00
print screen, error-message
2015-11-21 18:19:34 +00:00
print screen, c
2015-07-28 21:33:22 +00:00
reply 0/dummy, 0/quit, 1/error
2015-05-10 18:38:18 +00:00
}
2015-07-29 08:23:22 +00:00
reply rank, 0/quit, 0/error
2015-05-08 01:35:33 +00:00
]
# read a character from the given channel and check that it's what we expect
2015-05-10 18:38:18 +00:00
# return true on error
2016-01-20 07:18:03 +00:00
recipe expect-from-channel stdin:address:shared:channel, expected:character, screen:address:shared:screen -> result:boolean, stdin:address:shared:channel, screen:address:shared:screen [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-29 08:23:22 +00:00
c:character, stdin <- read stdin
2015-05-10 18:38:18 +00:00
{
2015-07-29 08:23:22 +00:00
match?:boolean <- equal c, expected
break-if match?
2016-01-20 07:18:03 +00:00
s:address:shared:array:character <- new [expected character not found]
2015-11-21 18:01:12 +00:00
print screen, s
2015-05-10 18:38:18 +00:00
}
2015-11-19 05:36:36 +00:00
result <- not match?
2015-05-08 01:35:33 +00:00
]
scenario read-move-blocking [
2015-07-28 21:33:22 +00:00
assume-screen 20/width, 2/height
2015-05-08 01:35:33 +00:00
run [
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- new-channel 2
2:number/routine <- start-running read-move, 1:address:shared:channel, screen:address:shared:screen
2015-05-08 01:35:33 +00:00
# 'read-move' is waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 01:35:33 +00:00
assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause after coming up (before any keys were pressed)]
# press 'a'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 97/a
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 01:35:33 +00:00
# 'read-move' still waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 01:35:33 +00:00
assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause after rank 'a']
# press '2'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 50/'2'
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 01:35:33 +00:00
# 'read-move' still waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 01:35:33 +00:00
assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause after file 'a2']
# press '-'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 45/'-'
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 01:35:33 +00:00
# 'read-move' still waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 01:35:33 +00:00
assert 4:boolean/waiting?/routine-state, [
F read-move-blocking: routine failed to pause after hyphen 'a2-']
# press 'a'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 97/a
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 01:35:33 +00:00
# 'read-move' still waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 01:35:33 +00:00
assert 4:boolean/waiting?/routine-state, [
F read-move-blocking: routine failed to pause after rank 'a2-a']
# press '4'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 52/'4'
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 01:35:33 +00:00
# 'read-move' still waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 01:35:33 +00:00
assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause after file 'a2-a4']
# press 'newline'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 10 # newline
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 01:35:33 +00:00
# 'read-move' now completes
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number
2015-07-28 21:33:22 +00:00
4:boolean/completed? <- equal 3:number/routine-state, 1/completed
2015-05-08 01:35:33 +00:00
assert 4:boolean/completed?, [
F read-move-blocking: routine failed to terminate on newline]
2015-08-21 17:37:25 +00:00
trace 1, [test], [reached end]
2015-05-08 01:35:33 +00:00
]
trace-should-contain [
test: reached end
]
]
2015-05-08 05:27:30 +00:00
scenario read-move-quit [
2015-07-28 21:33:22 +00:00
assume-screen 20/width, 2/height
2015-05-08 05:27:30 +00:00
run [
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- new-channel 2
2:number/routine <- start-running read-move, 1:address:shared:channel, screen:address:shared:screen
2015-05-08 05:27:30 +00:00
# 'read-move' is waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 05:27:30 +00:00
assert 4:boolean/waiting?, [
F read-move-quit: routine failed to pause after coming up (before any keys were pressed)]
# press 'q'
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 113/q
2015-05-13 17:03:26 +00:00
restart 2:number/routine
2015-05-08 05:27:30 +00:00
# 'read-move' completes
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-07-28 21:33:22 +00:00
4:boolean/completed? <- equal 3:number/routine-state, 1/completed
2015-05-08 05:27:30 +00:00
assert 4:boolean/completed?, [
F read-move-quit: routine failed to terminate on 'q']
2015-08-21 17:37:25 +00:00
trace 1, [test], [reached end]
2015-05-08 05:27:30 +00:00
]
trace-should-contain [
test: reached end
]
]
scenario read-move-illegal-file [
2015-07-28 21:33:22 +00:00
assume-screen 20/width, 2/height
2015-05-08 05:27:30 +00:00
run [
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- new-channel 2
2:number/routine <- start-running read-move, 1:address:shared:channel, screen:address:shared:screen
2015-05-08 05:27:30 +00:00
# 'read-move' is waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 05:27:30 +00:00
assert 4:boolean/waiting?, [
F read-move-file: routine failed to pause after coming up (before any keys were pressed)]
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 50/'2'
2015-05-13 17:03:26 +00:00
restart 2:number/routine
wait-for-routine 2:number
2015-05-08 05:27:30 +00:00
]
2015-05-10 18:38:18 +00:00
screen-should-contain [
.file too low: 2 .
. .
2015-05-08 05:27:30 +00:00
]
]
scenario read-move-illegal-rank [
2015-07-28 21:33:22 +00:00
assume-screen 20/width, 2/height
2015-05-08 05:27:30 +00:00
run [
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- new-channel 2
2:number/routine <- start-running read-move, 1:address:shared:channel, screen:address:shared:screen
2015-05-08 05:27:30 +00:00
# 'read-move' is waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-08 05:27:30 +00:00
assert 4:boolean/waiting?, [
F read-move-file: routine failed to pause after coming up (before any keys were pressed)]
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 97/a
1:address:shared:channel <- write 1:address:shared:channel, 97/a
2015-05-13 17:03:26 +00:00
restart 2:number/routine
wait-for-routine 2:number
2015-05-08 05:27:30 +00:00
]
2015-05-10 18:38:18 +00:00
screen-should-contain [
.rank too high: a .
. .
2015-05-10 18:44:33 +00:00
]
]
scenario read-move-empty [
2015-07-28 21:33:22 +00:00
assume-screen 20/width, 2/height
2015-05-10 18:44:33 +00:00
run [
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- new-channel 2
2:number/routine <- start-running read-move, 1:address:shared:channel, screen:address:shared:screen
2015-05-10 18:44:33 +00:00
# 'read-move' is waiting for input
2015-05-13 17:03:26 +00:00
wait-for-routine 2:number
3:number <- routine-state 2:number/id
2015-08-21 17:37:25 +00:00
4:boolean/waiting? <- equal 3:number/routine-state, 3/waiting
2015-05-10 18:44:33 +00:00
assert 4:boolean/waiting?, [
F read-move-file: routine failed to pause after coming up (before any keys were pressed)]
2016-01-20 07:18:03 +00:00
1:address:shared:channel <- write 1:address:shared:channel, 10/newline
1:address:shared:channel <- write 1:address:shared:channel, 97/a
2015-05-13 17:03:26 +00:00
restart 2:number/routine
wait-for-routine 2:number
2015-05-10 18:44:33 +00:00
]
screen-should-contain [
.that's not enough .
. .
2015-05-08 05:27:30 +00:00
]
]
2015-05-08 06:18:46 +00:00
2016-01-20 07:18:03 +00:00
recipe make-move board:address:shared:array:address:shared:array:character, m:address:shared:move -> board:address:shared:array:address:shared:array:character [
2015-07-14 05:43:16 +00:00
local-scope
2015-11-19 05:36:36 +00:00
load-ingredients
2015-07-29 08:23:22 +00:00
from-file:number <- get *m, from-file:offset
from-rank:number <- get *m, from-rank:offset
to-file:number <- get *m, to-file:offset
to-rank:number <- get *m, to-rank:offset
2016-01-20 07:18:03 +00:00
f:address:shared:array:character <- index *board, from-file
2015-07-29 08:23:22 +00:00
src:address:character/square <- index-address *f, from-rank
2015-11-19 05:36:36 +00:00
f <- index *board, to-file
2015-07-29 08:23:22 +00:00
dest:address:character/square <- index-address *f, to-rank
*dest <- copy *src
*src <- copy 32/space
2015-05-08 06:18:46 +00:00
]
scenario making-a-move [
2015-07-28 21:33:22 +00:00
assume-screen 30/width, 12/height
2015-05-08 06:18:46 +00:00
run [
2016-01-20 07:18:03 +00:00
2:address:shared:array:address:shared:array:character/board <- initial-position
3:address:shared:move <- new move:type
4:address:number <- get-address *3:address:shared:move, from-file:offset
2015-07-29 08:23:22 +00:00
*4:address:number <- copy 6/g
2016-01-20 07:18:03 +00:00
5:address:number <- get-address *3:address:shared:move, from-rank:offset
2015-07-29 08:23:22 +00:00
*5:address:number <- copy 1/'2'
2016-01-20 07:18:03 +00:00
6:address:number <- get-address *3:address:shared:move, to-file:offset
2015-07-29 08:23:22 +00:00
*6:address:number <- copy 6/g
2016-01-20 07:18:03 +00:00
7:address:number <- get-address *3:address:shared:move, to-rank:offset
2015-07-29 08:23:22 +00:00
*7:address:number <- copy 3/'4'
2016-01-20 07:18:03 +00:00
2:address:shared:array:address:shared:array:character/board <- make-move 2:address:shared:array:address:shared:array:character/board, 3:address:shared:move
screen:address:shared:screen <- print-board screen:address:shared:screen, 2:address:shared:array:address:shared:array:character/board
2015-05-08 06:18:46 +00:00
]
screen-should-contain [
# 012345678901234567890123456789
.8 | r n b q k b n r .
.7 | p p p p p p p p .
.6 | .
.5 | .
.4 | P .
.3 | .
.2 | P P P P P P P .
.1 | R N B Q K B N R .
. +---------------- .
. a b c d e f g h .
. .
]
]