The Big Book of Small EK9 Programs

Adapted from Al Sweigart's The Big Book of Small Python Projects: 81 Easy Practice Programs (No Starch Press, 2021; copyright © 2021 Al Sweigart), which he makes freely available under a Creative Commons Attribution-NonCommercial-ShareAlike licence at inventwithpython.com/bigbookpython. Changes were made: each program was ported from Python to EK9 and remodelled in EK9's idioms.

Project 65 — Shining Carpet

This is project 65 of the book, ported to EK9. Its chapter is still being written; until then, this page shows the port itself: a run of it, the whole program, and how it keeps to the original.

A sample run

Run it with:

ek9 shining-carpet.ek9

It prints:

_ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ __
 \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _
\ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ 
/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_
_/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \__
__/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \___
_ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ __
 \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _
\ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ 
/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_
_/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \__
__/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \___
_ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ __
 \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _
\ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ 
/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_
_/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \__
__/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \___
_ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ ___ \ \ \_/ __
 \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _ \ \ \___/ _
\ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ \ \ \_____/ 
/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_/ / / ___ \_
_/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \___/ / / _ \__
__/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \_____/ / / \___

The program

#!ek9
defines module bigbook.shiningcarpet

  defines constant

    //The book's X_REPEAT and Y_REPEAT: copies of the motif across, and down.
    XRepeat <- 6
    YRepeat <- 4

  defines class

    //THE OUTPUT: everything the reader sees goes through here. The carpet has no words, so
    //there is no Say, and it reads no input, so there is no LineInput.
    Console
      stdout as Stdout: Stdout()

      //The carpet is computed: it is shown as the game drew it.
      showCarpet()
        -> lines as List of String
        for line in lines
          stdout.println(line)

      override operator ? as pure
        <- rtn as Boolean: true

  defines function

    //THE GAME: the carpet, as lines, with no output.

    //The book's six pattern lines: one tile of the carpet. They are the material the carpet
    //is tessellated from - each is repeated across and the six are repeated down - so they are
    //data, like Hex Grid's tiles, not a picture printed as it stands.
    motifLines() as pure
      <- rtn as List of String: [
        "_ \\ \\ \\_/ __",
        " \\ \\ \\___/ _",
        "\\ \\ \\_____/ ",
        "/ / / ___ \\_",
        "_/ / / _ \\__",
        "__/ / / \\___"
      ]

    //A pure function may not change a variable once it is set (E08120), so there is no
    //accumulator here: n copies are one copy followed by n - 1 copies.
    repeated() as pure
      ->
        text as String
        times as Integer
      <- rtn as String: times > 0 <- text + repeated(text, times - 1) : ""

    carpetLines()
      <- rtn as List of String: List() of String
      motif <- motifLines()
      for copyDown in 1 ... YRepeat
        for line in motif
          rtn += repeated(line, XRepeat)

  defines program

    //Shining Carpet (Big Book project 65). Tessellates the hotel-carpet motif from six pattern
    //lines. The game decides; the Console shows it.
    ShiningCarpet()
      console <- Console()

      console.showCarpet(carpetLines())

About this port