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

#1 2013-01-13 22:58:13

KVTaniguchi
New Scratcher
Registered: 2013-01-12
Posts: 6

Help with my first Scratch coding project

Goal: create a chemistry quiz game where users click on the right combination of ion name and charge. 

Here is the first iteration code:

http://scratch.mit.edu/projects/KVTaniguchi/3035591

I really don't like the way code seems to repeat itself over and over.  Plus a user could just end up memorizing which certain sprites have the right answer, NOT which combinations of ions and charges are correct. 

Here is the second iteration code, where I need help.

http://scratch.mit.edu/projects/KVTaniguchi/3036671

Trying to optimize the code here by using lists of variables.  I want to generate completely 3 random sets of ions and charges while declaring certain sets of variables to be correct (Lithium, Sodium, Potassium, Rubidium are all 1+ charge and so on).
1) each sprite generates its own random combo of ions and charges (I need to declare local variables?)
2) each sprite then sends its combo to the stage
3) the stage then checks each combo to see if its correct
4) the stage then tells each sprite if it is right or wrong
5) if the user clicks on the sprite that is correct, the score is +1'd, else score is -1'd

Is all the back and forth between scripts even being 'optimized?' 

Thank you!

K

Offline

 

#2 2013-01-14 20:26:23

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Help with my first Scratch coding project

Here's an idea: create two lists, one named "Elements" and one named "Charges."  Populate the "Elements" list with the names of all the ions you want to quiz the user over (so, all relevant element names).  Then, populate the "Charges" list with the charges of each ion, so that the charge indicated by the first member in the "Charges" list corresponds to the element name specified by the first member of the "Elements" list, and so on (this simple data structure is called a parallel array). 

For example:

Code:

Elements = { Hydrogen, Lithium, Nitrogen, Oxygen, Fluorine, Sodium, Calcium, ... }
Charges = { 1+, 1+, 3-, 2-, 1-, 1+, 2+, ... }

Now, you want to provide the user with three options to pick from.  Two of the options should be false, but one should be true.  First, we need to select three random elements from the list of elements, but without repetition:

delete (all v) of [Numbers v]
set [i v] to [0]
repeat (length of [Elements v])
change [i v] by (1)
add (i) to [Number v]
end
set [i v] to (pick random (1) to (length of [Numbers v]))
set [WrongElement1 v] to (item (item (i) of [Numbers v]) of [Elements v])
set [WrongCharge1 v] to (item (any v) of [Charges v])
repeat until <not<(WrongCharge1) = (item (item (i) of [Numbers v]) of [Charges v])>>
set [WrongCharge1 v] to (item (any v) of [Charges v])
end
delete (i) of [Numbers v] // Prevents choosing same element twice.  

set [i v] to (pick random (1) to (length of [Numbers v]))
set [WrongElement2 v] to (item (item (i) of [Numbers v]) of [Elements v])
set [WrongCharge2 v] to (item (any v) of [Charges v])
repeat until <not<(WrongCharge2) = (item (item (i) of [Numbers v]) of [Charges v])>>
set [WrongCharge2 v] to (item (any v) of [Charges v])
end
delete (i) of [Numbers v] // Prevents choosing same element twice.  

set [i v] to (item (any v) of [Numbers v])
set [RightElement v] to (item (i) of [Elements v]) // Now we choose the correct one.  
set [RightCharge v] to (item (i) of [Charges v])
Every time this script is called, the "Numbers" list is populated with all the integers from 1 to the number of elements in the "Elements" list.  We select a random member of the "Numbers" list (item (i) of [Numbers v]).  We select an element from the main list, but then ensure that the charge we choose does not correspond to that element (that's the repeat until loop).  Finally, we delete that member of the "Numbers" list so that the same element cannot be chosen twice.  This process is repeated to generate two wrong element-charge pairs and one correct element-charge pair.  The correct pair is stored in the variables RightElement and RightCharge; the wrong pairs are stored in WrongElement1, WrongCharge1, WrongElement2, and WrongCharge2. 

i is a temporary variable that's used for a few things; first to iterate through the "Numbers" list, and then to hold the index of the index of the element or charge (if you're familiar with other programming languages, i is like a pointer to a pointer). 

