playground 34

NOT의 소용돌이

for i in 1 ... 13 { // 13번 반복해라 if !isBlocked { // 만약 타일 한 개만큼 앞으로 갈 수 있다면 moveForward() // 앞으로 한칸가라 }else { // 아니면 turnLeft() // 왼쪽으로 돌고 moveForward() // 앞으로 한칸가라 } } toggleSwitch() //13번 반복 후 스위치를 눌러라 *else 말고 else if를 사용해도 돌아감 스위프트 플레이그라운드 Swift Playground 코딩배우기1 - NOT의 소용돌이

Not 연산자 사용하기

for i in 1 ... 4 { //4번 반복하세요 if isBlocked{ //벽에 막히면은 turnLeft() turnLeft() //반바퀴 돌아라 moveForward() moveForward() //두칸 앞으로가고 turnLeft() //왼쪽으로 돌아라 } moveForward() // 한칸 앞으로 가라 if isOnGem { // 보석이 있으면 먹어라 collectGem() } else if !isOnGem{ //보석이 없으면 왼쪽으로 돌고 앞으로 두칸 잼을 먹어라 turnLeft() moveForward() moveForward() collectGem() } }

의사 결정 트리

func solveRightSide(){ //solveRightSide 함수 collectGem() turnRight() moveForward() moveForward() moveForward() turnLeft() moveForward() collectGem() turnLeft() turnLeft() moveForward() turnRight() moveForward() moveForward() moveForward() turnRight() //요약하자면 우측가서 보석먹고 돌아오기 } func solveLeftSide(){ // solveLeftSide 함수 toggleSwitch() turnLeft() moveForward() collectGem() turnRight() turnRight() move..

[playground] If문 활용1 (Swift)

func collectOrToogle(){ //collectOrToggle이라는 보석을 수집하고 닫힌 스위치를 여는 함수만듦 if isOnClosedSwitch { // 만약에 스위치가 닫혀있다면 toggleSwitch() // 스위치를 눌러라 } if isOnGem{ // 만약 보석이 있다면 collectGem() // 보석을 수집해라 } } for i in 1 ... 4{ //4번동안 반복문 앞으로한칸 + 보석수집, 스위치키기 moveForward() collectOrToggle() } turnLeft() // 왼쪽으로 돌기 for i in 1 ... 2 { //2번동안 반복문 앞으로한칸 + 보석수집, 스위치키기 moveForward() collectOrToggle() } turnLeft() for ..