# Chessboard program: you type in moves in algebraic notation, and it'll
# display the position after each move.

def main [
  open-console  # take control of screen, keyboard and mouse

  # The chessboard function takes keyboard and screen objects as 'ingredients'.
  #
  # In mu it is good form (though not required) to explicitly show the
  # hardware you rely on.
  #
  # Here the console and screen are both 0, which usually indicates real
  # hardware rather than a fake for testing as you'll see below.
  chessboard 0/screen, 0/console

  close-console  # clean up screen, keyboard and mouse
]

## But enough about mu. Here's what it looks like to run the chessboard program.

scenario print-board-and-read-move [
  trace-until 100/app
  # we'll make the screen really wide because the program currently prints out a long line
  assume-screen 120/width, 20/height
  # initialize keyboard to type in a move
  assume-console [
    type [a2-a4
]
  ]
  run [
    local-scope
    screen:&:screen, console:&:console <- chessboard screen:&:screen, console:&:console
    # icon for the cursor
    cursor-icon:char <- copy 9251/␣
    screen <- print screen, cursor-icon
  ]
  screen-should-contain [
  #            1         2         3         4         5         6         7         8         9         10        11
  #  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.                                                                                                        .
    .                                                                                                                        .
    .move: ␣                                                                                                                 .
    .                                                                                                                        .
    .                                                                                                                        .
  ]
]

## Here's how 'chessboard' is implemented.

type board = address:@:&:@:char

def chessboard screen:&:screen, console:&:console -> screen:&:screen, console:&:console [
  local-scope
  load-ingredients
  board:board <- initial-position
  # hook up stdin
  stdin-in:&:source:char, stdin-out:&:sink:char <- new-channel 10/capacity
  start-running send-keys-to-channel, console, stdin-out, screen
  # buffer lines in stdin
  buffered-stdin-in:&:source:char, buffered-stdin-out:&:sink:char <- new-channel 10/capacity
  start-running buffer-lines, stdin-in, buffered-stdin-out
  {
    print screen, [Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves.
]
    cursor-to-next-line screen
    print-board screen, board
    cursor-to-next-line screen
    print screen, [Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>.
]
    cursor-to-next-line screen
    print screen [Hit 'q' to exit.
]
    {
      cursor-to-next-line screen
      screen <- print screen, [move: ]
      m:&:move, quit:boolean, error:boolean <- read-move buffered-stdin-in, screen
      break-if quit, +quit:label
      buffered-stdin-in <- clear buffered-stdin-in  # cleanup after error. todo: test this?
      loop-if error
    }
    board <- make-move board, m
    screen <- clear-screen screen
    loop
  }
  +quit
]

## a board is an array of files, a file is an array of characters (squares)

def new-board initial-position:&:@:char -> board:board [
  local-scope
  load-ingredients
  # assert(length(initial-position) == 64)
  len:num <- length *initial-position
  correct-length?:boolean <- equal len, 64
  assert correct-length?, [chessboard had incorrect size]
  # board is an array of pointers to files; file is an array of characters
  board <- new {(address array character): type}, 8
  col:num <- copy 0
  {
    done?:boolean <- equal col, 8
    break-if done?
    file:&:@:char <- new-file initial-position, col
    *board <- put-index *board, col, file
    col <- add col, 1
    loop
  }
]

def new-file position:&:@:char, index:num -> result:&:@:char [
  local-scope
  load-ingredients
  index <- multiply index, 8
  result <- new character:type, 8
  row:num <- copy 0
  {
    done?:boolean <- equal row, 8
    break-if done?
    square:char <- index *position, index
    *result <- put-index *result, row, square
    row <- add row, 1
    index <- add index, 1
    loop
  }
]

