When cards are matched, they stay at the end of all cards with background of random color

This commit is contained in:
2023-04-17 21:49:38 +02:00
parent 595b82222c
commit d3d6b39213
3 changed files with 36 additions and 5 deletions

View File

@@ -11,6 +11,8 @@ import SwiftUI
struct SetGame { struct SetGame {
private(set) var cards: Array<Card> private(set) var cards: Array<Card>
private(set) var score: Int = 0 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 { if color && shading && symbol && numberOnCard {
for chosenCard in chosenCards { for chosenCard in chosenCards {
cards[cards.firstIndex(where: {chosenCard.id == $0.id})!].isMatched = true 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 score += 1
matches += 1
if (cardsOnTheTable < GameConstants.cardsOnTableInTheBeggining) { if (cardsOnTheTable < GameConstants.cardsOnTableInTheBeggining) {
addCardsToTable(GameConstants.numberOfCardsToDraw) addCardsToTable(GameConstants.numberOfCardsToDraw)
} }
@@ -54,8 +59,15 @@ struct SetGame {
for chosenCard in chosenCards { for chosenCard in chosenCards {
cards[cards.firstIndex(where: {chosenCard.id == $0.id})!].isSelected = false 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 { func all3EqualOrAllDifferent<EquatableObject: Equatable>(_ first: EquatableObject, _ second: EquatableObject, _ third: EquatableObject) -> Bool {
@@ -81,7 +93,10 @@ struct SetGame {
var displayedCards: Array<Card> { var displayedCards: Array<Card> {
cards.filter { card in 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 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 { struct Card: Identifiable, Equatable {
var id: Int var id: Int
@@ -126,6 +148,7 @@ struct SetGame {
var isSelected: Bool = false var isSelected: Bool = false
var isOnTheTable: Bool = false var isOnTheTable: Bool = false
var numberOnCard: Int var numberOnCard: Int
var matchId: Int = -1
} }
private struct GameConstants { private struct GameConstants {

View File

@@ -6,6 +6,7 @@
// //
import Foundation import Foundation
import SwiftUI
class SetGameModelView: ObservableObject { class SetGameModelView: ObservableObject {
@@ -38,5 +39,9 @@ class SetGameModelView: ObservableObject {
self.model.addCardsToTable(3) self.model.addCardsToTable(3)
} }
var colors: [Int : Color] {
self.model.colors
}
} }

View File

@@ -30,7 +30,7 @@ struct SetGameView: View {
private func ScrollOrAspectVGrid() -> some View { private func ScrollOrAspectVGrid() -> some View {
if (game.cardsOnTable.count <= 21) { if (game.cardsOnTable.count <= 21) {
AspectVGrid(items: game.cardsOnTable, aspectRatio: 2/3) { card in 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) game.choose(card)
} }
} }
@@ -39,7 +39,7 @@ struct SetGameView: View {
ScrollView { ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 70), spacing: 0)], spacing: 0) { LazyVGrid(columns: [GridItem(.adaptive(minimum: 70), spacing: 0)], spacing: 0) {
ForEach(game.cardsOnTable) { card in ForEach(game.cardsOnTable) { card in
CardView(card: card) CardView(card: card, colors: game.colors)
.aspectRatio(2/3, contentMode: .fit) .aspectRatio(2/3, contentMode: .fit)
.foregroundColor(.red) .foregroundColor(.red)
.onTapGesture { .onTapGesture {
@@ -50,11 +50,11 @@ struct SetGameView: View {
} }
} }
} }
} }
struct CardView: View { struct CardView: View {
var colors: [Int : Color]
var card: SetGameModelView.Card var card: SetGameModelView.Card
var numberOfSymbols: Int var numberOfSymbols: Int
var color: Color var color: Color
@@ -62,12 +62,13 @@ struct CardView: View {
var shading: CardShading var shading: CardShading
var selectedGreen: Color = Color(hue: 0.355, saturation: 1.0, brightness: 1.0) 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.card = card
self.numberOfSymbols = card.numberOnCard self.numberOfSymbols = card.numberOnCard
self.color = card.color self.color = card.color
self.symbol = card.symbol self.symbol = card.symbol
self.shading = card.shading self.shading = card.shading
self.colors = colors;
} }
var body: some View { var body: some View {
@@ -77,6 +78,8 @@ struct CardView: View {
cardShape.aspectRatio(2/3, contentMode: .fit) cardShape.aspectRatio(2/3, contentMode: .fit)
if (card.isSelected) { if (card.isSelected) {
cardShape.foregroundColor(selectedGreen) cardShape.foregroundColor(selectedGreen)
} else if (card.isMatched) {
cardShape.foregroundColor(colors[card.matchId])
} else { } else {
cardShape.foregroundColor(.white) cardShape.foregroundColor(.white)
} }