Discuss Scratch

LoGaNO_o
Scratcher
74 posts

Encode/Decode Working

So I've been trying to make a save code for a game I'm making and I don't know how to make a encode/decoder work properly. Could anyone help me?

For the future posters –> (Thanks for all the help!)
Spentine
Scratcher
1000+ posts

Encode/Decode Working

You can use this code to encode / decode a list:
define encode
set [save code v] to ()
set [index v] to (0)
repeat (length of [list v])
change [index v] by (1)
set [char index v] to (0)
repeat (length of (item (index) of [list v]))
change [char index v] by (1)
if <[/,] contains (letter (char index) of (item (index) of [list v]))? :: operators> then
set [save code v] to (join (savecode) [/]
end
set [save code v] to (join (save code) (letter (char index) of (item (index) of [list v])))
end
set [save code v] to (join (save code) [,])
end

define decode (string)
delete [all v] of [list v]
set [char index v] to ()
set [index v] to (1)
repeat until <(index) > (length of (string))>
if <(letter (index) of (string)) = [,]> then
add (char index) to [list v]
set [char index v] to ()
else
if <(letter (index) of (string)) = [/]> then
change [index v] by (1)
end
set [char index v] to (join (char index) (letter (index) of (string)))
end
change [index v] by (1)
end
LoGaNO_o
Scratcher
74 posts

Encode/Decode Working

Spentine wrote:

You can use this code to encode / decode a list:
define encode
set [save code v] to ()
set [index v] to (0)
repeat (length of [list v])
change [index v] by (1)
set [char index v] to (0)
repeat (length of (item (index) of [list v]))
change [char index v] by (1)
if <[/,] contains (letter (char index) of (item (index) of [list v]))? :: operators> then
set [save code v] to (join (savecode) [/]
end
set [save code v] to (join (save code) (letter (char index) of (item (index) of [list v])))
end
set [save code v] to (join (save code) [,])
end

define decode (string)
delete [all v] of [list v]
set [char index v] to ()
set [index v] to (1)
repeat until <(index) > (length of (string))>
if <(letter (index) of (string)) = [,]> then
add (char index) to [list v]
set [char index v] to ()
else
if <(letter (index) of (string)) = [/]> then
change [index v] by (1)
end
set [char index v] to (join (char index) (letter (index) of (string)))
end
change [index v] by (1)
end

Well, it works but it is a bit too simple. And by that I mean I could easily see what the thing was encoded, and decode it without the decoder. But other than that it works flawlessly!
LoGaNO_o
Scratcher
74 posts

Encode/Decode Working

Spentine wrote:

You can use this code to encode / decode a list:
define encode
set [save code v] to ()
set [index v] to (0)
repeat (length of [list v])
change [index v] by (1)
set [char index v] to (0)
repeat (length of (item (index) of [list v]))
change [char index v] by (1)
if <[/,] contains (letter (char index) of (item (index) of [list v]))? :: operators> then
set [save code v] to (join (savecode) [/]
end
set [save code v] to (join (save code) (letter (char index) of (item (index) of [list v])))
end
set [save code v] to (join (save code) [,])
end

define decode (string)
delete [all v] of [list v]
set [char index v] to ()
set [index v] to (1)
repeat until <(index) > (length of (string))>
if <(letter (index) of (string)) = [,]> then
add (char index) to [list v]
set [char index v] to ()
else
if <(letter (index) of (string)) = [/]> then
change [index v] by (1)
end
set [char index v] to (join (char index) (letter (index) of (string)))
end
change [index v] by (1)
end

I was sorta going for like encryption almost, like numbers ex. 13275138462356871283895 (That doesn't mean anything)
Spentine
Scratcher
1000+ posts

Encode/Decode Working

You can use it in conjunction with this cloud encoder (but don't use it for cloud).
define encodenum (str)
set [index v] to (0)
set [return v] to ()
repeat (length of (str))
change [index v] by (1)
set [return v] to (join (return) (item # of (letter (index) of (str)) in [chars v] :: list))
end

define decodenum (num)
set [index v] to (0)
set [return v] to ()
repeat ((length of (num)) / (2))
change [index v] by (2)
set [return v] to (join (return) (item (join (letter ((index) - (1)) of (num)) (letter (index) of (num))) of [chars v]))

Before you run the script, add all the characters you will use into the chars list, like a, b, c, 1, 2, 3… and so on. Any capitalization will be lost, so A will become a. There is a limit of 99 characters you are able to add due to limitations.

Last edited by Spentine (March 14, 2023 23:40:38)

Powered by DjangoBB