traincrossingvalley
Version:
This JavaScript code generates a simple animation of a train crossing a valley surrounded by mountains.
166 lines (143 loc) • 5.17 kB
JavaScript
class TrainAnimation {
constructor(valleyWidth, peakHeight) {
this.valleyWidth = valleyWidth;
this.peakHeight = peakHeight;
this.train = '🚂';
this.trainLength = 5;
this.animationFrames = 10;
this.animationDelay = 200;
this.mountainValley = this.drawMountain();
this.trainPosition = 0;
}
drawMountain() {
let mountain = '';
for (let i = 0; i < this.peakHeight; i++) {
let spaces = ' '.repeat(this.peakHeight - i);
let peaks = '*'.repeat(2 * i + 1);
mountain += spaces + peaks + spaces + '\n';
}
let valley = '='.repeat(this.valleyWidth);
return mountain + valley;
}
animate() {
for (let i = 0; i < this.animationFrames; i++) {
setTimeout(() => {
this.updateTrainPosition();
this.displayAnimation();
}, i * this.animationDelay);
}
}
updateTrainPosition() {
this.trainPosition += this.trainLength;
if (this.trainPosition > this.valleyWidth) {
this.trainPosition = 0;
}
}
displayAnimation() {
this.clearConsole();
console.log(this.mountainValley);
console.log(' '.repeat(this.trainPosition) + this.train);
}
clearConsole() {
process.stdout.write('\x1B[2J\x1B[0f');
}
}
function startTrainAnimation(valleyWidth, peakHeight) {
const animation = new TrainAnimation(valleyWidth, peakHeight);
animation.animate();
}
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function generateRandomArray(length, min, max) {
const randomArray = [];
for (let i = 0; i < length; i++) {
randomArray.push(generateRandomNumber(min, max));
}
return randomArray;
}
function shuffleArray(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]];
}
return array;
}
function generateRandomString(length) {
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let randomString = '';
for (let i = 0; i < length; i++) {
randomString += characters.charAt(Math.floor(Math.random() * characters.length));
}
return randomString;
}
function generateRandomBoolean() {
return Math.random() < 0.5;
}
function generateRandomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function generateRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function generateRandomEmail() {
const domains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'icloud.com'];
const username = generateRandomString(8);
const domain = domains[Math.floor(Math.random() * domains.length)];
return `${username}@${domain}`;
}
function generateRandomIP() {
const ipSegments = [];
for (let i = 0; i < 4; i++) {
ipSegments.push(Math.floor(Math.random() * 256));
}
return ipSegments.join('.');
}
function generateRandomPhoneNumber() {
const areaCode = Math.floor(Math.random() * (999 - 100 + 1) + 100);
const firstPart = Math.floor(Math.random() * 1000);
const secondPart = Math.floor(Math.random() * 10000);
return `(${areaCode}) ${firstPart}-${secondPart}`;
}
function generateRandomISBN() {
const isbnPrefix = '978';
const groupIdentifier = Math.floor(Math.random() * 10);
const publisherCode = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
const titleCode = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
const checkDigit = Math.floor(Math.random() * 10);
return `${isbnPrefix}-${groupIdentifier}-${publisherCode}-${titleCode}-${checkDigit}`;
}
function generateRandomUUID() {
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
uuid = uuid.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return uuid;
}
function generateRandomCurrency() {
const currencies = ['USD', 'EUR', 'GBP', 'JPY', 'CAD', 'AUD'];
const currency = currencies[Math.floor(Math.random() * currencies.length)];
const amount = (Math.random() * 1000).toFixed(2);
return `${currency} ${amount}`;
}
function generateRandomHexColor() {
return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}
function generateRandomTimeZone() {
const timeZones = [
'America/New_York',
'America/Los_Angeles',
'Europe/London',
'Europe/Berlin',
'Asia/Tokyo',
'Australia/Sydney'
];
return timeZones[Math.floor(Math.random() * timeZones.length)];
}