Chemist-i

My fisrt program on JS - rock, scissors, paper by Chemist

Feb 12th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.61 KB | None | 0 0
  1. <html>
  2. <meta charset="UTF-8">
  3. <body>
  4.     <h1>My small program for learn JavaScript</h1>
  5.  
  6.     <link rel="stylesheet" type="text/css" media="screen" href="file:///D:/xxx.css" />
  7.  
  8.     <script type="text/javascript">
  9.  
  10.         //global variables
  11.         var Title;
  12.         var Hint;
  13.  
  14.         var rock;
  15.         var paper;
  16.         var scissors;
  17.         var tie;
  18.         var wins;
  19.  
  20.         var uChoice;
  21.         var cChoice;
  22.  
  23.         function changeLang() {
  24.             var lang = "";
  25.             if (document.getElementById("lang") != null) {
  26.                 lang = document.getElementById("lang").value;
  27.             } else {
  28.                 lang = "EN";
  29.             }
  30.  
  31.             if (lang === "RU") {
  32.                 Title = "Игра камень, ножницы, бумага by Chemist";
  33.                 Hint = "Пожалуйста введите Ваш вариант (камень, ножницы или бумага):";
  34.                 rock = "камень";
  35.                 paper = "бумага";
  36.                 scissors = "ножницы";
  37.                 tie = "Ничья";
  38.                 wins = "победил вариант - ";
  39.                 uChoice = "Вы выбрали";
  40.                 cChoice = "Компьютер выбрал";
  41.             } else {
  42.                 if (lang === "UK") {
  43.                     Title = "Гра камінь, ножиці, бумага by Chemist";
  44.                     Hint = "Будь-ласка оберіть Ваш варіант (каміть, ножиці чи бумага)";
  45.                     rock = "камінь";
  46.                     paper = "бумага";
  47.                     scissors = "ножиці";
  48.                     tie = "Нічия";
  49.                     wins = "переміг варіант - ";
  50.                     uChoice = "Ви обрали"
  51.                     cChoice = "Комп'ютер обрав";                  
  52.                 } else { //EN
  53.                     Title = "Game rock, scissors, paper by Chemist"
  54.                     Hint = "Please to choice Your variant (rock, scissors, paper)";
  55.  
  56.                     rock = "rock";
  57.                     paper = "paper";
  58.                     scissors = "scissors";
  59.                     tie = "tie"
  60.                     wins = "wins - "
  61.                     uChoice = "You choice"
  62.                     cChoice = "Computer choice";                   
  63.                 }
  64.             }
  65.  
  66.             document.getElementById("title1").innerHTML = Title;
  67.             document.getElementById("hint1").innerHTML = Hint;
  68.            
  69.             document.getElementById("staff").options[0] = new Option(rock, rock);
  70.             document.getElementById("staff").options[1] = new Option(scissors, scissors);
  71.             document.getElementById("staff").options[2] = new Option(paper, paper);
  72.  
  73.             //console.log( document.getElementById("title1").innerHTML  );
  74.         }
  75.  
  76.         function calculate() {
  77.             //get user Choice;
  78.             //var userChoice = document.getElementById('myInput').value;
  79.  
  80.             var userChoice = document.getElementById("staff").value //selectedIndex;
  81.  
  82.             //document.write(uChoice + " " + userChoice + "<br>");
  83.             document.getElementById("div1").innerHTML = uChoice + " " + userChoice + "<br>"
  84.  
  85.             var computerChoice = Math.random();
  86.            
  87.             if (computerChoice < (1.0/3.0)) {
  88.                 computerChoice = rock;
  89.             } else if(computerChoice <= (2.0/3.0)) {
  90.                 computerChoice = paper;
  91.             } else {
  92.                 computerChoice = scissors;
  93.             }
  94.            
  95.             //console.log("Computer: " + computerChoice);
  96.             //document.write(cChoice + " " + computerChoice + "<br>");
  97.             document.getElementById("div2").innerHTML = cChoice + " " + computerChoice + "<br>"
  98.  
  99.             var compare = function (choice1, choice2) {
  100.                 if (choice1 == choice2) {
  101.                     return tie;
  102.                 } else {
  103.                    
  104.                     if (choice1 === rock) {
  105.                         if (choice2 === scissors) {
  106.                             return wins + rock;
  107.                         } else {
  108.                             return wins + paper;
  109.                         }
  110.                     } else {
  111.                         if (choice1 === paper) {
  112.                             if (choice2 === rock) {
  113.                                 return wins + paper;
  114.                             } else {
  115.                                 return wins + scissors;
  116.                             }
  117.                        
  118.                         } else { //choice1 === scissors
  119.                             if (choice2 === rock) {
  120.                                 return wins + rock;
  121.                             } else {
  122.                                 return wins + scissors;
  123.                             }
  124.                         }
  125.                     }
  126.                 }
  127.                    
  128.             }          
  129.  
  130.             //document.write( compare(userChoice, computerChoice) + "<br>");
  131.             document.getElementById("div3").innerHTML = compare(userChoice, computerChoice) + "<br>";
  132.         }
  133.  
  134.        
  135.     </script>
  136.     <select size="1" id="lang">
  137.         <option selected value="EN">English</option>
  138.         <option value="RU">Русский</option>
  139.         <option value="UK">Українська</option>
  140.     </select>
  141.  
  142.     <input type="button" onclick="changeLang()" value="Change"/><br>
  143.  
  144.     <h3><div id="title1"></div></h3><br>
  145.         <div id="hint1"></div><br>
  146.  
  147.  
  148.     <select size="1" id="staff">
  149.         <option value="камень">камень</option>
  150.         <option value="ножницы">ножницы</option>
  151.         <option value="бумага">бумага</option>
  152.     </select><br>
  153.  
  154.     <input type="button" onclick="calculate()" value="Calculate!"/><br><br>
  155.  
  156.     <div id="div1"></div>
  157.     <div id="div2"></div>
  158.     <div id="div3"></div>
  159.  
  160.     <br><br>
  161.     <h3>end.</h3>
  162.  
  163.     <script type="text/javascript">
  164.         changeLang();
  165.     </script>
  166. </body>
  167. </html>
Advertisement
Add Comment
Please, Sign In to add comment