If you have any questions, I'd be happy to explain further.

Wait... are you taking CS50?

Last edited by amcerbu (2013-01-14 21:18:20)

Offline

 

#3 2013-01-14 21:26:06

KVTaniguchi
New Scratcher
Registered: 2013-01-12
Posts: 6

Re: Help with my first Scratch coding project

Thank you!  Oh I wish I was a Harvard cs50 student, it looks like a super fun course to take.  I'm a chemistry teacher who is independently 'playing along at home' as my newest, funnest hobby ever.

I get the parallel array concept - its like creating a roulette wheel (only with elements for 'colors' and the charges for #'s, and they can both spin and be matched up in different orders).  I also like how the 'repeat' block in the very beginning allows for future expansions of the roulette wheel.  And I totally get the use of the 'i' variable, very clever! 

Thank you!

Offline

 

#4 2013-01-14 21:48:18

KVTaniguchi
New Scratcher
Registered: 2013-01-12
Posts: 6

Re: Help with my first Scratch coding project

Oh and a question - can scratch take a variable and then search a list to see if the variable is in the list?

Offline

 

#5 2013-01-14 21:51:22

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Help with my first Scratch coding project

KVTaniguchi wrote:

Oh and a question - can scratch take a variable and then search a list to see if the variable is in the list?

Yes, yes it can. Just do this:

<[List v] contains (variable)>

I hope that this helps! Also, hello KVTaniguchi and welcome to Scratch! I wish you the best of times here!

Last edited by ErnieParke (2013-01-14 21:52:02)


http://i46.tinypic.com/35ismmc.png

Offline

 

#6 2013-01-14 23:00:44

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Help with my first Scratch coding project

KVTaniguchi wrote:

Oh and a question - can scratch take a variable and then search a list to see if the variable is in the list?

ErnieParke is right; there is a shortcut for determining whether a list contains a specific element.  But to find where the element is, you have to iterate through the list, using a loop and an index (the i variable from before). 

delete (all v) of [RandomNumbers v]
repeat (100)
add (pick random (1) to (100)) to [RandomNumbers v] // Fill the list
end
set [i v] to (0)
delete (all v) of [DivisSeven v]
repeat (length of [RandomNumbers v]) // Look for numbers divisible by 7
change [i v] by (1)
if <((item (i) of [RandomNumbers v]) mod (7)) = [0]>
add (i) to [DivisSeven v]
end
DivisSeven contains the indexes of all the elements in RandomNumbers that are divisible by seven. 

I'm glad I could help!  And welcome to Scratch!

Last edited by amcerbu (2013-01-14 23:13:30)

Offline

 

#7 2013-01-15 19:40:30

KVTaniguchi
New Scratcher
Registered: 2013-01-12
Posts: 6

Re: Help with my first Scratch coding project

And here is iteration 3:

http://scratch.mit.edu/projects/KVTaniguchi/3040318

But its super buggy.  I need to group the elements by charge, assign them to lists, have each sprite pick a random set of element and charge, then have the stage check each sprite to see if its correct.  I don't mind having two or more sprites be right.

Offline

 

#8 2013-01-15 20:10:41

KVTaniguchi
New Scratcher
Registered: 2013-01-12
Posts: 6

Re: Help with my first Scratch coding project

The issue I'm running into with parallel arrays is that every 'slot' in the parallel arrays has to be different from the other slots, otherwise you could end up with a situation where a user chooses an answer that is chemically right:  1: Br 2:1-  but according to how the program is checking it (1:2 is wrong) the user is told he/she is wrong.

Offline

 

#9 2013-01-15 20:14:56

mollovespandas
Scratcher
Registered: 2011-02-22
Posts: 22

Re: Help with my first Scratch coding project

delete all ▼ of Numbers ▼set i ▼ to 0repeat length of Elements ▼change i ▼ by 1add i to Number ▼set i ▼ to pick random 1 to length of Numbers ▼set WrongElement1 ▼ to item item i of Numbers ▼ of Elements ▼set WrongCharge1 ▼ to item any ▼ of Charges ▼repeat until notWrongCharge1 = item item i of Numbers ▼ of Charges ▼set WrongCharge1 ▼ to item any ▼ of Charges ▼delete i of Numbers ▼ Prevents choosing same element twice. set i ▼ to pick random 1 to length of Numbers ▼set WrongElement2 ▼ to item item i of Numbers ▼ of Elements ▼set WrongCharge2 ▼ to item any ▼ of Charges ▼repeat until notWrongCharge2 = item item i of Numbers ▼ of Charges ▼set WrongCharge2 ▼ to item any ▼ of Charges ▼delete i of Numbers ▼ Prevents choosing same element twice. set i ▼ to item any ▼ of Numbers ▼set RightElement ▼ to item i of Elements ▼ Now we choose the correct one. set RightCharge ▼ to item i of Charges ▼

when gf clicked
repeat until <(timer) > [10]>
  go to [mouse-pointer v]
end
think [Scripts in your posts!] for (3) secs


The fool doth think he is wise, but the wise man knows himself to be a fool. ~William Shakespeare

Offline

 

#10 2013-01-15 20:16:34

mollovespandas
Scratcher
Registered: 2011-02-22
Posts: 22

Re: Help with my first Scratch coding project

ErnieParke wrote:

KVTaniguchi wrote:

Oh and a question - can scratch take a variable and then search a list to see if the variable is in the list?

Yes, yes it can. Just do this:

<[List v] contains (variable)>

I hope that this helps! Also, hello KVTaniguchi and welcome to Scratch! I wish you the best of times here!

HI! Sorry this is not about the topic right now, but I wanted to say that the golden rule is "Thou shalt do unto others as thou would have them do unto you."


The fool doth think he is wise, but the wise man knows himself to be a fool. ~William Shakespeare

Offline

 

#11 2013-01-16 00:51:38

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Help with my first Scratch coding project

KVTaniguchi wrote:

The issue I'm running into with parallel arrays is that every 'slot' in the parallel arrays has to be different from the other slots, otherwise you could end up with a situation where a user chooses an answer that is chemically right:  1: Br 2:1-  but according to how the program is checking it (1:2 is wrong) the user is told he/she is wrong.

Try the code I posted before.  It was set up to select two incorrect element-charge pairs (it keeps on getting a random charge until that charge is not equal to the correct charge), and one correct pair. 

It uses a list of integers (list "Numbers") to choose three random numbers, without repetition.  These three represent the indexes of the three elements that will be presented as options.  In order to choose random numbers without repetition, the program populates the "Numbers" list with the integers from 1 to (number of elements).  When it selects a random number, it deletes that number from "Numbers," so that the same random number cannot be selected more than once.

Last edited by amcerbu (2013-01-16 00:53:16)

Offline

 

#12 2013-01-16 17:19:20

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Help with my first Scratch coding project

mollovespandas wrote:

ErnieParke wrote:

KVTaniguchi wrote:

Oh and a question - can scratch take a variable and then search a list to see if the variable is in the list?

Yes, yes it can. Just do this:

<[List v] contains (variable)>

I hope that this helps! Also, hello KVTaniguchi and welcome to Scratch! I wish you the best of times here!

HI! Sorry this is not about the topic right now, but I wanted to say that the golden rule is "Thou shalt do unto others as thou would have them do unto you."

With all mind, I ask, "Who hast thou quoth?"
Is it Shakespeare, Mark Twain, or even both?
With that out of mind, thank you for that spark,
For it was very kindly of you, I remark.

If you didn't understand the above, basically what I'm saying is:

Who are you quoting? Anyway, thank you for reminding me of that rule.  smile


http://i46.tinypic.com/35ismmc.png

Offline

 

#13 2013-01-16 18:54:47

spoilb
New Scratcher
Registered: 2013-01-16
Posts: 1

Re: Help with my first Scratch coding project

The computer has tow registers (R1 and R2)dedicated to arithmetic operations and only the following operations
Load R,M copy the value from memory location M(M is one of A,X,Y, or Z) into register R(R is either R1 or R2)
Store R,M copy the value from register R into memory location M
Add add the values of R1 and R2 together and store the result in R1
SUB subtract the value of R2 from R1 and store the result in R1
Swap exchange the values stored in R1 and R2
Halt stop program execution

Offline

 

Board footer