ã¯ãã¼ã¸ã£ã¨ã³ã«ã¼ãã³ï¼ï¼ï¼ï¼ã¯ãã¼ã¸ã£ã§turtle APIãæ¡å¼µãã¦ã¿ã
ã¯ããã«
ååã®ã
クロージャとコルーチン(1)-状態遷移関数をつくってみた - Minecraftとタートルと僕
ããå°ãéãããã¦ãã¾ãã¾ãããã
ã¯ãã¼ã¸ã£ã使ã£ãTurtleAPIæ¡å¼µããç´¹ä»ãã¾ãã
è£è¶³
ãªããã®ããã«APIãæ¡å¼µããã®ã3è¡ã§èª¬æ
- æ¨æºã®turtle.select(ã¹ãããçªå·)ã§ã¯ãã¹ãããçªå·ãç´æ¥æå®ãã¦é¸æã¹ããããå¤æ´ããã
- æ¨æºã®TurtleAPIã§ã¯ãç¾å¨ã®é¸æã¹ãããçªå·ãç¥ãæ¹æ³ããªããã次ã®ã¹ãããããåã®ã¹ããããã®ããã«ç¸å¯¾çã«é¸æã¹ããããå¤æ´ããæ¹æ³ããªãã
- ãã£ã¦ãç¾å¨ã®é¸æã¹ããããè¿ãé¢æ°ã¨ãç¸å¯¾æå®ã§é¸æã¹ããããå¤æ´ããé¢æ°ãä½ã£ãã
Turtle APIã®æ¡å¼µ
以ä¸ã®ããã°ã©ã ã¯ãæ¨æºã®TurtleAPIã«å¯¾ãã¦ä»¥ä¸ã®ãããªå¤æ´ãè¡ããæ©è½ãæ¡å¼µããã
turtle.select_org(ï¼ã¹ãããçªå·ï¼)
- æ¨æºã® turtle.select()ã®ããã¯ã¢ããã
turtle.select(ï¼ã¹ãããçªå·ï¼)
- ã¦ã¼ã¶ã¼ããã¿ãã¨å ããããé¢æ°ã¨æ©è½çãªéãã¯ãªãã
- ãããå
é¨å¦çã¨ãã¦ã¯ãturtle.select()ãå®è¡ãããã³ã«ãæ°ãããã®é¸æã¹ããããåæä½ç½®ã¨ãã以ä¸ã®3ã¤ã®é¢æ°ãæ°ããä½æããã
- turtle.getSelectedSlot()
- turtle.selectNext()
- turtle.selectPrev()
turtle.getSelectedSlot()
- å¼æ°ãªã
- è¿å¤ï¼ï¼ç¾å¨ã®é¸æã¹ãããçªå·ãè¿ãã
turtle.selectNext()
- é¸æã¹ãããã次ã®ã¹ãããã«ç§»ãã
- ç¾å¨ã®é¸æã¹ãããã1ãªãã°2ã«ç§»ããã¾ããç¾å¨ãã¹ããã16ãªãã°ã¹ããã1ã«ç§»ãã
- å¼æ°ãªã
- è¿å¤ï¼ï¼ true/false
turtle.selectPrev()
- é¸æã¹ããããä¸ã¤åã®ã¹ãããã«ç§»ãã
- ç¾å¨ã®é¸æã¹ãããã2ãªãã°1ã«ç§»ããã¾ããç¾å¨ãã¹ããã1ãªãã°ã¹ããã16ã«ç§»ãã
- å¼æ°ãªã
- è¿å¤ï¼ï¼ true/false
ã¤ã³ã¹ãã¼ã«æ¹æ³ã»å©ç¨æ¹æ³
ãã¦ã³ãã¼ã
pastebinã³ãã³ãã使ããªã以ä¸ã®ã¨ããã
> pastebin get 1dCNsXNi ex_turtle_select
å©ç¨æ¹æ³
ãã®ããã°ã©ã ã1度å®è¡ãããã¨ã§Turtle APIãæ¸ãæãããã®å¤æ´ã¯ã³ã³ãã¥ã¼ã¿ãåèµ·åããã¾ã§æå¹ã«ãªãã
ãããã¯ãèªåã®ããã°ã©ã ã®å
é é¨åã«æ¬ã½ã¼ã¹ã³ã¼ããåãè¾¼ãã§ãè¯ãã
å©ç¨æ¹æ³ï¼ï¼ãæåå®è¡
> ex_turtle_select
å©ç¨æ¹æ³ï¼ï¼ãåèµ·åãããã³ã«èªåå®è¡
ãrom/autorun/ããã©ã«ã以ä¸ã«ããex_turtle_selectãããã°ã©ã ãè¨ç½®ããã
å©ç¨æ¹æ³ï¼ï¼èªåã®ããã°ã©ã ã«åãè¾¼ã¿
ã³ãã¼ï¼ãã¼ã¹ãã§èªåã®ããã°ã©ã ã«æ¬ã½ã¼ã¹ã³ã¼ããåãè¾¼ããããããã¯ä»¥ä¸ã®ã³ã¼ããåãè¾¼ãã
loadfile("ex_turtle_select")
ã½ã¼ã¹ã³ã¼ã
- L16ï¼ããªãªã¸ãã«ã®turtle.select()ã¯ãturtle.select_orgã¨ãã¦éé¿ã
- L18-31ï¼ãturtle.select()ãåå®ç¾©ã
- turtle.select()ãå®è¡ãããã³ã«ãã¯ãã¼ã¸ã£ã使ã£ãç¶æ é·ç§»é¢æ°funcãä¸ã¤ã ãä½æã
- ãã®funcã使ã£ã¦ãgetSelectedSlot()ãselectNext()ãselectPrev()ãçæã
- L19ï¼ããªãªã¸ãã«select()ã使ã£ã¦é¸æã¹ãããå¤æ´ããªããã®ãªãªã¸ãã«ã¯å¼æ°ãããããã¨ãã®ã¨ã©ã¼å¦çã¨ãæåããã¨ããtrueããè¿ããªãï¼falseã¯ç¡ãï¼ããã®ãããL30ã§trueã®ã¿è¿ãã¦ããã
- L21-24ï¼ãé¢æ°funcã¯ç¾å¨ã®é¸æã¹ããã"slot"ããä¸ä½å¤ãã¨ãã¦ä¿æãfunc()ãå®è¡ãããã³ã«"slot"ã®å¤ãå¤æ´ããã
- ãã®é¢æ°ãè¿ãå¤ã¯ã1ï½16ã¾ã§ã®å¤ãã¨ã循ç°ããï¼16ã®æ¬¡ã¯1ã1ã®åã¯16ï¼ã
- func(x)ã«ããã¦å¼æ°xã¯å¤ä½éã0ãªãç¾å¨ã®é¸æã¹ããããæ£ãªããã®å¤ã ããã©ã¹ãè² ãªããã¤ãã¹ããå¤ãè¿ãã
- L26,27,28ï¼ãgetSelectedSlot()ãselectNext()ãselectPrev()ã¯ãã®ç¶æ é·ç§»é¢æ°func(x)ãå é¨çã«ä½¿ç¨ãã¦é¸æã¹ããããå®éã«å¤æ´ããã©ããã¼ã
- L32,33ï¼ãé¢æ°åæåï¼é¢æ°çæï¼ã¨åæåãããã¨ã示ãã¡ãã»ã¼ã¸ï¼ãã®ããã°ã©ã ãå®è¡ãããã¨ã示ãããï¼ã