Write javascript code that generates a random dungeons and dragons player character
- User
Write javascript code that generates a random dungeons and dragons player character
- Assistant
There are many ways to generate a random Dungeons & Dragons player character in JavaScript. The following code is just one example that creates a simple random character with a random race and class, and randomly rolled ability scores:
// Array of available races const races = [ "Human", "Elf", "Dwarf", "Gnome", "Halfling", "Half-Elf", "Half-Orc", "Dragonborn", "Tiefling", ]; // Array of available classes const classes = [ "Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard", ]; // Function to generate ability scores using 4d6 drop lowest method function rollAbilityScore() { const roll = () => Math.floor(Math.random() * 6) + 1; const rolls = [roll(), roll(), roll(), roll()]; rolls.sort((a, b) => b - a); rolls.pop(); return rolls.reduce((sum, roll) => sum + roll, 0); } // Function to generate an ability score set function generateAbilityScores() { return { strength: rollAbilityScore(), dexterity: rollAbilityScore(), constitution: rollAbilityScore(), intelligence: rollAbilityScore(), wisdom: rollAbilityScore(), charisma: rollAbilityScore(), }; } // Generate random race and class index const raceIndex = Math.floor(Math.random() * races.length); const classIndex = Math.floor(Math.random() * classes.length); // Build random character const randomCharacter = { race: races[raceIndex], class: classes[classIndex], abilityScores: generateAbilityScores(), }; console.log(randomCharacter);This code will generate a random character object with random race, class, and ability scores. You can then output the data or use it in your application as needed. Note that this is a basic implementation and you can further customize the character generation process based on your specific requirements, such as including character backgrounds, subclasses, and starting equipment.
- User
Modify this code to also create a random character name, correlated to the character's race
- Assistant
I've added a function that generates a random character name correlated to the character's race. The names are simple examples and you can replace them with more appropriate names if needed.
// Array of available races and their corresponding example names const races = [ { race: "Human", exampleNames: ["John", "Alice", "Michael", "Emma"], }, { race: "Elf", exampleNames: ["Aelar", "Lia", "Faenor", "Eirlys"], }, { race: "Dwarf", exampleNames: ["Thorin", "Hilda", "Gimli", "Dori"], }, { race: "Gnome", exampleNames: ["Fizbo", "Bimble", "Chiqi", "Lorix"], }, { race: "Halfling", exampleNames: ["Frodo", "Pippin", "Sam", "Rosie"], }, { race: "Half-Elf", exampleNames: ["Arannis", "Aya", "Rienan", "Seraphine"], }, { race: "Half-Orc", exampleNames: ["Grom", "Thura", "Rogar", "Nalgrek"], }, { race: "Dragonborn", exampleNames: ["Balasar", "Kava", "Arjhan", "Maelis"], }, { race: "Tiefling", exampleNames: ["Morthos", "Kallista", "Soren", "Lilith"], }, ]; // Function to pick a random example name for the given raceIndex function getRandomName(raceIndex) { const exampleNames = races[raceIndex].exampleNames; const randomNameIndex = Math.floor(Math.random() * exampleNames.length); return exampleNames[randomNameIndex]; } // Generate random race and class index const raceIndex = Math.floor(Math.random() * races.length); const classIndex = Math.floor(Math.random() * classes.length); // Build random character const randomCharacter = { name: getRandomName(raceIndex), race: races[raceIndex].race, class: classes[classIndex], abilityScores: generateAbilityScores(), }; console.log(randomCharacter);This modification adds a property
exampleNamesto each race object in theracesarray. It contains an array of example names for that race. ThegetRandomName()function selects a random name from theexampleNamesarray based on theraceIndex. The random character object is then updated to include the randomly generatedname.