Basics of Ring

Descrition del contenete del págine

Conversion de old BASIC-programas a Ring por aprender lu elementari de ti-ci lingue.

Etiquettes:

3D Plot

/*
3D Plot

Original version in BASIC:
    Creative Computing (Morristown, New Jersey, USA), ca. 1980.

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-03-15.

Last modified: 20240318T1303+0100.
*/

SPACE = " "
DOT = "*"
WIDTH = 56

// Clear the terminal and move the cursor to the top left position.
func clear() {
    system("clear")
}

// Display the credits and wait for a keypress.
func printCredits() {
    print("3D Plot\n\n")
    print("Original version in BASIC:\n")
    print("    Creative computing (Morristown, New Jersey, USA), ca. 1980.\n\n")
    print("This version in Ring:\n")
    print("    Copyright (c) 2024, Marcos Cruz (programandala.net)\n")
    print("    SPDX-License-Identifier: Fair\n\n")
    print("Press Enter to start the program.\n")
    getString()
}

func a(z) {
    return 30.0 * exp(-z * z / 100.0)
}

func draw() {
    l = 0
    z = 0
    y1 = 0
    line = list(WIDTH)
    x = -30.0
    while x <= 30.0 {
        for pos = 1 to WIDTH {
            line[pos] = SPACE
        }
        l = 0
        y1 = 5 * floor(sqrt(900.0 - x * x) / 5.0)
        y = y1
        while y >= -y1 {
            z = floor(25.0 + a(sqrt(x * x + y * y)) - 0.7 * y)
            if z > l {
                l = z
                line[z] = DOT
            }
            y += -5
        } // y loop
        print(SPACE)
        for pos = 1 to WIDTH {
            print(line[pos])
        }
        print(nl)
        x += 1.5
    } // x loop
}

func main {
    clear()
    printCredits()
    clear()
    draw()
}

Bagels

# Bagels

# Original version in BASIC:
#   D. Resek, P. Rowe, 1978.
#   Creative Computing (Morristown, New Jersey, USA), 1978.

# This version in Ring:
#   Copyright (c) 2024, Marcos Cruz (programandala.net)
#   SPDX-License-Identifier: Fair
#
# Written in 2024-04-05/09
#
# Last modified: 20240409T1055+0200.

ASCII_0 = 48
DIGITS =  3
TRIES  = 20
MAX_DIGIT = 9

# Clear the screen and move the cursor to the home position.
func clearScreen
    system("clear")
end

# Prompt the user to enter a command and return it.
func command(prompt)
    if prompt = null
        prompt = "> "
    end
    print(prompt)
    return getString()
end

# Print the given prompt and wait until the user enters an empty string.
func pressEnter(prompt)
    while command(prompt) != ""
    end
end

# Return `true` if the given string is "yes" or a synonym.
func isItYes(answer)
    switch lower(answer)
    case "ok"
        return true
    case "y"
        return true
    case "yeah"
        return true
    case "yes"
        return true
    else
        return false
    end
end

# Return `true` if the given string is "no" or a synonym.
func isItNo(answer)
    switch lower(answer)
    case "n"
        return true
    case "no"
        return true
    case "nope"
        return true
    else
        return false
    end
end

# Print the given prompt, wait until the user enters a valid yes/no
# string, and return `true` for "yes" or `false` for "no".
func yes(prompt)
    answer = ""
    while not (isItYes(answer) || isItNo(answer))
        answer = command(prompt)
    end
    return isItYes(answer)
end

# Clear the screen, display the credits and wait for a keypress.
func printCredits
    clearScreen()
    print("Bagels\n")
    print("Number guessing game\n\n")
    print("Original source unknown but suspected to be:\n")
    print("    Lawrence Hall of Science, U.C. Berkely.\n\n")
    print("Original version in BASIC:\n")
    print("    D. Resek, P. Rowe, 1978.\n")
    print("    Creative computing (Morristown, New Jersey, USA), 1978.\n\n")
    print("This version in Ring:\n")
    print("    Copyright (c) 2024, Marcos Cruz (programandala.net)\n")
    print("    SPDX-License-Identifier: Fair\n\n")
    pressEnter("Press Enter to read the instructions. ")
