본문 바로가기
JAVA

java - 행맨 게임 만들기(명품 자바 프로그래밍 8장-Open Challenge)

by Big Sun 2022. 4. 29.
728x90
package Example;

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

public class HangMan {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		File f = new File("C:\\Users\\19431\\Desktop\\project\\words.txt");
		Vector<String> v = new Vector<String>();
		String eng = "";
		String randomEng = "";
		String xrandomEng = "";
		String choice = "y";
		try {
			FileReader fr = new FileReader(f);
			BufferedReader fis = new BufferedReader(fr);
			while((eng = fis.readLine()) != null) {
				if(eng.length() > 3) {
					v.add(eng);
				}
			}
			System.out.println("지금부터 행맨 게임을 시작합니다.");
			while(choice.equals("y")) {
				
				int random = (int)(Math.random()*100);
				randomEng = v.get(random);
				xrandomEng = randomEng.replace(randomEng.charAt(1),'1');
				xrandomEng = xrandomEng.replace(xrandomEng.charAt(3),'2');
				String str1;
				String str2;
				do {
					System.out.println(xrandomEng);
					System.out.print(">>");
					str1 = scan.next();
					System.out.print(">>");
					str2 = scan.next();
				}while(!(randomEng.charAt(1) == str1.charAt(0) && randomEng.charAt(3) == str2.charAt(0)));
				System.out.println("정답 : " + randomEng);
				System.out.print("Next?? (y/n)");
				choice = scan.next();
				
				
				
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		

	}

}

 

 

다른 Open Challenge에비해 그닥 어렵지 않은 것 같습니다

이상입니다~!

 

요즘 공부하기 너무 귀찮은데 이럴 때 일수록 힘내야합니다!!

파이팅!!

728x90