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.
8BSSJPS+4ͱɺΠϯλϥΫςΟϒͳํ๏Ͱ+BWB4DSJQUͱਓೳΛָ ͘͠ڭ͑ΔͨΊʹઃܭ͞ΕͨήʔϜɻ WarriorJS is a game designed to teach JavaScript and artificial intelligence in a fun, interactive way.
͋ͳͨɺڊౝͷ࠷্֊Ͱʻ͜͜ʹ͋ͳͨͷϞνϕʔγϣϯͱͳΔԿ͔Λૠೖʼ ͢ΔͨΊɺͦΕΛొΔઓ࢜ͱͯ͠ϓϨʔ͠·͢ɻ֤֊Ͱఢͱઓ͍ɺؒ Λٹग़͠ɺ࣍ͷ֊ʹͭͳ͕Δ֊ஈ౸ୡ͢ΔͨΊʹɺ+BWB4DSJQU &4ϑϧαϙʔτ Λॻ͍ͯઓ࢜ʹࢦࣔΛ༩͑Δඞཁ͕͋Γ·͢ɻ You play as a warrior climbing a tall tower to 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.
Ҏ্ʂݱࡏͷॴʹ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 } }
͋ͳͨͷతɺ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.
Լهɺఢʹ৮Εͨ߹߈ܸ͠ɺͦΕҎ֎ͷ߹લਐ͢ΔࢦࣔΛઓ ࢜ʹ༩͑Δ؆୯ͳྫͰ͢ɿ 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(); } } }
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.
֊ͷ్தͰίʔυΛมߋ͢Δ͜ͱͰ͖·ͤΜɻॳΊ͔Βͦͷ֊Ͱى͜ Γ͑Δͯ͢ͷ͜ͱΛߟྀʹೖΕɺઓ࢜ʹదͳࢦࣔΛ༩͑Δඞཁ͕͋ Γ·͢ɻ 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.
ମྗ 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.
ޭʢ֊ஈʹ౸ୡʣ͢ΔͱɺϓϩϑΝΠϧͷ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.
͋ͳͨͷඪ֊ஈʹ౸ୡ͢Δ͜ͱ͚ͩͰͳ͘ɺ࠷ߴಘΛ֫ಘ͢Δ ͜ͱͰ͋Γ·͢ɻଟ͘ͷํ๏ͰϙΠϯτΛಘΔ͕Ͱ͖·͢ɻ Your objective is to not only reach the stairs, but to get the highest score you can. There are many ways you can earn points on a level.
ొ͖ͬͯͨ֊ͷ߹ܭಘه͞Ε·͢ɻ֊Λ௨ա͢Δ͝ͱʹͦͷ֊ͷ ಘ߹ܭಘʹՃࢉ͞Ε·͢ɻ A total score is kept as you progress through the levels. When you pass a level, that score is added to your total.
͜ͷήʔϜςΩετϕʔεͰ͋Δ͕ɺ্ۭ͔Β၆ᛌ͍ͯ͠Δ%ήʔ Ϝͱߟ͍͑ͯͩ͘͞ɻͯ͢ͷ֊ํܗͰ͋ΓɺෳͷϚεʹΑͬͯ ߏ͞Ε͍ͯ·͢ɻϚεʹҰͭͷཁૉ͔͠ଘࡏ͢Δ͜ͱ͕Ͱ͖·ͤΜɻ ͋ͳͨͷඪ֊ஈͷ͋ΔϚεΛݟ͚ͭΔ͜ͱͰ͢ɻ 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.
͡Ίɺઓ࢜Θ͔ͣͳΞϏϦςΟ͔͍࣋ͬͯ͠ͳ͍͕ɺ֊্͕͕Δ ʹͭΕͯΞϏϦςΟ͠·͢ɻେ͖͚ͯ͘ೋछྨͷΞϏϦςΟ͕ ͋Γ·͢ɿΞΫγϣϯͱηϯεɻ When you first start, your warrior will only have a few abilities, but with each level your abilities will grow. A warrior has two kinds of abilities: Actions and Senses.
ଟ͘ͷΞϏϦςΟɺԼهͷ͖Ͱߦ͏͜ͱ͕Ͱ͖·͢ɿ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');.
ΞΫγϣϯԿ͔͠ΒͷӨڹΛήʔϜʹ༩͑·͢ɻλʔϯ͝ͱʹ࣮ߦͰ ͖ΔΞΫγϣϯҰͭͷΈͰ͢ͷͰɺݡ͍બΛ͍ͯͩ͘͠͞ɻ An action is something that affects the game in some way. Only one action can be performed per turn, so choose wisely.
warrior.rescue([direction]) ༩͑ΒΕͨํ σϑΥϧτͰલํ ͷؒΛٹग़͠·͢ ϙΠϯτΛ ֫ಘ ɻ Rescue a captive from his chains (earning 20 points) in given direction (forward by default).
warrior.detonate([direction]) ༩͑ΒΕͨํ σϑΥϧτͰલํ ͷരΛരൃͤ͞ΔɻपғϚε ࣗ ؚࣗΉ ʹμϝʔδΛ༩͑·͢ɻ Detonate a bomb in a given direction (forward by default) which damages that space and surrounding 4 spaces (including yourself).
ηϯεͱɺݱࡏͷ֊ͷใΛऩू͢ΔΞϏϦςΟͰ͢ɻपғͷ֬ೝ దͳΞΫγϣϯͷબͷͨΊɺඞཁͳͱ͖ʹηϯεΛߦ͏͜ͱ͕Ͱ͖ ·͢ɻ 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.
warrior.distanceOf(space) Ҿͱͯ͠εϖʔεΛ͢͜ͱͰɺͦͷεϖʔε·ͰͷڑΛද͢Λ ฦ͠·͢ɻ Pass a Space as an argument, and it will return an integer representing the distance to that space.
ײͨ͠ใλʔϯ͝ͱʹมԽ͢ΔͷͰɺ࣍ͷλʔϯͰ༻͢ΔͨΊ ूΊͨใɺه͓ͯ͘͠ඞཁ͕͋Γ·͢ɻྫ͑ɺମྗ͕લͷλʔ ϯΑΓݮ͍ͬͯΔ͔Ͳ͏͔Ͱɺ߈ܸ͞Ε͍ͯΔ͔Ͳ͏͔Λ֬ೝ͢Δ͜ ͱ͕Ͱ͖·͢ɻ 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.
εϖʔεϚοϓͷϚεΛද͠·͢ɻ͋ΔΤϦΞʹରͯ͠ηϯεΛߦͬ ͨ߹ɺ௨ৗҰͭ·ͨෳͷ ҰͭͷྻͰ εϖʔε͕ฦ͞Ε·͢ɻ A space is an object representing a square in the level. Whenever you sense an area, often one or multiple spaces (in an array) will be returned.
space.isEmpty() trueͷ߹ɺ͜ͷॴʹԿͳ͘ ֊ஈΛআ͘ ɺ͜͜·Ͱา͚Δ͜ͱ Λҙຯ͠·͢ɻ If true, this means that nothing (except maybe stairs) is at this location and you can walk here.
ଟ͘ͷ߹ɺηϯεͷޙʹ͜ΕΒͷϝιουΛݺͼग़͢͜ͱʹͳΓ· ͢ɻྫ͑ɺ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();
͋͠Δ֊Ͱߦ͖٧·ͬͨ߹ɺ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).
+BWB4DSJQUͰ࡞ۀ͍ͯ͠Δ͜ͱΛΕͳ͍Ͱ͍ͩ͘͞ɻͨͩ୯ʹଟ͘ ͷίʔυͰplayTurnϝιουΛຒΊͳ͍Ͱ͍ͩ͘͞ɻϝιουΫϥ εΛͬͯཧ͠·͠ΐ͏ɻ Remember, you're working in JavaScript here. Don't simply fill up the playTurn method with a lot of code. Organize it with methods and classes.
ηϯεؾܰʹ༻Ͱ͖·͢ɻײ͞ΕͨใΛอଘ͓ͯ͘͜͠ͱͰɺ ͜ͷઌԿͷΞΫγϣϯΛߦ͏͖͔ʹ͍ͭͯͷΑΓΑ͍அʹཱͯͯ ͍ͩ͘͞ɻ Senses are cheap, so use them liberally. Store the sensed information to help you better determine what actions to take in the future.
ϓϩϑΝΠϧσΟϨΫτϦʹ͍ΔؒɺwarriorjsΛ࣮ߦ͢Δͱɺϓ ϩϑΝΠϧΛࣗಈతʹબ͠·͢ɻͦͷͨΊɺ࣮ߦͷͨͼʹબ͢Δඞ ཁ͋Γ·ͤΜɻ Running warriorjs while you are in your profile directory will auto-select that profile so you don't have to each time.
͠ߴಘΛૂ͍ͬͯΔͳΒɺΕͣʹΤϦΞશମΛݟ͍ͯͩ͘͠͞ɻ ͨͱ͑֊ஈۙ͘ʹ͍ͨͱͯ͠ɺ ମྗ͕͋ΔͳΒ ͯ͢ͷͷΛ ೖख͢Δ·ͰதʹೖΒͳ͍Ͱ͍ͩ͘͞ɻԕڑηϯε ྫ͑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.