Swift PlayGrounds コードを学ぼう2 記述ノート

バージョン

Swift Playgrounds 3.3.1
iPad/Mac

コードを学ぼう2 Swift 5.1版

変数

記録する

var gemCounter = 0

for i in 1...2{
    moveForward()
}
collectGem()
gemCounter += 1

値を増やす

var gemCounter = 0

while gemCounter < 5{
    moveForward()
    collectGem()
    gemCounter += 1
}

値を増やす

var gemCounter = 0

func examineGem(){
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}


func moveNOTBkockedIf(){
    
    while !isBlocked{
        moveForward()
        examineGem()
    }
}
moveNOTBkockedIf()
turnRight()
moveNOTBkockedIf()
turnRight()
moveNOTBkockedIf()

7つの宝石を集める

var gemCounter = 0 
while gemCounter < 7{
    if isBlocked{
        turnLeft()
        turnLeft()
    }
    moveForward()
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}

3つの宝石と4つのスイッチ

var gemCounter = 0
var switchCounter = 0
func walk(){
    if isBlocked && isBlockedLeft{
        turnRight()
    } else if isBlocked {
        turnLeft()
    }
    moveForward()
}

while gemCounter < 3{
    walk()
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}
while switchCounter < 4{
    walk()
    if isOnClosedSwitch{
        toggleSwitch()
        switchCounter += 1
    }
}

値が等しいかどうかを調べる

var gemCounter = 0
let switchCounter = numberOfSwitches

func walk() {
    if isBlocked && isBlockedLeft{
        turnRight()
    } else if isBlocked{
        turnLeft()
    }
    moveForward()
}

while gemCounter != switchCounter{
    walk()
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}

決まった数だけスイッチを入れる

var gemCounter = 0
var switchCounter = 0

func walk() {
    if isBlocked && isBlockedLeft{
        turnRight()
    } else if isBlocked{
        turnLeft()
    }
    moveForward()
}

while !isOnClosedSwitch{
    walk()
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}
while gemCounter != switchCounter {
    if isOnClosedSwitch{
        toggleSwitch()
        switchCounter += 1
    }
    walk()
}

決まった数だけ集める

let totalGems = randomNumberOfGems
var gemCounter = 0

func walk() {
    if isBlocked && isBlockedLeft && isBlockedRight{
        turnLeft()
        turnLeft()
    } else if isBlocked && isBlockedLeft{
        turnRight()
    } else if isBlocked{
        turnLeft()
    }
    moveForward()
}


while gemCounter != totalGems {
    walk()
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}

ワープをオフにする

greenPortal.isActive = false
var activeSwitchCounter = 0

func walk() {
    if isBlocked && isBlockedLeft && isBlockedRight{
        turnLeft()
        turnLeft()
    } else if !isBlockedRight{
        turnRight()
    }
    moveForward()
}

while activeSwitchCounter < 3 {
    walk()
    if isOnClosedSwitch{
        toggleSwitch()
        activeSwitchCounter += 1
    }
}

ワープのオンとオフ

var gemCounter = 0

func examineGemAndSwitch() {
    if isOnGem{
        collectGem()
        gemCounter += 1
    } else if isOnClosedSwitch{
        toggleSwitch()
    }
}


func walk() {
    
    if isBlocked {
        turnLeft()
        turnLeft()
        if purplePortal.isActive == true{
            purplePortal.isActive = false
        } else {
            purplePortal.isActive = true
        }
    } 
    moveForward()
}

while gemCounter < 7{
    walk()
    examineGemAndSwitch()
}

ワープを正しく操作する

var gemCounter = 0

func examineGem(){
    if isOnGem {
        collectGem()
        gemCounter += 1
    }
}
func walk() {
    if isBlocked{
        turnLeft()
        turnLeft()
        if bluePortal.isActive == true{
            bluePortal.isActive = false
        } else {
            bluePortal.isActive = true
        }
    }
    moveForward()
}

