LoginSignup
0
0

More than 1 year has passed since last update.

Playgrounds コードを学ぼう2 エキスパートを初期化する、クリア!

Posted at

仕事の忙しさにかまけて、自宅学習を怠っていました。
これでは、結果はついてきません。

久しぶりに、Playgroundsでswiftプログラミングの練習を実施。

以下、表題クリア時のソースです。

test.swift
var daiya = 3
var ii = 0


// 前進して、ボタンを押す処理
func zenshin(){
    // 正面ブロックでなければ、進む
    while !expert.isBlocked {
        expert.moveForward()
    }
    expert.turnLockUp()// 鍵を開ける
    expert.turnRight()// 右を向く
}

func an(){
    // 定義ダイヤの数と取得ダイヤの数がイコールでなければ実行
    while daiya != ii {
        //全方向行き止まりではないなら、実行
        if !expert.isBlocked && !expert.isBlockedLeft && !expert.isBlockedRight {
            // ジェムの上に来たら、、、ジェムを取得
            if expert.isOnGem {
                expert.collectGem()
                ii = ii + 1}
            expert.turnLeft()
            expert.moveForward()
        }
        //左が行き止まり、かつ正面が行き止まりでない
        if expert.isBlockedLeft && !expert.isBlocked {
            // ジェムの上に来たら、、、ジェムを取得
            if expert.isOnGem {
                expert.collectGem()
                ii = ii + 1}
            expert.moveForward()
            //右が行き止まり、かつ正面が行き止まりでない
        } else if expert.isBlockedRight && !expert.isBlocked {
            // ジェムの上に来たら、、、ジェムを取得
            if expert.isOnGem {
                expert.collectGem()
                ii = ii + 1}
            expert.turnLeft()
            expert.moveForward()
            //右が行き止まり、かつ正面も行き止まり
        } else if expert.isBlocked && expert.isBlockedRight {
            // ジェムの上に来たら、、、ジェムを取得
            if expert.isOnGem {
                expert.collectGem()
                ii = ii + 1}
            expert.turnLeft()
            expert.moveForward()
            //正面が行き止まりならば
        } else if expert.isBlocked {
            // ジェムの上に来たら、、、ジェムを取得
            if expert.isOnGem {
                expert.collectGem()
                ii = ii + 1}
            expert.turnRight()
            expert.moveForward()

        } else {

        }


    }

}
/*ここからが実行部分*/
zenshin()
an()




0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0