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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 id = "container" class = "class_container">
  <div id="canvas">
    <canvas id = "Hangman" , width = "300" height = "400" style = "border: 1px black solid;"></canvas>
  </div>
  
  <div id = "game">
    <h3>Hint: </h3>
    <div id ="hint" class="clearfix"></div>
    <div id="input">
      <b>Try A Letter:</b>
      <input id="letter" type="text" maxlength= "1">
      
    </div>
    
    <div id="triedLetters" class="clearfix"><b>you already tried: </b></div>
    
    <div id="answerBox">
                <b>Answer: </b>
                <div id="answer"></div>
            </div>
    
    <div id="answerBox">
                <b>Finished words:</b>
                <div id="res"></div>
            </div>
  </div>
</div>
  </body>

              
            
!

CSS

              
                body{
  margin: 0;
}

.class_container{
  width: 960px;
  margin: 0 auto;
}

#container{
  margin-top: 15px;
}

#canvas{
    float: left;
    width: 35%;
}

#Hangman{
        border: 2px solid black;
    }

#game{
    float: left;
    width: 61%;
    margin-left: 20px;
}

 #hint{
        width: 92%;
        background-color: #FFCC33;
        border: 3px dashed #000000;
        padding: 20px;
        text-align: center;
        margin-bottom: 10px;
    }

 #input{
        width: 45%;
        padding: 20px;
        background-color: #d1d1d1;
        border: 3px solid #FFCC33;
        text-align: center;
        box-sizing: border-box;
        float: left;
}

       #letter{
            width: 20px;
        }

#triedLetters{
        width: 52%;
        float: left;
        margin-left: 3%;

        padding: 20px;
        background-color: #d1d1d1;
        border: 3px solid #FFCC33;
        text-align: left;
        box-sizing: border-box;
    }

#answerBox{
        width: 100%;
        float: left;
        box-sizing: border-box;
        padding: 20px;
        background-color: #d1d1d1;
        border: 3px solid #FFCC33;
        margin-top: 10px;
        text-align: center;
    }


.clearfix:after {
    content: ".";
    visibility: hidden;
    display: block;
    height: 0;
    clear: both;
}






              
            
!

JS

              
                var c = document.getElementById("Hangman");
var ctx = c.getContext("2d");
ctx.lineWidth = 10;

var words = [
    {
        word: "red",
        hint: "Blood color"
    },
    {
        word: "read",
        hint: "To look at carefully so as to understand the meaning of (something written, printed, etc.)"
    },
    {
        word: "write",
        hint: "Authors usually..."
    },
    {
        word: "welcome",
        hint: "A kindly greeting or reception, as to one whose arrival gives pleasure"
    },
  {
    word: "range",
    hint: "a variety of different things or activities"
  },
  {
    word: "skill",
    hint: "an ability that has been acquired by training"
  },
  {
    word: "gravity",
    hint: "the force of attraction between all masses in the universe"
  },
  {
    word:"contract",
    hint: "a binding agreement that is enforceable by law"
  },
  {
    word: "formula",
    hint: "a group of symbols that make a mathematical statement"
  },
  {
    word: "variable",
    hint: "something that is likely to change"
  }
];

var usedWords = [];
var word;
var hint;
var answer;
var hangs;

function hang(hangs)
{
  ctx.strokestyle = 'black' ;
  ctx.lineWidth = 4;
  switch (hangs){
        case 1:
            ctx.beginPath();
            ctx.arc(200, 80, 20, 0, 2 * Math.PI);
            ctx.fillStyle = 'black';
            ctx.fill();
            break;

        case 2:
            ctx.moveTo(200, 80);
            ctx.lineTo(200, 180);
            ctx.stroke();
            break;

        case 3:
            ctx.moveTo(200, 110);
            ctx.lineTo(170, 150);
            ctx.stroke();
            break;

        case 4:
            ctx.moveTo(200, 110);
            ctx.lineTo(230, 150);
            ctx.stroke();
            break;

        case 5:
            ctx.moveTo(200, 180);
            ctx.lineTo(170, 230);
            ctx.stroke();
            break;

        case 6:
            ctx.moveTo(200, 180);
            ctx.lineTo(230, 230);
            ctx.stroke();
            break;

        default:
            break;
    }
}


function writeAnswer(){
    $('#answer').html("");
    for(var i=0; i<answer.length; i++){
        var s = answer[i]==null? '_ ':answer[i] + ' ';
        $('#answer').append($('<span>').html(s));
    }
}

function triedWords(){
    if(usedWords.length>0){
        $('#res').html('<ul></ul>');
        for(var i=0; i<usedWords.length; i++){
            $('#res ul').append($('<li>').html(usedWords[i].word));
        }
        //$('#res').html(usedWords);
        console.log(usedWords);
    }
}

function drawHang(){
    ctx.beginPath();
    ctx.clearRect(0, 0, c.width, c.height);

    ctx.strokeStyle = 'sienna';
    ctx.moveTo(15, 15);
    ctx.lineTo(15, 455);

    ctx.moveTo(10, 15);
    ctx.lineTo(200, 15);

    ctx.moveTo(145, 15);
    ctx.lineTo(15, 100);

    ctx.moveTo(200, 10);
    ctx.lineTo(200, 65);

    ctx.stroke();
}

function newGame(){
  triedWords();
    drawHang();
    
    while (true){
        var random = Math.floor(Math.random() * words.length);
        if(usedWords.indexOf(words[random]) == -1){
            word = words[random].word.split("");
            hint = words[random].hint;
            usedWords[usedWords.length] = words[random];
            break;
        }
    }
  $('#hint').html(hint);
    $('#triedLetters').html('<b>You already tried: </b>');
    answer = new Array(word.length);

    hangs = 1;

    writeAnswer();
}
  
 function tryLetter(){
    var letter = $('#letter').val().toLowerCase();
    var letPos = word.indexOf(letter);

    if(letPos != -1 && letter != ""){
        answer[letPos] = letter;
        word[letPos] = null;
        writeAnswer();
    }else{
        $('#triedLetters').append(letter);
        hang(hangs++);
    }

    if(word.count(null) == word.length){
        alert("you win");
        newGame();
    }

    if(hangs > 6 ){
        alert ("you lose");
        location.reload(true);
    }
   if(usedWords.length == words.length)
{
  alert("new game");
  location.reload(true);
}

    $('#letter').val("");
}

// Array.prototype.count = function(element){
//     var c = 0;
//     for(var i= 0; i<this.length; i++){
//         if(this[i] == element)
//             c++;
//     }

//     return c;
// }
Array.prototype.count = function(item){ return this.filter((x)=>x===item).length}




    $(document).ready(newGame());
    $('#letter').keyup(function () {
        tryLetter();
    });
              
            
!
999px

Console