0% found this document useful (0 votes)
29 views3 pages

Car

Uploaded by

Siva Virat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Car

Uploaded by

Siva Virat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<!

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;
}

let resultContainerEl = document.getElementById("resultContainer");

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;
});

let subString = '';


for (let iter = 0; iter < 5; iter++) {
subString += this.petrolPumps[iter] + ", ";
}

subString += this.petrolPumps[this.petrolPumps.length - 1];


const pumpsEl = document.createElement("p");
pumpsEl.textContent = "Petrol pumps generated at " + subString;
resultContainerEl.appendChild(pumpsEl);
}

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
}
}

nextMove(randomDist, gameStatus = '') {

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;

const moveEl = document.createElement("p");


moveEl.textContent = `Move ${move} - car at ${distance}, petrol remaining $
{petrol}` + gameStatus;
resultContainerEl.appendChild(moveEl);
const randomDistance = this.generateNextDistance();
this.validationCheck(randomDistance);

}
lastMove(randomDist) {
this.moveCount += 1;
this.currentDistance += randomDist;
this.petrolLeft -= randomDist * 1 / this.milage;

const refillStatus = this.refill();


if (refillStatus) {
const randomDistance = this.generateNextDistance();
this.validationCheck(randomDistance);
} else {
const move = this.moveCount;
const distance = this.currentDistance;
const petrol = this.petrolLeft;

const moveEl = document.createElement("p");


moveEl.textContent = `Move ${move} - car at ${distance}, petrol
remaining ${petrol}, game over`;
resultContainerEl.appendChild(moveEl);
}
}
validationCheck(randomDist) {
if (this.currentDistance + randomDist <= 100 && this.petrolLeft -
(randomDist * 1 / this.milage) >= 0) {
this.nextMove(randomDist);
} else {
const dragDist = this.petrolLeft * this.milage;
this.lastMove(dragDist);
}
}

start() {
this.setDefaults();
const startEl = document.createElement("p");
startEl.textContent = "Game started";
resultContainerEl.appendChild(startEl);
this.createRandomPetrolPumps();
const randomDist = this.generateNextDistance();
this.validationCheck(randomDist);

}
}

const myCar = new Car();

const onStart = function() {


myCar.start();
};

You might also like