ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: ComputerCrafter on Apr 13, 2024, 03:07 AM

Poll
Question: Which idea should I use to check if a character is matched?
Option 1: LOTS of If Statements votes: 0
Option 2: If Statements with Functions votes: 0
Option 3: Use custom Table API votes: 1
Option 4: Use different methods than above votes: 0
Option 5: Use Variables instead of Tables votes: 0
Title: Need help getting tables to work.
Post by: ComputerCrafter on Apr 13, 2024, 03:07 AM
I was trying to make a character guessing game...but it didn't go so well.
The goal was to have a table-config system where you can add custom characters and prompts.

I couldn't make it work because the wiki site was down, and finding a way to check the table prompts was too hard.

I hope some more well-versed coders here can help me solve my problem! :)
I've already tried using variables, and using if statements (that's going to be very hard because of the sheer number of characters), and they didn't work.

Likewise, I added a poll if you want to vote for different ideas on how to solve these problems.
Title: Need help getting tables to work.
Post by: martsadas on Apr 13, 2024, 09:19 PM
local letters = {'a', 'c', 'e', 'f', 'm', 'o', 'p', 'r', 't', 'u'}

local function guess(letter)
    -- not not converts it to a bool

    return not not table.concat(letters):find(letter)
end
Title: Need help getting tables to work.
Post by: Purrcival on Jun 12, 2024, 11:03 PM
Quote from: ComputerCrafter on Apr 13, 2024, 03:07 AM-snip-
Tables are sorted as lists in JSON, but with a different format to it.
Here's one example. With whitespaces it can look like this:
{
  ["key"] = "value",
  [123] = 1246409.249,
  [0x1] = 0xff,
}
And another without whitespaces.
{1,"2",0x3}
Basically tables help out as databases for your programs, such as info they can retrieve. However if you want it to look through the list based on the function it's trying to execute, you can use the for loop like this:
for key, value in pairs(tableVariableName) do
    -- functionality here
end
Table lengths can be done via the # symbol like #tableVariableName for example. When this method is done, it returns the length of the table, which is highly better than indexing it to get it's length.

There are more documentation of functions with the table API built-in to Lua, so don't feel afraid to hop on this website (https://lua.org/docs/) to look at the documentation. To find the version, just simply open the lua program on your CC computer and type this and press enter. Your shell should look similar to this.
CraftOS 1.9
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> _VERSION
"Lua 5.1"
lua> _
This is what it looked like on my end, however I'm not sure about yours since your copy is different. Hope you get successful with the fun program you're making!