자바
알파벳 맞추기
148june
2025. 2. 6. 16:51
- 컴퓨터가 랜덤으로 영어단어를 선택합니다.
- 영어단어의 자리수를 알려줍니다.
- ex ) PICTURE = 7자리 ⇒ _ _ _ _ _ _ _
-
더보기더보기[
"airplane",
"apple",
"arm",
"bakery",
"banana",
"bank",
"bean",
"belt",
"bicycle",
"biography",
"blackboard",
"boat",
"bowl",
"broccoli",
"bus",
"car",
"carrot",
"chair",
"cherry",
"cinema",
"class",
"classroom",
"cloud",
"coat",
"cucumber",
"desk",
"dictionary",
"dress",
"ear",
"eye",
"fog",
"foot",
"fork",
"fruits",
"hail",
"hand",
"head",
"helicopter",
"hospital",
"ice",
"jacket",
"kettle",
"knife",
"leg",
"lettuce",
"library",
"magazine",
"mango",
"melon",
"motorcycle",
"mouth",
"newspaper",
"nose",
"notebook",
"novel",
"onion",
"orange",
"peach",
"pharmacy",
"pineapple",
"plate",
"pot",
"potato",
"rain",
"shirt",
"shoe",
"shop",
"sink",
"skateboard",
"ski",
"skirt",
"sky",
"snow",
"sock",
"spinach",
"spoon",
"stationary",
"stomach",
"strawberry",
"student",
"sun",
"supermarket",
"sweater",
"teacher",
"thunderstorm",
"tomato",
"trousers",
"truck",
"vegetables",
"vehicles",
"watermelon",
"wind"
] - 아래 단어들을 활용해보세요!
- 사용자는 A 부터 Z 까지의 알파벳 중에서 하나를 입력합니다.
- 입력값이 A-Z 사이의 알파벳이 아니라면 다시 입력을 받습니다
- 힌트
- Java 의 Charactor.isLetter() 을 활용해보세요
- 입력값이 한 글자가 아니라면 다시 입력을 받습니다
- 이미 입력했던 알파벳이라면 다시 입력을 받습니다.
- 입력값이 정답에 포함된 알파벳일 경우 해당 알파벳이 들어간 자리를 전부 보여주고, 다시 입력을 받습니다.
- ex ) 정답이 eyes 인 경우에 E 를 입력했을 때
- _ _ _ _ → E _ E _
- ex ) 정답이 eyes 인 경우에 E 를 입력했을 때
- 입력값이 정답에 포함되지 않은 알파벳일 경우 기회가 하나 차감되고, 다시 입력을 받습니다.
- 입력값이 A-Z 사이의 알파벳이 아니라면 다시 입력을 받습니다
- 사용자가 9번 틀리면 게임오버됩니다.
- 게임오버 되기 전에 영어단어의 모든 자리를 알아내면 플레이어의 승리입니다.
이 문제는 여러단어를 입력받아서 그중에 랜덤으로 출력된다.
random 임포트를 사용해야한다.
글자가 다맞추는게 아닌 " 포함하냐 "이니 contain을 사용한다.
import java.util.*;
public class WordGuessingGame {
public static void main(String[] args) {
// 단어 목록
List<String> words = Arrays.asList(
"airplane", "apple", "arm", "bakery", "banana", "bank", "bean", "belt", "bicycle", "biography",
"blackboard", "boat", "bowl", "broccoli", "bus", "car", "carrot", "chair", "cherry", "cinema",
"class", "classroom", "cloud", "coat", "cucumber", "desk", "dictionary", "dress", "ear", "eye",
"fog", "foot", "fork", "fruits", "hail", "hand", "head", "helicopter", "hospital", "ice", "jacket",
"kettle", "knife", "leg", "lettuce", "library", "magazine", "mango", "melon", "motorcycle", "mouth",
"newspaper", "nose", "notebook", "novel", "onion", "orange", "peach", "pharmacy", "pineapple",
"plate", "pot", "potato", "rain", "shirt", "shoe", "shop", "sink", "skateboard", "ski", "skirt",
"sky", "snow", "sock", "spinach", "spoon", "stationary", "stomach", "strawberry", "student", "sun",
"supermarket", "sweater", "teacher", "thunderstorm", "tomato", "trousers", "truck", "vegetables",
"vehicles", "watermelon", "wind"
);
// 랜덤 단어 선택
Random random = new Random();
String word = words.get(random.nextInt(words.size()));
// 플레이어 상태
Set<Character> guessedLetters = new HashSet<>();
int attemptsLeft = 9;
// 게임 시작
Scanner scanner = new Scanner(System.in);
// 단어 자리 수 출력
char[] wordArray = word.toCharArray();
char[] displayedWord = new char[word.length()];
Arrays.fill(displayedWord, '_');
System.out.println("단어의 자리수를 맞추세요! ");
System.out.println("단어는 " + word.length() + "자리입니다.");
while (attemptsLeft > 0) {
// 현재 상태 출력
System.out.println("현재 단어: " + String.valueOf(displayedWord));
System.out.println("남은 기회: " + attemptsLeft);
// 사용자 입력 받기
System.out.print("알파벳을 입력하세요: ");
String input = scanner.nextLine().toLowerCase();
// 입력이 올바른지 체크
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
System.out.println("유효하지 않은 입력입니다. A-Z 사이의 한 글자를 입력하세요.");
continue;
}
char guess = input.charAt(0);
// 이미 입력한 글자인지 확인
if (guessedLetters.contains(guess)) {
System.out.println("이미 입력한 글자입니다.");
continue;
}
// 입력한 알파벳을 이미 맞췄는지 확인
guessedLetters.add(guess);
// 입력한 알파벳이 단어에 포함되는지 확인
boolean correctGuess = false;
for (int i = 0; i < word.length(); i++) {
if (wordArray[i] == guess) {
displayedWord[i] = guess;
correctGuess = true;
}
}
// 정답에 포함되지 않은 알파벳일 경우
if (!correctGuess) {
attemptsLeft--;
System.out.println("잘못된 입력입니다. 기회가 하나 차감되었습니다.");
} else {
System.out.println("정답을 맞췄습니다!");
}
// 사용자가 모든 자리를 맞췄으면 게임 승리
if (String.valueOf(displayedWord).equals(word)) {
System.out.println("축하합니다! 단어를 모두 맞췄습니다.");
break;
}
}
if (attemptsLeft == 0) {
System.out.println("게임 오버! 정답은: " + word);
}
scanner.close();
}
}
초기값을 9로 선언 시도마다 하나씩 까기
이번에는 가변값이기때문에 map이아닌 hashmap을 사용하는 모습