-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
218 lines (197 loc) · 6.36 KB
/
script.js
File metadata and controls
218 lines (197 loc) · 6.36 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Card suits and values
const suits = ["♠", "♥", "♦", "♣"];
const values = [
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
];
// Global variables
let currentCards = [];
let sortingLog = [];
let isAnimating = false;
// DOM elements
const cardCountInput = document.getElementById("cardCount");
const drawBtn = document.getElementById("drawBtn");
const sortBtn = document.getElementById("sortBtn");
const helpBtn = document.getElementById("helpBtn");
const cardContainer = document.getElementById("cardContainer");
const sortingLogContainer = document.getElementById("sortingLog");
const modal = document.getElementById("algorithmModal");
const closeBtn = document.getElementsByClassName("close")[0];
drawBtn.addEventListener("click", drawCards);
sortBtn.addEventListener("click", startSorting);
helpBtn.addEventListener("click", openModal);
closeBtn.addEventListener("click", closeModal);
window.addEventListener("click", outsideClick);
class Card {
constructor(suit, value) {
this.suit = suit;
this.value = value;
this.numericValue = this.getNumericValue();
this.color = suit === "♥" || suit === "♦" ? "red" : "black";
}
getNumericValue() {
switch (this.value) {
case "A": return 1;
case "J": return 11;
case "Q": return 12;
case "K": return 13;
default: return parseInt(this.value);
}
}
toString() {
return `${this.value}${this.suit}`;
}
}
function generateRandomCards(count) {
const cards = [];
for (let i = 0; i < count; i++) {
const randomSuit = suits[Math.floor(Math.random() * suits.length)];
const randomValue = values[Math.floor(Math.random() * values.length)];
cards.push(new Card(randomSuit, randomValue));
}
return cards;
}
function createCardElement(card, index, additionalClass = "") {
const cardElement = document.createElement("div");
cardElement.className = `card ${card.color} ${additionalClass}`;
cardElement.setAttribute("data-index", index);
cardElement.innerHTML = `
<div class="value">${card.value}</div>
<div class="suit">${card.suit}</div>
<div class="value-bottom">${card.value}</div>
`;
return cardElement;
}
function createLogCardElement(card, additionalClass = "") {
const logCard = document.createElement("div");
logCard.className = `log-card ${card.color} ${additionalClass}`;
logCard.innerHTML = `
<div>${card.value}</div>
<div>${card.suit}</div>
`;
return logCard;
}
function renderCards(cards, highlightIndices = {}) {
cardContainer.innerHTML = "";
cards.forEach((card, index) => {
let additionalClass = "";
if (highlightIndices.comparing?.includes(index)) {
additionalClass = "comparing";
} else if (highlightIndices.selected?.includes(index)) {
additionalClass = "selected";
} else if (highlightIndices.sorted && index <= highlightIndices.sorted) {
additionalClass = "sorted";
}
const cardElement = createCardElement(card, index, additionalClass);
cardContainer.appendChild(cardElement);
});
}
function drawCards() {
const count = parseInt(cardCountInput.value);
if (count < 2 || count > 20) {
alert("Por favor, ingresa un número entre 2 y 20");
return;
}
currentCards = generateRandomCards(count);
sortingLog = [];
renderCards(currentCards);
updateSortingLog();
sortBtn.disabled = false;
}
function addLogStep(stepNumber, cards, comparingIndices = [], selectedIndices = [], sortedIndex = -1) {
const logStep = {
step: stepNumber,
cards: [...cards],
comparing: [...comparingIndices],
selected: [...selectedIndices],
sorted: sortedIndex
};
sortingLog.push(logStep);
updateSortingLog();
}
function updateSortingLog() {
sortingLogContainer.innerHTML = "";
sortingLog.forEach((logStep, index) => {
const stepElement = document.createElement("div");
stepElement.className = "log-step";
const stepTitle = document.createElement("h4");
stepTitle.textContent = `${index}`;
stepElement.appendChild(stepTitle);
const cardsContainer = document.createElement("div");
cardsContainer.className = "log-cards";
logStep.cards.forEach((card, cardIndex) => {
let additionalClass = "";
if (logStep.comparing.includes(cardIndex)) {
additionalClass = "comparing";
} else if (logStep.selected.includes(cardIndex)) {
additionalClass = "selected";
} else if (cardIndex <= logStep.sorted) {
additionalClass = "sorted";
}
const logCard = createLogCardElement(card, additionalClass);
cardsContainer.appendChild(logCard);
});
stepElement.appendChild(cardsContainer);
sortingLogContainer.appendChild(stepElement);
});
sortingLogContainer.scrollTop = sortingLogContainer.scrollHeight;
}
async function selectionSort(cards) {
const n = cards.length;
let stepCount = 0;
addLogStep(stepCount++, cards);
for (let i = 0; i < n - 1; i++) {
let minIndex = i;
renderCards(cards, { selected: [i], sorted: i - 1 });
await sleep(600);
for (let j = i + 1; j < n; j++) {
renderCards(cards, { comparing: [j, minIndex], selected: [i], sorted: i - 1 });
await sleep(400);
if (cards[j].numericValue < cards[minIndex].numericValue) {
minIndex = j;
renderCards(cards, { selected: [i, minIndex], sorted: i - 1 });
await sleep(300);
}
}
if (minIndex !== i) {
renderCards(cards, { comparing: [i, minIndex], sorted: i - 1 });
await sleep(500);
[cards[i], cards[minIndex]] = [cards[minIndex], cards[i]];
renderCards(cards, { sorted: i });
await sleep(400);
} else {
renderCards(cards, { sorted: i });
await sleep(300);
}
addLogStep(stepCount++, cards, [], [], i);
}
renderCards(cards, { sorted: n - 1 });
addLogStep(stepCount++, cards, [], [], n - 1);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function startSorting() {
if (isAnimating) return;
isAnimating = true;
sortBtn.disabled = true;
drawBtn.disabled = true;
sortingLog = [];
await selectionSort([...currentCards]);
isAnimating = false;
drawBtn.disabled = false;
}
function init() {
drawCards();
}
document.addEventListener("DOMContentLoaded", init);
function openModal() {
modal.style.display = "block";
}
function closeModal() {
modal.style.display = "none";
}
function outsideClick(e) {
if (e.target === modal) {
modal.style.display = "none";
}
}