[Java] 야구 게임

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;


import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.KeyStroke;


public class Baseball extends JFrame implements ActionListener{

JMenuBar menubar;

JMenu game, help;

JMenuItem newGame, exit, rule, info;

JPanel panel;

JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnInsert, btnCancel;

JTextArea jta;

String total="";

int cnt = 1;

int count = 1;

int com[], user[];

int strike, ball;

public Baseball() {

super("재미있는 야구게임");

user = new int[3];

menubar = new JMenuBar();

game = new JMenu("게임(G)");

game.setMnemonic('g'); // setMnemonic 단축키 설정

help = new JMenu("도움말(H)");

help.setMnemonic('h');

newGame = new JMenuItem("새게임(N)",'n');

newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_MASK));

newGame.addActionListener(this);

exit = new JMenuItem("종료(X)",'x');

exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.CTRL_MASK));

exit.addActionListener(this);

rule = new JMenuItem("게임 규칙(R)",'r');

rule.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,KeyEvent.CTRL_MASK));

rule.addActionListener(this);

info = new JMenuItem("정보(I)",'i');

info.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,KeyEvent.CTRL_MASK));

info.addActionListener(this);

getContentPane().add(menubar, BorderLayout.NORTH);

menubar.add(game);

menubar.add(help);

game.add(newGame);

game.addSeparator();

game.add(exit);

help.add(rule);

help.add(info);

panel = new JPanel();

panel.setLayout(new GridLayout(2,6));

btn0 = new JButton("0");

btn0.addActionListener(this);

panel.add(btn0);

btn1 = new JButton("1");

btn1.addActionListener(this);

panel.add(btn1);

btn2 = new JButton("2");

btn2.addActionListener(this);

panel.add(btn2);

btn3 = new JButton("3");

btn3.addActionListener(this);

panel.add(btn3);

btn4 = new JButton("4");

btn4.addActionListener(this);

panel.add(btn4);

btn5 = new JButton("5");

btn5.addActionListener(this);

panel.add(btn5);

btn6 = new JButton("6");

btn6.addActionListener(this);

panel.add(btn6);

btn7 = new JButton("7");

btn7.addActionListener(this);

panel.add(btn7);

btn8 = new JButton("8");

btn8.addActionListener(this);

panel.add(btn8);

btn9 = new JButton("9");

btn9.addActionListener(this);

panel.add(btn9);

btnInsert = new JButton("입력");

btnInsert.addActionListener(this);

panel.add(btnInsert);

btnCancel = new JButton("취소");

btnCancel.addActionListener(this);

panel.add(btnCancel);

getContentPane().add(panel, BorderLayout.SOUTH);

jta = new JTextArea();

jta.setEditable(false);

getContentPane().add(jta, BorderLayout.CENTER);

setSize(450,450);

setVisible(true);

Dimension dim = getToolkit().getScreenSize();

setLocation(dim.width/2 - 225, dim.height/2 - 225);

setDefaultCloseOperation(EXIT_ON_CLOSE);

computerRandomValue();

}

public void computerRandomValue() {

com = new int[3];

com[0] = (int)(Math.random()*10);

com[1] = (int)(Math.random()*10);

com[2] = (int)(Math.random()*10);

while(com[0]==com[1]) {

com[1] = (int)(Math.random()*10);

}

while(com[0]==com[2] || com[1]==com[2]) {

com[2] = (int)(Math.random()*10);

}

}


@Override