end

# Clear the screen, print the instructions and wait for a keypress.
func printInstructions
    clearScreen()
    print("Bagels\n")
    print("Number guessing game\n\n")
    print("I am thinking of a three-digit number that has no two digits the same.\n")
    print("Try to guess it and I will give you clues as follows:\n\n")
    print("   PICO   - one digit correct but in the wrong position\n")
    print("   FERMI  - one digit correct and in the right position\n")
    print("   BAGELS - no digits correct\n")
    pressEnter("\nPress Enter to start. ")
end

# Print the given prompt and return a three-digit number from the user.
func userInput(prompt)
    userNumber = list(DIGITS)
    while true
        inputString = command(prompt)
        if len(inputString) != DIGITS
            print("Remember it's a #{DIGITS}-digit number.\n")
            continue
        end
        if not isDigit(inputString)
            print("What?\n")
            continue
        end
        for digit = 1 to DIGITS
            userNumber[digit] = inputString[digit]
        next
        for digit = 1 to DIGITS
            if find(userNumber, userNumber[digit]) != digit
                print("Remember my number has no two digits the same.\n")
                continue 2
            end
        next
        break
    end
    return userNumber
end

# Init and run the game loop.
func play
    computerNumber = list(DIGITS) # random number
    userNumber = list(DIGITS) # user guess
    score = 0
    fermi = 0  # counter
    pico = 0   # counter
    while true # game loop
        clearScreen()
        for digit = 1 to DIGITS
            computerNumber[digit] = -1
        end
        for digit = 1 to DIGITS
            computerNumber[digit] = random(MAX_DIGIT)
            while find(computerNumber, computerNumber[digit]) != digit
                computerNumber[digit] = random(MAX_DIGIT)
            end
        end
        print("O.K.  I have a number in mind.\n")
        for guess = 1 to TRIES
            if guess < 10
                guessNumber = "#0" + guess
            else
                guessNumber = "#" + guess
            end
            userNumber = userInput("Guess " + guessNumber + ": ")
            fermi = 0
            pico = 0
            for i = 1 to DIGITS
                for j = 1 to DIGITS
                    if computerNumber[i] = userNumber[j]
                        if i = j
                            fermi += 1
                        else
                            pico += 1
                        end
                    end
                next
            next
            for x = 1 to pico
                print("PICO ")
            next
            for x = 1 to fermi
                print("FERMI ")
            next
            if pico + fermi = 0
                print("BAGELS")
            end
            print("\n")
            if fermi = DIGITS
                break
            end
        end # tries loop
        if fermi = DIGITS
            print("You got it!!!\n")
            score += 1
        else
            print("Oh well.\n")
            print("That's #{TRIES} guesses.  My number was ")
            print("" + computerNumber[1] + computerNumber[2] + computerNumber[3] + ".\n")
        end
        if not yes("Play again? ")
            break
        end
    end # game loop
    if score != 0
        print("A #{score}-point bagels, buff!!\n")
    end
    print("Hope you had fun.  Bye.\n")
end

func main()
    printCredits()
    printInstructions()
    play()
end

Bunny

/*
Bunny

Original version in BASIC:
    Creative Computing (Morristown, New Jersey, USA), 1978.

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-03-15.

Last modified: 20240318T1302+0100.
*/

EOL = -1 # end of line identifier
WIDTH = 53
SPACE = ' '

# Clear the terminal and move the cursor to the top left position.
func clearScreen()
    system("clear")
end

# Clear the screen, print the credits and wait for the Enter key.
func printCredits()
    print("Bunny\n\n")
    print("Original version in BASIC:\n")
    print("    Creative Computing (Morristown, New Jersey, USA), 1978.\n\n")
    print("This version in Ring:\n")
    print("    Copyright (c) 2024, Marcos Cruz (programandala.net)\n")
    print("    SPDX-License-Identifier: Fair\n\n")
    print("Press Enter to start the program.\n")
    getString()
