Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
String manipulation function (Alice 2.2)
Old
yossiy
Member
 
Status: Offline
Posts: 19
Join Date: Apr 2012
Location: Tel Aviv, Israel
Default String manipulation function (Alice 2.2) - 05-06-2012, 08:56 AM

Hi,
I'm teaching Alice in a school for gifted children (1-day per week). First of all, I must say that this is by far my favourite class - the kids come out with such *cool* and creative ideas, I'm looking forward to see what they came up with each week...
Currently my students are working on their final projects in Alice. One of them wanted to build a "hangman" game (a little gory, as the hanging man becomes quite realistic...)
The thing is that I could not find a function like "substr" nor could I find any way of treating a string like an array of characters (you can understand that I have a C/C++ programming background).
Is there any way to go about this, or should I tell him to find another idea?
Yossi Yaron
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Default 05-06-2012, 12:43 PM

Would a simple way of doing this not be to read in the keypresses of letters, using key events, and add them to a list? Through Alice code, string concatenation is possible, but substrings are not.

This would require many key events for 26 letters, but you can simplify it:
Create a new method entitled "key press" or something like that.
Give it a string parameter with a suitable name (ie. "char").
Create a new "when key is typed" event, leave it as "any key".
Drag the "key press" method into the key event (in place of "Nothing").
Go to world functions, scroll down and find the "what as a string" function.
Drag that in as the char parameter for the key press method you just added.
When setting "what" for the function, scroll down to "expressions >" and choose "world.event.keyChar".

Now when a key is typed, the "key press" method will have its "char" parameter set to a string representation of the key.

You could add that straight to the list, or you could do some input validation. The easiest method would be to have a list of all alphabet characters and then add a "For all together" and check if "char" is equal to the item from the list. For some reason the "contains" method of lists always returns false, so iterating through the list is necessary.

Please ask again if that didn't make sense.


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬

Last edited by arty-fishL; 05-06-2012 at 12:45 PM.
   
Reply With Quote
String manipulation function
Old
yossiy
Member
 
Status: Offline
Posts: 19
Join Date: Apr 2012
Location: Tel Aviv, Israel
Default String manipulation function - 05-12-2012, 02:28 PM

Hi Arty,
Thanks for the quick response, and sorry for the delay in mine.
This is an interesting workaround, thanks!
I was thinking more in terms of supplying the program with a pre-defined dictionary of optional words, which will enable a single-player game. Your suggestion will enable only multiple-players (one gives the word, the other will try to guess it).
If this is the only possible way round it, we'll check into it, but I would be most grateful if you (or anyone) could let us know if there is any way of handling strings as an array of characters or applying string functions (maybe using Java string functions through some API?).
Thanks again,
Yossi
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Default 05-12-2012, 06:34 PM

Quote:
Originally Posted by yossiy View Post
maybe using Java string functions through some API?i
Yes. There is no way to do this in pure Alice code, but if you are willing to venture out into the more advanced side of Alice, then it is entirely possible. By this I mean you can do what you are asking through the use of Jython scripting. Jython is a mix of Python and Java, so you have a lot of string manipulation functions available.

More info at this website, including a tutorial on enabling Jython scripting in Alice > http://www.westga.edu/~drocco/alice/

If you are interested then I can show you what to do, but otherwise I would suggest just using lists of characters as I said above.


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 05-13-2012, 12:41 AM

I thought of a way you could have a pre-defined dictionary of words stored and accessed by the computer alone, making a single player game possible. It's the exact opposite of good programming, however, and is only a good solution for a program like Alice which has no character array (or even character) support.

The example world has a dictionary of 14 words in the form of a large list of each words' individual letters, separated by asterisks. The program chooses a random number, uses that to choose two asterisks on the list, writes all the letters between those asterisks into a new list, and compiles them into a single string, thus making a sort of character array and a complete word, perfect for a hangman game.
Attached Files
File Type: a2w WordsAndLetters.a2w (184.1 KB, 29 views)


(')>
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Default 05-13-2012, 07:31 AM

Quote:
Originally Posted by x2495iiii View Post
I thought of a way you could have a pre
...
haracter array and a complete word, perfect for a hangman game.
Very interesting concept. It would require extra time to add every letter, but a good programmer is always willing to sacrifice time for quality.


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Cool! Thanks you!
Old
yossiy
Member
 
Status: Offline
Posts: 19
Join Date: Apr 2012
Location: Tel Aviv, Israel
Default Cool! Thanks you! - 05-13-2012, 11:22 PM

Cool! Thank you both very much!
Jython is an interesting option that I was not aware of. I'll look into it, although it appears to be above the level of most of my students.
The code that x2495iiii a very cool workaround. Thanks!
Yossi
   
Reply With Quote
Reply

Tags
string, string functions, substr, substring


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.