Answer Key

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Your final code should look something like this:

  import SwiftUI

  struct ContentView: View {
    var data = [
      [
       ["🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩",
          "🟩", "🟩","🟩","🟩"],

       ["🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟩","🟧","🟧","🟧",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟧","🟩"],

       ["🟩","🟧","🟧","🟧","🟧","🟧","🟩","🟧","🟧","🟧","🟧","🟧",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟧","🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟧","🟩"],

       ["🟩","🟩","🟩","🟩","🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟧",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟧","🟧","🟧","🟧","🟧","🟩","🟩","🟩","🟩","🟧","🟩",
          "🟧","🟩","🟧","🟩"],

       ["🟩","🟧","🟩","🟩","🟩","🟧","🟩","🟧","🟧","🟧","🟧","🟩",
          "🟧","🟩","🟧","🟩"],

       ["🟩","🟧","🟧","🟧","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟧","🟩"],

       ["🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟩","🟩","🥕","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟩","🟩"],
      ],
      [
       ["🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟩","🟩"],

       ["🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟩","🟧","🟧","🟧",
          "🟧","🟧","🟧","🟩" ],

       ["🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟧","🟧","🟧","🟩","🟩",
          "🟩","🟩","🟧","🟧"],

       ["🟩","🟧","🟧","🟩","🟧","🟧","🟩","🟧","🟩","🟧","🟧","🟧",
          "🟩","🟧","🟧","🟩"],


       ["🟩","🟩","🟧","🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟧","🟩"],


       ["🟩","🟩","🟧","🟩","🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟩",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟩","🟧","🟧","🟩","🟧","🟩","🟩","🟩","🟩","🟧","🟩",
          "🟧","🟩","🟩","🟩"],

       ["🥕","🟧","🟩","🟩","🟩","🟧","🟩","🟧","🟧","🟧","🟧","🟩",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟧","🟧","🟧","🟩","🟧","🟩","🟧","🟩","🟩","🟧","🟩",
          "🟩","🟩","🟧","🟩"],


       ["🟩","🟩","🟩","🟧","🟧","🟧","🟩","🟧","🟧","🟧","🟧","🟧",
          "🟧","🟧","🟧","🟩"],

       ["🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩",
          "🟩","🟩","🟩","🟩"],
      ]
    ]

    @State var playerRow = 2
    @State var playerCol = 15
    @State var wonGame = false
    @State var carrotRow = 10
    @State var carrotCol = 3
    @State var currentMaze = 1
    var carrotLocations = [[10, 3], [7,0]]
    var playerLocations = [[0, 7], [3, 15]]

    func generateRow(col: [String], drawPlayer: Bool) -> Text {
      var textString = ""
      for colIndex in 0...col.count - 1 {
        if colIndex == playerCol && drawPlayer {
          textString += "🐰"
        } else {
          textString += col[colIndex]
        }
      }
      return Text(textString)
    }

    func isValidMove(row: Int, col: Int) -> Bool {
      let maze = data[currentMaze]
      return maze[row][col] == "🟧" || maze[row][col] == "🥕"
    }

    var body: some View {
      Spacer()
      VStack {
        ForEach(0..<data[currentMaze].count, id: \.self) { rowIndex in
          HStack {
            generateRow(col: data[currentMaze][rowIndex], drawPlayer:
              playerRow == rowIndex)
          }
        }
      }
      Spacer()
      HStack {
        Button {
          if playerRow > 0 {
            if (isValidMove(row: playerRow - 1, col: playerCol)) {
              playerRow -= 1
              wonGame = playerRow == carrotRow && playerCol == carrotCol
            }
          }
        } label: {
          Text("⬆️").font(.system(size: 50))
        }
        Button {
          let maze = data[currentMaze]
          if playerRow < maze.count - 1 {
            if (isValidMove(row: playerRow + 1, col: playerCol)) {
              playerRow += 1
              wonGame = playerRow == carrotRow && playerCol == carrotCol
            }
          }
        } label: {
          Text("⬇️").font(.system(size: 50))
        }
        Button {
          if playerCol > 0 {
            if (isValidMove(row: playerRow, col: playerCol - 1)) {
              playerCol -= 1
              wonGame = playerRow == carrotRow && playerCol == carrotCol
            }
          }
        } label: {
          Text("⬅️").font(.system(size: 50))
        }
        Button {
          let maze = data[currentMaze]
          if playerCol < maze[0].count - 1 {
            if (isValidMove(row: playerRow, col: playerCol + 1)) {
              playerCol += 1
              wonGame = playerRow == carrotRow && playerCol == carrotCol
            }
          }
        } label: {
          Text("➡️").font(.system(size: 50))
        }
     }
     .padding(.bottom)
     .alert("Congrats! You won!", isPresented: $wonGame) {
       Button("OK") {
         currentMaze += 1
         if currentMaze == data.count {
           currentMaze = 0
         }
         carrotRow = carrotLocations[currentMaze][0]
         carrotCol = carrotLocations[currentMaze][1]
         playerRow = playerLocations[currentMaze][0]
         playerCol = playerLocations[currentMaze][1]
       }
     }
     Spacer()
    }
  }
See forum comments
Download course materials from Github
Previous: Deliverables