end

# Draw the graphic.
func draw()

    letter = ['B', 'U', 'N', 'N', 'Y']
    letters = len(letter)

    data = [
        1, 2, EOL, 0, 2, 45, 50, EOL, 0, 5, 43, 52, EOL, 0, 7, 41, 52, EOL,
        1, 9, 37, 50, EOL, 2, 11, 36, 50, EOL, 3, 13, 34, 49, EOL, 4, 14,
        32, 48, EOL, 5, 15, 31, 47, EOL, 6, 16, 30, 45, EOL, 7, 17, 29, 44,
        EOL, 8, 19, 28, 43, EOL, 9, 20, 27, 41, EOL, 10, 21, 26, 40, EOL,
        11, 22, 25, 38, EOL, 12, 22, 24, 36, EOL, 13, 34, EOL, 14, 33, EOL,
        15, 31, EOL, 17, 29, EOL, 18, 27, EOL, 19, 26, EOL, 16, 28, EOL,
        13, 30, EOL, 11, 31, EOL, 10, 32, EOL, 8, 33, EOL, 7, 34, EOL, 6,
        13, 16, 34, EOL, 5, 12, 16, 35, EOL, 4, 12, 16, 35, EOL, 3, 12, 15,
        35, EOL, 2, 35, EOL, 1, 35, EOL, 2, 34, EOL, 3, 34, EOL, 4, 33,
        EOL, 6, 33, EOL, 10, 32, 34, 34, EOL, 14, 17, 19, 25, 28, 31, 35,
        35, EOL, 15, 19, 23, 30, 36, 36, EOL, 14, 18, 21, 21, 24, 30, 37, 37,
        EOL, 13, 18, 23, 29, 33, 38, EOL, 12, 29, 31, 33, EOL, 11, 13, 17,
        17, 19, 19, 22, 22, 24, 31, EOL, 10, 11, 17, 18, 22, 22, 24, 24, 29,
        29, EOL, 22, 23, 26, 29, EOL, 27, 29, EOL, 28, 29, EOL ]

    line = list(WIDTH) # line buffer
    for x = 1 to WIDTH
        line[x] = SPACE
    end

    dataIndex = 1
    while dataIndex <= len(data)
        firstColumn = data[dataIndex]
        dataIndex += 1
        if firstColumn = EOL
            for x = 1 to WIDTH
                print(line[x])
                line[x] = SPACE
            end
            print(nl)
        else
            lastColumn = data[dataIndex]
            dataIndex += 1
            for column = firstColumn to lastColumn
                line[column + 1] = letter[column % letters + 1]
            end
        end
    end
end

func main
    clearScreen()
    printCredits()
    clearScreen()
    draw()
end

Diamond

/*
Diamond

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-03-15.

Last modified: 20240315T1537+0100.
*/

LINES = 17

func main

    for i = 1 to LINES / 2 + 1
        for j = 1 to (LINES + 1) / 2 - i + 1
            put " "
        next
        for j = 1 to i * 2 - 1
            put "*"
        next
        put nl
    next
    for i = 1 to LINES / 2
        for j = 1 to i + 1
            put " "
        next
        for j = 1 to ((LINES + 1) / 2 - i) * 2 - 1
            put "*"
        next
        put nl
    next

end

Math

/*
Math

Original version in BASIC:
Example included in Vintage BASIC 1.0.3.
http://www.vintage-basic.net

This version in Ring:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair

Written on 2024-03-18 and 2024-04-04.

Last modified: 20240404T1954+0200.
*/

load "stdlib.ring" // `sign()`

// If the given string is a valid number, i.e. it contains only digits and one
// optional dot, return `true`; otherwise return `false`.
func isValidNumber(s)
    decimalPointFound = false
    for n = 1 to len(s)
        if s[n] = "."
            if decimalPointFound
                return false
            else
                decimalPointFound = true
                continue
            end
        elseif not isDigit(s[n])
            if not (n = 1 and s[n] = "-")
                return false
            end
        end
    next
    return true
