Discuss Scratch

PullJosh
Scratcher
1000+ posts

Counting number of custom blocks in script?

I've got an idea for a game which involves seeing inside and modifying code to play. You have to solve puzzles using provided blocks, and a portion of the scoring is based on how many custom blocks are used. I'm trying to think of a way in which to count the number of custom blocks are contained within the script (using the script itself). It would be easy if you did something like this:

when i receive [run level v]
custom block 1
custom block 2 :: custom
custom block 1
custom block 1
custom block 1
custom block 2 :: custom

define custom block 1
if <(mode) = [blockcount]> then
change [count v] by (1)
else
run block like normal :: grey
end

However, there's a catch: If a player uses a loop (which is allowed), the blocks will be counted incorrectly. (I want to count the number of individual blocks used, not the amount of times they are run.)

repeat (4)
custom block 1 :: custom // reports 4, but should report 1
end

Is there a way to solve this issue?
wizzwizz7
Scratcher
500+ posts

Counting number of custom blocks in script?

Why not have a list?
define custom block 1
if <(mode)=[blockcount]> then
replace item (1 v) of [blocks v] with [1]
else
run block like normal::grey
end

define custom block 2
if <(mode)=[blockcount]> then
replace item (2 v) of [blocks v] with [1]
else
run block like normal::grey
end
… And so on. Then, at the beginning, you could have this:
when I receive [run level v]
delete (all v) of [blocks v]
repeat (number of custom blocks::grey)
add [0] to [blocks v]
end
level code here::grey
What do you think?

Last edited by wizzwizz7 (Sept. 5, 2016 16:58:02)




turkey3
Scratcher
1000+ posts

Counting number of custom blocks in script?

I like this idea.

PullJosh
Scratcher
1000+ posts

Counting number of custom blocks in script?

wizzwizz7 wrote:

What do you think?
Sorry, I suppose I wasn't clear… it's possible that they will include the same block more than once, and it should count more than once in that case.

So this script:
repeat (99)
custom block 1 :: custom
custom block 2 :: custom
custom block 1 :: custom
Should output 3.
wizzwizz7
Scratcher
500+ posts

Counting number of custom blocks in script?

PullJosh wrote:

wizzwizz7 wrote:

What do you think?
Sorry, I suppose I wasn't clear… it's possible that they will include the same block more than once, and it should count more than once in that case.

So this script:
repeat (99)
custom block 1 :: custom
custom block 2 :: custom
custom block 1 :: custom
Should output 3.
No, there's no way to solve it. Unless, of course, you render the blocks with pen and have them in the project…



PullJosh
Scratcher
1000+ posts

Counting number of custom blocks in script?

Okay, I've got a bit of an idea, but not a full working concept:

A script will run until it hits one of a few specific points, and then move on to run the next script in line. After running each script to a stopping point, it will restart back at the current position in the first script and continue moving.

One of those stop points is a wait block, and another is the bottom of a loop. This is the key to the idea. What if we had another script running which would keep track of the stop points (by constantly increasing a variable, or something like that) and used that to find out when loops restart. Could that allow the blocks to be accurately counted?

A practical example:

when gf clicked
forever
change [loop checker v] by (1)

define custom block
if <(loop checker) > [0]> then
switch costume to [costume 2 v]

when gf clicked // In this variation, no loop is used.
switch costume to [costume 1 v]
set [loop checker v] to [0]
custom block
custom block
custom block
custom block
custom block
custom block
custom block
custom block
custom block
custom block
... // Still costume 1

Or, alternatively: :: motion

when gf clicked // This variation uses a loop instead
switch costume to [costume 1 v]
set [loop checker v] to [0]
repeat (10)
custom block
end
... // Now on costume 2

The loop is being detected (and the costume is being changed), because when the loop hits the bottom, the other script (forever loop checker++) is given a chance to run.


I don't know exactly how this would play out to count the custom blocks being used, but it might be a good place to start…
PullJosh
Scratcher
1000+ posts

Counting number of custom blocks in script?

Okay, I'm getting somewhere with this. I now have a script which will convert a script into a list of actions. Here's an example:

when I receive [run bot v]
block 1 :: custom
repeat (2)
block 1 :: custom
block 2 :: custom
end
block 2 :: custom
block 1 :: custom
block 2 :: custom

will turn into the following list:

block 1
block 1
block 2
loop end
block 1
block 2
loop end
block 2
block 1
block 2
loop end

A few things to note:
1) An additional “loop end” is added when the script finishes. This is trivial to remove.
2) Adding a “wait” block will result in a massive amount of “loop end” items to be added to the list. (wait 0 secs results in 1 “loop end” being added; wait one 1 secs results in 21 being added)

The question now is whether it's possible to work backwards to figure out the original script (or at least how many custom blocks it contained). It seems doable, but I haven't had enough time to really figure it out just yet.

