Code Combat|AIRDROP(java script)の解答

Code Combat|AIRDROP(java script) solution

自力で解きたい方は見ないようにしてください。

// すべての剣を集め、村を守れ!
function onSpawn(event) {
    while (true) {
        var item = hero.findNearestItem();
        //  The pet should fetch the item if it exists:
        if (item) {
            pet.fetch(item);
        }
    }
}
// Assign onSpawn function for the pet's "spawn".
pet.on("spawn", onSpawn);
while (true) {
    // Guard the left passage: 
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }
}

if (item) の箇所、テンプレートだと

if (item.type == “potion”)

のようになってるので “potion” を “sword” とか “arms” に変えたりしたのですがそれではうまく動かず、if (item) のみにしたら正解でした。

しかし、codecombatの解答を検索してもPython版ばかり出てくるのでjava scriptを学んでいることに躊躇してしまいます。

Code Combat|SHORT-SIGHTED BURL(java script)の解答

結構悩んだので共有します。自力で解きたい方は見ないようにしてください。

Code Combat, SHORT-SIGHTED BURL (java script) stage solution.

画面全体
code
// Collect coins and run, or else the burl will find you.

// This function allows your hero take an item.
function takeItem(item) {
    hero.moveXY(item.pos.x, item.pos.y);
}

// Write the function "checkTakeRun" with one parameter.
// If the item exists, use "takeItem" function to take it.
// Go back to the start (green mark), with or without the item.
function checkTakeRun(target) {
if (target) {
    hero.moveXY(target.pos.x, target.pos.y);
}
    hero.moveXY(40, 12);
}

// Don't change this code.
while (true) {
    hero.moveXY(16, 56);
    var coin = hero.findNearestItem();
    checkTakeRun(coin);
    
    hero.moveXY(64, 56);
    coin = hero.findNearestItem();
    checkTakeRun(coin);
}

Code Combat|9つの宝石ステージの解答

4つのコードのみで9つの宝石をゲットすることがクリア条件となるステージ。

私が解いたのはこちらのコード。

hero.moveXY(32, 48);
hero.moveXY(32, 14);
hero.moveXY(64, 48);
hero.moveXY(44, 48);

良くある一筆書きクイズの解き方を参考にしました。

1ブロック分、大回りすることで全部回ることができます。

CodeCombat|ゲーム開発|9. PERSISTENCE PAYSの解答

CodeCombatでプログラミングの勉強をしています。

CodeCombat – PythonとJavaScriptを学ぶインデックスゲーム | コードコンバット

https://codecombat.com/

まずはJavascriptのコースを進めています。

基本的にはヒントを見れば解けるのですがたまに引っかかる問題があるので、そういった箇所の解答をメモしておこうと思います。

今回は以下の問題。

ゲーム開発キャンペーン|9. PERSISTENCE PAYS

// You can use a database to store persistent data.
// Persistent data stays the same between plays of your game!
var generator = game.spawnXY("generator", 60, 40);
generator.spawnType = "munchkin";
generator.spawnDelay = 1;
var player = game.spawnPlayerXY("raider", 36, 30);
player.maxHealth = 70;
player.attackDamage = 10;
game.addSurviveGoal(8);

// db stands for database
// db.add(key, value) increments a value stored in the database.
// This adds 1 to the "plays" key in the database.
db.add("plays", 1);

// Show the value of the "plays" and other keys in the db
ui.track(db, "plays");
ui.track(db, "wins");
ui.track(db, "total defeated");
ui.track(game, "time");

// Show the value of the "defeated" property of the game object
ui.track(game, "defeated");

// The code below will run when the player wins the game.
function onVictory(event) {
    db.add("wins", 1);

    // Use db.add(key, value) to add the value of
    // game.defeated to the database with the key "total defeated"
    db.set("defeated", game.defeated);
}
game.on("victory", onVictory);

どうしても解けない方は参考にしてみてください。