No announcement yet.

Island Builder - Swift Playgrounds

  • 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:	5800
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.

    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.
     } else {
      // Append to sea array.
    // 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


    • #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


      • #4
        I've been over and over it too, my code was the same (except my island was 3x3) and it just builds the island and nothing else. It must be doing something unseen though as the code doesn’t actually stop running until after a period for when the sea would be built. I did notice that if you copy the FOR code to place blocks on the sea coordinates it will then build the sea on top of them but it doesn’t build the island.

        As a final test I copied and pasted Royleeyy's code from above and it does exactly the same thing (apart from the island being 4x4). I assume it must be a bug in a previous update of the app.
        Last edited by Deckie; 01-12-2020, 01:50 PM.


        • #5
          The reason the above code is not working is because there is a slight error in the second to last line of the code.

          Instead of ....

          world.removeItems(at: coordinate)


          world.removeAllBlocks(at: coordinate)


          • #6
            I have a lot of fun reading this blog, I want to take some information course also I needed a lot of course then I found this site https://exer6.org/ and I went here and I found many I got the information about the course and then I did the course, if anyone wants to do the course, then I will tell you on this site that go here and select the course and start the course.​


            Apple Swift Programming Language

            About this Group

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


            Sponsored Box


            Latest Group Topics


            There are no results that meet this criteria.