end

// Prompt the user for a valid number, check it and return it.
func validNumber
    while true
        put "Enter a number: "
        get n
        if isValidNumber(n)
            break
        else
            put "Number expected. Retry." + nl
        end
    end
    return 0 + n
end

func main

    n = validNumber()

    print("ABS(#{n}) --> fabs(#{n}) --> ")
    put fabs(n) + nl
    print("ATN(#{n}) --> atan(#{n}) --> ")
    put atan(n) + nl
    print("COS(#{n}) --> cos(#{n}) --> ")
    put cos(n) + nl
    print("EXP(#{n}) --> exp(#{n}) --> ")
    put exp(n) + nl
    print("INT(#{n}) --> floor(#{n}) --> ")
    put floor(n) + nl
    print("LOG(#{n}) --> log(#{n}) --> ")
    put log(n) + nl
    print("SGN(#{n}) --> sign(#{n}) --> ")
    put sign(n) + nl
    print("SQR(#{n}) --> sqrt(#{n}) --> ")
    put sqrt(n) + nl
    print("TAN(#{n}) --> tan(#{n}) --> ")
    put tan(n) + nl

end

Mugwump

/*
Mugwump

Original version in BASIC:
    Written by Bud Valenti's students of Project SOLO (Pittsburg, Pennsylvania, USA).
    Slightly modified by Bob Albrecht of People's Computer Company.
    Published by Creative Computing (Morristown, New Jersey, USA), 1978.
    - https://www.atariarchives.org/basicgames/showpage.php?page=114
    - http://vintage-basic.net/games.html

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2024-03/04.

Last modified: 20240405T1416+0200.
*/

GRID_SIZE = 10
TURNS = 10
MUGWUMPS = 4

mugwump = list(MUGWUMPS)

found = 0 // counter

# Clear the terminal and move the cursor to the top left position.
func clearScreen()
    system("clear")
end

// Return `true` if the given string is "yes" or a synonym.
func isYes(answer) {
    switch lower(answer) {
        case "y" return true
        case "yeah" return true
        case "yes" return true
        case "ok" return true
        else return false
    }
}

// Return `true` if the given string is "no" or a synonym.
func isNo(answer) {
    switch lower(answer) {
        case "n" return true
        case "no" return true
        case "nope" return true
        else return false
    }
}

// Print the given prompt, wait until the user enters a valid yes/no
// String, and return `true` for "yes" or `false` for "no".
func yes(prompt) {
    answer = ""
    while (!(isYes(answer) || isNo(answer))) {
        print(prompt + nl)
        answer = getString()
    }
    return isYes(answer)
}

// Clear the screen, print the credits and ask the user to press enter.
func printCredits() {
    clearScreen()
    print("Mugwump\n\n")
    print("Original version in BASIC:\n")
    print(" Written by Bud Valenti's students of Project SOLO (Pittsburg, Pennsylvania, USA).\n")
    print(" Slightly modified by Bob Albrecht of People's Computer Company.\n")
    print(" Published by Creative Computing (Morristown, New Jersey, USA), 1978.\n")
    print(" - https://www.atariarchives.org/basicgames/showpage.php?page=114\n")
    print(" - http://vintage-basic.net/games.html\n\n")
    print("This version in Ring:\n")
    print(" Copyright (c) 2024, Marcos Cruz (programandala.net)\n")
    print(" SPDX-License-Identifier: Fair\n\n")
    print("Press Enter to read the instructions. ")
    getString()
}

// Clear the screen, print the instructions and ask the user to press enter.
func printInstructions() {
    clearScreen()
    print("Mugwump\n\n")
    print("The object of this game is to find four mugwumps\n")
    print("hidden on a 10 by 10 grid.  Homebase is position 0,0.\n")
    print("Any guess you make must be two numbers with each\n")
    print("number between 0 and 9, inclusive.  First number\n")
    print("is distance to right of homebase and second number\n")
    print("is distance above homebase.\n\n")
    print("You get #{TURNS} tries.  After each try, you will see\n")
    print("how far you are from each mugwump.\n\n")
    print("Press Enter to start. ")
    getString()
}

