(사진출처 : https://ko.depositphotos.com/62834167/stock-illustration-baseball-game.html)
숫자 야구게임 (+GUI 기능추가)
2018/05/19 - [Java/Made of Java Game] - Baseball game program
기능 구현
1. JFrame를 기반으로 이전 포스팅 내용인 숫자 야구게임을 구현하였습니다.
2. 콘솔창을 대신하여 TextArea에 입출력 문제를 해결하였습니다.
3. 게임시작 버튼을 누르기 전에 "정답확인 및 텍스트창 지우기"기능을 선택 불가로 제어하였습니다.
4. 텍스트필드를 분할하여 입력값을 따로 받을 수 있습니다.
- 이전 : String형식으로 "-" 토큰을 잘라서 배열에 넣어 사용
- 이후 : String을 분할 & 검사의 로직이 없어짐
5. TextField가 순차적으로 입력을 받아야 다음 필드에 입력을 받을 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | package baseballGame_02; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.BevelBorder; public class baseballGameClass extends JFrame implements ActionListener { // GUI Variable JPanel pl_textView, pl_ta, pl_tf; JPanel pl_btView, pl_bt1, pl_bt2; JButton bt_start, bt_clear, bt_answer, bt_addFunc, bt_exit; JTextArea ta; JScrollPane scr; JLabel lb1, lb2, lb3; JTextField tf1, tf2, tf3; // Game Variable private boolean swit[]; private int hideNums[]; private int userNums[]; private int chance, round, strike, ball; // Initialize public void init() { chance = 10; round = 0; hideNums = new int[3]; userNums = new int[3]; for (int i = 0; i < userNums.length; i++) { userNums[i] = -1; } swit = new boolean[10]; for (int i = 0; i < swit.length; i++) { swit[i] = false; } while (true) { int i = 0; while (i < hideNums.length) { int temp = (int) (Math.random() * 10); if (swit[temp] == false) { swit[temp] = true; hideNums[i] = temp + 1; i++; } else { continue; } } break; } } public baseballGameClass() { super("Number Baseball Game ver.02"); // TextView pl_textView = new JPanel(); pl_textView.setLayout(new BorderLayout()); pl_ta = new JPanel(); pl_ta.setLayout(new BorderLayout(5, 5)); pl_ta.setBackground(Color.ORANGE); ta = new JTextArea("# Baseball Game!!" + "\n" + "- 게임시작을 눌러주세요" + "\n"); ta.setFont(new Font("Gothic", Font.BOLD, 20)); ta.setEditable(false); scr = new JScrollPane(ta); scr.setBorder(new BevelBorder(BevelBorder.LOWERED)); pl_ta.add("Center", scr); pl_ta.add("North", new JLabel()); pl_ta.add("East", new JLabel()); pl_ta.add("West", new JLabel()); pl_textView.add("Center", pl_ta); pl_tf = new JPanel(); pl_tf.setLayout(new FlowLayout()); pl_tf.setBackground(Color.ORANGE); lb1 = new JLabel("1번 공격 "); pl_tf.add(lb1); tf1 = new JTextField(5); tf1.setEditable(false); // pl_tf.add(tf1); lb2 = new JLabel("2번 공격 "); pl_tf.add(lb2); tf2 = new JTextField(5); tf2.setEditable(false); // pl_tf.add(tf2); lb3 = new JLabel("3번 공격 "); pl_tf.add(lb3); tf3 = new JTextField(5); tf3.setEditable(false); // pl_tf.add(tf3); pl_textView.add("South", pl_tf); pl_btView = new JPanel(); pl_btView.setLayout(new GridLayout(1, 3)); bt_start = new JButton("Game Start"); pl_btView.add(bt_start); pl_bt1 = new JPanel(); pl_bt1.setLayout(new GridLayout(2, 1)); bt_clear = new JButton("Reset"); bt_clear.setEnabled(false); pl_bt1.add(bt_clear); bt_answer = new JButton("Answer"); bt_answer.setEnabled(false); pl_bt1.add(bt_answer); pl_btView.add(pl_bt1); pl_bt2 = new JPanel(); pl_bt2.setLayout(new GridLayout(2, 1)); bt_addFunc = new JButton("AddFunc"); pl_bt2.add(bt_addFunc); bt_exit = new JButton("Exit"); pl_bt2.add(bt_exit); pl_btView.add(pl_bt2); setLayout(new BorderLayout()); setBackground(Color.LIGHT_GRAY); add("Center", pl_textView); add("South", pl_btView); setVisible(true); setBounds(720, 290, 480, 500); setDefaultCloseOperation(EXIT_ON_CLOSE); listener(); } // Listener public void listener() { tf1.addActionListener(this); tf2.addActionListener(this); tf3.addActionListener(this); bt_start.addActionListener(this); bt_clear.addActionListener(this); bt_answer.addActionListener(this); bt_addFunc.addActionListener(this); bt_exit.addActionListener(this); } // Check ASCII Code public boolean check(String str) { boolean check = true; if (str.length() < 1 || str.length() > 2) { check = false; } for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if ((int) c < 48 || (int) c > 57) { JOptionPane.showMessageDialog(null, "1~10 사이의 수를 입력하시오"); check = false; } } return check; } @Override public void actionPerformed(ActionEvent e) { Object obj = (Object) e.getSource(); if (obj == bt_start) { init(); for (int i = 0; i < swit.length; i++) { swit[i] = false; } tf1.setEditable(true); // 1번 공격 열어주기 bt_clear.setEnabled(true); bt_answer.setEnabled(true); bt_start.setEnabled(false); ta.append(" # Game Start !!!" + "\n"); ta.append(" # 1번칸에 공격할 수를 입력하시오 (1~10)" + "\n"); } else if (obj == tf1) { if (tf1.equals("")) { return; } String temp = tf1.getText(); if (check(temp)) { ta.append(" # 2번칸에 공격할 수를 입력하시오 (1~10)" + "\n"); tf2.setEditable(true); } else { tf1.setText(""); } } else if (obj == tf2) { if (tf2.equals("")) { return; } if (tf1.getText().equals(tf2.getText())) { JOptionPane.showMessageDialog(this, "중복되지 않는 숫자를 입력해주세요"); tf2.setText(""); return; } String temp = tf2.getText(); if (check(temp)) { ta.append(" # 3번칸에 공격할 수를 입력하시오 (1~10)" + "\n"); tf3.setEditable(true); } else { tf2.setText(""); } } else if (obj == tf3) { if (tf3.equals("")) { return; } if (tf1.getText().equals(tf3.getText())||tf2.getText().equals(tf3.getText())) { JOptionPane.showMessageDialog(this, "중복되지 않는 숫자를 입력해주세요"); tf3.setText(""); return; } String temp = tf3.getText(); if (check(temp)) { userNums[0] = Integer.parseInt(tf1.getText()); userNums[1] = Integer.parseInt(tf2.getText()); userNums[2] = Integer.parseInt(tf3.getText()); strike = 0; ball = 0; String userNumber = "공격 : [" + tf1.getText() + "]" + "[" + tf2.getText() + "]" + "[" + tf3.getText() + "]"; ta.append(userNumber + "\n"); tf1.setText(""); tf2.setText(""); tf3.setText(""); // strike for (int i = 0; i < hideNums.length; i++) { if (hideNums[i] == userNums[i]) { strike++; } } // ball for (int i = 0; i < hideNums.length; i++) { for (int j = 0; j < userNums.length; j++) { if (hideNums[i] == userNums[j] && i != j) { ball++; } } } if (strike == hideNums.length) { ta.append(" 축하합니다!! " + "\n" + (chance - round) + " 기회를 남기고" + "\n" + "타자를 " + hideNums.length + "strike으로 이겼습니다!! "); return; } else { round++; ta.append((chance - round) + " 기회가 남았습니다" + "\n" + " << " + strike + " Strike \t" + ball + " Ball !!! >>" + "\n"); } if (round == chance) { ta.append(" Game Over!! 모든 기회를 소진하였습니다. " + "\n"); } }else { tf3.setText(""); return; } } else if (obj == bt_clear) { new baseballGameClass(); this.dispose(); } else if (obj == bt_answer) { String answer = "[" + hideNums[0] + "]" + "[" + hideNums[1] + "]" + "[" + hideNums[2] + "]"; ta.append("정답은 : " + answer + " 입니다." + "\n"); } else if (obj == bt_addFunc) { JOptionPane.showMessageDialog(null, "추가기능입니다"); } else if (obj == bt_exit) { System.exit(0); } } public static void main(String[] args) { new baseballGameClass(); } } | cs |
..
..
'오랜된 포스팅 > Java' 카테고리의 다른 글
Chat_Application_TCP_Server(+exe file) (0) | 2018.06.17 |
---|---|
Baseball member management project(+Singleton Func) (0) | 2018.06.10 |
이클립스 한글 깨짐 방지 환경설정 (1) | 2018.06.07 |
Lotto program ver_01 (0) | 2018.06.03 |
List Management Functions in java (0) | 2018.06.01 |