This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2010-08-10 17:06:16

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Turning this into a block (maybe JD)

Hey all, I noticed that some people have really got a knack for turning scripts into single CYOB blocks... could any of you have a go at this? it would really help  wink

http://dl.dropbox.com/u/6273449/becomes.gif

(clicking "view image" and then magnifying it should make it the full size)

the yellow reporter at the bottom is what the block looks like in the end  smile

Last edited by sparks (2010-08-10 17:07:40)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#2 2010-08-10 17:11:15

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: Turning this into a block (maybe JD)

Ill try


You can now reach me on Twitter @johnnydean1_

Offline

 

#3 2010-08-10 17:12:29

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: Turning this into a block (maybe JD)

BTW you should zoom it in


You can now reach me on Twitter @johnnydean1_

Offline

 

#4 2010-08-10 17:24:03

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Turning this into a block (maybe JD)

If you told me what it DID, I could make it around four lines long.


nXIII

Offline

 

#5 2010-08-10 17:34:58

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Turning this into a block (maybe JD)

it does this: the file has tags for areas like this:
<shelf>
then it has items, with values. first a from-to number out of 99 to indicate how likely they are to get picked (two digits) then three digits that indicate the maximum and minimum value of the objects (three for max and three for min) then the name.

so

<shelf>
0050010020bottle
5199000200bag
</shelf>

the block will pick a random number from o to 99. say it's 32. 32 is more than 00 and less than 50 so bottle is selected, not bag. next the value of the bottle (such as contents in mm) is chosen with another random from it's max to it's min (010 to 020) then the random value and the name is reported... e.g. "015,bottle"


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#6 2010-08-10 17:36:19

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: Turning this into a block (maybe JD)

how to do repeat until I have a meathod but not the most practical...


You can now reach me on Twitter @johnnydean1_

Offline

 

#7 2010-08-10 18:02:09

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: Turning this into a block (maybe JD)

Here without a few things:

Name:
choose from file $String$ with tag $String$

Type
-r

Code:

| homearea tag choice count level output temp temp2 temp3 temp4 temp5 |

homearea_ t1.
tag_ t2.
choice_ self randomFrom: (0) to: (99).
count_ 0.

"Start Repeat Until t2 = true"
count_ count + 1.
temp_ self readLine: (count) ofFile: (homearea).
temp2_ self concatenate: ('<') with: (tag).
temp2_ self concatenate: (tag) with: ('>').
temp = temp2
ifTrue: [
temp2_ true.
]
ifFalse: [
temp2_ false.
].

"End repeat until"
count_ count + 1.


"Start Repeat Until t2 = true"


temp_ self readLine: (count) ofFile: (homearea).
temp2_ self concatenate: ('</') with: (tag).
temp2_ self concatenate: (tag) with: ('>').
temp = temp2
ifTrue: [
temp2_ true.
]
ifFalse: [
temp2_ false.
].




temp4_ 0.
temp2_ self letters: (3) through: (4) of: (temp).
temp2 > choice
ifTrue: [
temp4_ 99
].

temp2 = choice
ifTrue: [
temp4_ temp4 + 33.
].

temp2_ self letters: (1) through: (2) of: (temp).
temp2 < choice
ifTrue: [
temp4_ temp4 + 33.
].

temp2 = choice
ifTrue: [
temp4_ temp4 + 33.
].
temp4_ temp4 + 1.

temp4 = 200
ifTrue: [
temp4_ true.
].
temp4 = 100
ifTrue: [
temp4 _ true.
].
temp4 = true
ifFalse: [
temp4_ false.
].

temp4 = true
ifTrue: [
name_ self readLine: (count) ofFile: (homearea).
level_ self stringLength: (name).
name_ self letters: (11) through: (level) of: (name).
level_ self readLine: (count) ofFile: (homearea).
temp5_ self letters: (5) through: (7) of: (level).
level_ self letters: (8) through: (10) of: (level).
level_ self randomFrom: (temp5) to: (level).

"Stop Script"
].   
count_ count + 1.

"End Repeat Until "

output_ self concatenate: (level) with: (',').
output_ self concatenate: (output) with: (name).

^ output.


You can now reach me on Twitter @johnnydean1_

Offline

 

#8 2010-08-10 18:56:07

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Turning this into a block (maybe JD)

I wrote some of it, refreshed my browser, then ....
I think it works  wink
Tested it, it does! YAY!

chooseFrom: t1 tag: t2
| code i j arr item getItem rnd |
code := self readFile: t1.
i := (code findString: '<',t2,'>') + 2 + t2 size.
j := (code findString: '</',t2,'>') - 1.
code := code copyFrom: i to: j.
arr := OrderedCollection new.
getItem := [:it |
    | i0 j0 k0 l0 |
    i0 := it findString: '-'.
    j0 := it findString: ',' startingAt: i0 + 1.
    k0 := it findString: '-' startingAt: j0 + 1.
    l0 := it findString: ',' startingAt: k0 + 1.
    {(it copyFrom: 1 to: i0 - 1) asNumberNoError.
        (it copyFrom: i0 + 1 to: j0 - 1) asNumberNoError.
        (it copyFrom: j0 + 1 to: k0 - 1) asNumberNoError.
        (it copyFrom: k0 + 1 to: l0 - 1) asNumberNoError.
        it copyFrom: l0 + 1 to: it size}].
