-
Notifications
You must be signed in to change notification settings - Fork 0
/
create game logic
80 lines (53 loc) · 1.26 KB
/
create game logic
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
//cards Object
var cards = [
{
rank: "queen",
suit: "hearts",
cardImage: "images/queen-of-hearts.png"
},
{
rank:"queen",
suit: "diamonds",
cardImage: "images/queen-of-diamonds.png"
},
{
rank: "king",
suit: "hearts",
cardImage: "images/king-of-hearts.png"
},
{
rank:"king",
suit:"hearts",
cardImage: "images/king-of-diamonds.png"
}
];
// Steps for checking for match
var cardsInPlay = [];
var checkForMatch = function () {
if (cardsInPlay.length === 2) {
if (cardsInPlay[0]===cardsInPlay[1]) {
alert("You found a match!");
} else {
alert("Sorry, try again.");
}
}
};
var flipCard = function () {
var cardId = this.getAttribute("data-id");
console.log ("User flipped" + cards[cardId].rank)
cardsInPlay.push(cards[cardId].rank);
this.setAttribute("src", cards[cardId].cardImage)
checkForMatch();
console.log (cards[cardId].suit);
console.log (cards[cardId].cardImage);
};
var createBoard =function(){
for (var i = 0; i < cards.length; i++) {
var cardElement = document.createElement("img");
cardElement.setAttribute ("src", "images/back.png");
cardElement.setAttribute ("data-id", i);
cardElement.addEventListener("click", flipCard);
document.getElementById("game-board").appendChild(cardElement);
}
};
createBoard();