discussion icon indicating copy to clipboard operation
discussion copied to clipboard

12 coin problem simulator in jeforth

Open hcchengithub opened this issue 9 years ago • 18 comments

image

Chocolate factory is in trouble. One of the twelve chocolates is either lighter or heavier. Your job is to find it out by using the sponge floating in a bucket. If you were anticipating a balance scale, the sponge is its substitution. Play Now this game works fine on the Google Chrome web browser. To verify your answer, put all chocolates into the water. The lighter floats, the heavier sinks, all others balance in the water.

In addition to drag-and-drop the chocolates and the sponge by the mouse, these are useful commands:

'replay' command to re-assign the defected chocolate.

'home' command to bring all chocolates and the sponge back to their initial position especially if you have thrown them off the space 😉

'view' command to see the list of all chocolates with their weights ( or mass ) and recent positions.

'freeze' and 'unfreeze' commands work on the water.

Twelve chocolates are numbered 0 .. 11, command line "0 1 2 3 2:2" drops chocolate #0, #1 on the left side and #2, #3 on the right side of the sponge. Similar commands '1:1', '3:3' and friends are to drop corresponding quantities of chocolates respectively.

Above the input box and under the canvas is the output box that is where messages are printed to. If they become too many and annoying then, to erase text messages, type 'er' command or press the key multiple times when the input box is empty. Press Alt+ to do the same thing but erase from top of the output box.

You probably won't need to know this, but messages on the output box can be none-text, i.e. HTML elements. To erase them, we need to also press <Ctrl> key. Thus Ctrl+<backspace> and Alt+Ctrl+<backspace> work on HTML elements. If you went too far you might erase the canvas itself, then you need to reload the web page, that can be done by pressing <F5>.

This game is written in the Forth programming language, jeforth.3we actually. To view the source code, type 'char 12choc.f readTextFileAuto .' don't miss the ending dot, or clone the jeforth.3we project from GitHub. There are many other demos. I wish you like them.

Happy programming!

H.C. Chen [email protected] FigTaiwan 2016.11.27

Updated: The "Play now" URL is now https://cdn.rawgit.com/hcchengithub/jeforth.3we/develop/index.html?include%2012choc.f that has solved the Cross Origin Request issue that happens on IE, Firefox, Edge, ..etc.

hcchengithub avatar Nov 29 '16 06:11 hcchengithub

Thanks, that's fun. How might I encapsulate this demo into a Sugar Web activity? Then I could ship it on OLPC laptop. Previous apps have been JavaScript only. http://github.com/sugarlabs/sugar-web

quozl avatar Nov 29 '16 06:11 quozl