// Init the mugwumps' positions, `hidden` flags and count.
func hideMugwumps() {
    for m = 1 to MUGWUMPS {
        mugwump[m].x = random(GRID_SIZE - 1) + 1
        mugwump[m].y = random(GRID_SIZE - 1) + 1
        mugwump[m].hidden = true
    }
    found = 0 // counter
}

// Create the mugwumps.
func createMugwumps() {
    for m = 1 to MUGWUMPS {
        mugwump[m] = new Mugwump { x = 0 y = 0 hidden = false }
    }
}

// Print the given prompt, wait until the user enters a valid coord
// and return it.
func getCoord(prompt) {
    while (true) {
        print(prompt)
        coord = getNumber()
        if (coord < 0 || coord >= GRID_SIZE) {
            print("Invalid value #{coord}: not in range [0, #{GRID_SIZE - 1}].\n")
        else
            break
        }
    }
    return coord
}

// Return `true` if the given mugwump is hidden in the given coords.
func isHere(m, x, y) {
    return mugwump[m].hidden && mugwump[m].x = x && mugwump[m].y = y
}

// Return the distance between the given mugwump and the given coords.
func distance(m, xCoord, yCoord) {
    return floor(sqrt(((mugwump[m].x - xCoord) ** 2) + ((mugwump[m].y - yCoord) ** 2)))
}

// Return a plural ending (default: "s") if the given number is greater than 1;
// otherwise return a singular ending (default: an empty string).
func plural(n, pluralEnding, singularEnding) {
    if (n > 1) {
        if pluralEnding = null {
            return "s" // default
        else
            return pluralEnding
        }
    else
        if singularEnding = null {
            return "" // default
        else
            return singularEnding
        }
    }
}

// Run the game.
func play() {
    createMugwumps()
    while (true) { // game
        clearScreen()
        hideMugwumps()
        turn = 1
        while (turn <= TURNS) {
            print("Turn number #{turn}\n\n")
            print("What is your guess (in range 0..#{GRID_SIZE - 1})?\n")
            x = getCoord("Distance right of homebase (x-axis): ")
            y = getCoord("Distance above homebase (y-axis): ")
            print("Your guess is (#{x}, #{y}).\n")
            for m = 1 to len(mugwump) {
                if (isHere(m, x, y)) {
                    mugwump[m].hidden = false
                    found += 1
                    print("You have found mugwump #{m}!\n")
                    if (found = len(mugwump)) { break 2 }
                }
            }
            for m = 1 to len(mugwump) {
                if (mugwump[m].hidden) {
                    print("You are #{distance(m, x, y)} units from mugwump #{m}.\n")
                }
            }
            print("\n")
            turn += 1
        } // turns
        if (found = len(mugwump)) {
            print("\nYou got them all in #{turn} #{plural(turn, null, null)}!\n")
            print("That was fun! let's play again…\n")
            print("Four more mugwumps are now in hiding.\n")
        else
            print("Sorry, that's #{TURNS} tr" + plural(TURNS, "ies", "y") + "\n")
            print("Here is where they're hiding:\n")
            for m = 1 to len(mugwump) {
                if (mugwump[m].hidden) {
                    print("Mugwump #{m} is at (#{mugwump[m].x}, #{mugwump[m].y}).\n")
                }
            }
        }
        print("\n")
        if (!yes("Do you want to play again? ")) { break }
    } // game
}

func main() {
    printCredits()
    printInstructions()
    play()
}

class Mugwump {
    x = 0
    y = 0
    hidden = false
}

Name

/*
Name

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-03-18.

Last modified: 20240318T1305+0100.
*/


put "What is your name? "
get name

while true
    put "Enter a number: "
    n = getNumber()
    // NOTE: Somehow `isNumber()` does not work as expected with values
    // returned by `get` or `getNumber()`; therefore `isString()` is used
    // instead:
    if not isString(n)
        break
    else
        put "Number expected. Retry." + nl
    end
