-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.gleam
152 lines (131 loc) · 3.35 KB
/
game.gleam
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import gleam/map
import gleam/bool
import gleam/list
import gleam/string
pub type Card {
Card(id: String, name: String, image: String, flipped: Bool, paired: Bool)
}
pub type Engine {
Engine(
cards: List(Card),
winner: Bool,
animating: Bool,
score: Int,
playing_cards: List(String),
random: Bool,
)
}
pub fn pair_cards(cards: List(Card), engine: Engine) -> Engine {
let paired_cards =
list.map(
cards,
fn(card: Card) {
case card.flipped {
True -> Card(..card, paired: True, flipped: False)
_ -> card
}
},
)
Engine(..engine, cards: paired_cards)
}
pub fn declare_winner(engine: Engine) -> Engine {
let total = list.length(engine.cards)
let paired =
list.filter(engine.cards, fn(card: Card) { card.paired == True })
|> list.length()
case total == paired {
True -> Engine(..engine, winner: True)
_ -> Engine(..engine, winner: False)
}
}
pub fn attempt_match(cards: List(Card), engine: Engine) -> Engine {
let flipped_cards =
list.filter(cards, fn(card: Card) { card.flipped == True })
|> list.map(fn(card: Card) { card.name })
case list.length(flipped_cards) == 2 {
True ->
case flipped_cards {
[one, two] if one == two -> pair_cards(cards, engine)
_ -> Engine(..engine, cards: cards, animating: True)
}
_ -> Engine(..engine, cards: cards)
}
}
pub fn flip(engine: Engine, flip_id: String) -> Engine {
case engine.animating, engine.winner {
_, True -> engine
True, _ -> engine
_, _ ->
list.map(
engine.cards,
fn(card: Card) {
case card.id == flip_id {
True -> Card(..card, flipped: True)
_ -> card
}
},
)
|> attempt_match(engine)
|> declare_winner()
}
}
pub fn unpair_cards(engine: Engine) -> Engine {
let unpaired =
list.map(engine.cards, fn(card: Card) { Card(..card, paired: False) })
Engine(..engine, cards: unpaired)
}
pub fn prepare_restart(engine: Engine) -> Engine {
case engine.winner {
True -> unpair_cards(engine)
_ -> engine
}
}
pub fn unflip(engine: Engine) -> Engine {
let cards =
list.map(engine.cards, fn(card: Card) { Card(..card, flipped: False) })
Engine(..engine, cards: cards, animating: False)
}
pub fn generate_cards(playing_cards: List(String)) -> List(Card) {
list.map(
playing_cards,
fn(name: String) {
let one =
Card(
id: string.join([name, "1"], with: ""),
name: name,
image: string.join(["/images/cards/", name, ".png"], with: ""),
flipped: False,
paired: False,
)
let two =
Card(
id: string.join([name, "2"], with: ""),
name: name,
image: string.join(["/images/cards/", name, ".png"], with: ""),
flipped: False,
paired: False,
)
[one, two]
},
)
|> list.flatten()
}
pub fn init(playing_cards: List(String), random: Bool) -> Engine {
let total = list.length(playing_cards)
let cards = case random {
True ->
generate_cards(playing_cards)
|> list.take(total * 2)
False ->
generate_cards(playing_cards)
|> list.take(total * 2)
}
Engine(
cards: cards,
winner: False,
animating: False,
score: 0,
playing_cards: playing_cards,
random: random,
)
}