A name board
A board at the entrance of your base: a frame of = signs on a display wall, a greeting in capitals and the name of the owner in the middle. However the name is typed – sTEVE, steve or STEVE – the board shows Steve, and a name that is too long is cut off with ....
Functions: UCASE, LCASE, LEFT, RIGHT, SPACE, FILL, and DIV of c0de/math
What you need
A trigger block with a button, a display wall of 2 × 1 display blocks (32 columns and 8 rows) and a variable item name of type STRING with the name, for example sTEVE. Optionally a second variable item title of type STRING for another greeting. For the first exercise: a variable item edge of type CHAR.
The idea
A screen is a grid of characters, and every row has the same width. To put a text in the middle, you need as many spaces in front of it as behind it; to draw a line, you need the same character many times. c0de/strings makes both texts for you: SPACE gives a number of spaces, FILL a number of copies of one character.
The name needs two more things. UCASE and LCASE change the case of a whole text, so a name gets its capital first letter in two parts: LEFT takes the first letter for UCASE, RIGHT the rest for LCASE. And LEFT also cuts a name that doesn't fit, so it never wraps into the next row and breaks the frame.
The program
PROGRAM nameboard
NOOP
INCLUDE c0de/strings
INCLUDE c0de/math
NOOP
CONSTANT WIDTH INTEGER 32
NOOP
FUNCTION center
PARAMETER text STRING
RESULT result STRING
BEGIN
VARIABLE inner INTEGER
VARIABLE length INTEGER
VARIABLE before INTEGER
VARIABLE left STRING
VARIABLE right STRING
ASSIGN inner WIDTH - 2 ;
CALL LEN
PARAMETER value text ;
RESULT result length
IF length > inner ;
BEGIN
CALL c0de/strings/LEFT
PARAMETER text text ;
PARAMETER length inner - 3 ;
RESULT result text
ASSIGN text text ⊕ "..." ;
ASSIGN length inner ;
END IF
CALL c0de/math/DIV
PARAMETER dividend inner - length ;
PARAMETER divisor 2 ;
RESULT result before
CALL c0de/strings/SPACE
PARAMETER count before ;
RESULT result left
CALL c0de/strings/SPACE
PARAMETER count inner - length - before ;
RESULT result right
ASSIGN result "|" ⊕ left ⊕ text ⊕ right ⊕ "|" ;
END FUNCTION
NOOP
FUNCTION show
PARAMETER name STRING
PARAMETER title STRING "Welcome to the base of"
BEGIN
VARIABLE border STRING
VARIABLE line STRING
VARIABLE first STRING
VARIABLE rest STRING
VARIABLE length INTEGER
CALL c0de/strings/FILL
PARAMETER count WIDTH - 2 ;
PARAMETER char '=' ;
RESULT result border
ASSIGN border "+" ⊕ border ⊕ "+" ;
CALL c0de/strings/UCASE
PARAMETER text title ;
RESULT result title
CALL LEN
PARAMETER value name ;
RESULT result length
CALL c0de/strings/LEFT
PARAMETER text name ;
PARAMETER length 1 ;
RESULT result first
CALL c0de/strings/UCASE
PARAMETER text first ;
RESULT result first
CALL c0de/strings/RIGHT
PARAMETER text name ;
PARAMETER length length - 1 ;
RESULT result rest
CALL c0de/strings/LCASE
PARAMETER text rest ;
RESULT result rest
CALL CLS
CALL PRINT
PARAMETER value border ;
CALL center
PARAMETER text "" ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL center
PARAMETER text title ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL center
PARAMETER text first ⊕ rest ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL center
PARAMETER text "" ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL PRINT
PARAMETER value border ;
END FUNCTIONBuilding and running it
- Build the display wall from two display blocks side by side. Right-click one of them, click the buttons until they show
nameboardandshow, and click ✔. The whole wall is bound. - Set the trigger block to the program
nameboardand the functionshow, and put the variable itemnameinto its input inventory. - Press the button. The wall shows:
+==============================+
| |
| WELCOME TO THE BASE OF |
| Steve |
| |
+==============================+
With the name alexander_the_great_redstone_builder and the title Hello, the name row becomes |Alexander_the_great_redston...|.
How it works
WIDTHis the width of the wall. Everything else is computed from it, so a wall of 3 × 1 blocks needs onlyWIDTH48.- FILL makes the 30
=of the border, and⊕adds the corners. FILL takes a CHAR ('='in single quotes), not a STRING. - UCASE turns the whole title into capitals. The result goes back into the parameter
title, which is a copy, so the caller's value doesn't change. - For the name, LEFT with
length1 gives the first letter and RIGHT withlength - 1all the others. UCASE and LCASE treat each part, andfirst ⊕ restjoins them again. centerframes one row. A text longer than the 30 inner columns is cut by LEFT to 27 characters, and...fills the rest. The free space is split in two with DIV of c0de/math, because/always gives a FLOAT. When the space is odd, the extra space goes behind the text.- SPACE makes the gaps. With
count0 it gives the empty text, so a text that fills the row exactly needs no special case. - Each row is exactly 32 characters wide and ends at the edge of the wall, so PRINT starts the next one in the row below without an empty row.
Try it yourself
- Let the player choose the character of the frame with a variable item
edgeof type CHAR, for example*.
Solution
PROGRAM nameboardborder
NOOP
INCLUDE c0de/strings
INCLUDE c0de/math
NOOP
CONSTANT WIDTH INTEGER 32
NOOP
FUNCTION center
PARAMETER text STRING
RESULT result STRING
BEGIN
VARIABLE inner INTEGER
VARIABLE length INTEGER
VARIABLE before INTEGER
VARIABLE left STRING
VARIABLE right STRING
ASSIGN inner WIDTH - 2 ;
CALL LEN
PARAMETER value text ;
RESULT result length
IF length > inner ;
BEGIN
CALL c0de/strings/LEFT
PARAMETER text text ;
PARAMETER length inner - 3 ;
RESULT result text
ASSIGN text text ⊕ "..." ;
ASSIGN length inner ;
END IF
CALL c0de/math/DIV
PARAMETER dividend inner - length ;
PARAMETER divisor 2 ;
RESULT result before
CALL c0de/strings/SPACE
PARAMETER count before ;
RESULT result left
CALL c0de/strings/SPACE
PARAMETER count inner - length - before ;
RESULT result right
ASSIGN result "|" ⊕ left ⊕ text ⊕ right ⊕ "|" ;
END FUNCTION
NOOP
FUNCTION show
PARAMETER name STRING
PARAMETER title STRING "Welcome to the base of"
PARAMETER edge CHAR '='
BEGIN
VARIABLE border STRING
VARIABLE line STRING
VARIABLE first STRING
VARIABLE rest STRING
VARIABLE length INTEGER
CALL c0de/strings/FILL
PARAMETER count WIDTH - 2 ;
PARAMETER char edge ;
RESULT result border
ASSIGN border "+" ⊕ border ⊕ "+" ;
CALL c0de/strings/UCASE
PARAMETER text title ;
RESULT result title
CALL LEN
PARAMETER value name ;
RESULT result length
CALL c0de/strings/LEFT
PARAMETER text name ;
PARAMETER length 1 ;
RESULT result first
CALL c0de/strings/UCASE
PARAMETER text first ;
RESULT result first
CALL c0de/strings/RIGHT
PARAMETER text name ;
PARAMETER length length - 1 ;
RESULT result rest
CALL c0de/strings/LCASE
PARAMETER text rest ;
RESULT result rest
CALL CLS
CALL PRINT
PARAMETER value border ;
CALL center
PARAMETER text "" ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL center
PARAMETER text title ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL center
PARAMETER text first ⊕ rest ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL center
PARAMETER text "" ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL PRINT
PARAMETER value border ;
END FUNCTIONThe parameter edge has the default '=', so the board still works without the item. With *, the top and bottom rows are +******************************+.
- A door sign: show
Room ofand the name in capitals at the right edge of the wall, without a frame.
Solution
PROGRAM doorsign
NOOP
INCLUDE c0de/strings
NOOP
CONSTANT WIDTH INTEGER 32
NOOP
FUNCTION alignRight
PARAMETER text STRING
RESULT result STRING
BEGIN
VARIABLE gap STRING
CALL c0de/strings/SPACE
PARAMETER count WIDTH ;
RESULT result gap
CALL c0de/strings/RIGHT
PARAMETER text gap ⊕ text ;
PARAMETER length WIDTH ;
RESULT result result
END FUNCTION
NOOP
FUNCTION show
PARAMETER name STRING
BEGIN
VARIABLE line STRING
CALL c0de/strings/UCASE
PARAMETER text name ;
RESULT result name
CALL alignRight
PARAMETER text "Room of" ;
RESULT result line
CALL PRINT
PARAMETER value line ;
CALL alignRight
PARAMETER text name ;
RESULT result line
CALL PRINT
PARAMETER value line ;
END FUNCTIONRIGHT can align, too: put 32 spaces in front of the text and take the last 32 characters. For Alex the second row is ALEX with 28 spaces in front.