Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
  <div class="container-fluid">
  <div class="row w-100 justify-content-center">
    <div class="col-6 text-center">
      <h1 class="pt-5 pb-5">Character Generator</h1>
  
      <div id="content">
        <form>  
          <div class="row justify-content-center pb-4">
             Enter Character Name: <input class="ml-2" type="text" id="name" name="name"/> 
          </div> 
          <div class="row justify-content-center">
             <input type="submit" value="Generate" onclick="genCharacter()"/>  
          </div>
        </form> 
      </div>
  
    </div>
      <div class="row w-100 pt-4">
      <div class="col-12 text-center">
        Made by Robert Gras
      <div>
        </div>
  </div>
      </div>
  
</body>
              
            
!

CSS

              
                body {
  background-color: #0c155f;
  color: white;
}

.win {
  color: green;
}

.lose {
  color: red;
}

.draw {
  color: yellow;
}
              
            
!

JS

              
                function Character() {
  this.intellect = Math.floor(Math.random() * Math.floor(50))+1;
  this.strength = Math.floor(Math.random() * Math.floor(50))+1;
  this.speed = Math.floor(Math.random() * Math.floor(50))+1; 
  this.age = Math.floor(Math.random() * Math.floor(50))+1;
  this.hitPoints = Math.floor(Math.random() * Math.floor(80))+20;
  this.charName = "";
}
//alert("here1");
Character.prototype.rollInfo = function () {
  this.charRace = charRaceList[Math.floor(Math.random()*charRaceList.length)];
  this.charClass = charClassList[Math.floor(Math.random()*charClassList.length)];
  this.charWeapon = charWeaponList[Math.floor(Math.random()*charWeaponList.length)];
}
//alert("here2");
var charClassList = ["Warrior", "Rogue", "Druid", "Warlock"];
var charRaceList = ["Troll", "Human", "Elf", "ManBearPig"];
var charWeaponList = ["Dagger", "Sword", "Staff", "Fists"];
var opponentNameList = ["Steve", "Charles", "Sandy", "Tim"];
var yourCharacter = new Character();
var opponent = new Character();

function checkEnd() {
  if(yourCharacter.hitPoints < 0 && opponent.hitPoints < 0)
     {
          var div = document.getElementById("content");
       div.innerHTML += "<div class=\"row w-100 draw pt-3 pb-1 justify-content-center\"><div class=\"col-12 text-center\"><h1>DRAW!</h1></div></div>";
     }
  else if(yourCharacter.hitPoints < 0)
     {
          var div = document.getElementById("content");
       div.innerHTML += "<div class=\"row w-100 lose pt-3 pb-1 justify-content-center\"><div class=\"col-12 text-center\"><h1>YOU LOSE!</h1></div></div>";
     }
  else if(opponent.hitPoints < 0)
     {
          var div = document.getElementById("content");
       div.innerHTML += "<div class=\"row w-100 win pt-3 pb-1 justify-content-center\"><div class=\"col-12 text-center\"><h1>YOU WIN!</h1></div></div>";
     }
}

