-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcentrationGame.swift
45 lines (38 loc) · 1.33 KB
/
ConcentrationGame.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// ConcentrationGame.swift
// Concentration
//
// Created by Владимир Ведерников on 16.08.2023.
//
import Foundation
class ConcentrationGame {
var cards = [Card]()
var indexOfOneAndOnlyFaceUpCard: Int?
func chooseCard(at index: Int) {
if !cards[index].isMatched {
if let matchingIndex = indexOfOneAndOnlyFaceUpCard, matchingIndex != index {
if cards[matchingIndex].identifier == cards[index].identifier {
cards[matchingIndex].isMatched = true
cards[index].isMatched = true
}
cards[index].isFaceUp = true
indexOfOneAndOnlyFaceUpCard = nil
} else {
for flipDown in cards.indices {
cards[flipDown].isFaceUp = false
}
cards[index].isFaceUp = true
indexOfOneAndOnlyFaceUpCard = index
}
}
}
init(numberOfPairsOfCards: Int) {
for _ in 1...numberOfPairsOfCards {
let card = Card()
//добавляем два раза одинаковую карточку с одинаковым идентификатором
cards += [card, card]
}
// TODO: Shuffle the cards
cards.shuffle()
}
}