WarriorJS: Game written in JavaScript for learning JavaScript and artificial intelligence

274c474bb14a19f48ee86907c5be2a1d?s=47 Kite
October 05, 2016

WarriorJS: Game written in JavaScript for learning JavaScript and artificial intelligence

WarriorJS とは、インタラクティブな方法で JavaScript と人工知能を楽しく教えるために設計されたゲーム。
WarriorJS is a game designed to teach JavaScript and artificial intelligence in a fun, interactive way.

274c474bb14a19f48ee86907c5be2a1d?s=128

Kite

October 05, 2016
Tweet

Transcript

  1. 2.

    Kite KITERETZ inc. CEO & Founder kite.koga ixkaito ixkaito Web

    & graphic designer Programmer WordPress core contributor Ruby on Rails contributor React contributor Wocker developer Bathe developer Frasco developer
  2. 6.

    ͋ͳͨ͸ɺڊౝͷ࠷্֊Ͱʻ͜͜ʹ͋ͳͨͷϞνϕʔγϣϯͱͳΔԿ͔Λૠೖʼ ͢ΔͨΊɺͦΕΛొΔઓ࢜ͱͯ͠ϓϨʔ͠·͢ɻ֤֊Ͱఢͱઓ͍ɺ஥ؒ Λٹग़͠ɺ࣍ͷ֊ʹͭͳ͕Δ֊ஈ΁౸ୡ͢ΔͨΊʹ͸ɺ+BWB4DSJQU &4ϑϧαϙʔτ Λॻ͍ͯઓ࢜ʹࢦࣔΛ༩͑Δඞཁ͕͋Γ·͢ɻ You play as a warrior

    climbing a tall tower to <insert something that motivates you here> at the top level. On each floor, you need to write JavaScript (with full ES2015 support) to instruct the warrior to battle enemies, rescue captives, and reach the stairs.
  3. 11.

    Ҏ্ʂݱࡏͷ৔ॴʹXBSSJPSKTσΟϨΫτϦ͕࡞੒͞Εɺͦͷதͷϓϩ ϑΝΠϧσΟϨΫτϦͰ1MBZFSΫϥεΛؚΜͩ1MBZFSKTϑΝΠϧΛݟ ͚ͭΔ͜ͱ͕Ͱ͖·͢ɻ That's it! This will create a warriorjs

    directory in your current location, where you will find a Player.js file in your profile's directory containing the Player class. class Player { playTurn(warrior) { // Cool code goes here } }
  4. 13.

    ͋ͳͨͷ໨త͸ɺplayTurnϝιουʹ໋ྩΛهೖ͢Δ͜ͱͰɺઓ࢜ʹԿ Λ͢΂͖͔ࢦࣔΛ༩͑Δ͜ͱͰ͢ɻ֤֊Ͱɺઓ࢜ͷΞϏϦςΟ͸೉қ౓ ͱͱ΋ʹ੒௕͠·͢ɻݱࡏͷ֊Ͱઓ͕࢜ར༻Ͱ͖ΔΞϏϦςΟͷৄࡉʹ ͍ͭͯ͸ɺϓϩϑΝΠϧσΟϨΫτͷ3&"%.&Λࢀর͍ͯͩ͘͠͞ɻ Your objective is to fill the

    playTurn method with commands to instruct the warrior what to do. With each level, your abilities will grow along with the difficulty. See the README in your profile's directory for details on what abilities your warrior has available on the current level.
  5. 14.

    Լه͸ɺఢʹ৮Εͨ৔߹͸߈ܸ͠ɺͦΕҎ֎ͷ৔߹͸લਐ͢ΔࢦࣔΛઓ ࢜ʹ༩͑Δ؆୯ͳྫͰ͢ɿ Here is a simple example which will instruct

    the warrior to attack if he feels an enemy, otherwise he will walk forward: class Player { playTurn(warrior) { if (warrior.feel().isEnemy()) { warrior.attack(); } else { warrior.walk(); } } }
  6. 16.

    1MBZFSKTͷฤू͕׬ྃͨ͠ΒɺϑΝΠϧΛอଘ͠ɺͦͷ֊ΛϓϨʔ͢Δ ʹ͸࠶౓warriorjsίϚϯυΛ࣮ߦ͍ͯͩ͘͠͞ɻҰ࿈ͷλʔϯͷ࠶ ੜ͕࢝·Γ·͢ɻ֤λʔϯͰ͸ɺϓϨΠϠʔͱఢͷplayTurnϝιου ͕ಉ࣌ʹ࣮ߦ͞Ε·͢ɻ Once you are done editing Player.js,

    save the file and run the warriorjs command again to start playing the level. The play happens through a series of turns. On each one, your playTurn method is called along with any enemy's.
  7. 17.

    ֊ͷ్தͰίʔυΛมߋ͢Δ͜ͱ͸Ͱ͖·ͤΜɻॳΊ͔Βͦͷ֊Ͱى͜ Γ͑Δ͢΂ͯͷ͜ͱΛߟྀʹೖΕɺઓ࢜ʹద੾ͳࢦࣔΛ༩͑Δඞཁ͕͋ Γ·͢ɻ You cannot change your code in the

    middle of a level. You must take into account everything that may happen on that level and give your warrior the proper instructions from the start.
  8. 18.

    ମྗ IFBMUI Λશࣦͯ͏ͱͦͷ֊͸ࣦഊʹͳΓ·͢ɻࣦഊʹΑΔേ͸͋ Γ·ͤΜɻ1MBZFSKTϑΝΠϧʹ໭ΓɺίʔυΛվળͯ͠࠶ࢼߦͯͩ͘͠ ͍͞ɻ Losing all of your health

    will cause you to fail the level. You are not punished by this, you simply need to go back to your Player.js file, improve your code, and try again.
  9. 19.

    ੒ޭʢ֊ஈʹ౸ୡʣ͢ΔͱɺϓϩϑΝΠϧͷ3&"%.&͸࣍ͷ֊ͷͨΊ ʹߋ৽͞Ε·͢ɻ࣍ͷ֊ΛϓϨʔ͢Δʹ͸ɺ1MBZFSKTϑΝΠϧΛมߋ͠ɺ ࠶౓warriorjsΛ࣮ߦ͍ͯͩ͘͠͞ɻ Once you pass a level (by reaching

    the stairs), the profile README will be updated for the next level. Alter the Player.js file and run warriorjs again to play the next level.
  10. 28.

    ͜ͷήʔϜ͸ςΩετϕʔεͰ͸͋Δ͕ɺ্ۭ͔Β၆ᛌ͍ͯ͠Δ%ήʔ Ϝͱߟ͍͑ͯͩ͘͞ɻ͢΂ͯͷ֊͸௕ํܗͰ͋Γɺෳ਺ͷϚεʹΑͬͯ ߏ੒͞Ε͍ͯ·͢ɻϚεʹ͸Ұͭͷཁૉ͔͠ଘࡏ͢Δ͜ͱ͕Ͱ͖·ͤΜɻ ͋ͳͨͷ໨ඪ͸֊ஈͷ͋ΔϚεΛݟ͚ͭΔ͜ͱͰ͢ɻ Even though this is a text-based

    game, think of it as two-dimensional where you are viewing from overhead. Each level is always rectangular in shape and is made up of a number of squares. Only one unit can be on a given square at a time, and your objective is to find the square with the stairs.
  11. 29.

    Լه͸ϑϩΞϚοϓͱཁૉͷҰྫͰ͢ɿ Here is an example level map and key: ╔════╗

    ║C s>║ ║ S s║ ║C @ ║ ╚════╝ > = Stairs | ֊ஈ @ = Warrior (20 HP) | ઓ࢜ s = Sludge (12 HP) | εϥΠϜ S = Thick Sludge (24 HP) | ڧεϥΠϜ C = Captive (1 HP) | ஥ؒ
  12. 32.

    ଟ͘ͷΞϏϦςΟ͸ɺԼهͷ޲͖Ͱߦ͏͜ͱ͕Ͱ͖·͢ɿGPSXBSE લ ɺ CBDLXBSE ޙ ɺMFGU ࠨ ɺSJHIU ӈ ɻ࠷ॳͷҾ਺ͱͯ͠޲͖ͷจࣈ

    ྻΛ౉͢ඞཁ͕͋Γ·͢ɻྫɿwarrior.walk('backward');ɻ Many abilities can be performed in the following directions: forward, backward, left and right. You have to pass a string with the direction as the first argument, e.g. warrior.walk('backward');.
  13. 43.

    warrior.detonate([direction]) ༩͑ΒΕͨํ޲ σϑΥϧτͰલํ ͷര஄Λരൃͤ͞ΔɻपғϚε ࣗ ෼ࣗ਎΋ؚΉ ʹμϝʔδΛ༩͑·͢ɻ Detonate a bomb

    in a given direction (forward by default) which damages that space and surrounding 4 spaces (including yourself).
  14. 46.

    ηϯεͱ͸ɺݱࡏͷ֊ͷ৘ใΛऩू͢ΔΞϏϦςΟͰ͢ɻपғͷ֬ೝ΍ ద੾ͳΞΫγϣϯͷબ୒ͷͨΊɺඞཁͳͱ͖ʹηϯεΛߦ͏͜ͱ͕Ͱ͖ ·͢ɻ A sense is something which gathers information

    about the floor. You can perform senses as often as you want per turn to gather information about your surroundings and to aid you in choosing the proper action.
  15. 55.

    ײ஌ͨ͠৘ใ͸λʔϯ͝ͱʹมԽ͢ΔͷͰɺ࣍ͷλʔϯͰ࢖༻͢ΔͨΊ ूΊͨ৘ใ͸ɺه࿥͓ͯ͘͠ඞཁ͕͋Γ·͢ɻྫ͑͹ɺମྗ͕લͷλʔ ϯΑΓ΋ݮ͍ͬͯΔ͔Ͳ͏͔Ͱɺ߈ܸ͞Ε͍ͯΔ͔Ͳ͏͔Λ֬ೝ͢Δ͜ ͱ͕Ͱ͖·͢ɻ Since what you sense will change

    each turn, you should record what information you gather for use on the next turn. For example, you can determine if you are being attacked if your health has gone down since the last turn.
  16. 57.
  17. 66.

    ଟ͘ͷ৔߹ɺηϯεͷ௚ޙʹ͜ΕΒͷϝιουΛݺͼग़͢͜ͱʹͳΓ· ͢ɻྫ͑͹ɺfeelηϯε͸SpaceΛฦ͠·͢ɻ͜Εʹରͯ͠ isCaptive()Λݺͼग़͢͜ͱͰɺ໨ͷલʹั·͍ͬͯΔ஥͕͍ؒΔ͔Ͳ ͏͔Λ֬ೝ͢Δ͜ͱ͕Ͱ͖·͢ɻ You will often call these methods

    directly after a sense. For example, the feel sense returns one Space. You can call isCaptive() on this to determine if a captive is in front of you: warrior.feel().isCaptive();
  18. 68.

    ΋͋͠Δ֊Ͱߦ͖٧·ͬͨ৔߹͸ɺ3&"%.&υΩϡϝϯςʔγϣϯΛ ݟ௚͠ɺ͢΂ͯͷΞϏϦςΟΛࢼ͍ͯ͠Δ͔Ͳ͏͔Λ֬ೝ͍ͯͩ͘͠͞ɻ ΋͠ମྗΛҡ࣋Ͱ͖ͳ͍৔߹͸ɺपΓʹఢ͕͍ͳ͍ͱ͖ʹ ମྗʹ஫ҙ ͠ͳ͕Β ճ෮͍ͯͩ͘͠͞ɻ·ͨɺՄೳͳ͔͗Γԕڑ཭෢ث ྫ͑͹ ٷͳͲ Λ࢖༻͍ͯͩ͘͠͞ɻ If

    you ever get stuck on a level, review the README documentation and be sure you're trying each ability out. If you can't keep your health up, be sure to rest when no enemy is around (while keeping an eye on your health). Also, try to use far-ranged weapons whenever possible (such as the bow).
  19. 72.

    ΋͠ߴಘ఺Λૂ͍ͬͯΔͳΒɺ๨ΕͣʹΤϦΞશମΛݟ౉͍ͯͩ͘͠͞ɻ ͨͱ͑֊ஈۙ͘ʹ͍ͨͱͯ͠΋ɺ ମྗ͕͋ΔͳΒ͹ ͢΂ͯͷ΋ͷΛ ೖख͢Δ·ͰதʹೖΒͳ͍Ͱ͍ͩ͘͞ɻԕڑ཭ηϯε ྫ͑͹MPPLͱ MJTUFO Λ࢖ͬͯɺఢ͕࢒͍ͬͯΔ͔Ͳ͏͔Λ֬ೝ͍ͯͩ͘͠͞ɻ If you're

    aiming for points, remember to sweep the area. Even if you're close to the stairs, don't go in until you've gotten everything (if you have the health). Use far- ranged senses (such as look and listen) to determine if there are any enemies left.