[(i := code findString: '|') > 0] whileTrue: [
    item := code copyFrom: 1 to: i - 1.
    arr add: (getItem value: item).
    code := code copyFrom: i + 1 to: code size.
].
arr add: (getItem value: code).
rnd := self randomFrom: 0 to: 99.
arr do: [:it |
    rnd >= it first & (rnd <= it second) ifTrue: [
        rnd := self randomFrom: it third to: it fourth.
        ^ rnd asString,',',it fifth.
    ].
].

Here, let me indent it.

Oh, and here's the new syntax:

Code:

<shelf>
0-50,10-20,bottle|51-99,0-200,bag
</shelf>

I think newlines are OK between items.
And that's a PIPE.

Last edited by nXIII (2010-08-10 18:59:32)


nXIII

Offline

 

#9 2010-08-11 04:37:25

markyparky56
Scratcher
Registered: 2008-03-20
Posts: 1000+

Re: Turning this into a block (maybe JD)

nXIII wrote:

I wrote some of it, refreshed my browser, then ....
I think it works  wink
Tested it, it does! YAY!

chooseFrom: t1 tag: t2
| code i j arr item getItem rnd |
code := self readFile: t1.
i := (code findString: '<',t2,'>') + 2 + t2 size.
j := (code findString: '</',t2,'>') - 1.
code := code copyFrom: i to: j.
arr := OrderedCollection new.
getItem := [:it |
    | i0 j0 k0 l0 |
    i0 := it findString: '-'.
    j0 := it findString: ',' startingAt: i0 + 1.
    k0 := it findString: '-' startingAt: j0 + 1.
    l0 := it findString: ',' startingAt: k0 + 1.
    {(it copyFrom: 1 to: i0 - 1) asNumberNoError.
        (it copyFrom: i0 + 1 to: j0 - 1) asNumberNoError.
        (it copyFrom: j0 + 1 to: k0 - 1) asNumberNoError.
        (it copyFrom: k0 + 1 to: l0 - 1) asNumberNoError.
        it copyFrom: l0 + 1 to: it size}].
[(i := code findString: '|') > 0] whileTrue: [
    item := code copyFrom: 1 to: i - 1.
    arr add: (getItem value: item).
    code := code copyFrom: i + 1 to: code size.
].
arr add: (getItem value: code).
rnd := self randomFrom: 0 to: 99.
arr do: [:it |
    rnd >= it first & (rnd <= it second) ifTrue: [
        rnd := self randomFrom: it third to: it fourth.
        ^ rnd asString,',',it fifth.
    ].
].

Here, let me indent it.

Oh, and here's the new syntax:

Code:

<shelf>
0-50,10-20,bottle|51-99,0-200,bag
</shelf>

I think newlines are OK between items.
And that's a PIPE.

New Syntax? thats a different language. (Looks a bit like an _ML language.


http://j.mp/jgVnTq
Check out my game engine development site: NewDawn I'm a Level 171 Scratcher.I am http://bit.ly/nkvLNT

Offline

 

#10 2010-08-11 05:29:30

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Turning this into a block (maybe JD)

no, that language just specifically applys to reading these files. I used html <tags> to indacate which area in the file to search.

n, does this mean that rather than writing 0150 (1 to 50) 0's before numbers no longer need to be put in?


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#11 2010-08-11 06:13:31

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Turning this into a block (maybe JD)

NXIII, your block works really well! the one glitch is that if the item selected is the last item in that tag, it displays it with a blank second line... I overcome this by putting a dummy "0-0,0-0,0" item at the end that cannot be triggered. also, it runs a lot faster if all the items are on one line  tongue  EDIT: I take that back it does not appear to affect the speed... I did a test  tongue

Last edited by sparks (2010-08-11 06:17:02)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#12 2010-08-11 16:46:01

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Turning this into a block (maybe JD)

sparks wrote:

NXIII, your block works really well! the one glitch is that if the item selected is the last item in that tag, it displays it with a blank second line... I overcome this by putting a dummy "0-0,0-0,0" item at the end that cannot be triggered. also, it runs a lot faster if all the items are on one line  tongue  EDIT: I take that back it does not appear to affect the speed... I did a test  tongue

Oh, cool!  big_smile
I didn't test it much, just looked to see if it gave something that looked right  wink .


nXIII

Offline

 

#13 2010-08-11 17:27:25

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Turning this into a block (maybe JD)

yeah! apart from that last item glitch, it works great!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

Board footer