-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemo.HTML
136 lines (119 loc) · 4.13 KB
/
Memo.HTML
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
<!DOCTYPE html>
<html>
<head>
<title>Memory</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
font-size: 48px;
text-decoration: underline;
font-weight: bold;
text-align: center;
}
#memory-grid {
margin-top: 20px;
text-align: center;
}
.card {
width: 9cm;
height: 6cm;
margin-right: 0.5cm;
margin-bottom: 0.5cm;
display: inline-block;
background-repeat: no-repeat;
background-size: contain;
cursor: pointer;
}
#restart-button {
margin-top: 20px;
display: block;
margin-left: auto;
margin-right: auto;
width: 5cm;
height: 3cm;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Memory</h1>
<div id="memory-grid">
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
<div class="card" onclick="flipCard(this)"></div>
</div>
<button id="restart-button" onclick="restartGame()">Neustart</button>
<script>
var cards = [
{ frontImage: 'A', flipped: false },
{ frontImage: 'A', flipped: false },
{ frontImage: 'B', flipped: false },
{ frontImage: 'B', flipped: false },
{ frontImage: 'C', flipped: false },
{ frontImage: 'C', flipped: false },
{ frontImage: 'D', flipped: false },
{ frontImage: 'D', flipped: false },
{ frontImage: 'E', flipped: false },
{ frontImage: 'E', flipped: false }
];
var flippedCards = [];
var matchedCards = [];
restartGame();
function flipCard(card) {
if (!card.flipped && flippedCards.length < 2) {
var cardIndex = Array.prototype.indexOf.call(card.parentNode.children, card);
card.style.backgroundImage = "url('https://mrtobiaswagner.github.io/Organisationslehre/" + cards[cardIndex].frontImage + ".png')";
cards[cardIndex].flipped = true;
flippedCards.push(card);
if (flippedCards.length === 2) {
var flippedIndices = flippedCards.map(function (flippedCard) {
return Array.prototype.indexOf.call(flippedCard.parentNode.children, flippedCard);
});
if (cards[flippedIndices[0]].frontImage === cards[flippedIndices[1]].frontImage) {
matchedCards.push(flippedCards[0], flippedCards[1]);
flippedCards = [];
if (matchedCards.length === cards.length) {
alert("Glückwunsch! Du hast das Memory-Spiel gewonnen!");
}
} else {
setTimeout(function() {
flippedCards[0].style.backgroundImage = "url('https://mrtobiaswagner.github.io/Organisationslehre/memoback.png')";
flippedCards[1].style.backgroundImage = "url('https://mrtobiaswagner.github.io/Organisationslehre/memoback.png')";
cards[flippedIndices[0]].flipped = false;
cards[flippedIndices[1]].flipped = false;
flippedCards = [];
}, 1000);
}
}
}
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function restartGame() {
flippedCards = [];
matchedCards = [];
shuffleArray(cards);
var cardElements = document.getElementsByClassName("card");
for (var i = 0; i < cardElements.length; i++) {
cardElements[i].style.backgroundImage = "url('https://mrtobiaswagner.github.io/Organisationslehre/memoback.png')";
cards[i].flipped = false;
}
}
</script>
</body>
</html>