function fight(){
  if(yourCharacter.hitPoints > 0 && opponent.hitPoints > 0)
    {
      var div = document.getElementById("content");
  var yourHit = Math.floor(yourCharacter.strength * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
      
      var opponentAttack = Math.floor(Math.random() * Math.floor(90));
      var opponentHit = 0;
      var opponentWording = "";
      if(opponentAttack < 30)
        {
          opponentHit = Math.floor(opponent.strength * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent fought: ";
        }
      else if(opponentAttack >=60)
        {
          opponentHit = Math.floor(opponent.intellect * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent hid: ";
        }
      else
        {
          opponentHit = Math.floor(opponent.speed * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent ran: ";
        }
      
  yourCharacter.hitPoints -= opponentHit;
  opponent.hitPoints -= yourHit;
          div.innerHTML += "<div class=\"row w-100 pt-3 pb-1 justify-content-center\"><div class=\"col-12 text-center\">You fought: You hit your opponent for " + yourHit + " hitpoints.<br />" + opponentWording + " Your opponent hit you for " + opponentHit + " hitpoints.</div><div class=\"col-12 text-center\"><span class=\"font-weight-bold\">Your hitpoints: " + yourCharacter.hitPoints + " Opponent's Hitpoints: " + opponent.hitPoints + "</span></div></div>";
      checkEnd();
    }
  else
    {
      alert("The battle has already ended.");
    }
}

function hide(){
  if(yourCharacter.hitPoints > 0 && opponent.hitPoints > 0)
    {
      var div = document.getElementById("content");
  var yourHit = Math.floor(yourCharacter.intellect * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
      
      var opponentAttack = Math.floor(Math.random() * Math.floor(90));
      var opponentHit = 0;
      var opponentWording = "";
      if(opponentAttack < 30)
        {
          opponentHit = Math.floor(opponent.strength * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent fought: ";
        }
      else if(opponentAttack >= 60)
        {
          opponentHit = Math.floor(opponent.intellect * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent hid: ";
        }
      else
        {
          opponentHit = Math.floor(opponent.speed * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent ran: ";
        }
      
  yourCharacter.hitPoints -= opponentHit;
  opponent.hitPoints -= yourHit;
          div.innerHTML += "<div class=\"row w-100 pt-3 pb-1 justify-content-center\"><div class=\"col-12 text-center\">You hid: You hit your opponent for " + yourHit + " hitpoints.<br />" + opponentWording + " Your opponent hit you for " + opponentHit + " hitpoints.</div><div class=\"col-12 text-center\"><span class=\"font-weight-bold\">Your hitpoints: " + yourCharacter.hitPoints + " Opponent's Hitpoints: " + opponent.hitPoints + "</span></div></div>";
      checkEnd();
    }
  else
    {
      alert("The battle has already ended.");
    }
}

function run(){
  if(yourCharacter.hitPoints > 0 && opponent.hitPoints > 0)
    {
      var div = document.getElementById("content");
  var yourHit = Math.floor(yourCharacter.speed * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
      
      var opponentAttack = Math.floor(Math.random() * Math.floor(90));
      var opponentHit = 0;
      var opponentWording = "";
      if(opponentAttack < 30)
        {
          opponentHit = Math.floor(opponent.strength * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent fought: ";
        }
      else if(opponentAttack >= 60)
        {
          opponentHit = Math.floor(opponent.intellect * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent hid: ";
        }
      else
        {
          opponentHit = Math.floor(opponent.speed * ((Math.floor(Math.random() * Math.floor(70))+1)/100+0.2));
          opponentWording = "Your opponent ran: ";
        }
      
  yourCharacter.hitPoints -= opponentHit;
  opponent.hitPoints -= yourHit;
          div.innerHTML += "<div class=\"row w-100 pt-3 pb-1 justify-content-center\"><div class=\"col-12 text-center\">You ran: You hit your opponent for " + yourHit + " hitpoints.<br />" + opponentWording + " Your opponent hit you for " + opponentHit + " hitpoints.</div><div class=\"col-12 text-center\"><span class=\"font-weight-bold\">Your hitpoints: " + yourCharacter.hitPoints + " Opponent's Hitpoints: " + opponent.hitPoints + "</span></div></div>";
      checkEnd();
    }
  else
    {
      alert("The battle has already ended.");
    }
}

function genCharacter(){  
  event.preventDefault()
  if(document.getElementById("name").value == "")
     {
      alert("Please enter a name.");
      return;
     }
  
  yourCharacter.rollInfo();
  yourCharacter.charName = document.getElementById("name").value;
  
  opponent.rollInfo();
  opponent.charName = opponentNameList[Math.floor(Math.random()*opponentNameList.length)];
  
  var versus = "<div class=\"row w-100 justify-content-center\"><div class=\"col-3\"><h3>You</h3>Name: " + yourCharacter.charName + "<br />Race: "+ yourCharacter.charRace + "<br />Class: " + yourCharacter.charClass + "<br />Age: " + yourCharacter.age +"<br />Weapon: " + yourCharacter.charWeapon +"<br />Hitpoints: " + yourCharacter.hitPoints + "</div><div class=\"col-3\"><h1>VS</h1></div><div class=\"col-3\"><h3>Your Opponent</h3>Name: " + opponent.charName + "<br />Race: "+ opponent.charRace + "<br />Class: " + opponent.charClass + "<br />Age: " + opponent.age +"<br />Weapon: " + opponent.charWeapon +"<br />Hitpoints: " + opponent.hitPoints + "</div></div><div class=\"row w-100 justify-content-center\"><div class=\"col-5\">Would you like to fight, hide, or run?<br /><input type=\"button\" value=\"Fight\" onclick=\"fight()\"/> <input type=\"button\" value=\"Hide\" onclick=\"hide() \"/> <input type=\"button\" value=\"Run\" onclick=\"run() \"/> <br /><small>Use these buttons to continue the fight!</small> </div></div>";
  
  document.getElementById("content").innerHTML = versus;
}  
              
            
!
999px

Console