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();
JLabel h[] = new JLabel[6];
ImageIcon gameover = new ImageIcon("gameover.png");
JLabel gover = new JLabel(gameover);
JButton yes = new JButton("Yes");
JButton no = new JButton("No");
JPanel zone = new JPanel();
JLabel yn = new JLabel("Next word(y/n)");
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);
for(int i=0;i<5;i++){
h[i] = new JLabel(new ImageIcon("하트.png"));
c.add(h[i]);
h[i].setSize(32,32);
h[i].setLocation(750+32*i,10);
}
gover.setSize(950,700);
gover.setLocation(0,0);
tf.setFont(new Font("Arial", Font.PLAIN, 120));
tf.setForeground(Color.BLACK);
tf.setText("a");
tf.setSize(130,130);
tf.setLocation(400,300);
yes.setSize(120,120);
yes.setLocation(320,300);
no.setSize(120,120);
no.setLocation(470,300);
c.add(gover);
gover.setVisible(false);
c.add(tf);
c.add(zone);
}
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가 문자열 내부에서 같은 문자가 있다면 그 위치의 문자를 보여주도록 바꾸기
int isfind=0;
for(int i=0 ; i<rd_str.length() ; i++) {
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++;
isfind=1;
System.out.println("cor_cnt :"+cor_cnt);
}
}
else if(word[i].getText().equals(w+"")){
isfind=1;
break;
}
}
if(isfind==0) hcnt++;
for(int i=0 ; i<hcnt ; i++) {
h[i].setVisible(false);
if(hcnt==5) {
System.out.println("게임 종료");
gover.setVisible(true);
tf.setVisible(false);
}
}
// h5.setVisible(false);
// System.out.println("게임 종료");
// gover.setVisible(true);
// tf.setVisible(false);
//System.exit(0);
if(cor_cnt == rd_str.length()) {
System.out.println("성공함");
zone.removeAll(); //zone패널 내부의 컴포넌트 모두 삭제
zone.add(yn);
yn.setFont(new Font("Arial", Font.PLAIN, 80));
zone.repaint();
zone.revalidate();
c.remove(tf);
c.add(yes);
c.add(no);
//word[i].setText("next word(y/n)");
if(w == 'y') {
c = new Main();//다시시작
}
if(w == 'n') {
System.exit(0);
}
}
//단어를완성했는지?( 맞힌 문자 갯수==단어의길이)
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();
}
}
/*
0. 하트 코드 반복문과 배열을 사용해서 줄이기******완료
1. (다이얼로그)로그인기능 (개인별 점수 기록 기능)
2. 랭킹확인 버튼
3. 점수 기준 정하기 ( 연속으로 맞추면 콤보.. 추가점수)
*** 새로운 시도를 하기 전에 꼭!!! 백업하기*****(메일에)
4. 그 단어의 정답률 보여주기
5. 이외 아이디어 생각해서 추가하기
*/