Leave a rating/review
For your first Swift coding challenge, you’re going to add a new method to your Game data model that you can use to start a new round of the game.
We’re going to do this using Test-Driven development. We’ll write the test together, which will fail at first because we haven’t written the code yet. Your challenge will be to pause the video, and the implement the code so that the test passes.
Let’s start with the test.
Add this to BullseyeTests.swift:
func testNewRound() {
game.startNewRound(points: 100)
XCTAssertEqual(game.score, 100)
XCTAssertEqual(game.round, 2)
}
Show the error.
OK - now it’s your turn. Pause the video, annd write the startNewRound method so that this test passes.
I want to give you a hint: you are going to need to add the “mutating” keyword to this method, because that indicates that when you call this method, it will change some values in the struct itself. So put “mutating func” instead of just func.
Good luck!
Add to Game.swift:
mutating func startNewRound(points: Int) {
score = score + points
round = round + 1
}
Show how you can shorten to:
mutating func startNewRound(points: Int) {
score += points
round += 1
}
Cmd+U to run all tests.