Car
Car
DOCTYPE html>
<html>
<head> </head>
<body>
<div>
<button class="startBtn" type="button" onclick="onStart()">Start</button>
<div id="resultContainer">
</div>
</div>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?
family=Bree+Serif&family=Caveat:wght@400;700&family=Lobster&family=Monoton&family=O
pen+Sans:ital,wght@0,400;0,700;1,400;1,700&family=Playfair+Display+SC:ital,wght@0,4
00;0,700;1,700&family=Playfair+Display:ital,wght@0,400;0,700;1,700&family=Roboto:it
al,wght@0,400;0,700;1,400;1,700&family=Source+Sans+Pro:ital,wght@0,400;0,700;1,700&
family=Work+Sans:ital,wght@0,400;0,700;1,700&display=swap');
.startBtn {
width: 140px;
height: 40px;
background-color: blue;
border: 0px;
border-radius: 10px;
color: white;
cursor: pointer;
outline: none;
}
class Car {
constructor() {
this.petrol = 50;
this.maxDistance = 100;
this.milage = 0.5;
this.currentDistance = 0;
this.petrolLeft = 50;
this.moveCount = 0;
this.petrolPumps = [];
}
setDefaults() {
resultContainerEl.textContent = "";
this.petrol = 50;
this.maxDistance = 100;
this.milage = 0.5;
this.currentDistance = 0;
this.petrolLeft = 50;
this.moveCount = 0;
this.petrolPumps = [];
}
createRandomPetrolPumps() {
while (this.petrolPumps.length != 6) {
const randomVal = Math.ceil(Math.random() * 100);
if (!this.petrolPumps.includes(randomVal)) {
this.petrolPumps.push(randomVal);
}
}
this.petrolPumps.sort(function(a, b) {
return a - b;
});
generateNextDistance() {
const randomDist = Math.ceil(Math.random() * 6);
return randomDist;
}
refill() {
if (this.petrolPumps.includes(this.currentDistance)) {
this.petrolLeft += 30;
return true
} else {
return false
}
}
this.moveCount += 1;
this.currentDistance += randomDist;
this.petrolLeft -= randomDist * (1 / this.milage);
this.refill();
const move = this.moveCount;
const distance = this.currentDistance;
const petrol = this.petrolLeft;
}
lastMove(randomDist) {
this.moveCount += 1;
this.currentDistance += randomDist;
this.petrolLeft -= randomDist * 1 / this.milage;
start() {
this.setDefaults();
const startEl = document.createElement("p");
startEl.textContent = "Game started";
resultContainerEl.appendChild(startEl);
this.createRandomPetrolPumps();
const randomDist = this.generateNextDistance();
this.validationCheck(randomDist);
}
}