pinkPortal.isActive = false
while gemCounter < 3{
    walk()
    examineGem()
}
pinkPortal.isActive = true
while gemCounter < 4{
    walk()
    examineGem()
}

隅々まで歩き回る

var gemCounter = 0
var activeSwitchCounter = 0

func walkLBase() {
    if isBlocked && isBlockedLeft && isBlockedRight{
        turnLeft()
        turnLeft()
    } else if isBlocked && isBlockedLeft{
        turnRight()
    } else if !isBlockedLeft{
        turnLeft()
    }
    moveForward()
}
func walkRBase() {
    if isBlocked && isBlockedLeft && isBlockedRight{
        turnLeft()
        turnLeft()
    } else if isBlocked && isBlockedRight{
        turnLeft()
    } else if !isBlockedRight{
        turnRight()
    }
    moveForward()
}
func examineGemAndSwitch() {
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
    if isOnClosedSwitch{
        toggleSwitch()
        activeSwitchCounter += 1
    }
}

    
greenPortal.isActive = false
orangePortal.isActive = false
turnLeft()
moveForward()
while gemCounter < 6{
    walkLBase()
    examineGemAndSwitch()
}
orangePortal.isActive = true
walkLBase()
walkLBase()
examineGemAndSwitch()
orangePortal.isActive = false
greenPortal.isActive = true
while activeSwitchCounter < 3{
    walkRBase()
    examineGemAndSwitch()
}
greenPortal.isActive = false
while activeSwitchCounter < 6{
    walkLBase()
    examineGemAndSwitch()
}

宝石があちこちランダムに

let totalGems = randomNumberOfGems
var gemCounter = 0

func togglePortals() {
    if bluePortal.isActive == true{
        bluePortal.isActive = false
        pinkPortal.isActive = false
    } else {
        bluePortal.isActive = true
        pinkPortal.isActive = true
    }
}


func walk(){
    if isBlocked{
        togglePortals()
        turnLeft()
        turnLeft()
    }
    moveForward()
}
func examineGem(){
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}


while gemCounter < totalGems{
    walk()
    examineGem()
}

初期化

エキスパートを初期化する

let expert = Expert()

func moveThreeBlocks() {
    for i in 1...3{
        expert.moveForward()
    }
}


while !expert.isBlocked{
    expert.moveForward()
}
expert.turnLockUp()

for i in 1...3{
    expert.turnLeft()
    expert.turnLeft()
    moveThreeBlocks()
    expert.turnRight()
    moveThreeBlocks()
    expert.collectGem()
}

エキスパートを育てる

let expert = Expert()

var gemCounter = 0

func examineGem() {
    if expert.isOnGem{
        expert.collectGem()
        gemCounter += 1
    }
}

func walk(){
    if expert.isBlocked && expert.isBlockedLeft && expert.isBlockedRight{
        expert.turnLeft()
        expert.turnLeft()
    } else if expert.isBlocked && expert.isBlockedRight{
        expert.turnLeft()
    } else if !expert.isBlockedRight{
        expert.turnRight()
    }
    expert.moveForward()
}

expert.moveForward()
while gemCounter < 3{
    walk()
    examineGem()
}
expert.turnLockDown()
while gemCounter < 6{
    walk()
    examineGem()
}

違う型のインスタンスを使う

let expert = Expert()
let character = Character()

expert.moveForward()
expert.turnLockUp()

character.moveForward()
character.collectGem()
character.moveForward()
character.turnRight()
character.moveForward()
character.moveForward()

expert.turnLockDown()
expert.turnLockDown()

character.moveForward()
character.collectGem()

力を合わせて

let expert = Expert()
let character = Character()

func expertWalk() {
    if expert.isBlocked{
        expert.turnRight()
    }
    expert.moveForward()
}


expert.turnLeft()
for i in 1...5{
    expertWalk()
}
expert.turnLeft()
for i in 1...2{
    expert.turnLockDown()
}
for i in 1...10{
    expertWalk()
}
expert.turnLeft()
expert.turnLockUp()