public void actionPerformed(ActionEvent ae) {

Object obj = ae.getSource();

if(obj==exit) { //종료

int i = JOptionPane.showConfirmDialog

(this, "게임을 종료하시겠습니까?", "종료", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

if(i==JOptionPane.YES_OPTION) {

dispose();

System.exit(0);

}

}else if(obj==newGame) { //새게임

total = "";

cnt = 1;

count = 1;

jta.setText(total);

computerRandomValue();

}else if(obj==rule) { // 게임 룰

JFrame frame = new JFrame("게임 룰 설명");

JPanel panel = new JPanel();

panel.setBackground(new Color(255, 255, 230));

String rule = "<html>" + "<body ><p align=center><font size=+2>게임 규칙</font>" +

"</p>\n" + "<font face=굴림 size=4>" +

"<p>&nbsp;</p>" + "<p align=left>" +

"1. 컴퓨터가 임의의 숫자 3개를 생성합니다.<br><br>" +

"2. 숫자 버튼을 3개 click 후 입력을 click.<br><br>" + 

"3. 숫자가 같은 것이 같은 자리에 있을 경우 Strike.<br><br>" + 

"4. 숫자은 같으나 자리가 다를 경우 Ball.<br><br>" + 

"5. Strike가 3개면 승리.<br><br>" + 

"6. 기회는 10번입니다.<br><br></p></font></body></html>";

JLabel label = new JLabel(rule);

frame.getContentPane().add(panel, BorderLayout.CENTER);

panel.add(label);

Dimension dim = getToolkit().getScreenSize();

frame.setLocation(dim.width/2 - 200, dim.height/2 - 150);

frame.setSize(400,300);

frame.setVisible(true);

}else if(obj==info) { //게임정보

JDialog dialog = new JDialog(this,"정보",true);

JPanel panel1 = new JPanel();

panel1.setBackground(new Color(231,236,254));

String information = "<html>" + "<body ><p align=center><font size=+10>야구게임~!</font>" +

"&nbsp;&nbsp;\nv1.0</p>\n" + "<font face=굴림>" +

"<p align=center>" + "&nbsp;</p>" + 

"<p>&nbsp;</p>" + "<p align=center>" +

"만든이 : &nbsp; &nbsp;LSH</p>" +

"<p align=center>" + "제작일 : 2017.11.20</p></font></body></html>";

JLabel label = new JLabel(information,JLabel.CENTER);

dialog.getContentPane().add(panel1,"Center");

panel1.add(label);

Dimension dim = getToolkit().getScreenSize();

dialog.setLocation(dim.width/2-150, dim.height/2-150);

dialog.setSize(300,300);

dialog.setVisible(true);

dialog.setResizable(false);

}else if(obj==btn0) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn0.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn1) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn1.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn2) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn2.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn3) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn3.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn4) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn4.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn5) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn5.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn6) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn6.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn7) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn7.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn8) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn8.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btn9) {

if(cnt>3) {

JOptionPane.showMessageDialog

(this, "숫자는 3개만 입력해주세요","Grame Rule",JOptionPane.INFORMATION_MESSAGE);

total = "";

cnt = 1;

}else {

String a = btn9.getText();

user[cnt-1] = Integer.parseInt(a);

total = total+a;

cnt++;

}

}else if(obj==btnInsert) {

if(count<11) {

if(cnt-1==3) {

if(user[0]==user[1] || user[0]==user[2] || user[1]==user[2]) {

JOptionPane.showMessageDialog(this, "같은 숫자를 눌렀습니다.","Game Rule",JOptionPane.INFORMATION_MESSAGE);

total="";

cnt=1;

}else {

compare();

jta.append(total+"========================>>"+strike+"STRIKE"+ball+"BALL"+"입니다."+"\n");

total="";

cnt = 1;

strike = 0;

ball = 0;

count++;

}

}else {

JOptionPane.showMessageDialog(this, "숫자를 3개 누른 후에 입력을 눌러주세요","Game Rule",JOptionPane.INFORMATION_MESSAGE);

cnt = 1;

total="";

}

}else {

JOptionPane.showInternalMessageDialog(this, "10번의 기회를 다 사용하셧습니다.\n 정답은"+com[0]+com[1]+com[2]+"\n새게임을 눌러주세요","Game Rule", JOptionPane.INFORMATION_MESSAGE);

}

}else if(obj == btnCancel) {

total="";

cnt=1;

}

}

public void compare() {

for(int i=0; i<com.length;i++) {

for(int j=0; j<user.length;j++) {

if(com[i]==user[j]) {

if(i==j) {

strike++;

}else {

ball++;

}

}

}

}

if(strike==3) {

JOptionPane.showMessageDialog(this, "축하합니다.\n 컴퓨터에게 이겼습니다~!!","Game Rule",JOptionPane.INFORMATION_MESSAGE);

}

}

public static void main(String[] args) {

new Baseball();

}

}



'BACKEND > Java' 카테고리의 다른 글

[Java] 소수점 원하는 자리 수까지 표시하는 방법  (0) 2020.11.09
JAD Decompiler 설치 및 사용방법  (0) 2019.05.13
[Java] Network  (0) 2017.11.08
[Java] Swing  (0) 2017.11.07
[Java] Window Programming - <Menu>  (0) 2017.11.07

댓글

Designed by JB FACTORY