# War Card Game - Implementing Deal, Score, Win Conditions (Day 8)

Source: https://www.youtube.com/watch?v=olxOUGYxTRw
Recap page: https://rapidrecap.app/video/olxOUGYxTRw
Generated: 2025-12-05T15:03:45.484+00:00

---
## Quick Overview

The video concludes the 8-day beginner Swift series by implementing the core logic for the War card game, including randomizing card values between 2 and 14 (inclusive), updating the card images based on these values, calculating the winner of each round, and incrementing the player or CPU scores accordingly, while noting that SwiftUI's state management handles UI updates automatically.

**Key Points:**
- The speaker implements the logic for randomizing card values within the `dealCards()` function using `Int.random(in: 2...14)` to assign values between 2 and 14 inclusively to `playerValue` and `cpuValue`.
- The code updates the `playerCard` and `cpuCard` image assets by concatenating the string "card" with the randomly generated integer values and casting the result to a String, e.g., `playerCard = "card" + String(playerValue)`.
- The scoring logic is implemented using an `if-else if-else` structure on line 87 to compare `playerValue` and `cpuValue`.
- If `playerValue > cpuValue`, `playerScore` is incremented by 1 (line 88); if `cpuValue > playerValue`, `cpuScore` is incremented by 1 (line 91).
- The tie case (`playerValue == cpuValue`) is handled in the final `else` block (line 94), where neither score is incremented.
- The speaker notes that because `playerScore` and `cpuScore` are declared as `@State` properties, updating them automatically triggers a view refresh to display the new scores in the UI text labels.
- The video concludes by showing how to run the app on a physical device (iPhone XS running iOS 15.5, which is lower than the project's deployment target of iOS 16.0) after resolving a signing error by selecting a development team in Xcode's Signing & Capabilities settings.

![Screenshot at 10:26: The implementation of the conditional logic using 'if' and 'else if' within the 'dealCards' function to check which card value is greater and subsequently increment the corresponding player or CPU score.](https://ss.rapidrecap.app/screens/olxOUGYxTRw/00-10-26.png)

**Context:** This video is the final part of an 8-day beginner Swift tutorial series focused on building a functional War card game using SwiftUI. The main objective of this lesson is to complete the core game mechanics within the `dealCards()` function: randomizing card values, updating the displayed card images based on those values, and implementing the comparison logic to determine the winner of each round and update the scores.

## Detailed Analysis

The tutorial moves into implementing the core game logic in the `dealCards()` function (starting around line 77). First, the speaker addresses randomizing card values for both players, using `Int.random(in: 2...14)` to generate integers between 2 and 14, inclusive, for `playerValue` and `cpuValue`. Next, the card image assets are updated by concatenating the base string "card" with the integer value, ensuring the integer is converted to a String: `playerCard = "card" + String(playerValue)`. This is repeated for the CPU card. The crucial next step is implementing the scoring logic using an `if-else if-else` structure starting at line 87. If the player's value is greater, `playerScore` increments by 1. If the CPU's value is greater, `cpuScore` increments by 1. The final `else` block handles ties, where neither score is changed. The speaker emphasizes that because `playerScore` and `cpuScore` are declared with the `@State` property wrapper at the top of the view, updating them automatically re-renders the SwiftUI views displaying the scores. Finally, the speaker addresses the issue of running the app on a physical device, which requires setting up code signing by selecting a development team in Xcode's Signing & Capabilities tab and potentially lowering the minimum deployment target if using an older device (like the iPhone XS running iOS 15.5, which required lowering the target from iOS 16.0).

### Randomizing Card Values

- `playerValue` and `cpuValue` are assigned random integers between 2 and 14 inclusive using `Int.random(in: 2...14)` on lines 79-80.

### Updating Card Images

- The image asset strings for `playerCard` and `cpuCard` are constructed dynamically by concatenating "card" with the string representation of the random values on lines 82-83.

### Calculating Score (Player Wins)

- If `playerValue > cpuValue` (line 87), `playerScore` is incremented by 1 (line 88).

### Calculating Score (CPU Wins)

- The `else if cpuValue > playerValue` block (line 90) increments `cpuScore` by 1 on line 91.

### Handling Ties

- The final `else` block (line 94) handles the tie scenario where neither score is updated.

### UI Update Mechanism

- The speaker confirms that because scores are `@State` variables, updating them automatically updates the score labels in the view without manual intervention.

### Deploying to Device

- The speaker addresses the signing error encountered when running on a physical device, instructing viewers to select their Apple Developer Team in the Signing & Capabilities tab and potentially lower the Minimum Deployments target (e.g., from iOS 16.0 to iOS 17.0 or lower) to support older devices.

![Screenshot at 00:02: SwiftUI view for the War Card Game showing two cards \(King of Clubs and 8 of Diamonds\) and the DEAL button, with Xcode code visible on the left.](https://ss.rapidrecap.app/screens/olxOUGYxTRw/00-00-02.png)
![Screenshot at 02:25: Xcode code showing the implementation of the if/else if/else structure to compare playerValue and cpuValue for scoring.](https://ss.rapidrecap.app/screens/olxOUGYxTRw/00-02-25.png)
![Screenshot at 04:48: Code showing the dynamic string concatenation used to construct the image asset name for the player's card: playerCard = "card" + String\(playerValue\).](https://ss.rapidrecap.app/screens/olxOUGYxTRw/00-04-48.png)
![Screenshot at 12:07: The start of the conditional logic block using 'if' to compare playerValue and cpuValue to determine the winner.](https://ss.rapidrecap.app/screens/olxOUGYxTRw/00-12-07.png)
![Screenshot at 19:23: Xcode project settings showing the 'Signing & Capabilities' tab where the signing team needs to be selected to resolve the build error.](https://ss.rapidrecap.app/screens/olxOUGYxTRw/00-19-23.png)