while !character.isOnClosedSwitch{
    if character.isOnGem{
        character.collectGem()
    }
    character.moveForward()
}
character.toggleSwitch()

パラメータ

もっと遠くまで進む

let expert = Expert()

func move(distance: Int) {
    for i in 1...distance{
        expert.moveForward()
    }
}

func TurnLeft() {
    expert.turnLeft()
}
func TurnRight() {
    expert.turnRight()
}

move(distance: 6)
TurnRight()
move(distance: 2)
TurnRight()
move(distance: 5)
TurnLeft()
move(distance: 5)
TurnLeft()
expert.turnLockUp()
TurnLeft()
move(distance: 3)
TurnRight()
move(distance: 3)
TurnRight()
move(distance: 4)
expert.collectGem()

関数を作る

let expert = Expert()
let character = Character()

func turnLock(up: Bool, numberOfTimes: Int) {
    if up == true{
        for i in 1...numberOfTimes{
            expert.turnLockUp()
        }
    } else {
        for i in 1...numberOfTimes{
            expert.turnLockDown()
        }
    }
}

func expertTurn() {
    expert.turnLeft()
    expert.turnLeft()
}

func charactorMove() {
    character.moveForward()
}

func characterWalk() {
    if character.isBlocked && character.isBlockedLeft && character.isBlockedRight{
        character.turnLeft()
        character.turnLeft()
    } else if !character.isBlockedRight{
        character.turnRight()
    }
    charactorMove()
}

func characterWalkWhileIsOnGem() {
    while !character.isOnGem{
        characterWalk()
    }
    character.collectGem()
    character.turnLeft()
    character.turnLeft()
    charactorMove()
}



expertTurn()
turnLock(up: true, numberOfTimes: 3)
charactorMove()
turnLock(up: false, numberOfTimes: 3)
characterWalkWhileIsOnGem()

charactorMove()
expertTurn()
turnLock(up: true, numberOfTimes: 1)
characterWalkWhileIsOnGem()

turnLock(up: true, numberOfTimes: 2)
characterWalkWhileIsOnGem()

上げたり下げたり

let expert = Expert()
let character = Character()

var gemCounter = 0

func turnLock(up: Bool, numberOfTimes: Int ) {
    for i in 1...numberOfTimes{
        if up == true{
            expert.turnLockUp()
        } else {
            expert.turnLockDown()
        }
    }
}

func turnCharactor(){
    character.turnLeft()
    character.turnLeft()
}
func expertTurnLeft() {
        expert.turnLeft()
}

func walk() {
    if character.isBlocked && character.isBlockedLeft && character.isBlockedRight{
        turnCharactor()
    } else if character.isBlocked && character.isBlockedLeft{
        character.turnRight()
    } else if !character.isBlockedLeft{
        character.turnLeft()
    }
    character.moveForward()
}
func examineGem() {
    if character.isOnGem{
        character.collectGem()
        gemCounter += 1
    }
}



turnLock(up: true, numberOfTimes: 4)
for i in 1...3{
    expertTurnLeft()
    turnLock(up: true, numberOfTimes: 1)
}

character.moveForward()
while gemCounter < 2{
    walk()
    examineGem()
}

turnCharactor()
character.moveForward()

expertTurnLeft()
turnLock(up: false, numberOfTimes: 3)

character.turnRight()
while gemCounter < 6{
    walk()
    examineGem()
}
turnCharactor()
character.moveForward()

expertTurnLeft()
turnLock(up: true, numberOfTimes: 3)

character.turnRight()
character.moveForward()
examineGem()

決まった位置に置く

let expert = Expert()
world.place(expert, facing: .east, atColumn: 1, row: 6)

func move(distance: Int) {
    for i in 1...distance{
        expert.moveForward()
    }
}
func expertCollectGem() {
    expert.collectGem()
}