def print-board screen:&:screen, board:board -> screen:&:screen [
  local-scope
  load-ingredients
  row:num <- copy 7  # start pri
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - filesystem.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muControl { color: #c0a020; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># example program: copy one file into another, character by character</span>
<span class="Comment"># BEWARE: this will modify your file system</span>
<span class="Comment"># before running it, put some text into /tmp/mu-x</span>
<span class="Comment"># after running it, check /tmp/mu-y</span>

<span class="muRecipe">def</span> main [
  <span class="Constant">local-scope</span>
  source-file:&amp;:source:char<span class="Special"> &lt;- </span>start-reading <span class="Constant">0/real-filesystem</span>, <span class="Constant">[/tmp/mu-x]</span>
  sink-file:&amp;:sink:char, write-routine:num<span class="Special"> &lt;- </span>start-writing <span class="Constant">0/real-filesystem</span>, <span class="Constant">[/tmp/mu-y]</span>
  <span class="Delimiter">{</span>
    c:char, done?:boolean, source-file<span class="Special"> &lt;- </span>read source-file
    <span class="muControl">break-if</span> done?
    sink-file<span class="Special"> &lt;- </span>write sink-file, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  close sink-file
  <span class="Comment"># make sure to wait for the file to be actually written to disk</span>
  <span class="Comment"># (Mu practices structured concurrency: <a href="http://250bpm.com/blog:71)">http://250bpm.com/blog:71)</a></span>
  wait-for-routine write-routine
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
ant">2/discontinued assert waiting?, [ F read-move-blocking: routine failed to pause after file 'a2'] # press '-' sink <- write sink, 45/'-' restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-blocking: routine failed to pause after hyphen 'a2-'] # press 'a' sink <- write sink, 97/a restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-blocking: routine failed to pause after rank 'a2-a'] # press '4' sink <- write sink, 52/'4' restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-blocking: routine failed to pause after file 'a2-a4'] # press 'newline' sink <- write sink, 10 # newline restart read-move-routine # 'read-move' now completes wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine completed?:boolean <- equal read-move-state, 1/completed assert completed?, [ F read-move-blocking: routine failed to terminate on newline] trace 1, [test], [reached end] ] trace-should-contain [ test: reached end ] ] scenario read-move-quit [ assume-screen 20/width, 2/height run [ local-scope source:&:source:char, sink:&:sink:char <- new-channel 2/capacity read-move-routine:num <- start-running read-move, source, screen:&:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:num <- routine-state read-move-routine waiting?:boolean <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-quit: routine failed to pause after coming up (before any keys were pressed)] # press 'q' sink <- write sink, 113/q restart read-move-routine # 'read-move' completes wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine completed?:boolean <- equal read-move-state, 1/completed assert completed?, [ F read-move-quit: routine failed to terminate on 'q'] trace 1, [test], [reached end] ] trace-should-contain [ test: reached end ] ] scenario read-move-illegal-file [ assume-screen 20/width, 2/height run [ local-scope source:&:source:char, sink:&:sink:char <- new-channel 2/capacity read-move-routine:num <- start-running read-move, source, screen:&:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:num <- routine-state read-move-routine waiting?:boolean <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-illegal-file: routine failed to pause after coming up (before any keys were pressed)] sink <- write sink, 50/'2' restart read-move-routine wait-for-routine-to-block read-move-routine ] screen-should-contain [ .file too low: 2 . . . ] ] scenario read-move-illegal-rank [ assume-screen 20/width, 2/height run [ local-scope source:&:source:char, sink:&:sink:char <- new-channel 2/capacity read-move-routine:num <- start-running read-move, source, screen:&:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:num <- routine-state read-move-routine waiting?:boolean <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-illegal-rank: routine failed to pause after coming up (before any keys were pressed)] sink <- write sink, 97/a sink <- write sink, 97/a restart read-move-routine wait-for-routine-to-block read-move-routine ] screen-should-contain [ .rank too high: a . . . ] ] scenario read-move-empty [ assume-screen 20/width, 2/height run [ local-scope source:&:source:char, sink:&:sink:char <- new-channel 2/capacity read-move-routine:num <- start-running read-move, source, screen:&:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:num <- routine-state read-move-routine waiting?:boolean <- not-equal read-move-state, 2/discontinued assert waiting?, [ F read-move-empty: routine failed to pause after coming up (before any keys were pressed)] sink <- write sink, 10/newline sink <- write sink, 97/a restart read-move-routine wait-for-routine-to-block read-move-routine ] screen-should-contain [ .that's not enough . . . ] ] def make-move board:board, m:&:move -> board:board [ local-scope load-ingredients from-file:num <- get *m, from-file:offset from-rank:num <- get *m, from-rank:offset to-file:num <- get *m, to-file:offset to-rank:num <- get *m, to-rank:offset from-f:&:@:char <- index *board, from-file to-f:&:@:char <- index *board, to-file src:char/square <- index *from-f, from-rank *to-f <- put-index *to-f, to-rank, src *from-f <- put-index *from-f, from-rank, 32/space ] scenario making-a-move [ assume-screen 30/width, 12/height run [ local-scope board:board <- initial-position move:&:move <- new move:type *move <- merge 6/g, 1/'2', 6/g, 3/'4' board <- make-move board, move screen:&:screen <- print-board screen:&:screen, board ] 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 . . . ] ]