NOT演算子を使う@コードを学ぼう1
//NOT演算子を使う@コードを学ぼう1 for i in 1 ... 4 { moveForward() if !isOnGem{ turnLeft() moveForward() moveForward() collectGem() turnRight() turnRight() moveForward() moveForward() turnLeft() } collectGem() }
関数を二つ定義してみた。
func stepTwo() { moveForward() moveForward() } func turnAround() { turnLeft() turnLeft() } for i in 1 ... 4 { moveForward() if !isOnGem { turnLeft() stepTwo() collectGem() turnAround() stepTwo() turnLeft() } collectGem() }