/*
-랜덤으로 1에서 100 까지의 숫자를 고른 뒤 플레이어가 그 숫자를 맞추게 함
-자신이 예상한 숫자를 textfield에다가 입력
-10번 안에 맞추어야 하고 그러지 못하면 게임에서 진다
-게임이 끝나면 다시 메인 화면으로 돌아간다.
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame{
ImageIcon updown= new ImageIcon("arrows.png");
ImageIcon up= new ImageIcon("up-arrow.png");
ImageIcon down= new ImageIcon("down-arrow.png");
JLabel MainImage= new JLabel(updown);
JButton startB = new JButton("Game Start",updown);
JLabel MainL= new JLabel("UP AND DOWN GAME");
JTextField gameT= new JTextField(10);
JLabel gameL= new JLabel("1~100 숫자를 입력하시오:");
JLabel gameUD= new JLabel();
Container c= getContentPane();
String a;
int t, c1=0;
JLabel cL= new JLabel("남은 횟수: 15");
int flag=2;
JButton finish = new JButton("메인 화면으로 돌아가기");
JLabel wl= new JLabel();
int com;
public Main() {
setTitle("Up and Down Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(null);
c.setBackground(Color.YELLOW);
startB.setSize(200,200);
startB.setLocation(200,200);
startB.addActionListener(new myActionListener());
c.add(startB);
Font font= new Font("UP AND DOWN GAME",Font.BOLD,40);
MainL.setFont(font);
MainL.setSize(500, 100);
MainL.setLocation(90,100);
c.add(MainL);
gameT.setLocation(246,40);
gameT.setSize(200,30);
gameT.setVisible(false);
gameT.addActionListener(new myActionListener1());
c.add(gameT);
Font font1= new Font("UP AND DOWN GAME",Font.BOLD,13);
gameL.setFont(font1);
gameL.setLocation(90,5);
gameL.setSize(500,100);
gameL.setVisible(false);
c.add(gameL);
Font font2= new Font("UP AND DOWN GAME",Font.BOLD,13);
cL.setFont(font2);
cL.setLocation(500,-35);
cL.setSize(500,100);
cL.setVisible(false);
c.add(cL);
Font font3= new Font("UP AND DOWN GAME",Font.BOLD,60);
gameUD.setFont(font3);
gameUD.setLocation(220,250);
gameUD.setSize(500,100);
c.add(gameUD);
Font font4= new Font("UP AND DOWN GAME",Font.BOLD,30);
wl.setFont(font4);
wl.setLocation(60,230);;
wl.setSize(500,100);;
c.add(wl);
wl.setVisible(false);
finish.setLocation(350,480);
finish.setSize(200,70);
c.add(finish);
finish.setVisible(false);
finish.addActionListener(new myActionListener2());
setSize(600,600);
setVisible(true);
}
class myActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
com=(int)(Math.random()*100);
startB.setVisible(false);
MainL.setVisible(false);
gameT.setVisible(true);
gameL.setVisible(true);
cL.setVisible(true);
System.out.println(com);
}
}
class myActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
a=e.getActionCommand();
t=Integer.parseInt(a);
c1++;
run();
}
}
class myActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
StartScreen();
}
}
public void StartScreen() {
finish.setVisible(false);
wl.setVisible(false);
wl.setText("");
c1=0;
startB.setVisible(true);
gameT.setText("");
MainL.setVisible(true);
}
public void run() {
cL.setText("남은 횟수: "+Integer.toString(15-c1));
if(t>com) {
gameUD.setText("DOWN");
gameUD.setForeground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
}
if(t<com)
{
gameUD.setText("UP");
gameUD.setForeground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
}
if(t==com&&c1<11) {
flag=1;
result();
}
if(c1>14) {
flag=0;
result();
}
}
public void result() {
gameT.setVisible(false);
gameL.setVisible(false);
cL.setVisible(false);
gameUD.setText("");
wl.setVisible(true);
finish.setVisible(true);
if(flag==1) {
wl.setText("축하합니다 "+Integer.toString(c1)+"번 만에 맞추셨습니다");
}
if(flag==0) {
wl.setText("숫자는 "+Integer.toString(com)+" 였습니다");
}
}
public static void main(String[] args) {
new Main();
}
}



