Created
December 27, 2022 21:31
-
-
Save skorotkiewicz/a4efd7c3e9febca9756b81d8d150e727 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random from "random"; | |
import readline from "readline-sync"; | |
// Stwórz tablicę z dostępnymi słowami do wyboru | |
const words = ["słońce", "deszcz", "drzewo", "samochód", "komputer"]; | |
// Wybierz losowe słowo z tablicy | |
const targetWord = words[random.int(0, words.length - 1)]; | |
// Stwórz tablicę z gwiazdkami o długości słowa | |
const wordArray = new Array(targetWord.length).fill("*"); | |
// Stwórz tablicę na odgadnięte litery | |
const guessedLetters = []; | |
console.log("Zgadnij słowo: " + wordArray.join("")); | |
// Pętla zgadywania | |
while (true) { | |
// Pobierz odpowiedź od użytkownika | |
const guess = readline.question("Podaj słowo lub literkę:").toLowerCase(); | |
// Sprawdź, czy podane słowo jest poprawne | |
if (guess === targetWord) { | |
console.log("Gratulacje, zgadłeś/aś słowo: " + targetWord + "!"); | |
break; | |
} else if (guess.length === 1) { | |
// Jeśli użytkownik podał tylko jedną literkę, sprawdź, czy znajduje się ona w słowie | |
if (targetWord.includes(guess)) { | |
console.log("Zgadłeś/aś literkę!"); | |
// Dodaj odgadniętą literkę do tablicy | |
guessedLetters.push(guess); | |
} else { | |
console.log("Niestety, ta literka nie znajduje się w słowie."); | |
} | |
} else { | |
console.log("Podane słowo jest niepoprawne."); | |
} | |
// Zastąp gwiazdki odgadniętymi literami | |
for (let i = 0; i < targetWord.length; i++) { | |
if (guessedLetters.includes(targetWord[i])) { | |
wordArray[i] = targetWord[i]; | |
} | |
} | |
// Wyświetl aktualny stan słowa | |
console.log("Słowo: " + wordArray.join("")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script was generated 100% by ChatGPT