SHARE
TWEET

Untitled

a guest Feb 19th, 2020 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3.    <title>21 lucifers</title>
  4. </head>
  5. <body>
  6.     <h1> The Lucifer Game </h1>
  7.     <div id="setup">
  8.  
  9.         How many Lucifers do you want?<br>
  10.         <input type="text" id="numberOfLucifers"><br>
  11.         Player 1 name <br>
  12.         <input type="text" id="player1Field"><br>
  13.         Player 2 name <br>
  14.         <input type="text" id="player2Field"><br>
  15.         <button id="start" >start</button>
  16.     </div>
  17.     <div id="playground" style="visibility: hidden;">
  18.         <h4>aantal lucifers:</h4>
  19.         <h1 id="numberOfLucifersLeftHeader"></h1>
  20.         <hr>
  21.         <h4 id="turnText">... is aan de beurt</h4>
  22.         <input type="text" id="amountToTakeField">
  23.             <button id="takeButton" >Pak!</button>
  24.     </div>
  25.     <script>
  26.                    
  27.     var numberOfLucifers;
  28.     var player1;
  29.     var player2;
  30.     var playerTurn = 1;
  31.  
  32.     document.getElementById("takeButton").onclick = playRound;
  33.     document.getElementById("start").onclick = setup;
  34.     document.getElementById("amountToTakeField").onkeyup = disableButtonOnBadInput;
  35.    
  36.     function isNumberBetween1and3(value){
  37.         var isCorrect = true;
  38.         if(isPositiveNumber(value) == false){
  39.             isCorrect = false;
  40.         }else if(value > 3){
  41.             isCorrect = false;
  42.         }else if(value < 1){
  43.             isCorrect = false;
  44.         }  
  45.         return isCorrect;
  46.     }
  47.  
  48.     function amountInField(){
  49.         var amountField =  document.getElementById("amountToTakeField");
  50.         var result = Math.floor(Number( amountField.value ));
  51.         return result;
  52.     }
  53.  
  54.     function takeLucifers(amount) {
  55.         var numberLeft = numberOfLucifers - amount;
  56.        
  57.         return numberLeft;
  58.     }
  59.  
  60.     function writeNumberOfLucifers(amount){
  61.         document.getElementById("numberOfLucifersLeftHeader").innerHTML = amount;
  62.     }
  63.    
  64.     function toggleTurn(){
  65.         playerTurn *= -1;
  66.     }
  67.        
  68.     function activePlayer(){
  69.         if (playerTurn > 0){
  70.              return player1;
  71.         }else if (playerTurn < 0){
  72.              return player2;
  73.         }
  74.     }
  75.  
  76.     function showPlayground(){
  77.         document.getElementById("setup").style.visibility = "hidden";
  78.         document.getElementById("playground").style.visibility = "visible";
  79.     }
  80.  
  81.     function showSetupScreen(){
  82.         document.getElementById("setup").style.visibility = "visible";
  83.         document.getElementById("playground").style.visibility = "hidden";
  84.     }
  85.    
  86.     function writeActivePlayerName(){
  87.         document.getElementById("turnText").innerHTML = "It's " + activePlayer() + "'s turn.";
  88.     }
  89.  
  90.     function isPositiveNumber(value){
  91.         var isValidNumber = true;
  92.         if(isNaN(value)){
  93.             isValidNumber = false;
  94.         }else if(value < 1){
  95.             isValidNumber = false;
  96.         }
  97.         return isValidNumber;
  98.     }
  99.  
  100.     function isValidString(input){
  101.         var isValid = true;
  102.         if(!input){
  103.             isValid = false;
  104.         }
  105.         return isValid;
  106.     }
  107.  
  108.     function showWinner(){
  109.         alert(activePlayer() + ' has won!');
  110.     }
  111.  
  112.     function gameEnded(lucifersLeft){
  113.         var gameEnded = false;
  114.         if(lucifersLeft == 0){
  115.             gameEnded = true;
  116.         }
  117.         return gameEnded;
  118.     }
  119.  
  120.     function playerTookTooManyLuficers(lucifersLeft){
  121.         var isTooMuch = false;
  122.         if(lucifersLeft < 0){
  123.             isTooMuch = true;
  124.         }
  125.         return isTooMuch;
  126.     }
  127.  
  128.     function showPlayerTookToManyError(){
  129.         alert(activePlayer() + ' took too many. Try Again!');
  130.     }
  131.  
  132.     function isValidSetupFormInput(amount, name1, name2){
  133.         var isValidForm = true;
  134.         if(isPositiveNumber(amount) == false){
  135.             isValidForm = false;
  136.         }
  137.         if(isValidString(name1) == false){
  138.             isValidForm = false;
  139.         }
  140.         if(isValidString(name2) == false){
  141.             isValidForm = false;
  142.         }
  143.         return isValidForm;
  144.     }
  145.  
  146.     function showSetupFormErrors(value, name1, name2){
  147.         if(isPositiveNumber(value) == false){
  148.             alert('Not a valid number of lucifers');
  149.         }
  150.         if(isValidString(name1) == false){
  151.             alert('Please enter player 1');
  152.         }
  153.         if(isValidString(name2) == false){
  154.             alert('Please enter player 2');
  155.         }
  156.     }
  157.  
  158.  
  159.     function showCheatingError(){
  160.         alert('Only enter 1 , 2 or 3!');
  161.     }
  162.    
  163.    
  164.    
  165.     function disableButtonOnBadInput() {
  166.         if(isNumberBetween1and3(amountInField()) == false){
  167.             document.getElementById("takeButton").disabled = true;
  168.         }else{
  169.             document.getElementById("takeButton").disabled = false;
  170.         }
  171.     }
  172.    
  173.  
  174.     function setup(){
  175.         numberOfLucifers = document.getElementById("numberOfLucifers").value;
  176.         player1 = document.getElementById("player1Field").value;
  177.         player2 = document.getElementById("player2Field").value;
  178.        
  179.         if(isValidSetupFormInput(numberOfLucifers, player1, player2) ){
  180.             showPlayground();
  181.             writeNumberOfLucifers(numberOfLucifers);
  182.             writeActivePlayerName();   
  183.         }else{
  184.             showSetupFormErrors(numberOfLucifers, player1, player2);
  185.         }      
  186.     }
  187.  
  188.  
  189.     function playRound(){
  190.         var input = amountInField();
  191.         if(isNumberBetween1and3(input)){
  192.             var lucifersLeft = takeLucifers(input);
  193.             if(gameEnded(lucifersLeft)){
  194.                 showWinner();
  195.                 showSetupScreen();
  196.             }else if(playerTookTooManyLuficers(lucifersLeft)){
  197.                 showPlayerTookToManyError();
  198.             }else{
  199.                 numberOfLucifers = lucifersLeft;
  200.                 writeNumberOfLucifers(numberOfLucifers);
  201.                 toggleTurn();
  202.                 writeActivePlayerName();
  203.             }
  204.         }else{
  205.             showCheatingError();
  206.         }
  207.     }
  208.  
  209.   </script>  
  210. </body>
  211. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top