Skip to main content
Code Review

Return to Answer

deleted 3 characters in body
Source Link
Andrew
  • 181
  • 5
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 } }
Source Link
Andrew
  • 181
  • 5

Concentration.swift

And I have few notes:

  1. 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
  1. 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 }) }
  1. 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

  1. 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

AltStyle によって変換されたページ (->オリジナル) /