Swift code with Chris lesson 8 problem

I really enjoy this group of instructions. Lesson 8 creating logiic xcode. However I am stuck I am getting an error message “Thread 1: breakpoint 2.1(1)” right after I randomize the player card and cpu card when I run the simulator. Can you help?

Welcome to the community!! It seems like you created a breakpoint, and you need to remove it for your code to continue to execute.

Right click on the breakpoint and click Delete, or click on it and drag to the right or left, and release, and it will go away

I am now also getting an error msg. " Cannot assign to property: ‘self’ is immutable" when code is written to randomize the playercard and cpucard. This pertains to the War Card game lesson 8. Can you help?

Hi @Rocny1952

Can you post a screenshot of the Xcode View where the error is shown.

Also paste in all of your code from that View. To do that, paste your code in as text.

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. Like this:

```
Code goes here
```

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 - QWERTY keyboard).

Alternatively after you paste in your code, select that code block and click the </> button on the toolbar. This does the same thing as manually typing the 3 back ticks on the line above and below your code.

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

//
// ContentView.swift
// War Card Game
//
// Created by Pete Ciavarri on 3/26/23.
//

//
// ContentView.swift
// War Card Game
//
// Created by Pete Ciavarri on 3/20/23.
//

import SwiftUI

struct ContentView: View {

var playercard = "card7"
var cpucard = "card13"

var playerScore = 0
var cpuScore = 0

var body: some View {
    
    ZStack {
        
        Image("background-plain")
            .resizable()
            .ignoresSafeArea()
        
        VStack {
            Spacer()
            Image("logo")
            Spacer()
            HStack {
                Spacer()
                Image(playercard)
                Spacer()
                Image(cpucard)
                Spacer()
                
            }
            
            Spacer()
            
            
            
           
            
            Button {
                deal()
            } label: {
                Image("button")
            }

            
            Spacer()
            HStack {
                Spacer()
                VStack {
                    Text("Player")
                        .font(.headline)
                        .padding(.bottom, 10.0)
                    
                    Text(String(playerScore))
                        .font(.largeTitle)
                    
                    
                    
                }
                .padding(.bottom, 10.0)
                Spacer()
                VStack {
                    Text("CPU")
                        .font(.headline)
                        .padding(.bottom, 10.0)
                    Text(String(cpuScore))
                        .font(.largeTitle)
                }
                Spacer()
            }
            
            .foregroundColor(.white)
            Spacer()
        }
        
    }
}

func deal() {
    // Randomize the players card
        playercard = "card" + String(Int.random(in: 2...14))
        
        // Randomiize the cpus card
        
        cpucard = "card" + String(Int.random(in: 2...14))
        // Update the score
    ```
    }
    
}
    struct ContentView_Previews: PreviewProvider {
        
        
        static var previews: some View {
            ContentView()
        }
    }

The following iis the location of the error.

   playercard = "card" + String(Int.random(in: 2...14)).      Cannot assign to property: 'self' is immutable

        
        // Randomiize the cpus card
        
        cpucard = "card" + String(Int.random(in: 2...14)).     Cannot assign to property: 'self' is immutable

        // Update the score

 Cannot assign to property: 'self' is immutable

THis is the error message.

These 4 variables should be defined as @State variables in SwiftUI. ie:

@State var playercard = "card7"
@State var cpucard = "card13"

@State var playerScore = 0
@State var cpuScore = 0

@State is a special property wrapper in SwiftUI that allows a variable in a struct to be mutable (can be changed).

THank you so much. Looks like this solved the problem