asternauta

MajsterMajnd (stary)

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