Hi quozl, Cool! But I need to know what's Sugar Web Activity first. I don't quite understand it. Or give me a hand if possible. This game utilizes [chipmunk physics engine] (https://github.com/josephg/Chipmunk-js). If OLPC can run the demos then it's a step closer.

hcchengithub avatar Nov 29 '16 09:11 hcchengithub

A Sugar Web activity is a bundle of JavaScript which is unpacked and executed in the Sugar shell; a desktop environment for children.

The Sugar Browse activity (Python GTK3+ with WebKit2) does not open the 12 coin demo; "Error! Failed to read j/jeforth.f", which is why I suggested a Sugar Web activity; because they have greater permissions.

I don't know enough JavaScript yet to go into detail about Sugar Web activities, sorry. I've not written one. :grin:

quozl avatar Nov 30 '16 01:11 quozl

Thanks! Your Information is very important.

The suggestion is to port jeforth to Sugar Shell for running locally on OLPC, am I right? Sugar seems is for Linux and I am not working on Linux so far. However, jeforth has a NW.js version, named jeforth.3nw. If NW.js (was called Node-webkit) works on OLPC then jeforth.3nw should do too. I have the feeling that NW.js and Sugar Web Activity are the similar thing, not sure if it's correct.

This link https://rawgit.com/hcchengithub/jeforth.3we/develop/index.html?include%2012choc.f is amazing to me. rawgit.com magically brings a github project to web page. But for this game, only Google Chrome works. "Error! Failed to read j/jeforth.f" is also happening on IE and Firefox. According to Firefox's F12 debugger, the reason is: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://raw.githubusercontent.com/hcchengithub/jeforth.3we/develop/f/jeforth.f?text. (Reason: CORS preflight channel did not succeed)." Because jeforth needs to include ~.f modules and reading files violates the Same Origin Policy. I perceived that the URL was rawgit.com but somehow got changed to raw.githubusercontent.com that might be the reason. I will look into it when have the time.

hcchengithub avatar Nov 30 '16 03:11 hcchengithub

Can OLPC run Google Chrome browser? If it does, jeforth has Chrome Extension version and Chrome App version too (jeforth.3ce and jeforth.3ca).

hcchengithub avatar Nov 30 '16 03:11 hcchengithub

My suggestion was to find a way to bring the 12 coin puzzle to OLPC laptop; if that also makes other jeforth demos available, and jeforth itself, that's an unintended but potentially useful side-effect. The OLPC laptop already has a Forth firmware, but is not used by children.

Yes, the same origin policy seems to be a cause of problems. https://github.com/rgrove/rawgit/issues/117 suggests an alternate host name, but it did not work any differently when I tried it.

No, the Chrome browser is not included.

quozl avatar Nov 30 '16 03:11 quozl

to find a way to bring the 12 coin puzzle to OLPC laptop

If OLPC can run chipmunk.js demos then we can port the 12choc.f to 12choc**.js** for OLPC.

https://github.com/josephg/Chipmunk-js

hcchengithub avatar Nov 30 '16 06:11 hcchengithub

No, it does not run; the demo only downloads an index.html file. Same with Firefox on a desktop computer.

quozl avatar Nov 30 '16 06:11 quozl

Oh, yeah. However, download the entire chipmunk.js from GitHub then run demos in the 'demo' sub-directory should work.

Some one taught me another way, a better way, to run Web Applications from GitHub directly: https://rawcdn.githack.com/hcchengithub/jeforth.3we/develop/index.html?include_12choc.f Can this hyperlink work on OLPC?

hcchengithub avatar Dec 01 '16 06:12 hcchengithub

Thanks!

For Chipmunk-js, yes, a git clone works. Performance for the ball.html demo, at three points in our OLPC hardware range;

  • about 6 frames per second on the i386 CPU model XO-1, with WebKit based Browse v157.3,
  • about 12 frames per second on the ARM CPU model XO-4, with WebKit based Browse v157.3,
  • about 45 frames per second on the x86_64 CPU model NL3 (N2940), with WebKit2 based Browse v200,

For your rawcbn.githack.com link to 12choc.f;

  • WebKit based Browse v157.3; fails with Cross-origin script load denied by Cross-Origin Resource Sharing policy.,
  • WebKit2 based Browse v200; works okay,

So it looks like the next challenge is packaging. Thanks for your help!

Where can I find 12choc.f? I looked in jeforth.3we repository, git grep 12choc and find gave no result.

quozl avatar Dec 01 '16 07:12 quozl

Are you from the OLPC team? I am so glad to know you. I have 3 daughters. The 12choc.f is in the 'develop' branch. If you cloned or zip downloaded the 'master' branch then you won't find it. Sorry, it's my mistake for didn't mention that clearly. https://github.com/hcchengithub/jeforth.3we/blob/develop/playground/12choc.f

12choc.f is a modification from chipmunk-js demo buoyancy.html

jeforth is based on JavaScript so it can use HTML and javascript through JavaScript eval() function.

jeforth word <o> ... </o> puts HTML tags onto the Outputbox, for example <o> <h1> Hello World!</h1></o>

jeforth word <js> ... </js> runs Javascript , for example <js> alert("Hello World!")</js>

jeforth word <js> ... </jsV> runs Javascript and return the last statement's value, for example: <js> var x = 1 + 2 + 3; x; </jsV> leaves the sum 6 on top of data stack of jeforth because the last statement is : x;

js: is short form of <js> ... </js> if there's no space in the statement. js> is short form of <js> ... </jsV> if there's no spce in the statement.

Forth constant, variable, and value can be accessed through javascript vm.g.<name> directly. For example: 3.1416 constant PI where PI is a forth word js: alert(vm.g.PI) prints its value, where vm.g.PI or vm.g["PI"] is javascript way to access the same thing.

I like to help further. Please don't hesitate to let me know about anything I can do.

hcchengithub avatar Dec 01 '16 08:12 hcchengithub

Don't want to derail the discussion but... @quozl You make a 386-based laptop for kids? Wow, I'm quite surprised that those chips are still being manufactured.

alexdowad avatar Dec 01 '16 18:12 alexdowad

On 12/1/2016 8:32 AM, Alex
      Dowad wrote:
    
  
  
    Don't want to derail the discussion but... @quozl You make a
        386-based laptop for kids? Wow, I'm quite surprised that
        those chips are still being manufactured.
  
  
  The One Laptop Per Child project was quite active a few years
    ago, but is much less active now.  The first generation was
    build around an x86 "Geode" chipset from AMD.  The second
    generation was an x86 from Via.  Then for subsequent generations
    we switched to an ARM system-on-chip from Marvell.
  Several million OLPC machines were built and delivered to
      children in the developing world.
    
    Quozl and I both worked full time for One Laptop Per Child for
    several years.  OLPC's boot firmware was based on Open Firmware,
    written in Forth.  When development of new OLPC products wound
    down, I stopped working there but, as far as I know, Quozl is
    still involved in a part-time support capacity.
    
    Your comment about "still being manufactured" is quite apropos. 
    One of the big drivers behind new generations is end-of-life
    considerations for parts.  It's not only the CPU chips that are
    affected, but also things like RAM chips of a given interface
    technology.
  

  
    
      
       
      
    
    
  
  {"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/ForthHub/discussion","title":"ForthHub/discussion","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/ForthHub/discussion"}},"updates":{"snippets":[{"icon":"PERSON","message":"@alexdowad in #36: Don't want to derail the discussion but... @quozl You make a 386-based laptop for kids? Wow, I'm quite surprised that those chips are still being manufactured."}],"action":{"name":"View Issue","url":"https://github.com/ForthHub/discussion/issues/36#issuecomment-264254077"}}}