expertCollectGem()
move(distance: 1)
expert.turnLockUp()
expert.turnRight()
move(distance: 5)
expert.turnRight()
move(distance: 1)
expertCollectGem()
expert.turnLeft()
expert.turnLeft()
move(distance: 1)
expert.turnLockUp()
expert.turnRight()
while !expert.isOnGem{
    move(distance: 1)
}
expertCollectGem()

川を渡る

let expert = Expert()
world.place(expert, facing: .south, atColumn: 1, row: 8)

var gemCounter = 0

while gemCounter < 9{
    while !expert.isBlocked{
        if expert.isOnGem{
            expert.collectGem()
            gemCounter += 1
        }
        expert.moveForward()
    }
    if expert.isBlockedRight{
        expert.turnLockDown()
        expert.turnLeft()
    } else if expert.isBlockedLeft{
        expert.turnLockUp()
        expert.turnRight()
    }
}

2つのキャラクターを置く

let expert = Expert()
let character = Character()
world.place(expert, facing: .north, atColumn: 3, row: 0)
world.place(character, facing: .north, atColumn: 0, row: 0)

var gemCounter = 0

expert.toggleSwitch()

while gemCounter < 5{
    if character.isOnGem{
        character.collectGem()
        gemCounter += 1
    }
    
    if gemCounter == 3{
        character.turnRight()
    } else if gemCounter == 5{
        character.turnLeft()
    }
    character.jump()
    character.jump()
}
character.collectGem()

2つのエキスパート

let expert = Expert()
let expert2 = Expert()

world.place(expert, facing: .east, atColumn: 0, row: 0)
world.place(expert2, facing: .north, atColumn: 0, row: 4)

func expertMove(distance: Int){
    for i in 1...distance{
        expert.moveForward()
    }
}

//expert1
expert.collectGem()
expertMove(distance: 3)
expert.turnLeft()
for i in 1...2{
    expert.turnLockUp()
}
expert.turnRight()
//expert2
expert2.turnLockDown()
expert2.turnRight()
//expert1
expertMove(distance: 3)
for i in 1...2{
    expert.turnLockDown()
}
//expert2
while !expert2.isBlocked{
    if expert2.isOnGem{
        expert2.collectGem()
    }
    expert2.moveForward()
}

2つの山

let totalGems = randomNumberOfGems

let expert = Expert()
let character = Character()
world.place(expert, facing: .north, atColumn: 0, row: 4)
world.place(character, facing: .north, atColumn: 2, row: 0)

var gemCounter = 0

func examineGem() {
    if character.isOnGem && gemCounter < totalGems{
        character.collectGem()
        gemCounter += 1
    }
}
func jump(distance: Int) {
    for i in 1...distance{
        examineGem()
        character.jump()
    }
}
func walk() {
    while !character.isBlocked || !character.isBlockedLeft || !character.isBlockedRight{
    examineGem()
    if character.isBlocked && character.isBlockedLeft{
        character.turnRight()
    } else if character.isBlocked{
        character.turnLeft()
    }
    character.moveForward()
    }
}


expert.turnLockUp()

while gemCounter < totalGems{
    jump(distance: 6)
    walk()
    character.turnLeft()
    jump(distance: 6)
    expert.turnLock(up: true, numberOfTimes: 2)
    walk()
    expert.turnLock(up: false, numberOfTimes: 2)
    character.turnRight()
}

ステージを作る

道を作る

let block1 = Block()
world.place(block1, atColumn: 3, row: 3)

while !isOnClosedSwitch{
    if isBlocked && isBlockedLeft{
        turnRight()
    } else if isBlocked && isBlockedRight{
        turnLeft()
    }
    moveForward()
}
toggleSwitch()

道をつなぐ

let block1 = Block()
let block2 = Block()
let block3 = Block()
let block4 = Block()
let block5 = Block()

func setBlock(item: Item, atColumn: Int){
    world.place(item, atColumn: atColumn, row: 2)
}

