Learn Courses My Dashboard

Problem with WarCard game

Hello, I just finished the war card game. For some reason the CPU and Player scores are not displaying the correct numbers. An example is: I can see that the CPU’s card is higher but the player receives the point. Can I post or share my code to see if someone can find a solution? I also noticed that the final video in the playlist also has the same problem as me.


@zacka37

Hi Zack?,

Copy the code from your ContentView.swift file and paste it in a reply as text.

Place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code so that it is formatted nicely. The back-tick character is located on the same keyboard key as the tilde character ~ (below the Esc key).

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

//
//  ContentView.swift
//  War-challenge
//
//  Created by Zack Augustine on 9/2/21.
//

import SwiftUI

struct ContentView: View {
    
    @State private var playerCard = "card9"
    @State private var cpuCard = "card9"
    @State private var playerScore = 0
    @State private var cpuScore = 0
    
    var body: some View {
        ZStack{
        
            Image("background")
                .resizable()
                .ignoresSafeArea()
            
            VStack(){
                Spacer()
                Image("logo")
                Spacer()
                HStack{
                    Spacer()
                    Image(playerCard)
                    Spacer()
                    Image(cpuCard)
                    Spacer()
                }
                Spacer()
                Button(action: {
                    
                    // Generate a random number between 2 and 14
                    let playerRand = Int.random(in: 2...14)
                    let cpuRand = Int.random(in: 2...14)
                    
                    // Update cards
                    playerCard = "card" + String(playerRand)
                    cpuCard = "card" + String(cpuRand)
                    // Update score
                    if playerRand > cpuRand {
                        playerScore += 1
                    }
                    else if cpuRand > playerRand {
                        cpuScore += 1
                    } else if cpuRand == playerRand {
                        cpuScore += 0
                        playerScore += 0
                    }
                    
                }, label: {
                    Image("dealbutton")
                })
                    
                    Spacer()
                
                    HStack{
                        Spacer()
                        VStack{
                            Text("Player")
                                .font(.headline)
                                .foregroundColor(Color.white)
                                .padding(.bottom, 20.0)
                            Text(String(playerScore))
                                .font(.largeTitle)
                                .foregroundColor(Color.white)
                                
                                
                        }
                        Spacer()
                        VStack{
                            Text("CPU")
                                .font(.headline)
                                .foregroundColor(Color.white)
                                .padding(.bottom, 20.0)
                            Text(String(cpuScore))
                                .font(.largeTitle)
                                .foregroundColor(Color.white)
                        
                    }
                        Spacer()
                    
                }
                Spacer()
            }
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thanks for the help.

@zacka37

Your code is fine as far as I can tell.

Maybe close Xcode and then re-launch it and try the App again in your simulator.

I downloaded the instructors copy of the final code for the war project and I had the same problem. The problem is that the scores do not match the cards that are being seen. It has to be my system. Do you suggest any ideas to fix my problem. Like is there somehow I can reset some settings.

The cards in the assets should be named card2 through to card14 and that numbering should coincide with the cards 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King and Ace.

The War Card game image assets can be downloaded from the resources Dropbox link and you will find them in the folder path Module 1 > Lesson 5

The completed War Card game can be downloaded from the Lesson 12 folder.

I am using the same assets and your code in my project seems to be working fine.

I used the code from Lesson 12 and the problem still happened. It must be a problem with my system. I cant think of any solutions so Im just going to move onto the slot challenge and hope the problem does not continue.

@zacka37
I assume you have tried closing Xcode and re-launching it and also tried shutting down your mac and restarting it?

I have moved your post to its own thread in App Development. I have also posted a Private Message to you so please see that.

Yes. I tried shout down and restart but that did not fix anything. Im going to uninstall and install Xcode again to see if I have a corrupted file.

Sounds like a good plan.