Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java baseball game 김태근 #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified java_baseball_game/out/production/classes/com/taegeun/Main.class
Binary file not shown.
Binary file modified java_baseball_game/out/production/classes/com/taegeun/Player.class
Binary file not shown.
16 changes: 8 additions & 8 deletions java_baseball_game/src/main/java/com/taegeun/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import java.util.Random;

public class Computer {
List<Integer> comNum = new ArrayList<>();
public List<Integer> randNum() {
List<Integer> computerNumber = new ArrayList<>();

public List<Integer> randomNumber() {
int num;
Random random = new Random();

while (true) {
if (comNum.size() == 3) break;
num = random.nextInt(9)+1;
if (!comNum.contains(num)){
comNum.add(num);
while (computerNumber.size() != 3) {
num = random.nextInt(9) + 1;
if (!computerNumber.contains(num)) {
computerNumber.add(num);
}
}
return comNum;
return computerNumber;
}
}
43 changes: 21 additions & 22 deletions java_baseball_game/src/main/java/com/taegeun/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,51 @@
import java.util.List;

public class GameEngine {
private List<Integer> playerNumber = new ArrayList<>();
private List<Integer> computerNumber = new ArrayList<>();

private List<Integer> playerNum = new ArrayList<>();
private List<Integer> comNum = new ArrayList<>();
public void setComNum(List<Integer> comNum) {
this.comNum = comNum;
public void setComNum(List<Integer> n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 축약어인 메서드들의 이름 수정을 안하신 것 같아요!
IntelliJ 에서 macOS는 ⇧F6 , Windows/Linux에서는 Shift+F6을 사용하여 이름 변경 리팩토링이 안전하게 일괄적으로 가능합니다!
IntelliJ에는 정말 편리한 단축키 & 기능이 많으니 사용해보시고 익히시는 것도 좋은 것 같아요! 😊 (저도 아직 무지성 Alt + Enter만 하지만...😂)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗,,, 다 살펴보았다고 생각했는데,, 꼼꼼한 리뷰 감사합니다. 매의 눈이십니다😯
편의기능 추천도 감사합니다!

this.computerNumber = n;
}
public void setPlayerNum(List<Integer> playerNum){
this.playerNum = playerNum;

public void setPlayerNum(List<Integer> n) {
this.playerNumber = n;
}

public int checkStrike() {
int total=0;
for (int i=0; i<playerNum.size(); i++){
if (playerNum.get(i) == comNum.get(i)){
int total = 0;
for (int i = 0; i < playerNumber.size(); i++) {
if (playerNumber.get(i) == computerNumber.get(i)) {
total++;
}
}
return total;
}

public int checkBall() {
int total=0;
for (int i=0; i<playerNum.size(); i++){
if (playerNum.get(i) != comNum.get(i) && comNum.contains(playerNum.get(i))){
int total = 0;
for (int i = 0; i < playerNumber.size(); i++) {
if (playerNumber.get(i) != computerNumber.get(i) && computerNumber.contains(playerNumber.get(i))) {
total++;
}
}
return total;
}
public boolean continueGame(){
if (checkStrike()!=3){
return true;
}
else{
return false;
}

public boolean continueGame() {
return checkStrike() != 3;
}

public void printScore() {
int ball = checkBall();
int strike = checkStrike();
if (ball !=0){
if (ball != 0) {
System.out.print(ball + "볼 ");
}
if (strike !=0){
if (strike != 0) {
System.out.print(strike + "스트라이크");
}
if (ball==0 && strike==0){
if (ball == 0 && strike == 0) {
System.out.print("낫싱");
}
System.out.println();
Expand Down
40 changes: 22 additions & 18 deletions java_baseball_game/src/main/java/com/taegeun/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,56 @@
import java.util.Scanner;

public class GameManager {
private List<Integer> playerNum = new ArrayList<>();
private List<Integer> comNum = new ArrayList<>();
public void generateGame(){
private List<Integer> playerNumber = new ArrayList<>();
private List<Integer> computerNumber = new ArrayList<>();

public void generateGame() {
Computer com = new Computer();
// 컴퓨터 숫자 생성.
comNum = com.randNum();
computerNumber = com.randomNumber();
}
public void gameStart(){

public void gameStart() {
GameEngine gameEngine = new GameEngine();
gameEngine.setComNum(comNum);
gameEngine.setComNum(computerNumber);

while(true){
while (true) {
//플레이어 숫자 입력
Player player = new Player();
try{
playerNum = player.inputNum();
}catch (Exception e){
try {
playerNumber = player.inputNumber();
} catch (Exception e) {
System.out.println("잘못된 입력입니다.");
System.exit(0);
}

gameEngine.setPlayerNum(playerNum);
gameEngine.setPlayerNum(playerNumber);
gameEngine.printScore();
if (!gameEngine.continueGame()) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
break;
}
}
}
public boolean resumeGame(){

public boolean isResumeGame() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();

switch (choice) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 switch 문을 사용하신 이유가 있을까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자료구조 수업에서 switch구문이 확장성면에서 좋다고해서 수정해보았습니다. 하하😋

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각해보니, 확장의 여지가 없는 것 같기도 합니다🥲

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조건이 여러가지인 경우에는, (아니면 조건에 따라서 다른 종류의 객체를 반환해야 할때...) switch 문이 여러개의 if-else문 보다 깔끔하고 확장성 있지만, 이처럼 조건식이 몇 가지 없을 때는 단순하게 표현하는 것도 좋다고 생각합니다!
물론 이건 개인적인 성향이니 참고만 하시면 좋을 것 같아요!😊

case 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 경우에는 Enum을 활용해서 코드를 작성하시면 좋습니다! 딱 읽었을 때, 1 이 어떤 의미인지 한눈에 파악하기 어렵기 때문입니다!
자주 사용하는 중요한 상수들은 Enum으로 표현해보세요! ㅎㅎ

public enum GameSignal {
  RESTART(1), END(2);

  int signal;

  GameSignal(int signal) {
    this.signal = signal;
  }
  // add getter...
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하...! enum을 통해서 와... 신기합니다
감사합니다 수정하겠습니다.

return true;
default:
default:
return false;
}

}
public List<Integer> getPlayerNum(){
return playerNum;

public List<Integer> getPlayerNum() {
return playerNumber;
}
public List<Integer> getComNum(){
return comNum;

public List<Integer> getComNum() {
return computerNumber;
}
}
26 changes: 7 additions & 19 deletions java_baseball_game/src/main/java/com/taegeun/Main.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
package com.taegeun;

public class Main {
public static void main(String[] args){
while (true){
GameManager gameManager = new GameManager();

public static void main(String[] args) {
GameManager gameManager = new GameManager();
do {
gameManager.generateGame();
System.out.println(gameManager.getComNum());
gameManager.gameStart();

if (!gameManager.resumeGame()){
break;
}

}







} while (gameManager.isResumeGame());
}
}
}
//computer 숫자를 알고 싶을 때...
//System.out.println(gameManager.getComNum());
17 changes: 8 additions & 9 deletions java_baseball_game/src/main/java/com/taegeun/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
import java.util.Scanner;

public class Player {
List<Integer> myNum = new ArrayList<>();
public List<Integer> inputNum(){
List<Integer> playerNumber = new ArrayList<>();

public List<Integer> inputNumber() {
int num;
Scanner sc = new Scanner(System.in);
System.out.print("숫자를 입력해주세요 : ");
num = sc.nextInt();

myNum.add((num/100)%10);
myNum.add((num/10)%10);
myNum.add(num%10);

if (myNum.get(0) == 0) {
playerNumber.add((num / 100) % 10);
playerNumber.add((num / 10) % 10);
playerNumber.add(num % 10);

if (playerNumber.get(0) == 0) {
throw new IllegalArgumentException("3자리 수를 입력하세요!");
}

return myNum;
return playerNumber;
}
}