end

for i = 1 to n
    put "Hello, " + name + "!" + nl
next

Russian Roulette

# Russian Roulette

# Original version in BASIC:
#   Creative Computing (Morristown, New Jersey, USA), ca. 1980.

# This version in Ring:
#   Copyright (c) 2024, Marcos Cruz (programandala.net)
#   SPDX-License-Identifier: Fair
#
# Written on 2024-04-05.
#
# Last modified: 20240405T1441+0200.

# Clears the terminal and moves the cursor to the top left position.
def clear_screen
    system("clear")
end

# Prompts the user to enter a command and returns it.
def command(prompt)
    if prompt = null
        prompt = "> "
    end
    print(prompt)
    return getString()
end

def press_enter_to_start
    command("Press Enter to start. ")
end

def print_credits
    clear_screen()
    print("Russian Roulette\n\n")
    print("Original version in BASIC:\n")
    print("    Creative Computing (Morristown, New Jersey, USA), ca. 1980.\n\n")
    print("This version in Ring:\n")
    print("    Copyright (c) 2024, Marcos Cruz (programandala.net)\n")
    print("    SPDX-License-Identifier: Fair\n\n")
    press_enter_to_start()
end

def print_instructions
    clear_screen()
    print("Here is a revolver.\n")
    print("Type 'f' to spin chamber and pull trigger.\n")
    print("Type 'g' to give up, and play again.\n")
    print("Type 'q' to quit.\n\n")
end

def playing
    while true # game loop
        print_instructions()
        times = 0
        while true # play loop
            switch command("")
            case "f" # fire
                if random(100) > 83
                    print("Bang! You're dead!\n")
                    print("Condolences will be sent to your relatives.\n")
                    break
                else
                    times += 1
                    if times > 10
                        print("You win!\n")
                        print("Let someone else blow his brains out.\n")
                        break
                    else
                        print("Click.\n")
                    end
                end
            case "g" # give up
                print("Chicken!\n")
                break
            case "q" # quit
                return false
            end # switch
        end  # play loop
        press_enter_to_start()
    end # game loop
    return true # play again, do not quit
end

def main
    print_credits()
    while playing()
    end
    print("Bye!\n")
end

Sine Wave

/*
Sine Wave

Original version in BASIC:
    Creative Computing (Morristown, New Jersey, USA), ca. 1980.

This version in Ring:
    Copyright (c) 2023, 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Last modified 20250731T1954+0200.
*/

// global variables
word1 = ""
word2 = ""

func showCredits
    system("clear")
    put "Sine Wave" + nl + nl
    put "Original version in BASIC:" + nl
    put "    Creative Computing (Morristown, New Jersey, USA), ca. 1980." + nl + nl
    put "This version in Ring:" + nl
    put "    Copyright (c) 2023, 2024, Marcos Cruz (programandala.net)" + nl
    put "    SPDX-License-Identifier: Fair" + nl + nl
    put "Press Enter to start the program." + nl
    getChar()
end

func getWords
    system("clear")
    put "Enter the first word  : "
    get word1
    put "Enter the second word : "
    get word2
    put "Press any key to start. "
    getchar()
end

func draw
    even = false
    for angle = 0 to 40 step 0.25
        put copy(" ", 26 + 25 * sin(angle))
        if even
            put word1 + nl
        else
            put word2 + nl
        end
        even = not even
    next
end

func main
    showCredits()
    getWords()
    draw()
end

Stars

/*
Stars

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-03-18.

Last modified: 20240318T1944+0100.
*/

load "typehints.ring"

// Display the given question, accept a Y/N answer and return a correspondent
// boolean result.
func yes(question)

    afirmativeAnswers = ["y", "yeah", "yes", "ok"]
    negativeAnswers = ["n", "no", "nope"]

    while true

        print(question)
        answer = lower(getString())

        if find(afirmativeAnswers, answer)
            return true
        elseif find(negativeAnswers, answer)
            return false
        end

    end

end

