var allCardsHaveBeenMatched: Bool { !cards.reducecontains(true,where: { !0ドル && 1ドル.isMatched }) }
var allCardsHaveBeenMatched: Bool { cards.reduce(true, { 0ドル && 1ドル.isMatched }) }
var allCardsHaveBeenMatched: Bool { !cards.contains(where: { !0ドル.isMatched } }
Concentration.swift
And I have few notes:
- It's better to describe what this entity is about. You can add comment above declaration.
import Foundation
struct Concentration
{
private(set) var cards = [Card]()
private(set) var score = 0
- This code can be rewritеen:
var allCardsHaveBeenMatched: Bool {
for index in cards.indices {
if !cards[index].isMatched { return false }
}
return true
}
better/shorter version:
var allCardsHaveBeenMatched: Bool { cards.reduce(true, { 0ドル && 1ドル.isMatched }) }
- This method is doing too much and it's hard to read it, consider to spit it into few smaller methods (Single Responsibility Principle).
/**
Choose a card at an index.
Handles flip count, if a card is faced up, and matching of cards
*/
mutating func chooseCard(at index: Int){
Card.swift
- Use UUID() instead
private static var identifierFactory = 0
private static func getUniqueIdentifer() -> Int {
identifierFactory += 1
return identifierFactory
}
Theme.swift
Can be rewritten like this (Using enum without enclosing class):
import Foundation
enum Theme: UInt32 {
case halloween
case love
case animal
case waterCreatures
case plants
case weather
/// get an array of icons by theme
var icons: [String] {
switch self {
case .halloween:
return ["👻", "😈", "👹", "👺", "🎃", "☠️", "🦇", "💀"]
case .love:
return ["💕","💋", "💌", "💘", "❤️", "💓", "💖", "💔"]
case .animal:
return ["🐶", "🐒", "🐔", "🦊", "🦉", "🐭", "🐥", "🦎"]
case .waterCreatures:
return ["🦑", "🐙", "🐠", "🐡", "🐳", "🐚", "🦀", "🦈"]
case .plants:
return ["🍁", "🍄", "🌹", "💐", "🌾", "🌸", "🌺", "🌻"]
case .weather:
return ["🔥", "❄️", "☀️", "💦", "☔️", "🌬", "☁️", "🌪", "⛈"]
}
}
static func randomTheme() -> Theme {
let max = Theme.weather.rawValue
let randomIndex = arc4random_uniform(max + UInt32(1))
return Theme(rawValue: randomIndex) ?? Theme.halloween
}
}
To summarize: your code is not bad, but need some improvements. You are using encapsulation to hide implementation details and prefer values type (like structs) over reference types (classes). It's good stating point!
PS: Welcome to Code Review!!
default