MitchBradley avatar Dec 01 '16 18:12 MitchBradley

Yes, sorry, full disclosure; I'm still at OLPC, paid 35 hours a week, and happy with it. Sustaining engineering of hardware, firmware, software. Still in production for the XO-4 model; several new components cascaded into the design since Mitch was with us; two cameras, backlight, SPI FLASH, eMMC. Ready for production for a follow-on design by our manufacturing partner.

The "installed base" problem is on my mind whenever I'm working on the software. So in considering 12choc.f I ended up testing it on the slowest model in my lab; the AMD Geode based XO-1, which is effectively an i386 architecture, in Linux parlance. Sorry if I wasn't clear about why I did that. :grin:

@hcchengithub, thanks, very interesting, I'll take a closer look later, but the hybrid form of JavaScript enclosed in Forth looks like fun!

quozl avatar Dec 01 '16 21:12 quozl

OLPC is a legend. Here in FigTaiwan we admire you guys very much.

The physics engine chipmunk.js can work on XO-1 is a surprise to me.

The sugar CD (Fedora-SoaS-Live-x86_64-25-1.3.iso) boots on my vmware player virtual machine already. It's a virtual OLPC. Wow! IRC works, I chat with people there. They are Sugar users. I also tried the Python tutor, applets are all very cute.

How to open the Linux Shell is now my first question. ---> Ctrl-Alt-F2 , I've got it.

hcchengithub avatar Dec 02 '16 06:12 hcchengithub

