MacOS Playgrounds 学习编程一 第三十九关 富饶之地

在上一关里,我们发现第二段的代码才是最简洁的方法。所以,我们要知道不要被当前的影响桎梏,要正确思考问题的解决方案。

这一关里,可不像上一关那样子,这关的目标是随机变化的。

这可能就要我们多尝试几次,才能找到正确的解决方法。

关键点在于,我们如果控制角色的行进路线。这要利用条件的判断。while循环是我们的工具。

func ranRan() {   //角色自行判断前方障碍前选择无障碍路线

    if isBlocked { //前方堵了

        if isBlockedLeft { //左边也堵

            turnRight()

            moveForward()

            turnRight()

        }else if !isBlockedLeft && !isBlockedRight { //两边不堵

            turnLeft()

            moveForward()

            turnLeft()

        }

    }else if !isBlocked { //前面不堵

        moveForward()

        if isOnClosedSwitch { //开关位置

            toggleSwitch()

        }

        if isOnGem {  //宝石位置

            collectGem()

        }

    }

}

while isBlockedLeft || !isBlocked{ //当左边不能走,前面能走时

    ranRan()

    if isBlocked { //前面堵了

        ranRan()

    }

}

多尝试几遍,仔细体会,角色判断力的变化。要自己去敲代码进去。


传送门:

MacOS Playgrounds学习 学习编程一 前记--https://www.jianshu.com/p/a0de277374a4

MacOS Playgrounds 学习编程一 第一关 发出命令--https://www.jianshu.com/p/3c85397f1eae

MacOS Playgrounds 学习编程一 第二关 添加新命令--https://www.jianshu.com/p/4efa5c18dc50

MacOS Playgrounds 学习编程一 第三关 切换开关--https://www.jianshu.com/p/00556d6430bf

MacOS Playgrounds 学习编程一 第四关 传送门练习--https://www.jianshu.com/p/47bd57d0193f

MacOS Playgrounds 学习编程一 第五关 发现并修复程序错误--https://www.jianshu.com/p/5bc9200edbbe

MacOS Playgrounds 学习编程一 第六关 消灭程序错误练习--https://www.jianshu.com/p/f19bd2955ed8

MacOS Playgrounds 学习编程一 第七关 最短路线--https://www.jianshu.com/p/1ef210253be5

MacOS Playgrounds 学习编程一 第八关 组合新行为--https://www.jianshu.com/p/f548b1fd3134

MacOS Playgrounds 学习编程一 第九关 创建新函数--https://www.jianshu.com/p/22ab39aaea97

MacOS Playgrounds 学习编程一 第十关 收集、切换、重复--https://www.jianshu.com/p/e43d60f98bc7

MacOS Playgrounds 学习编程一 第十二关 嵌套模式--https://www.jianshu.com/p/645f6716217c

MacOS Playgrounds 学习编程一 第十三关 嵌入式阶梯--https://www.jianshu.com/p/d87f9662601d

MacOS Playgrounds 学习编程一 第十四关 寻宝--https://www.jianshu.com/p/18c73154e46b

MacOS Playgrounds 学习编程一 第十五关 使用循环--https://www.jianshu.com/p/f7c0866930ec

MacOS Playgrounds 学习编程一 第十六关 循环每一侧--https://www.jianshu.com/p/94d75e9d5eab

MacOS Playgrounds 学习编程一 第十七关 行至边缘再返回--https://www.jianshu.com/p/cc7e8095bb9a

MacOS Playgrounds 学习编程一 第十八关 循环跳跃者--https://www.jianshu.com/p/99618bef896e

MacOS Playgrounds 学习编程一 第十九关 拓展技能--https://www.jianshu.com/p/a20810b11d6d

MacOS Playgrounds 学习编程一 第二十关 宝石农场--https://www.jianshu.com/p/8c3808d1a646

MacOS Playgrounds 学习编程一 第二十一关 席卷四处--https://www.jianshu.com/p/cc7f6a1c34e7

MacOS Playgrounds 学习编程一 第二十二关 检查开关--https://www.jianshu.com/p/e34fb0431b6e

MacOS Playgrounds 学习编程一 第二十三关 使用 else if--https://www.jianshu.com/p/6414ef7692e2

MacOS Playgrounds 学习编程一 第二十四关 循环条件代码--https://www.jianshu.com/p/a657e47b851d

MacOS Playgrounds 学习编程一 第二十五关 满足条件时向上爬--https://www.jianshu.com/p/4a69a1c3435f

MacOS Playgrounds 学习编程一 第二十六关 定义更巧妙的函数--https://www.jianshu.com/p/d61e03e095ae

MacOS Playgrounds 学习编程一 第二十七关 围困其中--https://www.jianshu.com/p/88442c7ce61b

MacOS Playgrounds 学习编程一 第二十八关 决策树--https://www.jianshu.com/p/0843bd9ae286

MacOS Playgrounds 学习编程一 第二十九关 使用“非”运算符--https://www.jianshu.com/p/be8fa74b6c46

MacOS Playgrounds 学习编程一 第三十关 非之螺旋--https://www.jianshu.com/p/f42905cbaed1

MacOS Playgrounds 学习编程一 第三十一关 检查这个与那个--https://www.jianshu.com/p/67b1c6e16b6a

MacOS Playgrounds 学习编程一 第三十二关 检查这个或那个--https://www.jianshu.com/p/d94a67728a93

MacOS Playgrounds 学习编程一 第三十三关 逻辑迷宫--https://www.jianshu.com/p/62bbc0ce2490

MacOS Playgrounds 学习编程一 第三十四关 当……时运行代码--https://www.jianshu.com/p/9aa41733c6d0

MacOS Playgrounds 学习编程一 第三十五关 创建更巧妙的while循环--https://www.jianshu.com/p/3fdfb33a443f

MacOS Playgrounds 学习编程一 第三十六关 正确选取工具--https://www.jianshu.com/p/e5ed25be735e

MacOS Playgrounds 学习编程一 第三十七关 四乘以四--https://www.jianshu.com/p/81ed2e6009d4

MacOS Playgrounds 学习编程一 第三十八关 转身--https://www.jianshu.com/p/a3c9bae7d6a2

MacOS Playgrounds 学习编程一 第三十九关 富饶之地--https://www.jianshu.com/p/077743cdfcdd

MacOS Playgrounds 学习编程一 第四十关 套嵌循环--https://www.jianshu.com/p/d8670c9e7962

MacOS Playgrounds 学习编程一 第四十一关 随机矩形--https://www.jianshu.com/p/61b916f61e37

MacOS Playgrounds 学习编程一 第四十二关 始终右转--https://www.jianshu.com/p/b7cf92fbfcaa

MacOS Playgrounds 学习编程一 第四十三关 右手定则--https://www.jianshu.com/p/9ccd20151c96

MacOS Playgrounds 学习编程一 第四十四关 调整算法--https://www.jianshu.com/p/10c2a0de990d

MacOS Playgrounds 学习编程一 第四十五关 征服迷宫--https://www.jianshu.com/p/b4d731fe3639

MacOS Playgrounds 学习编程一 第四十六关 左转还是右转--https://www.jianshu.com/p/cd7fa32316e5

MacOS Playgrounds 学习编程一 第四十七关 向左走,向右走--https://www.jianshu.com/p/b9b1dc9c8f2e

MacOS Playgrounds学习 学习编程一 小结后记--https://www.jianshu.com/p/8ad6485e447a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。