import java.io.*;
import java.util.Scanner;
import java.util.Vector;
import javax.security.auth.login.FailedLoginException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
JLabel word[] = new JLabel[30];
JTextField tf = new JTextField();
ImageIcon ht1 = new ImageIcon("하트.png");
JLabel h1 = new JLabel(ht1);
ImageIcon ht2 = new ImageIcon("하트.png");
JLabel h2 = new JLabel(ht2);
ImageIcon ht3 = new ImageIcon("하트.png");
JLabel h3 = new JLabel(ht3);
ImageIcon ht4 = new ImageIcon("하트.png");
JLabel h4 = new JLabel(ht4);
ImageIcon ht5 = new ImageIcon("하트.png");
JLabel h5 = new JLabel(ht5);
JButton yes = new JButton();
JButton no = new JButton();
JPanel zone = new JPanel();
Container c;
static int cnt=0;
static int hcnt=0;
static int cor_cnt=0;
static String now_string;
static Scanner w = new Scanner(System.in);
static Vector<String> wordVector = new Vector<String>();
public void init() {
zone.setLayout(new FlowLayout());
zone.setSize(750,100);
zone.setBackground(Color.CYAN);
zone.setLocation(80,80);
h1.setSize(32,32);
h1.setLocation(750,20);
h2.setSize(32,32);
h2.setLocation(782,20);
h3.setSize(32,32);
h3.setLocation(814,20);
h4.setSize(32,32);
h4.setLocation(846,20);
h5.setSize(32,32);
h5.setLocation(878,20);
tf.setFont(new Font("Arial", Font.PLAIN, 120));
tf.setForeground(Color.BLACK);
tf.setText("a");
tf.setSize(130,130);
tf.setLocation(400,300);
c.add(tf);
c.add(zone);
c.add(h1);
c.add(h2);
c.add(h3);
c.add(h4);
c.add(h5);
}
public void game_start() {
cor_cnt=0;
//1. 랜덤 단어 위치 구하기
int x = (int)(Math.random()*25143);
//int w;
String rd_str = wordVector.get(x);
now_string=rd_str;
System.out.println(rd_str);
//rd_str의 각 문자를 레이블로 나누어서 넣기
for(int i=0;i<rd_str.length();i++) {
word[i] = new JLabel("_");
zone.add(word[i]);
word[i].setFont(new Font("Arial", Font.PLAIN,80));
c.repaint();
c.revalidate();
}
tf.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
if(tf.getText().length()==0) {
return;
}
char w = tf.getText().charAt(0);
//w가 문자열 내부에서 같은 문자가 있다면 그 위치의 문자를 보여주도록 바꾸기
for(int i=0 ; i<rd_str.length() ; i++) {
if(hcnt >= 1) {
h1.setVisible(false);
}
if(hcnt >= 2) {
h2.setVisible(false);
}
if(hcnt >= 3) {
h3.setVisible(false);
}
if(hcnt >= 4) {
h4.setVisible(false);
}
if(hcnt >= 5) {
h5.setVisible(false);
System.out.println("게임 종료");
System.exit(0);
}
if(word[i].getText().equals("_")) {
if(w == rd_str.charAt(i) || 'A'<=w && 'Z'>=w && w+32 == rd_str.charAt(i) || 'a'<=w && 'z'>=w && w-32 == rd_str.charAt(i) ) {
word[i].setText(rd_str.charAt(i)+"");
cor_cnt++;
System.out.println(cor_cnt);
}
}
else {
hcnt++;
}
}
if(cor_cnt == rd_str.length()) {
System.out.println("성공함");
}
//단어를완성했는지?( 맞힌 문자 갯수==단어의길이)
tf.setText("");
}
}
});
}
public Main() {
setTitle("HangMan");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = getContentPane();
c.setLayout(null);
setSize(950,700);
setVisible(true);
init();
game_start();
}
public static void main(String[] args) {
InputStreamReader in = null;
FileInputStream fin = null;
try {
fin = new FileInputStream("C:\\Users\\user\\Desktop\\words.txt");
in = new InputStreamReader(fin);
int c;
Scanner s = new Scanner(new FileReader("C:\\Users\\user\\Desktop\\words.txt"));
while(s.hasNext()) {
String word = s.nextLine();
wordVector.add(word);
}
in.close();
fin.close();
}
catch(IOException e) {
}
new Main();
}
}