Thanks.

As far as I can tell, Sugar Web Activity is a wrapper written in Python which creates a Gtk.Window and calls WebKit2 to load index.html. Think of it as a browser without controls. It can be found in the https://github.com/sugarlabs/sugar-toolkit-gtk3.git repository as file src/sugar3/activity/webactivity.py

The underlying platform is a Linux. We use both Fedora and Ubuntu as the Linux.

There are probably also example activities. The word activity is a jargon for application in Sugar.

quozl avatar Dec 02 '16 07:12 quozl

I have a question regarding the price of the XO-4. With devices such as the Raspberry Pi offering similar or better performance for a much lower price, why is the XO-4 still so pricey? For a program that's called "One Laptop per Child", the price seems highly unrealistic. If one were to assemble a similar laptop using Raspberry Pis and other off the shelf equipment, it will still be cheaper than the XO-4. @quozl do you think you can enlighten me on this?

Immortalin avatar Dec 02 '16 13:12 Immortalin

Raspberry Pi is indeed a nice
    device, but it is just a circuit board with a few chips on it. 
    Just as an exercise, how about searching for the best prices for
    the following additional features and adding up the costs?
    
    Sunlight readable display with multitouch capability
    Keyboard and trackpad
    Storage (eMMC or SD Card)
    WiFi w/ dual antennas
    Stereo speakers
    Waterproof, shockproof enclosure with sturdy hinges
    Battery pack + charging electronics
    Cabling to interconnect all those things
    System certification in multiple countries
    Inventory/supply chain management for all those parts
    System assembly and test
    
    The price/performance ratio for electronics follows Moore's law,
    but mechanical parts often go the other way, because the raw
    material costs often increase over time.
    
    Another hidden cost that people often find surprising is
    tooling.  Injection molds for plastic parts are enormously
    expensive, and they wear out over time.  They are big blocks of
    super hard steel that must be milled out to exacting tolerances
    by electric discharge machining.  After the shape of the part is
    established and verified (often requiring several re-milling
    passes to fix problems), the tool must be textured so the
    plastic surface has just the right look and feel.  That
    texturing process is very tricky because any small flaw can
    leave a blemish on the plastic surface.
    
    I spent a lot of time in the factory that built the first few XO
    generations.  The step of building the CPU board - which is the
    only part in a Raspberry Pi - is a tiny fraction of the system
    build process.  The CPU board build step is almost entirely
    automated.  The rest of the system assembly cannot easily be
    automated.
    
    
    On 12/2/2016 3:48 AM, Immortalin wrote:
  

  I have a question regarding the price of the XO-4. With
      devices such as the Raspberry Pi offering similar or better
      performance for a much lower price, why is the XO-4 still so
      pricey? For a program that's called "One Laptop per Child",
      the price seems highly unrealistic. If one were to assemble a
      similar laptop using Raspberry Pis and other off the shelf
      equipment, it will still be cheaper than the XO-4. @quozl do you think you
      can enlighten me on this?
  —
    You are receiving this because you commented.
    Reply to this email directly, view
        it on GitHub, or mute
        the thread.
  
    
      
       
      
    
    
  
  {"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/ForthHub/discussion","title":"ForthHub/discussion","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/ForthHub/discussion"}},"updates":{"snippets":[{"icon":"PERSON","message":"@Immortalin in #36: I have a question regarding the price of the XO-4. With devices such as the Raspberry Pi offering similar or better performance for a much lower price, why is the XO-4 still so pricey? For a program that's called \"One Laptop per Child\", the price seems highly unrealistic. If one were to assemble a similar laptop using Raspberry Pis and other off the shelf equipment, it will still be cheaper than the XO-4. @quozl do you think you can enlighten me on this?"}],"action":{"name":"View Issue","url":"https://github.com/ForthHub/discussion/issues/36#issuecomment-264457402"}}}

MitchBradley avatar Dec 02 '16 18:12 MitchBradley