Hopscotch code generator!


#1

Hey guys! Using a programming language, I created a program that creates hopscotch code. It really doesnt have a select purpose, its just fun to mess around and see what you can come up with. Click this link: goo.gl/ZObbGd
And it should take you to a screen that looks something like this: (It might look different for you because I am on my ipad)


All you have to do from here id click the "Execute" Button on the top bar, and on the bottom in the "Terminal" Section will pop up a random bit of hopscotch code! I've gotten some pretty funny things out of it. I'll definitely be updating it more and making it better, but it still works pretty well now!

Warning: If you are on an ipad or iphone it will still work but you will only be able to see the last two lines of code. It works much better on a computer.

Code explanation

This is made in a programming language called Lua, and in this section I want to explain to you how the code works.
Here is the whole code:

math.randomseed(os.time())
function rndString(length)
    all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    e = ""
    for i = 1, length do
      r = math.random(#all)
      e = e.. string.sub(all, r, r)
    end
    return e    
end
function split(pString, pPattern)
     if not pPattern then pPattern = " " end
     local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
     local fpat = "(.-)" .. pPattern
     local last_end = 1
     local s, e, cap = pString:find(fpat, 1)
     while s do
            if s ~= 1 or cap ~= "" then
           table.insert(Table,cap)
            end
            last_end = e+1
            s, e, cap = pString:find(fpat, last_end)
     end
     if last_end <= #pString then
            cap = pString:sub(last_end)
            table.insert(Table, cap)
     end
     return Table
end
function angle(num) 
  return num%360
end
varenv= {"rotation","x","y",}
funcs = {"Check once if:var:op:int;0 to 1000","if:int;0 to 1000:op:var","Leave a trail color:color:sep:int;0 to 100","Repeat times:int;1 to 100",}
colors = {"Purple","Light purple","Baby blue","Very Light Purple","Salmon","Pink","Magenta","Red","Orange","Gold","Yellow","Very light green","Lime green","Green","Turquoise","Very Light blue","Cyan","Light Blue","Blue","Grey","Black","Brown","Light Grey"}
ops = {">","=","<"}
charnames = {"Monkey","Dino","Chillanna","Cosmic Cody","Robo","Star Girl","Octopus","Gorilla","Cupcake","Bear","Frog","Jody","Mr. Mustache","Space Pod","Parrot","Raccoon","Bird"}
blocks = {"Move forward:int;0 to 1000","Flip","Turn:int;0 to 360","Set position to:int;0 to 1000:int;0 to 800","Change X by:int;-1000 to 1000","Change Y by:int;-800 to 800","Set speed:int;100 to 10000","Set angle:int;-360 to 360","Change trail color:color","Change trail width:int;0 to 1000","Clear","Grow by:int;0 to 100","Shrink by:int;0 to 100","Set size percent:int;1 to 300","Set value:string:int;1 to 100","Set value:string:int;1 to 100","Increase value:var:int;1 to 100","Set invisibility:int;0 to 100","Change pose"} --Put in sounds, you skipped them
function hopscotch(c)
  for i=1,math.floor(c/4) do
    infunction = 0
    print(" ")
    print(" ")
    print(charnames[math.random(1,#charnames)]..":")
  for i=1,c do
   if math.random(1,7)==4 then
    --If block
    if infunction>1 then infunction=infunction-1 
    else
    for i=1,infunction do
      io.write("  ")
    end
    infunction=infunction+1
    tblfunc = funcs[math.random(1,#funcs)]
    moves = split(tblfunc, ":")
    io.write(moves[1].."(")
    for l=2,#moves do
      if l==2 then
        if moves[l]=="0" then
          break
        end
      end
      movesmeta = split(moves[l],";")
     if movesmeta[1]=="var" then
        g = varenv[math.random(1,#varenv)]
     elseif movesmeta[1]=="op" then
       g = ops[math.random(1,#ops)]
     elseif movesmeta[1]=="int"then
        rnd = split(movesmeta[2]," to ")
        g=math.random(tonumber(rnd[1]),tonumber(rnd[2]))
    elseif movesmeta[1]=="string" then
      g = rndString(math.random(5,20))
    elseif movesmeta[1]=="color" then
      g = colors[math.random(1,#colors)]
    elseif movesmeta[1]=="sep" then
      g=","
     end
          io.write(g)
    end
    print(")")
    end 
   else
    move = blocks[math.random(1,#blocks)]
    moves = split(move, ":")
    for i=1,infunction do
      io.write("  ")
    end
    io.write(moves[1].."(")
     for l=2,#moves do
    movesmeta = split(moves[l],";")
     --Add new types here
     if movesmeta[1]=="int"then
        rnd = split(movesmeta[2]," to ")
        g=math.random(tonumber(rnd[1]),tonumber(rnd[2]))
    elseif movesmeta[1]=="string" then
      g = rndString(math.random(5,20))
    elseif movesmeta[1]=="color" then
      g = colors[math.random(1,#colors)]
    elseif movesmeta[1]=="var" then
        g = varenv[math.random(1,#varenv)]
    end
      if l==#moves then
          io.write(g)
      else
        io.write(g..",")
        if moves[1]=="Set value" then
          table.insert(varenv,g)
        end
      end
    end
    print(")")
  end
  end
end
end
hopscotch(10)

So let's break this down.

 math.randomseed(os.time())
    function rndString(length)
        all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        e = ""
        for i = 1, length do
          r = math.random(#all)
          e = e.. string.sub(all, r, r)
        end
        return e    
    end
    function split(pString, pPattern)
         if not pPattern then pPattern = " " end
         local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
         local fpat = "(.-)" .. pPattern
         local last_end = 1
         local s, e, cap = pString:find(fpat, 1)
         while s do
                if s ~= 1 or cap ~= "" then
               table.insert(Table,cap)
                end
                last_end = e+1
                s, e, cap = pString:find(fpat, last_end)
         end
         if last_end <= #pString then
                cap = pString:sub(last_end)
                table.insert(Table, cap)
         end
         return Table
    end
    function angle(num) 
      return num%360
    end

This piece of code is just defining some functions (abilities) that I can use later. First we define one called rndString, which will generate a random string of letters of a certain length. We then define a split function, which basically will take some text:

test1|test2|test3|test4

And split it by a certain character. What this means is that if we split that text by "|" it will give us a list of these:

test1
test2
test3
test4

And we define a function called angle, which basically tells us what angle you would be at if you turn a certain amount.
Now we have to sort through this code:

varenv= {"rotation","x","y",}
funcs = {"Check once if:var:op:int;0 to 1000","if:int;0 to 1000:op:var","Leave a trail color:color:sep:int;0 to 100","Repeat times:int;1 to 100",}
colors = {"Purple","Light purple","Baby blue","Very Light Purple","Salmon","Pink","Magenta","Red","Orange","Gold","Yellow","Very light green","Lime green","Green","Turquoise","Very Light blue","Cyan","Light Blue","Blue","Grey","Black","Brown","Light Grey"}
ops = {">","=","<"}
charnames = {"Monkey","Dino","Chillanna","Cosmic Cody","Robo","Star Girl","Octopus","Gorilla","Cupcake","Bear","Frog","Jody","Mr. Mustache","Space Pod","Parrot","Raccoon","Bird"}
blocks = {"Move forward:int;0 to 1000","Flip","Turn:int;0 to 360","Set position to:int;0 to 1000:int;0 to 800","Change X by:int;-1000 to 1000","Change Y by:int;-800 to 800","Set speed:int;100 to 10000","Set angle:int;-360 to 360","Change trail color:color","Change trail width:int;0 to 1000","Clear","Grow by:int;0 to 100","Shrink by:int;0 to 100","Set size percent:int;1 to 300","Set value:string:int;1 to 100","Set value:string:int;1 to 100","Increase value:var:int;1 to 100","Set invisibility:int;0 to 100","Change pose"} --Put in sounds, you skipped them
function hopscotch(c)
  for i=1,math.floor(c/4) do
    infunction = 0
    print(" ")
    print(" ")
    print(charnames[math.random(1,#charnames)]..":")
  for i=1,c do
   if math.random(1,7)==4 then
    --If block
    if infunction>1 then infunction=infunction-1 
    else
    for i=1,infunction do
      io.write("  ")
    end
    infunction=infunction+1
    tblfunc = funcs[math.random(1,#funcs)]
    moves = split(tblfunc, ":")
    io.write(moves[1].."(")
    for l=2,#moves do
      if l==2 then
        if moves[l]=="0" then
          break
        end
      end
      movesmeta = split(moves[l],";")
     if movesmeta[1]=="var" then
        g = varenv[math.random(1,#varenv)]
     elseif movesmeta[1]=="op" then
       g = ops[math.random(1,#ops)]
     elseif movesmeta[1]=="int"then
        rnd = split(movesmeta[2]," to ")
        g=math.random(tonumber(rnd[1]),tonumber(rnd[2]))
    elseif movesmeta[1]=="string" then
      g = rndString(math.random(5,20))
    elseif movesmeta[1]=="color" then
      g = colors[math.random(1,#colors)]
    elseif movesmeta[1]=="sep" then
      g=","
     end
          io.write(g)
    end
    print(")")
    end 
   else
    move = blocks[math.random(1,#blocks)]
    moves = split(move, ":")
    for i=1,infunction do
      io.write("  ")
    end
    io.write(moves[1].."(")
     for l=2,#moves do
    movesmeta = split(moves[l],";")
     --Add new types here
     if movesmeta[1]=="int"then
        rnd = split(movesmeta[2]," to ")
        g=math.random(tonumber(rnd[1]),tonumber(rnd[2]))
    elseif movesmeta[1]=="string" then
      g = rndString(math.random(5,20))
    elseif movesmeta[1]=="color" then
      g = colors[math.random(1,#colors)]
    elseif movesmeta[1]=="var" then
        g = varenv[math.random(1,#varenv)]
    end
      if l==#moves then
          io.write(g)
      else
        io.write(g..",")
        if moves[1]=="Set value" then
          table.insert(varenv,g)
        end
      end
    end
    print(")")
  end
  end
end
end
hopscotch(10)

This code is where everything happens. First, let's look at the beginning:

varenv= {"rotation","x","y",}
funcs = {"Check once if:var:op:int;0 to 1000","if:int;0 to 1000:op:var","Leave a trail color:color:sep:int;0 to 100","Repeat times:int;1 to 100",}
colors = {"Purple","Light purple","Baby blue","Very Light Purple","Salmon","Pink","Magenta","Red","Orange","Gold","Yellow","Very light green","Lime green","Green","Turquoise","Very Light blue","Cyan","Light Blue","Blue","Grey","Black","Brown","Light Grey"}
ops = {">","=","<"}
charnames = {"Monkey","Dino","Chillanna","Cosmic Cody","Robo","Star Girl","Octopus","Gorilla","Cupcake","Bear","Frog","Jody","Mr. Mustache","Space Pod","Parrot","Raccoon","Bird"}
blocks = {"Move forward:int;0 to 1000","Flip","Turn:int;0 to 360","Set position to:int;0 to 1000:int;0 to 800","Change X by:int;-1000 to 1000","Change Y by:int;-800 to 800","Set speed:int;100 to 10000","Set angle:int;-360 to 360","Change trail color:color","Change trail width:int;0 to 1000","Clear","Grow by:int;0 to 100","Shrink by:int;0 to 100","Set size percent:int;1 to 300","Set value:string:int;1 to 100","Set value:string:int;1 to 100","Increase value:var:int;1 to 100","Set invisibility:int;0 to 100","Change pose"} --Put in sounds, you skipped them

This might look really complicated, but it is actually pretty simple! We are just defining some tables (lists) here that we will use later.

First we define varenv, which stands for Variable Environment. This is just where all the variables of the different characters are stored. For example, if we create a new variable "test" with the value "testing" we will insert the following text into varenv:

test:testing

Right now it has the default variables, x, y, and rotation in it.

Next we define a list called funcs. This is where we store all of the control flow blocks that you can place other blocks in (Such as check once if, repeat times, etc.)

Now you can see that we don't just have "Check once if" in the list, we have this:
Check once if:var:op:int;0 to 1000

Well this is part of a special syntax my program uses to understand how to use the blocks. I'll talk about that later.

We have a list of all the colors that hopscotch has,
We have a list of all the operations that you can put in check once if, which means what you use to compare in a check once if block, which is either
Is it equal to?
Is it greater than?
or Is it less than?

Then we have a list called charnames. This is a list of all the characters names in Hopscotch.

And lastly, we have what makes all the code work, a list called blocks.
Blocks is where we store all of the blocks that hopscotch has. But, as you saw with the function list, it isn't just storing the blocks in plaintext. Move forward is this:
Move forward:int;0 to 1000

Set trail color is this:
Change trail color:color

And set value is this:
Set value:string:int;1 to 100

But why do they look like this? Well, all of these blocks take arguments. Now, you might be wondering what an argument is. No, it isn't when you and your sister fight :stuck_out_tongue: . This is a different meaning of the word, which means what you put in the blank place in the block.

So, if we have the set position block, it takes 2 arguments, the x and the y you want to teleport your character to.

Well, we need a way to describe to the computer what to put in those blank spaces in the block.
Let's take our set position block and break it down:

Set position to:int;0 to 1000:int;0 to 800

As you can see, every "statement" is seperated by a colon. Let's look at each of them.

Set position to

This is just the name of the block, and what will show up first.

int;0 to 1000

This is the first argument it takes, the x coordinate. What the "int" is saying is that it is an integer, meaning any number. The second part:

0 to 1000

Just says that the first argument has to be a number between 0 and 1000.

And then the last statement:

int;0 to 800

Is the second argument, the y coordinate. This just says it is an integer (a number) from 0 to 800.
And with that the computer now knows how to use the block!
It is the same for all the rest of the blocks, but instead of numbers sometimes you give it a color or some text or anything else.

And after that, the rest of the code just takes all those blocks and makes them into readable hopscotch code! (If you want a more in depth tutorial about how that happens just ask)

So, that is how the code works, thanks for reading!


Hopscotch quiz demo in python
#2

This is very cool. I tried it out.


#3

Love this so much BuildASnowman!!! Can you share how you made this (or is it a secret? :stuck_out_tongue:)

Can't wait to see future iterations!
Ian


#4

Well, because Hopscotch doesnt have a way to manipulate strings of text, I made this using a different programming language. You can see the code for it in the link, in between "Execute" and the terminal.
I'll be posting an explanation of the how the code works soon.


#5

This is very tricky coding. You are an expert!


#6

This thing is so Cool @BuildASnowman :smiley:


How to make a great project
#7

Amazing and very cool @BuildASnowman ! Love it, good work! Good way to find a random code if you are bored and want to make a project :joy: :grinning:


#9

Very cooooooool @BuildASnowman


#10

@BuildASnowman this is rad.


#11

So amazing and cool! I actually tried to use the Hopscotch Code that was in the Terminal box, in the Terminal app on my MacBook @BuildASnowman!


#12

You could be a future coder if you wanted to.
Hopscotch is proud of you.


#13

Thank you sooooooo muuuuucccchhh!


#14

Command Prompt

I love it @BuildASnowman! But can you possibly make it cmd instead?

Sorry! :wink:


#15

@H4x0r What do you mean by make it cmd?
If you copy the code you can run it on cmd if you install lua. Just type "lua .lua

If you mean make it so that you can run it on cmd without installing lua, I don't know enough batch programming to do that.


#16

cool!!!!!!!!!!!! @BuildASnowman


#17

@Bubblyoreo, I actulley want to be a programer.


#18

When I type it in this comes up:


I typed it in exactly


#19

That's awesome! We should make it a challenge: do your generator, then create it in Hopscotch, and the funniest out of only that code wins! I don't know how thrilling that'd be, though.

@Gabe_N Try copy and pasting the link. It worked for me


#20

Never mind I found out how to get there and did you know that they do JavaScript tutorials there:grinning:?


#21

@Gabe_N I am wondering the same thing. Anyways @BuildASnowman THIS IS AWESOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!