做题感悟(PlayGrounds随机宝石遍布各处)
做PlayGrounds随机宝石遍布各处这关时,总想着用while循环一次搞定,但是没有关键点和规律可找,一直钻牛角尖,说明还是不能很好的贯通知识,最后还是用笨方法,一步一步写,最后简化代码,发现规律,完成答题,感悟是:如果没有思路就用一步一步去做题,没有捷径就勤奋点
let totalGems = randomNumberOfGems
var gem = 0
pinkPortal.isActive=false
bluePortal.isActive=false
func move(){moveForward();if isOnGem {collectGem()
gem+=1
}
}
func zhuanshen() {
turnLeft();turnLeft()
}
for i in 1 ... 2 {
if i==2{move()}
move()
move()
turnLeft()
move()
zhuanshen()
move()
move()
zhuanshen()
move()
turnLeft()
pinkPortal.isActive=true
bluePortal.isActive=true
move()
move()
move()
zhuanshen()
move()
pinkPortal.isActive=false
bluePortal.isActive=false
move()
move()
move()
zhuanshen()
move()
pinkPortal.isActive=true
move()
}用的for循环遍历作出来的题,但缺点很明显,不能做步数多的,也不符合算法的适用性,还需要在思考
let totalGems = randomNumberOfGems
var gem = 0
var y=0
func move(){moveForward();if isOnGem {collectGem();gem+=1}}
while gem<randomNumberOfGems{
if isBlocked{turnLeft();if isBlocked {turnLeft();y+=1;moveForward()}}
if y==2{if !isBlockedLeft || !isBlockedRight{turnLeft()};
y-=2;
pinkPortal.isActive=true
bluePortal.isActive=true
}
else {pinkPortal.isActive=false
bluePortal.isActive=false
}
move()
用while循环做出来了,多思考找到每一步要改变的独有的特点,比如转两次要开起传送门,当左边/右边无阻挡时转弯,真的锻炼思维