Im trying to make a simple guessing game in lua for the ti nspire cx (code below) how do i convert this to a .tns file? Question Surprised Confused Laughing Thanks!


Code:

-- Number Guessing Game

-- Initialize variables
local targetNumber = math.random(1, 100)
local guess = 0
local attempts = 0
local maxAttempts = 10

-- Function to display a message
function displayMessage(message)
    print(message)
end

-- Function to check the player's guess
function checkGuess(playerGuess)
    attempts = attempts + 1
    if playerGuess < targetNumber then
        displayMessage("Too low! Try again.")
    elseif playerGuess > targetNumber then
        displayMessage("Too high! Try again.")
    else
        displayMessage("Congratulations! You guessed it right!")
        return true
    end
   
    if attempts >= maxAttempts then
        displayMessage("Sorry, you've run out of attempts. The number was " .. targetNumber)
        return true
    end
   
    return false
end

-- Main game loop
function gameLoop()
    displayMessage("Welcome to the Number Guessing Game!")
    displayMessage("Guess a number between 1 and 100.")

    while true do
        displayMessage("Enter your guess: ")
        guess = tonumber(io.read())
       
        if guess then
            local gameOver = checkGuess(guess)
            if gameOver then
                break
            end
        else
            displayMessage("Please enter a valid number.")
        end
    end
end

-- Start the game
gameLoop()
You can use Luna to convert Lua code to TNS files.
Also, that's not TI-Nspire compatible.
TI doesn't use/provide the io API, they have added an Nspire event-based API (and various other modules) so you need to use that.

Check https://compasstech.com.au/TNS_Authoring/Scripting/lua_tutorials.html and https://wiki.inspired-lua.org in addition to the official docs https://education.ti.com/en/resources/lua-scripting
Adriweb wrote:
Also, that's not TI-Nspire compatible.
TI doesn't use/provide the io API, they have added an Nspire event-based API (and various other modules) so you need to use that.

Check https://compasstech.com.au/TNS_Authoring/Scripting/lua_tutorials.html and https://wiki.inspired-lua.org in addition to the official docs https://education.ti.com/en/resources/lua-scripting


Ok thanks! That's a another problem I was having as well like I wasn't sure what was compatible with the nspire
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement