asternauta

MajsterMajnd

Feb 2nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 7.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title></title>
  5.         <meta charset="utf-8"/>
  6.     </head>
  7.     <body>     
  8.         <h1 onclick="Test()">Mastermind</h1>
  9.         <div id="menu">
  10.             <fieldset id="ramka_menu">
  11.                 <legend>Menu</legend>
  12.                 <button onclick="NowaGra()">Nowa gra</button>
  13.                 <button onclick="Rezygnacja()">Poddaję się</button>
  14.                 L. cyfr: <input id="l_cyfr" type="number" min="4" max="6" value="5" onchange="ZmianaIlCyfr()"/>
  15.             </fieldset>
  16.         </div>
  17.         <i id="komunikat"></i><br><br>
  18.         <div id="prosba_o_cyfry">Podaj cyfry: <input type="text" id="pole_cyfry" maxlength="5" size="5" onchange="Interakcja()"/></div>
  19.         <div id="wygrana">Wygrałeś/łaś! Wielkie brawa!</div>
  20.         <div id="przegrana">No cóż, nie udało ci się rozwiązać tej zagadki, prawidłową odpowiedzią jest <span id="rozwiazanie"></span>.</div>
  21.         <table id="tabela_prob">
  22.             <thead>
  23.                 <th>Nr</th>
  24.                 <th>Kombinacja</th>
  25.                 <th>Trafione</th>
  26.                 <th>Istniejące</th>
  27.             </thead>
  28.             <tbody id="lista_prob"></tbody>
  29.             <tfoot></tfoot>
  30.         </table>       
  31.         <span id="oszustwo"></span>
  32.         <script>           
  33.             var timer_komunikat;
  34.            
  35.             function Komunikat(id, t, czas)
  36.             {
  37.                 var el_komunikat = document.getElementById(id);
  38.                 if (t)
  39.                 {
  40.                     el_komunikat.innerHTML = t;
  41.                     el_komunikat.style.display = "inline-block";
  42.                    
  43.                     WlaczTimer(id, czas);
  44.                 }
  45.                 else
  46.                 {
  47.                     el_komunikat.style.display = "none";
  48.                     UsunTimer();
  49.                 }
  50.             }
  51.            
  52.             function UsunTimer()
  53.             {
  54.                 clearInterval(timer_komunikat);
  55.                 timer_komunikat=undefined; 
  56.             }
  57.            
  58.             function WlaczTimer(id, czas)
  59.             {
  60.                 if (timer_komunikat)
  61.                     UsunTimer();
  62.                
  63.                 timer_komunikat = setInterval(Komunikat, czas, id);
  64.             }
  65.            
  66.         </script>
  67.         <script>
  68.             const status = {w_toku: 0, wygrana: 1, przegrana: 2};
  69.  
  70.             var el_oszustwo = document.getElementById("oszustwo"),
  71.                 el_cyfry = document.getElementById("pole_cyfry"),
  72.                 el_prosba = document.getElementById("prosba_o_cyfry"),
  73.                 el_wygrana = document.getElementById("wygrana"),
  74.                 el_przegrana = document.getElementById("przegrana"),
  75.                 el_lista_prob = document.getElementById("lista_prob"),
  76.                 el_rozwiazanie = document.getElementById("rozwiazanie"),
  77.                 el_l_cyfr = document.getElementById("l_cyfr"),
  78.                 el_proby = document.getElementById("proby");
  79.                
  80.                
  81.             var proba_nr, rozw = "", il_cyfr = 5;
  82.            
  83.             function OdNowa()
  84.             {
  85.                 il_cyfr = parseInt(el_l_cyfr.value);
  86.                 proba_nr = 1;
  87.                 rozw = GenerujRozw();
  88.                 el_lista_prob.innerHTML = "";
  89.                 el_cyfry.value = "";
  90.                 el_cyfry.maxLength = el_cyfry.size = il_cyfr;
  91.                 PokazStatus(status.w_toku);
  92.                
  93.                 if (el_oszustwo) el_oszustwo.innerHTML = rozw;
  94.             }
  95.            
  96.             function LosujZnakCyfry()
  97.             {
  98.                 const max = 9;
  99.                 var liczba = Math.floor(Math.random() * max);          
  100.                 return liczba + "";        
  101.             }
  102.            
  103.             function GenerujRozw()
  104.             {
  105.                 var i = 0, znak_cyfra, t="";
  106.                 while (i < il_cyfr)
  107.                 {
  108.                     znak_cyfra = LosujZnakCyfry();
  109.                     if (t.indexOf(znak_cyfra) == -1)
  110.                     {
  111.                         t+=znak_cyfra;
  112.                         ++i;
  113.                     }
  114.                 }              
  115.                 return t;
  116.             }
  117.            
  118.             function Sprawdz(podane_cyfry)
  119.             {
  120.                 var i = 0, j, wyn = {pasujace: 0, istniejace: 0}, tab_ozn_poz=[], k;
  121.                 while (i < podane_cyfry.length)
  122.                 {
  123.                     j = 0;
  124.                     while (j < rozw.length)
  125.                     {
  126.                         if (rozw[j] == podane_cyfry[i])
  127.                         {
  128.                             if (i == j)
  129.                             {                          
  130.                                 tab_ozn_poz[j] = 'p';
  131.                             }
  132.                             else
  133.                             {
  134.                                 if (tab_ozn_poz[j] != 'p')
  135.                                     tab_ozn_poz[j] = 'i';
  136.                             }                              
  137.                         }
  138.                         ++j;
  139.                     }
  140.                     ++i;
  141.                 }
  142.                
  143.                 i = 0;
  144.                 while (i < tab_ozn_poz.length)
  145.                 {
  146.                     if (tab_ozn_poz[i] == 'p') ++wyn.pasujace;
  147.                     else if (tab_ozn_poz[i] == 'i') ++wyn.istniejace;
  148.                    
  149.                     ++i;
  150.                 }
  151.                 return wyn;
  152.             }
  153.             /*
  154.             function CzyCyfryUnikatowe(podane_cyfry)
  155.             {
  156.                 var i = 0, wyn = true, dl = podane_cyfry.length, poz;
  157.                 while (i < dl && wyn)
  158.                 {
  159.                     poz = podane_cyfry.indexOf(podane_cyfry[i],i+1);
  160.                     if (poz > -1)
  161.                         wyn = false;                   
  162.                    
  163.                     ++i;
  164.                 }
  165.                 return wyn;
  166.             }*/
  167.            
  168.             function SprawdzPopr(podane_cyfry)
  169.             {
  170.                 return (podane_cyfry.length == il_cyfr && podane_cyfry.search(/\D/) == -1 /*&& CzyCyfryUnikatowe(podane_cyfry)*/);
  171.             }
  172.            
  173.             function WypiszProbe(nr, cyfry, pas, istn)
  174.             {
  175.                 var html = "<tr><td>"+nr+"</td><td><table class='tabela_cyfry'><tr>";
  176.                 var i = 0;
  177.                 while (i < cyfry.length)
  178.                 {              
  179.                     html+="<td>"+cyfry[i]+"</td>";
  180.                     ++i;
  181.                 }
  182.                 html += "</tr></table></td><td>" + pas + "</td><td>" + istn + "</td></tr>";
  183.                
  184.                 el_lista_prob.innerHTML += html;
  185.             }
  186.            
  187.             function Interakcja()
  188.             {
  189.                 var tekst = el_cyfry.value;
  190.                 if (SprawdzPopr(tekst))
  191.                 {
  192.                     var spr = Sprawdz(tekst);
  193.                     WypiszProbe(proba_nr++, tekst, spr.pasujace, spr.istniejace);
  194.                     el_cyfry.value = "";
  195.                    
  196.                     if (spr.pasujace == il_cyfr && spr.istniejace == 0)
  197.                         Wygrana();
  198.                 }
  199.                 else
  200.                     Komunikat("komunikat", "Wprowadź tylko cyfry. Najlepiej żeby się nie powtarzały.", 3000);
  201.             }      
  202.            
  203.             function PokazStatus(s)
  204.             {
  205.                 el_prosba.style.display = (s == status.w_toku ? "block" : "none");
  206.                 el_wygrana.style.display = (s == status.wygrana ? "block" : "none");
  207.                 el_przegrana.style.display = (s == status.przegrana ? "block" : "none");
  208.             }
  209.  
  210.             function NowaGra()
  211.             {
  212.                 var ok = false;
  213.                 if (proba_nr >= 1)
  214.                     ok = confirm("Czy na pewno chcesz rozpocząć grę od nowa?");                 
  215.  
  216.                 if (proba_nr == 0 || ok)
  217.                     OdNowa();
  218.             }
  219.            
  220.             function Rezygnacja()
  221.             {  
  222.                 if (proba_nr == 1)         
  223.                     alert("Ej, no weź! Jeszcze nie podjąłeś/łaś próby odgadnięcia, a ty już klikasz ten guzik.\n\n:|\n\nWstydź się!");              
  224.                 else if (proba_nr > 1)
  225.                 {
  226.                     var ok = confirm("Napewno wywieszasz białą flagę?");
  227.                     if (ok)
  228.                         Przegrana();
  229.                 }
  230.             }
  231.            
  232.             function Wygrana(){ PokazStatus(status.wygrana); }
  233.            
  234.             function Przegrana()
  235.             {
  236.                 el_rozwiazanie.innerHTML = rozw;
  237.                 proba_nr = 0;
  238.                 PokazStatus(status.przegrana);
  239.             }
  240.            
  241.             function ZmianaIlCyfr() { Komunikat('komunikat','Zmiany zostaną zastosowane po kliknięciu "Nowa Gra"', 4000); }
  242.  
  243.             OdNowa();
  244.         </script>
  245.         <style>
  246.             body {
  247.                 font-family: "Arial", "Verdana";
  248.                 text-align: center;
  249.                 margin: auto;
  250.             }
  251.            
  252.             .tabela_cyfry, #tabela_prob { margin: auto; }
  253.            
  254.             .tabela_cyfry, #pole_cyfry, #rozwiazanie {
  255.                 font-family: "Courier New", "Tahoma";
  256.                 font-size: 25px;
  257.                 font-weight: bold;
  258.             }          
  259.        
  260.             #tabela_prob,
  261.             .tabela_cyfry { border-collapse: collapse; }
  262.            
  263.             #tabela_prob th { font-size: 18px; }           
  264.             #tabela_prob td { font-size: 22px; }
  265.            
  266.             #tabela_prob th,
  267.             #tabela_prob td,
  268.             .tabela_cyfry td { border: 2px solid; padding: 5px;}
  269.            
  270.             .tabela_cyfry td { font-size: 25px !important; }               
  271.             #ramka_menu { display:inline;}         
  272.             #prosba_o_cyfry { padding-top: 5px; padding-bottom: 5px; }         
  273.             #wygrana { color: green; display: none; }          
  274.             #przegrana { color: red; display: none; }
  275.            
  276.             #rozwiazanie { background-color: red; color: white; }          
  277.             #l_cyfr { width: 30px; }
  278.            
  279.             #komunikat {
  280.                 border: 3px double;
  281.                 background-color: #fffac9;
  282.                 padding: 2px;
  283.                 display: none;
  284.             }
  285.         </style>
  286.     </body>
  287. </html>
Advertisement
Add Comment
Please, Sign In to add comment