Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Island Builder - Swift Playgrounds

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Island Builder - Swift Playgrounds

    The challenge of this lesson is to build an island surrounded by a sea.

    Tired of being landlocked? Use code to create your very own island!

    First, create two empty arrays that each store an array of coordinates. One will store coordinates for the island, and the other will store coordinates for the sea.

    Next, write a set of conditions within your if statement to append coordinates to your island array. These coordinates should be in the center of the map, and might be a 3x3 or 4x4 block. Append any coordinates that don't meet these conditions to your sea array.

    Adding water:
    To add water, remove existing items first.

    world.removeItems(at: coordinate)
    world.place(Water(), at: coordinate)

    After you've appended coordinates to each array, place blocks for each coordinate in the island array, and water coordinates in the sea array. Good luck!

    Click image for larger version

Name:	Screen Shot 2017-01-06 at 2.23.51 PM.png
Views:	634
Size:	155.4 KB
ID:	1954

    Hint: You can create an empty array of coordinates like this: var island: [Coordinate] = []. This code creates a new variable that stores an array of coordinates, initialized with no items. After you create your island and sea arrays, write conditions for your if statement that determine if a coordinate is in the island region of the world. For example, you might want to append any coordinate with both the column and row greater than 3 and less than 7.

    Code:
    let allCoordinates = world.allPossibleCoordinates
    //Create two empty arrays of type [Coordinate].
    var island: [Coordinate] = []
    var sea: [Coordinate] = []
    
    for coordinate in allCoordinates {
     if coordinate.column > 3 && coordinate.column < 7 && coordinate.row > 3 && coordinate.row < 7 {
      // Append to island array.
      island.append(coordinate)
     } else {
      // Append to sea array.
      sea.append(coordinate)
     }
    }
    
    // For your island array, place blocks.
    for coordinate in island {
     world.place(Block(), at:coordinate)
    }
    
    // For your sea array, place water.
    for coordinate in sea {
     world.removeItems(at: coordinate)
     world.place(Water(), at: coordinate)
    }
    Your powers are growing! With arrays, you can manage a lot of information quickly and efficiently to create amazing worlds.

    Did you notice how the array allCoordinates was initialized? You can use the all possibleCoordinates property of the world instance to give you an array of all the coordinates in the puzzle world. This lets you iterate over every coordinate. How cool is that?

    Next we will be looking at "Appending Removed Values".

  • #2
     

    Comment


    • #3
      Okay, guys. I can’t not get my code to work. I’ve gone over it about 10 times and I can’t find where the error is. I’m sure the issue is right in front of my eyes.
      My “island” renders properly and then nothing. The other blocks aren’t removed, and the water doesn’t display. The app runs for about 30 seconds and then just stops. It feels like the program isn’t removing the other blocks first. I don’t know if that is the case or not. HELP!!!
      Attached Files
      Last edited by Stylist_Billy; 07-09-2019, 09:59 PM. Reason: Added PDF of my code

      Comment

      Apple Swift Programming Language

      About this Group

      Discussion group for Swift programming language by apple for Xcode
      Type: Public
      Topics: 112
      Comments: 83

      Owner

      Sponsored Box

      Collapse

      Latest Group Topics

      Collapse

      There are no results that meet this criteria.

      Working...
      X