setBlock(item: block1, atColumn: 2)
setBlock(item: block2, atColumn: 2)
setBlock(item: block3, atColumn: 4)
setBlock(item: block4, atColumn: 6)
setBlock(item: block5, atColumn: 6)

for i in 1...3{
    move(distance: 2)
    turnRight()
    move(distance: 4)
    collectGem()
    turnLeft()
    turnLeft()
    move(distance: 4)
    turnRight()
    toggleSwitch()
}

ワープを作る

let greenPortal = Portal(color: #colorLiteral(red: 0.4028071761, green: 0.7315050364, blue: 0.2071235478, alpha: 1))
var gemCounter = 0

func examineGem() {
    if isOnGem{
        collectGem()
        gemCounter += 1
    }
}
func walk() {
    if isBlocked && isBlockedLeft && isBlockedRight{
        turnLeft()
        turnLeft()
    } else if !isBlockedRight{
        turnRight()
    }
    moveForward()
}



while gemCounter < 4{
    examineGem()
    walk()
}
world.place(greenPortal, atStartColumn: 1, startRow: 4, atEndColumn: 5, endRow: 0)
moveForward()
greenPortal.isActive = false
while gemCounter < 8{
    examineGem()
    walk()
}

階段を置く

var activeSwitchCounter = 0

func setStair(facing: Direction, atColumn: Int, row: Int) {
    world.place(Stair(), facing: facing, atColumn: atColumn, row: row)
}
setStair(facing: .south, atColumn: 3, row: 1)
setStair(facing: .south, atColumn: 3, row: 3)

var StairRow = 4
var StairColumn = 2
for i in 1...3{
    setStair(facing: .east, atColumn: 5, row: StairRow)
    setStair(facing: .west, atColumn: 1, row: StairRow)
    setStair(facing: .north, atColumn: StairColumn, row: 7)
    StairRow += 1
    StairColumn += 1
}

func walk() {
    if isBlocked && isBlockedRight{
        turnLeft()
    } else if !isBlockedRight{
        turnRight()
    }
    moveForward()
}

move(distance: 4)
turnRight()
move(distance: 2)
while activeSwitchCounter < 9{
    walk()
    if isOnClosedSwitch{
        toggleSwitch()
        activeSwitchCounter += 1
    }
}

島を渡る

let expert = Expert()
let character = Character()
world.place(Portal(color: #colorLiteral(red: 0.9372549019607843, green: 0.34901960784313724, blue: 0.19215686274509805, alpha: 1.0)), atStartColumn: 1, startRow: 0, atEndColumn: 2, endRow: 1)
world.place(Block(), atColumn: 1, row: 4)
world.place(Block(), atColumn: 1, row: 5)
var blockRow = 2
for i in 1...5{
    world.place(Block(), atColumn: 1, row: blockRow)
    blockRow += 1
}

world.place(Block(), atColumn: 5, row: 3)

world.place(expert, facing: .north, atColumn: 1, row: 6)
world.place(character, facing: .south, atColumn: 6, row: 4)

func expertWalk() {
    if expert.isBlocked && expert.isBlockedLeft && expert.isBlockedRight{
        expert.turnLeft()
        expert.turnLeft()
    } else if !expert.isBlockedLeft{
        expert.turnLeft()
    }
    expert.moveForward()
}

//expert
expert.collectGem()
while !expert.isOnGem{
    expertWalk()
    if expert.isOnClosedSwitch{
        expert.toggleSwitch()
    }
}
expert.collectGem()

//character
character.collectGem()
character.jump()
character.toggleSwitch()
for i in 1...2{
    character.turnRight()
    character.moveForward()
    if character.isOnClosedSwitch{
        character.toggleSwitch()
    }
}

ループを作る

let totalGems = randomNumberOfGems

let charactor = Character()

var gemCounter = 0

world.place(Block(), atColumn: 0, row: 2)
world.place(Block(), atColumn: 3, row: 3)
world.place(Portal(color: #colorLiteral(red: 0.9372549019607843, green: 0.34901960784313724, blue: 0.19215686274509805, alpha: 1.0)), atStartColumn: 2, startRow: 3, atEndColumn: 7, endRow: 3)
world.place(charactor, facing: .south, atColumn: 7, row: 3)

func walk() {
    if charactor.isBlocked && charactor.isBlockedRight{
        charactor.turnLeft()
    } else if charactor.isBlocked{
        charactor.turnRight()
    }
    charactor.moveForward()
}
func examineGem() {
    if charactor.isOnGem{
        charactor.collectGem()
        gemCounter += 1
    }
}

while gemCounter < totalGems{
    walk()
    examineGem()
}

自分だけのステージは省略

配列

ここら辺から急に難易度が上がる。あと説明が雑になる。

情報をまとめて保存

// 抜けている行を配列に追加します。
var rows = [0, 1, 2, 3, 4, 5]

// 行の配列に含まれる各行にキャラクターを置きます。
placeCharacters(at: rows)

反復処理を試す

let columns = [0, 1, 2, 3, 4]

// ループの変数に名前を付けて配列に渡します。
for currentColumn in columns {
    // 各列に宝石とスイッチを配置します。
    world.place(Gem(), atColumn: currentColumn, row: 1)
    world.place(Switch(), atColumn: currentColumn, row: 1)
}

ブロックを積む

// 残りの2隅の座標を追加します。
var blockLocations = [
    Coordinate(column: 0, row: 0),
    Coordinate(column: 3, row: 3),
    Coordinate(column: 0, row: 3),
    Coordinate(column: 3, row: 0)
]
// 各座標に5つのブロックを置きます。
for i in 1 ... 5 {
    
    for coordinate in blockLocations {
        world.place(Block(), at: coordinate)
    }
}

順番に並べる

characters = [
    Character(name: .blu),
    Portal(color: #colorLiteral(red: 0.9098039215686274, green: 0.47843137254901963, blue: 0.6431372549019608, alpha: 1.0))
    Character(name: .hopper),
    Gem()
]


// ワープを削除します。
characters.remove(at: 1)
// 宝石を削除します。
characters.remove(at: 2)
// エキスパートを追加します。
characters.insert(Expert(), at: 1)
var rowPlacement = 0
for character in characters {
    world.place(character, at: Coordinate(column: 1, row: rowPlacement))
    rowPlacement += 1
}

配列に追加する

let allCoordinates = world.allPossibleCoordinates
var blockSet: [Coordinate] = []

for coordinate in allCoordinates {
    // 座標の列の値が5よりも大きいか、または、行の値が4より小さいかを調べます。
    if coordinate.column > 5 || coordinate.row < 4 {
        // blockSetに座標を追加します。
        blockSet.append(coordinate)
    }
}

// blockSet内の各座標について、6個のブロックを配置します。
for coordinate in blockSet {
    for i in 1...6{
        world.place(Block(), at: coordinate)
    }
}

島を作る

let allCoordinates = world.allPossibleCoordinates
// [Coordinate]型の空の配列を2つ作成します。
var blcockSet : [Coordinate] = []
var waterSet : [Coordinate] = []

for coordinate in allCoordinates {
    if coordinate.column > 3 && coordinate.column < 8 && coordinate.row > 3 && coordinate.row < 8 {
        // 陸地の配列に追加します。
        blcockSet.append(coordinate)
    } else {
        // 海の配列に追加します。
        waterSet.append(coordinate)
    }
}
// 陸地の配列内の各座標に、ブロックを配置します。
for coordinate in blcockSet {
    world.place(Block(), at: coordinate)
}

// 海の配列内の各座標に、水を配置します。
for coordinate in waterSet {
    world.removeAllBlocks(at: coordinate)
    world.place(Water(), at: coordinate)
}

取り除いた値を追加する

// 行2のすべての座標を含む配列を作成します。
var allCoordinatesOfRow2 = world.row(2)
// 座標を入れるための空の配列を作成します。
var  coodinateBox: [Coordinate] = []

for i in 1...12 {
    for coordinate in allCoordinatesOfRow2 {
        world.place(Block(), at: coordinate)
    }
    // 座標を取り除いて、空の配列に追加します。
    coodinateBox.append(allCoordinatesOfRow2.removeFirst())
}

// 空の配列に追加した各座標にキャラクターを置きます。
for coordinate in coodinateBox {
    world.place(Character(), at: coordinate)
}

インデックスの範囲外エラーを直す

var teamBlu: [Character] = []

// 配列にBluのインスタンスが何個含まれるか数えてみてください。
for i in 1...9 {
    teamBlu.append(Character(name: .blu))
}

// 行4の各列にBluを置きます。
var columnPlacement = 0
for blu in teamBlu {
    world.place(blu, at: Coordinate(column: columnPlacement, row: 4))
    columnPlacement += 1
}

// インデックスの範囲外エラーを見つけてください。
teamBlu[0].jump()
teamBlu[2].collectGem()
teamBlu[4].jump()
teamBlu[6].collectGem()
teamBlu[8].jump()


//一番下の行を削除

地形を作る

var heights: [Int] = [6,3,8,1,2]
let allCoordinates = world.allPossibleCoordinates

var index = 0
for coordinate in allCoordinates {
    if index == heights.count {
        index = 0
    }
    for i in 0...heights[index] {
        // ブロックを置きます。
        world.place(Block(), at: coordinate)
    }
    // インデックスを1増やします。
    index += 1
}

ランダムな地形

let allCoordinates = world.allPossibleCoordinates
var heights: [Int] = []

// 高さの配列にランダムな数を追加します。
    for i in 1...10{
        heights.append(randomInt(from: 0, to: 14))
    }

var index = 0
for coordinate in allCoordinates {
    if index == heights.count {
        index = 0
    }
    
    // 現在のインデックス位置の高さをcurrentHeightに保存します。
    var currentHeight = heights[index]
    

    if currentHeight == 0 {
        // currentHeightが0の場合に何かおもしろいことをしてみましょう。
        world.place(Platform(), at: coordinate)

    } else {
        for i in 1...currentHeight {
            world.place(Block(), at: coordinate)
        }
        if currentHeight > 10 {
            // キャラクターを置くなど、何か別のことをします。
            world.place(Character(name: .blu), at: coordinate)
        } else if coordinate.column >= 3 && coordinate.column < 6 {
            // 水を置くなど、さらに別のことをします。
            world.removeAllBlocks(at: coordinate)
            world.place(Water(), at: coordinate)
        }
        // さらにコードを追加して、自分だけのステージを作りましょう。
        else  {
            world.place(Character(name: .hopper), at: coordinate)
        }
        
    }
    index += 1
    
}

違う方法で配列を作る

let allCoordinates = world.allPossibleCoordinates

for coordinate in allCoordinates {
    // 各座標について、その列と行の値を足したものを高さに設定します。
    let height = coordinate.column + coordinate.row
    
    for i in 0...height {
        world.place(Block(), at: coordinate)
    }
    
    if height >= 8 && height < 10 {
        world.place(Character(name: .blu), at: coordinate)
    } else if height > 9 {
        world.place(Character(name: .hopper), at: coordinate)
    }
}

// ステージ内にすでにいるキャラクターを含む配列を初期化します。
let characters = world.existingCharacters(at: allCoordinates)
// 各キャラクターを動かします。
for charactor in characters {
    charactor.danceLikeNoOneIsWatching()
    charactor.turnUp()
    charactor.breakItDown()
    charactor.grumbleGrumble()
    charactor.argh()
}

配列の技、ステージを作るは省略

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Proudly powered by WordPress | テーマ: Baskerville 2 by Anders Noren

ページ先頭へ ↑