(For anyone interested, the project id is 120108110. Y'all can find the code from that.)
NickyNouse
Scratcher
1000+ posts

Counting number of custom blocks in script?

PullJosh wrote:

Okay, I'm getting somewhere with this. I now have a script which will convert a script into a list of actions. Here's an example:

when I receive [run bot v]
block 1 :: custom
repeat (2)
block 1 :: custom
block 2 :: custom
end
block 2 :: custom
block 1 :: custom
block 2 :: custom

will turn into the following list:

block 1
block 1
block 2
loop end
block 1
block 2
loop end
block 2
block 1
block 2
loop end

A few things to note:
1) An additional “loop end” is added when the script finishes. This is trivial to remove.
2) Adding a “wait” block will result in a massive amount of “loop end” items to be added to the list. (wait 0 secs results in 1 “loop end” being added; wait one 1 secs results in 21 being added)

The question now is whether it's possible to work backwards to figure out the original script (or at least how many custom blocks it contained). It seems doable, but I haven't had enough time to really figure it out just yet.

(For anyone interested, the project id is 120108110. Y'all can find the code from that.)
This is looking super cool so far. Working backwards like that should be possible, but are there conditionals/variables that might cause loops to repeat a varying number of times? Because then nested loops could become close to impossible. Also you'll wanna account for empty repeat blocks
CodeLegend
Scratcher
500+ posts

Counting number of custom blocks in script?

With multiple layers of repeats, this will get very complex very quickly. It might not detect correctly with your hackish solution Like Nicky said, will there be conditionals/variable loops? If so, there's probably no solution. If not, why not do the following?
repeat (10)
block 1::custom
block 2::custom
end

//becomes

repeat the next (2) blocks (10) times::custom
block 1::custom
block 2::custom
This could allow multiple depths, too.

Last edited by CodeLegend (Sept. 5, 2016 22:30:40)

NickyNouse
Scratcher
1000+ posts

Counting number of custom blocks in script?

ooh, what if you did something like
repeat (repeat count (3) :: custom stack)
block 1::custom
end
NickyNouse
Scratcher
1000+ posts

Counting number of custom blocks in script?

CodeLegend wrote:

With multiple layers of repeats, this will get very complex very quickly. It might not detect correctly with your hackish solution Like Nicky said, will there be conditionals/variable loops? If so, there's probably no solution. If not, why not do the following?
repeat (10)
block 1::custom
block 2::custom
end

//becomes

repeat the next (2) blocks (10) times::custom
block 1::custom
block 2::custom
This could allow multiple depths, too.
or a repeat start and repeat end block?

or an extension with a c-shaped repeat block?

Last edited by NickyNouse (Sept. 5, 2016 22:38:52)

Jonathan50
Scratcher
1000+ posts

Counting number of custom blocks in script?

NickyNouse wrote:

or an extension with a c-shaped repeat block?
C-blocks in extensions can't do anything with their inner script (yet)

Not yet a Knight of the Mu Calculus.
CodeLegend
Scratcher
500+ posts

Counting number of custom blocks in script?

NickyNouse wrote:

CodeLegend wrote:

With multiple layers of repeats, this will get very complex very quickly. It might not detect correctly with your hackish solution Like Nicky said, will there be conditionals/variable loops? If so, there's probably no solution. If not, why not do the following?
repeat (10)
block 1::custom
block 2::custom
end

//becomes

repeat the next (2) blocks (10) times::custom
block 1::custom
block 2::custom
This could allow multiple depths, too.
or a repeat start and repeat end block?

or an extension with a c-shaped repeat block?
No, I think this one block is enough.
For multiple levels:
repeat (10)
block 1::custom
repeat (5)
block 2::custom
end
end

//becomes

repeat the next (3) blocks (10) times::custom //notice 3 instead of 2
block 1::custom
repeat the next (1) blocks (5) times::custom
block 2::custom

EDIT: Although it definitely depends on the target audience. A separate block for start and stop might make more sense, depending on the context. Either way, though, I think this method is preferable because the custom blocks themselves don't do anything except add themselves to a list. So the user couldn't mess with things while they're running. Also, the script could be rendered on the stage with indents and whatever fancy symbols you want!

Last edited by CodeLegend (Sept. 5, 2016 23:24:08)

PullJosh
Scratcher
1000+ posts

Counting number of custom blocks in script?

Thanks for all the suggestions guys! Allowing native repeat loops does appear to be impossible, which is a bit disappointing, but I think codelegend's solution (or something similar) should do the trick. I was hoping to also include conditionals, but I suppose that could all be done with more custom blocks.

Having the block-based language built right into the game is also an option, but I sort of like the idea of taking advantage of custom blocks just for the novelty if nothing else.
CodeLegend
Scratcher
500+ posts

Counting number of custom blocks in script?

PullJosh wrote:

Thanks for all the suggestions guys! Allowing native repeat loops does appear to be impossible, which is a bit disappointing, but I think codelegend's solution (or something similar) should do the trick. I was hoping to also include conditionals, but I suppose that could all be done with more custom blocks.

Having the block-based language built right into the game is also an option, but I sort of like the idea of taking advantage of custom blocks just for the novelty if nothing else.
I actually think that Nicky's idea of having a loop start and end might be better.
repeat () ::custom stack
do stuff::custom
---------^::custom //not this ugly of course
PullJosh
Scratcher
1000+ posts

Counting number of custom blocks in script?

CodeLegend wrote:

I actually think that Nicky's idea of having a loop start and end might be better.
Agreed.

Powered by DjangoBB