Learn Courses My Dashboard

Confusing Error

Hey, I was coding a tic tac toe game and while doing so, it was running just fine but a random error appeared that I don’t quite know how to lose.

It suddenly appeared? When no code changed?

I’d double check you didn’t accidentally delete any curly braces

1 Like

I added a few things but it was fine at first and then when I tried running it again, the error appeared. I’ll double-check the curly braces though!

I just checked and it wasn’t the braces, I’m so confused

Can you paste your code here?

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key). You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

import SwiftUI

struct ContentView: View {
    
    let columns: [GridItem] = [GridItem(.flexible()),
                               GridItem(.flexible()),
                               GridItem(.flexible())]
    
    @State private var moves: [Move?] = Array(repeating: nil , count: 9)
    @State private var isHumanTurn = true
    
    var body: some View{
        GeometryReader { geometry in
            VStack{
                Spacer()

                LazyVGrid(columns: columns, spacing: 5) {
                    ForEach(0..<9){ i in
                        ZStack{
                            Circle()
                                .foregroundColor(.black).opacity(1)
                                .frame(width: geometry.size.width/3 - 15,
                                       height: geometry.size.width/3 - 15)
                                .shadow(radius: 7)
                            
                            Image(systemName: moves[i]? .indicator ?? "")
                                .resizable()
                                .frame(width: 40, height: 40)
                                .foregroundColor(.mint).opacity(0.95)
                    }
                        .onTapGesture {
                            if isSquareOccupied(in: moves, forIndex: i) { return
                                
                            }
                            
                            moves[i] = Move(player: .human, boardIndex: i)
                            
                            //check for win condition or draw
                            
                            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
                                let computerPosition = determineComputerMovePosition(in: moves)
                                moves[computerPosition] = Move(player: .computer, boardIndex: computerPosition)
                            }
                        }
                }
                        
            }
            Spacer()
            
        }
    }
        func isSquareOccupied(in moves: [Move?], forIndex index: Int) -> Bool{
            return moves.contains(where: { $0?.boardIndex == index})
        }
        
        func determineComputerMovePosition(in moves: [Move?]) -> Int{
            var movePosition = Int.random(in: 0..<9)
            
            while isSquareOccupied(in: moves, forIndex: movePosition) {
                movePosition = Int.random(in: 0..<9)
            }
            
            return movePosition
            
        }
}

enum Player{
    case human, computer
}
    
struct Move{
    let player: Player
    let boardIndex: Int
    
    var indicator: String{
        return player == .human ? "sun.max.fill" : "moon.stars"
    }
}
    
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
}

I used the code that you provided and there was a misplaced closing brace in the body property which meant that the compiler was complaining.

Try this:

import SwiftUI

struct ContentView: View {

    let columns: [GridItem] = [GridItem(.flexible()),
                               GridItem(.flexible()),
                               GridItem(.flexible())]

    @State private var moves: [Move?] = Array(repeating: nil , count: 9)
    @State private var isHumanTurn = true

    var body: some View{
        GeometryReader { geometry in
            VStack{
                Spacer()

                LazyVGrid(columns: columns, spacing: 5) {
                    ForEach(0..<9){ i in
                        ZStack{
                            Circle()
                                .foregroundColor(.black).opacity(1)
                                .frame(width: geometry.size.width/3 - 15,
                                       height: geometry.size.width/3 - 15)
                                .shadow(radius: 7)

                            Image(systemName: moves[i]? .indicator ?? "")
                                .resizable()
                                .frame(width: 40, height: 40)
                                .foregroundColor(.mint).opacity(0.95)
                        }
                        .onTapGesture {
                            if isSquareOccupied(in: moves, forIndex: i) { return

                            }

                            moves[i] = Move(player: .human, boardIndex: i)

                            //check for win condition or draw

                            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
                                let computerPosition = determineComputerMovePosition(in: moves)
                                moves[computerPosition] = Move(player: .computer, boardIndex: computerPosition)
                            }
                        }
                    }
                }
                Spacer()
            }
        }
    }

    func isSquareOccupied(in moves: [Move?], forIndex index: Int) -> Bool{
        return moves.contains(where: { $0?.boardIndex == index})
    }

    func determineComputerMovePosition(in moves: [Move?]) -> Int{
        var movePosition = Int.random(in: 0..<9)

        while isSquareOccupied(in: moves, forIndex: movePosition) {
            movePosition = Int.random(in: 0..<9)
        }
        return movePosition
    }
}

enum Player{
    case human, computer
}

struct Move{
    let player: Player
    let boardIndex: Int

    var indicator: String{
        return player == .human ? "sun.max.fill" : "moon.stars"
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
1 Like