forked from szymonSys/memoryGameJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquare.js
109 lines (87 loc) · 2.28 KB
/
Square.js
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
class Square {
constructor(value, order, partner = null) {
const _COLORS = {
passive: "#888888",
active: "#1ED760",
matched: "#4169E1",
failed: "#ff0000"
};
this.getPassiveColor = () => _COLORS.passive;
this.getActiveColor = () => _COLORS.active;
this.getMatchedColor = () => _COLORS.matched;
this.getFailedColor = () => _COLORS.failed;
this._value = value;
this._order = order;
this._color = this.getPassiveColor();
this._isActive = false;
this._isMatched = false;
this._partner = partner;
if (partner instanceof Square) partner.partner = this;
}
get value() {
return this._value;
}
set value(value) {
if (value !== this.partner.value) this.partner.value = value;
return (this._value = value);
}
get order() {
return this._order;
}
set order(order) {
return (this._order = order);
}
get color() {
return this._color;
}
set color(color) {
return (this._color = color);
}
get partner() {
return this._partner;
}
set partner(partner) {
if (this.value === partner.value) return (this._partner = partner);
}
get element() {
return this._element;
}
set element(element) {
return (this._element = element);
}
get isActive() {
return this._isActive;
}
get isMatched() {
return this._isMatched;
}
changeActivity() {
this.color = !this.isActive ? this.getActiveColor() : this.getPassiveColor();
return (this._isActive = !this._isActive);
}
checkMatching(square) {
if (square.isActive) {
square.changeActivity();
return (this.value === square.value && this.order !== square.order) ? true : false;
}
}
makeMatching(square) {
if (this.checkMatching(square)) {
this._isMatched = true;
this.color = this.getMatchedColor();
this.partner._isMatched = true;
this.partner.color = this.getMatchedColor();
this._element.classList.add('done');
this.partner._element.classList.add('done');
return true;
} else {
this.color = this.getFailedColor();
square.color = this.getFailedColor();
setTimeout(() => {
this.color = this.getPassiveColor();
square.color = this.getPassiveColor();
}, 1000)
return false;
}
}
}