-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
322 lines (288 loc) · 7.84 KB
/
script.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
var weapons = [
{
name: "Blacktail",
imgSrc: "img/Blacktail.jpg",
class: "pistol",
alt: "Blacktail",
damage: 3.60,
capacity: 13,
weight: 6.0
},
{
name: "Bolt",
imgSrc: "img/Bolt.png",
class: "pistol",
alt: "Bolt Thrower",
damage: 2.40,
capacity: 20,
weight: 6.0
},
{
name: "Broken",
imgSrc: "img/Broken.png",
class: "pistol",
alt: "Broken Butterfly",
damage: 40.5,
capacity: 10,
weight: 6.0
},
{
name: "Chicago",
imgSrc: "img/Chicago.jpg",
class: "rifle",
alt: "Chicago",
damage: 2.00,
capacity: 100,
weight: 10.0
},
{
name: "CQBR",
imgSrc: "img/CQBR.png",
class: "rifle",
alt: "CQBR AR",
damage: 5.10,
capacity: 32,
weight: 10.0
},
{
name: "Handcannon",
imgSrc: "img/Handcannon.png",
class: "pistol",
alt: "Handcannon",
damage: 36.0,
capacity: 6,
weight: 6.0
},
{
name: "Killer",
imgSrc: "img/Killer.png",
class: "pistol",
alt: "Killer7",
damage: 28.0,
capacity: 15,
weight: 6.0
},
{
name: "LE-5",
imgSrc: "img/LE.jpg",
class: "rifle",
alt: "LE 4",
damage: 1.20,
capacity: 60,
weight: 10.0
},
{
name: "Matilda",
imgSrc: "img/Matilda.jpg",
class: "pistol",
alt: "Matilda",
damage: 2.00,
capacity: 60,
weight: 6.0
},
{
name: "Punisher",
imgSrc: "img/Punisher.jpg",
class: "pistol",
alt: "Punisher",
damage: 1.90,
capacity: 24,
weight: 6.0
},
{
name: "Red-9",
imgSrc: "img/Red-9.jpg",
class: "pistol",
alt: "Red-9",
damage: 4.05,
capacity: 16,
weight: 6.0
},
{
name: "Riot",
imgSrc: "img/Riot.png",
class: "rifle",
alt: "Riot Gun",
damage: 19.2,
capacity: 12,
weight: 10.0
},
{
name: "Sentinel",
imgSrc: "img/Sentinel.jpg",
class: "pistol",
alt: "Sentinel Nine",
damage: 1.9,
capacity: 31,
weight: 6.0
},
{
name: "SG-09",
imgSrc: "img/SG-09.jpg",
class: "pistol",
alt: "SG-09 R",
damage: 2.00,
capacity: 18,
weight: 6.0
},
{
name: "Skull",
imgSrc: "img/Skull.png",
class: "rifle",
alt: "Skull Shaker",
damage: 18.1,
capacity: 6,
weight: 10.0
},
{
name: "SR-M1903",
imgSrc: "img/SR.png",
class: "rifle",
alt: "SR M1903",
damage: 10.6,
capacity: 13,
weight: 10.0
},
{
name: "Stingray",
imgSrc: "img/Stingray.png",
class: "rifle",
alt: "Stingray",
damage: 4.90,
capacity: 18,
weight: 10.0
},
{
name: "Striker",
imgSrc: "img/Striker.png",
class: "rifle",
alt: "Striker",
damage: 16.2,
capacity: 48,
weight: 10.0
},
{
name: "TMP",
imgSrc: "img/TMP.jpg",
class: "pistol",
alt: "TMP",
damage: 1.65,
capacity: 70,
weight: 6.0
},
{
name: "W-870",
imgSrc: "img/W.png",
class: "rifle",
alt: "W-870",
damage: 20.2,
capacity: 10,
weight: 10.0
}
];
var armasUsuario = [];
const capacity = 70.0;
// Função para adicionar arma
function adicionarArma(armaId, armaNome) {
if (armasUsuario.includes(armaId)) {
alert('Arma já adicionada!');
return;
}
if (calcularPesoTotal() + obterPesoArma(armaId) > capacity) {
alert('Capacidade da mochila excedida!');
return;
}
armasUsuario.push(armaId);
atualizarArmasUsuario();
}
// Função para calcular o peso total das armas do usuário
function calcularPesoTotal() {
var pesoTotal = 0;
armasUsuario.forEach(function (armaId) {
pesoTotal += obterPesoArma(armaId);
});
return pesoTotal;
}
// Função para obter o peso de uma arma
function obterPesoArma(armaId) {
const arma = weapons.find(w => w.name === armaId);
return arma ? arma.weight : 0;
}
// Função para atualizar a exibição das armas selecionadas
function atualizarArmasUsuario() {
var listaArmasUsuario = document.getElementById('armasUsuario');
listaArmasUsuario.innerHTML = '';
armasUsuario.forEach(function (armaId) {
var arma = weapons.find(function (w) { return w.name === armaId; });
if (arma) {
var itemLista = document.createElement('div');
itemLista.innerHTML = `<strong>${arma.name}</strong> - Dano: ${arma.damage.toFixed(2)}, Capacidade: ${arma.capacity}`;
listaArmasUsuario.appendChild(itemLista);
}
});
var pesoTotalElement = document.createElement('div');
pesoTotalElement.innerHTML = `Peso Total: ${calcularPesoTotal().toFixed(2)} / ${capacity}<br>Dano Total: ${calcularDanoTotalUsuario().toFixed(2)}`;
listaArmasUsuario.appendChild(pesoTotalElement);
}
document.addEventListener("DOMContentLoaded", function () {
knapsack(weapons, 70);
console.log("Script rodando!");
const armasSelecionadasDiv = document.getElementById("armasSelecionadas");
const result = knapsack(weapons, capacity);
const armasSelecionadas = result.selectedWeapons;
const htmlArmasSelecionadas = armasSelecionadas.map(arma => `
<div>
<strong>${arma.name}</strong> - Dano: ${arma.damage}, Capacidade: ${arma.capacity}
</div>
`).join('');
armasSelecionadasDiv.innerHTML = htmlArmasSelecionadas;
var pesoTotalElement = document.createElement('div');
pesoTotalElement.textContent = 'Dano Total Melhores Armas: ' + calcularDanoTotalMelhoresArmas().toFixed(2);
armasSelecionadasDiv.appendChild(pesoTotalElement);
});
// Função para calcular o dano total das armas do usuário
function calcularDanoTotalUsuario() {
var danoTotalUsuario = 0;
armasUsuario.forEach(function (armaId) {
var arma = weapons.find(function (w) { return w.name === armaId; });
if (arma) {
danoTotalUsuario += arma.damage * arma.capacity;
}
});
return danoTotalUsuario;
}
// Função para calcular o dano total das melhores armas
function calcularDanoTotalMelhoresArmas() {
var danoTotalMelhoresArmas = 0;
const armasSelecionadas = knapsack(weapons, capacity).selectedWeapons;
armasSelecionadas.forEach(function (arma) {
danoTotalMelhoresArmas += arma.damage * arma.capacity;
});
return danoTotalMelhoresArmas;
}
function knapsack(weapons, capacity) {
const n = weapons.length;
const dp = Array.from({ length: n + 1 }, () => Array(capacity + 1).fill(0));
for (let i = 1; i <= n; i++) {
const { damage, weight, capacity: weaponCapacity } = weapons[i - 1];
for (let w = 1; w <= capacity; w++) {
if (weight <= w) {
dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - weight] + damage * weaponCapacity);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
let result = dp[n][capacity];
let w = capacity;
const selectedWeapons = [];
for (let i = n; i > 0 && result > 0; i--) {
if (result !== dp[i - 1][w]) {
const { name, damage, capacity: weaponCapacity } = weapons[i - 1];
selectedWeapons.push({ name, damage, capacity: weaponCapacity });
result -= damage * weaponCapacity;
w -= weapons[i - 1].weight;
}
}
return { totalDamage: dp[n][capacity], selectedWeapons };
}