When cards are matched, they stay at the end of all cards with background of random color
This commit is contained in:
@@ -11,6 +11,8 @@ import SwiftUI
|
||||
struct SetGame {
|
||||
private(set) var cards: Array<Card>
|
||||
private(set) var score: Int = 0
|
||||
private(set) var colors : [Int : Color] = [:]
|
||||
private var matches: Int = 0
|
||||
|
||||
|
||||
|
||||
@@ -43,8 +45,11 @@ struct SetGame {
|
||||
if color && shading && symbol && numberOnCard {
|
||||
for chosenCard in chosenCards {
|
||||
cards[cards.firstIndex(where: {chosenCard.id == $0.id})!].isMatched = true
|
||||
cards[cards.firstIndex(where: {chosenCard.id == $0.id})!].matchId = matches
|
||||
}
|
||||
colors.updateValue(randomColor(), forKey: self.matches)
|
||||
score += 1
|
||||
matches += 1
|
||||
if (cardsOnTheTable < GameConstants.cardsOnTableInTheBeggining) {
|
||||
addCardsToTable(GameConstants.numberOfCardsToDraw)
|
||||
}
|
||||
@@ -54,8 +59,15 @@ struct SetGame {
|
||||
for chosenCard in chosenCards {
|
||||
cards[cards.firstIndex(where: {chosenCard.id == $0.id})!].isSelected = false
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else if let chosenIndex = cards.firstIndex(where: {card.id == $0.id}),
|
||||
!cards[chosenIndex].isMatched,
|
||||
cards[chosenIndex].isSelected,
|
||||
cards[chosenIndex].isOnTheTable {
|
||||
cards[chosenIndex].isSelected = false
|
||||
}
|
||||
}
|
||||
|
||||
func all3EqualOrAllDifferent<EquatableObject: Equatable>(_ first: EquatableObject, _ second: EquatableObject, _ third: EquatableObject) -> Bool {
|
||||
@@ -81,7 +93,10 @@ struct SetGame {
|
||||
|
||||
var displayedCards: Array<Card> {
|
||||
cards.filter { card in
|
||||
card.isOnTheTable && !card.isMatched
|
||||
card.isOnTheTable
|
||||
// && !card.isMatched
|
||||
}.sorted {
|
||||
$0.matchId < $1.matchId
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +131,13 @@ struct SetGame {
|
||||
return deck
|
||||
}
|
||||
|
||||
private func randomColor() -> Color {
|
||||
let red = Double.random(in: 0...1)
|
||||
let blue = Double.random(in: 0...1)
|
||||
let green = Double.random(in: 0...1)
|
||||
return Color(red: red, green: green, blue: blue).opacity(0.5)
|
||||
}
|
||||
|
||||
|
||||
struct Card: Identifiable, Equatable {
|
||||
var id: Int
|
||||
@@ -126,6 +148,7 @@ struct SetGame {
|
||||
var isSelected: Bool = false
|
||||
var isOnTheTable: Bool = false
|
||||
var numberOnCard: Int
|
||||
var matchId: Int = -1
|
||||
}
|
||||
|
||||
private struct GameConstants {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
|
||||
class SetGameModelView: ObservableObject {
|
||||
@@ -38,5 +39,9 @@ class SetGameModelView: ObservableObject {
|
||||
self.model.addCardsToTable(3)
|
||||
}
|
||||
|
||||
var colors: [Int : Color] {
|
||||
self.model.colors
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ struct SetGameView: View {
|
||||
private func ScrollOrAspectVGrid() -> some View {
|
||||
if (game.cardsOnTable.count <= 21) {
|
||||
AspectVGrid(items: game.cardsOnTable, aspectRatio: 2/3) { card in
|
||||
CardView(card: card).foregroundColor(.red).onTapGesture {
|
||||
CardView(card: card, colors: game.colors).foregroundColor(.red).onTapGesture {
|
||||
game.choose(card)
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ struct SetGameView: View {
|
||||
ScrollView {
|
||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: 70), spacing: 0)], spacing: 0) {
|
||||
ForEach(game.cardsOnTable) { card in
|
||||
CardView(card: card)
|
||||
CardView(card: card, colors: game.colors)
|
||||
.aspectRatio(2/3, contentMode: .fit)
|
||||
.foregroundColor(.red)
|
||||
.onTapGesture {
|
||||
@@ -50,11 +50,11 @@ struct SetGameView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct CardView: View {
|
||||
var colors: [Int : Color]
|
||||
var card: SetGameModelView.Card
|
||||
var numberOfSymbols: Int
|
||||
var color: Color
|
||||
@@ -62,12 +62,13 @@ struct CardView: View {
|
||||
var shading: CardShading
|
||||
var selectedGreen: Color = Color(hue: 0.355, saturation: 1.0, brightness: 1.0)
|
||||
|
||||
init(card: SetGameModelView.Card) {
|
||||
init(card: SetGameModelView.Card, colors: [Int : Color]) {
|
||||
self.card = card
|
||||
self.numberOfSymbols = card.numberOnCard
|
||||
self.color = card.color
|
||||
self.symbol = card.symbol
|
||||
self.shading = card.shading
|
||||
self.colors = colors;
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@@ -77,6 +78,8 @@ struct CardView: View {
|
||||
cardShape.aspectRatio(2/3, contentMode: .fit)
|
||||
if (card.isSelected) {
|
||||
cardShape.foregroundColor(selectedGreen)
|
||||
} else if (card.isMatched) {
|
||||
cardShape.foregroundColor(colors[card.matchId])
|
||||
} else {
|
||||
cardShape.foregroundColor(.white)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user