-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (149 loc) · 5.3 KB
/
Copy pathscript.js
File metadata and controls
170 lines (149 loc) · 5.3 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
document.addEventListener("DOMContentLoaded", () => {
const allPairs = {
"Alabama": "Montgomery",
"Alaska": "Juneau",
"Arizona": "Phoenix",
"Arkansas": "Little Rock",
"California": "Sacramento",
"Colorado": "Denver",
"Connecticut": "Hartford",
"Delaware": "Dover",
"Florida": "Tallahassee",
"Georgia": "Atlanta",
"Hawaii": "Honolulu",
"Idaho": "Boise",
"Illinois": "Springfield",
"Indiana": "Indianapolis",
"Iowa": "Des Moines",
"Kansas": "Topeka",
"Kentucky": "Frankfort",
"Louisiana": "Baton Rouge",
"Maine": "Augusta",
"Maryland": "Annapolis",
"Massachusetts": "Boston",
"Michigan": "Lansing",
"Minnesota": "Saint Paul",
"Mississippi": "Jackson",
"Missouri": "Jefferson City",
"Montana": "Helena",
"Nebraska": "Lincoln",
"Nevada": "Carson City",
"New Hampshire": "Concord",
"New Jersey": "Trenton",
"New Mexico": "Santa Fe",
"New York": "Albany",
"North Carolina": "Raleigh",
"North Dakota": "Bismarck",
"Ohio": "Columbus",
"Oklahoma": "Oklahoma City",
"Oregon": "Salem",
"Pennsylvania": "Harrisburg",
"Rhode Island": "Providence",
"South Carolina": "Columbia",
"South Dakota": "Pierre",
"Tennessee": "Nashville",
"Texas": "Austin",
"Utah": "Salt Lake City",
"Vermont": "Montpelier",
"Virginia": "Richmond",
"Washington": "Olympia",
"West Virginia": "Charleston",
"Wisconsin": "Madison",
"Wyoming": "Cheyenne"
}; // Full set of state-capital pairs
let firstCard = null;
let secondCard = null;
let matchedPairs = 0;
let currentLevel = 1; // Start at level 1
const cards = document.querySelectorAll(".card");
// Shuffle an array
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// Randomize card values for the current level
function setupCards() {
const states = Object.keys(allPairs);
const capitals = Object.values(allPairs);
// Extract random pairs equal to the number of cards divided by 2
const numPairs = cards.length / 2;
const selectedPairs = [];
// Randomly select state-capital pairs for the level
while (selectedPairs.length < numPairs * 2) {
const randomIndex = Math.floor(Math.random() * states.length);
const state = states[randomIndex];
const capital = allPairs[state]; // Get the corresponding capital for the state
// Add state and capital to the selected pairs if not already added
if (!selectedPairs.includes(state) && !selectedPairs.includes(capital)) {
selectedPairs.push(state, capital); // Add state and its capital
}
}
// Shuffle the selected pairs
shuffle(selectedPairs);
// Assign randomized values to cards
cards.forEach((card, index) => {
card.dataset.value = selectedPairs[index];
card.textContent = "?"; // Hide card values
card.classList.remove("matched", "correct", "wrong"); // Reset card states
card.addEventListener("click", handleCardClick);
});
}
// Handle card click
function handleCardClick(e) {
const clickedCard = e.target;
// Prevent clicking the same card twice or clicking more than two cards
if (clickedCard === firstCard || clickedCard.classList.contains("matched") || secondCard) return;
clickedCard.textContent = clickedCard.dataset.value; // Reveal card value
if (!firstCard) {
firstCard = clickedCard; // Set the first card
} else {
secondCard = clickedCard; // Set the second card
checkMatch();
}
}
// Check if the cards match
function checkMatch() {
const isMatch =
allPairs[firstCard.dataset.value] === secondCard.dataset.value ||
allPairs[secondCard.dataset.value] === firstCard.dataset.value;
if (isMatch) {
// Match found
firstCard.classList.add("matched", "correct");
secondCard.classList.add("matched", "correct");
matchedPairs++;
resetCards();
// Check if all pairs are matched
if (matchedPairs === cards.length / 2) {
setTimeout(() => {
alert(`Congratulations! Moving to Level ${currentLevel + 1}!`);
// Redirect to the next level
if (currentLevel <= 10) {
window.location.href = `classic${currentLevel + 1}.html`;
} else {
alert("You've completed all levels!");
}
}, 500);
}
} else {
// No match
firstCard.classList.add("wrong");
secondCard.classList.add("wrong");
setTimeout(() => {
firstCard.textContent = "?";
secondCard.textContent = "?";
firstCard.classList.remove("wrong");
secondCard.classList.remove("wrong");
resetCards();
}, 1000);
}
}
// Reset selected cards
function resetCards() {
firstCard = null;
secondCard = null;
}
// Initialize the game for the current level
setupCards();
});