본문 바로가기

OM

숫자 야구 게임

Baseball.java========================================================================================

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Baseball extends JApplet implements ActionListener
{
    int count;
    int key;
    int strike, ball;

    int[] usr= new int[3];
    int[] com= new int[3];

    boolean gameover= false;

    JLabel display;
    JTextArea history;

    JPanel numPanel;
    JButton[] buttons= new JButton[12];

    public void init()
    {
        Random r = new Random();
        com[0] = Math.abs(r.nextInt()%9) + 1;

        do{
            com[1]= Math.abs(r.nextInt() % 9) + 1;
        }while(com[1]==com[0]);

        do{
            com[2]= Math.abs(r.nextInt() % 9) + 1;
        }while(com[2]==com[0] || com[2]==com[1]);

        System.out.println(com[0] + ", " + com[1] + ", " + com[2]);

        count= 0;
        key= 0;
        usr[0]= usr[1]= usr[2]= 0;

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        display = new JLabel(" ", JLabel.RIGHT);
        contentPane.add(display, BorderLayout.NORTH);

        numPanel = new JPanel();
        numPanel.setLayout(new GridLayout(4, 3));

        for(int i=7; i>0; i-=3)
        {
          for(int j=0; j<3; j++)
          {
            buttons[i+j]= new JButton(String.valueOf(i+j));
            numPanel.add(buttons[i+j]);
          }
        }

        buttons[0]= new JButton("←");
        numPanel.add(buttons[0]);

        buttons[10]= new JButton("다시입력");
        numPanel.add(buttons[10]);

        buttons[11]= new JButton("입력완료");
        numPanel.add(buttons[11]);

        contentPane.add(numPanel, BorderLayout.CENTER);

        history = new JTextArea(10, 20);
        JScrollPane scroll = new JScrollPane(history);

        contentPane.add(scroll, BorderLayout.SOUTH);

        for(int i=0; i<12; i++)
        {
            buttons[i].addActionListener(this);
        }

    }

    public void actionPerformed(ActionEvent e)
    {
        if(gameover)
        {
            return;
        }

        if(e.getSource() == buttons[0])
        {
            if(key>0)
            {
                key--;
                display.setText(" ");

                for(int i=0; i<key; i++)
                {
                    display.setText(display.getText() + usr[i]);
                }
            }
        }
        else if(e.getSource() == buttons[10]){
            key = 0;
            usr[0] = usr[1] = usr[2] = 0;
            display.setText(" ");
        }
        else if(e.getSource() == buttons[11]){
            if(key == 3)
            {
                strike = ball = 0;

                if(usr[0]==com[0]) strike++;
                if(usr[1]==com[1]) strike++;
                if(usr[2]==com[2]) strike++;

                if(usr[0]==com[1]) ball++;
                if(usr[0]==com[2]) ball++;
                if(usr[1]==com[0]) ball++;
                if(usr[1]==com[2]) ball++;
                if(usr[2]==com[0]) ball++;
                if(usr[2]==com[1]) ball++;

                history.append(usr[0] + ", " + usr[1] + ", " + usr[2] +
                    " -> Strike : " + strike + " Ball : " + ball + "\n");

                count++;
                key = 0;
                usr[0] = usr[1] = usr[2] = 0;
                display.setText(" ");

                if(strike==3 || count==11)
                {
                    if(count<=2)
                    {
                        history.append("\n평가: 참 잘했어요!");
                    }
                    else if(count<=5){
                        history.append("\n평가: 잘했어요!");
                    }
                    else if(count<=9){
                        history.append("\n평가: 보통이네요!");
                    }
                    else{
                        history.append("\n평가: 분발하세요!");
                    }

                    gameover= true;
                    display.setText(" Game Over ! ");
                    history.append("\n\n<<<게임 끝>>>");
                }
            }
        }
        else{
            if(key<3)
            {
                char butVal= ((JButton) e.getSource()).getLabel().charAt(0);
                usr[key] = Integer.valueOf(String.valueOf(butVal)).intValue();

                boolean same = false;
               
                switch(key)
                {
                    case 2:
                        if(usr[key]==usr[1]) same= true;
                    case 1:
                        if(usr[key]==usr[0]) same= true;
                }

                if(!same)
                {
                    display.setText(display.getText() + butVal);
                    key++;
                }
            }
        }
    }
}

=====================================================================================================

'OM' 카테고리의 다른 글

생명게임  (0) 2008.05.05
Card Match 2  (0) 2008.05.04
원 그리기 (MouseListener)  (0) 2008.05.02
디지털 시계  (0) 2008.05.02
가위바위보 게임  (2) 2008.04.28
War zero  (0) 2008.04.14
동영상 플레이어 (FLVPlayback) 2  (0) 2008.04.10
동영상 플레이어 (FLVPlayback)  (0) 2008.04.10
3X3 Puz  (0) 2008.04.10