// Display the given prompt, accept a valid number and return it.
func inputNumber(prompt)

    while true
        print(prompt)
        get n
        if isDigit(n)
            break
        else
            put "Number expected. Retry." + nl
        end
    end
    return 0 + n

end

func main

    puts("What is your name? ")
    name = getString()
    print("Hello, #{name}.\n")

    while true
        n = inputNumber("How many stars do you want? ")
        put copy("*", n) + nl
        if not yes("Do you want more stars? ")
            break
        end
    end

    print("Goodbye, #{name}.\n")

end

Strings

/*
Strings

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Ring:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2024-03, 2024-04.

Last modified: 20240411T0954+0200.
*/

// Print the given prompt, get an integer from the user, check it and return it.
func validInteger(prompt)
    while true
        put prompt
        get n
        if isDigit(n)
            break
        else
            put "Integer number expected. Retry." + nl
        end
    end
    return 0 + n
end

func main() {

    print("Enter a string: ")
    s = getString()
    n = validInteger("Enter a number: ")

    print(`ASC("#{s}") --> `)
    print(`ascii(left("#{s}", 1)) --> `)
    ? ascii(left(s, 1))

    print(`CHR$(#{n}) --> `)
    print(`char(#{n}) --> `)
    ? char(n)

    print(`LEFT$("#{s}", #{n}) --> `)
    print(`left("#{s}", n) --> `)
    ? left(s,n)

    print(`MID$("#{s}", #{n}) --> `)
    print(`substr("#{s}", #{n}) --> `)
    ? substr(s, n)

    print(`MID$("#{s}", #{n}, 3) --> `)
    print(`substr("#{s}", #{n}, 3) --> `)
    ? substr(s, n, 3)

    print(`RIGHT$("#{s}", #{n}) --> `)
    print(`right("#{s}", n) --> `)
    ? right(s,n)

    print(`LEN("#{s}") --> `)
    print(`len("#{s}") --> `)
    ? len(s)

    print(`VAL("#{s}") --> `)
    print(`number("#{s}") --> `)
    ? number(s)
    print(`VAL("#{s}") --> `)
    print(`0 + "#{s}" --> `)
    ? 0 + s

    print(`STR$(#{n}) --> `)
    print(`string(#{n}) --> `)
    ? string(n)

    print(`SPC(#{n}) --> `)
    print(`copy(" ", #{n}) --> `)
    ? `"` + copy(" ", n) + `"`

}

Págines relatet

