Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
2.5 kB
1
Indexable
Never
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wisielec</title>
<style>
  body {
    font-family: Arial, sans-serif;
  }
  #word {
    font-size: 24px;
    margin-bottom: 20px;
  }
  #letters {
    margin-bottom: 20px;
  }
  #letters button {
    font-size: 18px;
    margin: 5px;
    padding: 5px 10px;
    cursor: pointer;
  }
  #result {
    font-size: 24px;
    font-weight: bold;
  }
</style>
</head>
<body>

<div id="word"></div>
<div id="letters"></div>
<div id="result"><p>Zostało ci <p id="proba"></p> Prób</p></div>


<script>
  const words = ["KOT", "PIES", "SAMOCHÓD", "KOMPUTER", "DOM","ZUPA","MAMA","TATA","CYBERPUNK","WIEDŹMIN","TOMEK","MLEKO","MONITOR","ODDAJE","WYPŁATA","REKLAMACJA","KONSTANTYNOPOLITYŃCZYKOWIANECZKA","BOBER","BRZĘCZYSZCZYKIEWICZ","ŻÓŁĆ"];

  let selectedWord = words[Math.floor(Math.random() * words.length)];
  let guessedLetters = new Set();
  let remainingAttempts = 6;

  function displayWord() {
    let wordDisplay = '';
    for (let char of selectedWord) {
      if (guessedLetters.has(char.toUpperCase()) || guessedLetters.has(char.toLowerCase())) {
        wordDisplay += char + ' ';
      } else {
        wordDisplay += '_ ';
      }
    }
    document.getElementById('word').textContent = wordDisplay;
  }

  function displayLetters() {
    let letters = '';
    const polishAlphabet = "AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻ";
    for (let char of polishAlphabet) {
      if (!guessedLetters.has(char.toUpperCase()) && !guessedLetters.has(char.toLowerCase())) {
        letters += `<button onclick="guess('${char}')">${char}</button>`;
      }
    }
    document.getElementById('letters').innerHTML = letters;
  }

  function guess(letter) {
    guessedLetters.add(letter.toUpperCase());
    guessedLetters.add(letter.toLowerCase());
    if (!selectedWord.toUpperCase().includes(letter.toUpperCase())) {
      remainingAttempts--;
      document.getElementById("proba").textContent = remainingAttempts;
    }
    displayWord();
    displayLetters();
    checkResult();
  }

  function checkResult() {
    if (remainingAttempts === 0) {
      document.getElementById('result').textContent = 'Przegrałeś. Szukane słowo to: ' + selectedWord;
    } else if (!document.getElementById('word').textContent.includes('_')) {
      document.getElementById('result').textContent = 'Gratulacje! Wygrałeś!';
    }
  }

  displayWord();
  displayLetters();
</script>

</body>
</html>
Leave a Comment