Basics off
Metaprojecte pri li projectes «Basics of…».
Basics of 8th
Conversion de old BASIC-programas a 8th por aprender lu elementari de ti-ci lingue.
Basics of Ada
Conversion de old BASIC-programas a Ada por aprender lu elementari de ti-ci lingue.
Basics of Arturo
Conversion de old BASIC-programas a Arturo por aprender lu elementari de ti-ci lingue.
Basics of C#
Conversion de old BASIC-programas a C# por aprender lu elementari de ti-ci lingue.
Basics of C3
Conversion de old BASIC-programas a C3 por aprender lu elementari de ti-ci lingue.
Basics of Chapel
Conversion de old BASIC-programas a Chapel por aprender lu elementari de ti-ci lingue.
Basics of Clojure
Conversion de old BASIC-programas a Clojure por aprender lu elementari de ti-ci lingue.
Basics of Crystal
Conversion de old BASIC-programas a Crystal por aprender lu elementari de ti-ci lingue.
Basics of D
Conversion de old BASIC-programas a D por aprender lu elementari de ti-ci lingue.
Basics of Elixir
Conversion de old BASIC-programas a Elixir por aprender lu elementari de ti-ci lingue.
Basics of F#
Conversion de old BASIC-programas a F# por aprender lu elementari de ti-ci lingue.
Basics of Factor
Conversion de old BASIC-programas a Factor por aprender lu elementari de ti-ci lingue.
Basics of FreeBASIC
Conversion de old BASIC-programas a FreeBASIC por aprender lu elementari de ti-ci lingue.
Basics of Gleam
Conversion de old BASIC-programas a Gleam por aprender lu elementari de ti-ci lingue.
Basics of Go
Conversion de old BASIC-programas a Go por aprender lu elementari de ti-ci lingue.
Basics of Hare
Conversion de old BASIC-programas a Hare por aprender lu elementari de ti-ci lingue.
Basics of Haxe
Conversion de old BASIC-programas a Haxe por aprender lu elementari de ti-ci lingue.
Basics of Icon
Conversion de old BASIC-programas a Icon por aprender lu elementari de ti-ci lingue.
Basics of Io
Conversion de old BASIC-programas a Io por aprender lu elementari de ti-ci lingue.
Basics of Janet
Conversion de old BASIC-programas a Janet por aprender lu elementari de ti-ci lingue.
Basics of Julia
Conversion de old BASIC-programas a Julia por aprender lu elementari de ti-ci lingue.
Basics of Kotlin
Conversion de old BASIC-programas a Kotlin por aprender lu elementari de ti-ci lingue.
Basics of Lobster
Conversion de old BASIC-programas a Lobster por aprender lu elementari de ti-ci lingue.
Basics of Lua
Conversion de old BASIC-programas a Lua por aprender lu elementari de ti-ci lingue.
Basics of Nature
Conversion de old BASIC-programas a Nature por aprender lu elementari de ti-ci lingue.
Basics of Neat
Conversion de old BASIC-programas a Neat por aprender lu elementari de ti-ci lingue.
Basics of Neko
Conversion de old BASIC-programas a Neko por aprender lu elementari de ti-ci lingue.
Basics of Nelua
Conversion de old BASIC-programas a Nelua por aprender lu elementari de ti-ci lingue.
Basics of Nim
Conversion de old BASIC-programas a Nim por aprender lu elementari de ti-ci lingue.
Basics of Nit
Conversion de old BASIC-programas a Nit por aprender lu elementari de ti-ci lingue.
Basics of Oberon-07
Conversion de old BASIC-programas a Oberon-07 por aprender lu elementari de ti-ci lingue.
Basics of OCaml
Conversion de old BASIC-programas a OCaml por aprender lu elementari de ti-ci lingue.
Basics of Odin
Conversion de old BASIC-programas a Odin por aprender lu elementari de ti-ci lingue.
Basics of Pike
Conversion de old BASIC-programas a Pike por aprender lu elementari de ti-ci lingue.
Basics of Pony
Conversion de old BASIC-programas a Pony por aprender lu elementari de ti-ci lingue.
Basics of Python
Conversion de old BASIC-programas a Python por aprender lu elementari de ti-ci lingue.
Basics of Racket
Conversion de old BASIC-programas a Racket por aprender lu elementari de ti-ci lingue.
Basics of Raku
Conversion de old BASIC-programas a Raku por aprender lu elementari de ti-ci lingue.
Basics of Retro
Conversion de old BASIC-programas a Retro por aprender lu elementari de ti-ci lingue.
Basics of Rexx
Conversion de old BASIC-programas a Rexx por aprender lu elementari de ti-ci lingue.
Basics of Rust
Conversion de old BASIC-programas a Rust por aprender lu elementari de ti-ci lingue.
Basics of Scala
Conversion de old BASIC-programas a Scala por aprender lu elementari de ti-ci lingue.
Basics of Scheme
Conversion de old BASIC-programas a Scheme por aprender lu elementari de ti-ci lingue.
Basics of Styx
Conversion de old BASIC-programas a Styx por aprender lu elementari de ti-ci lingue.
Basics of Swift
Conversion de old BASIC-programas a Swift por aprender lu elementari de ti-ci lingue.
Basics of V
Conversion de old BASIC-programas a V por aprender lu elementari de ti-ci lingue.
Basics of Vala
Conversion de old BASIC-programas a Vala por aprender lu elementari de ti-ci lingue.
Basics of Zig
Conversion de old BASIC-programas a Zig por aprender lu elementari de ti-ci lingue.

Extern ligamentes relatet