-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "java_baseball_game_\uAE40\uD0DC\uADFC"
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
package com.taegeun; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class Computer { | ||
List<Integer> comNum = new ArrayList<>(); | ||
public List<Integer> randNum() { | ||
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); | ||
} | ||
} | ||
return comNum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.taegeun; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class GameEngine { | ||
|
||
private List<Integer> playerNum = new ArrayList<>(); | ||
private List<Integer> comNum = new ArrayList<>(); | ||
public void setComNum(List<Integer> comNum) { | ||
this.comNum = comNum; | ||
} | ||
public void setPlayerNum(List<Integer> playerNum){ | ||
this.playerNum = playerNum; | ||
} | ||
|
||
public int checkStrike() { | ||
int total=0; | ||
for (int i=0; i<playerNum.size(); i++){ | ||
if (playerNum.get(i) == comNum.get(i)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오우.. 링크까지 감사합니다 !! |
||
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))){ | ||
total++; | ||
} | ||
} | ||
return total; | ||
} | ||
public boolean continueGame(){ | ||
if (checkStrike()!=3){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
public boolean continueGame(){
return checkStrike() != 3;
} |
||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
public void printScore() { | ||
int ball = checkBall(); | ||
int strike = checkStrike(); | ||
if (ball !=0){ | ||
System.out.print(ball + "볼 "); | ||
} | ||
if (strike !=0){ | ||
System.out.print(strike + "스트라이크"); | ||
} | ||
if (ball==0 && strike==0){ | ||
System.out.print("낫싱"); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,56 @@ | ||
package com.taegeun; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class GameManager { | ||
Player player = new Player(); | ||
Computer com = new Computer(); | ||
private List<Integer> playerNum = new ArrayList<>(); | ||
private List<Integer> comNum = new ArrayList<>(); | ||
public void generateGame(){ | ||
Computer com = new Computer(); | ||
// 컴퓨터 숫자 생성. | ||
comNum = com.randNum(); | ||
} | ||
public void gameStart(){ | ||
GameEngine gameEngine = new GameEngine(); | ||
gameEngine.setComNum(comNum); | ||
|
||
while(true){ | ||
//플레이어 숫자 입력 | ||
Player player = new Player(); | ||
try{ | ||
playerNum = player.inputNum(); | ||
}catch (Exception e){ | ||
System.out.println("잘못된 입력입니다."); | ||
System.exit(0); | ||
} | ||
|
||
gameEngine.setPlayerNum(playerNum); | ||
gameEngine.printScore(); | ||
if (!gameEngine.continueGame()) { | ||
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
break; | ||
} | ||
} | ||
} | ||
public boolean resumeGame(){ | ||
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
Scanner sc = new Scanner(System.in); | ||
int choice = sc.nextInt(); | ||
|
||
switch (choice) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자료구조 수업에서 switch구문이 확장성면에서 좋다고해서 수정해보았습니다. 하하😋 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생각해보니, 확장의 여지가 없는 것 같기도 합니다🥲 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조건이 여러가지인 경우에는, (아니면 조건에 따라서 다른 종류의 객체를 반환해야 할때...) |
||
case 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 경우에는 public enum GameSignal {
RESTART(1), END(2);
int signal;
GameSignal(int signal) {
this.signal = signal;
}
// add getter...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하...! enum을 통해서 와... 신기합니다 |
||
return true; | ||
default: | ||
return false; | ||
} | ||
|
||
} | ||
public List<Integer> getPlayerNum(){ | ||
return playerNum; | ||
} | ||
public List<Integer> getComNum(){ | ||
return comNum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,24 @@ | |
|
||
public class Main { | ||
public static void main(String[] args){ | ||
GameManager gameManager = new GameManager(); | ||
gameManager.start(); | ||
while (true){ | ||
GameManager gameManager = new GameManager(); | ||
|
||
gameManager.generateGame(); | ||
System.out.println(gameManager.getComNum()); | ||
gameManager.gameStart(); | ||
|
||
if (!gameManager.resumeGame()){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
네이밍이 진짜 제일 힘든거 같습니다...😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드 네이밍에 조금 더 신경써야함을 크게 느꼈습니다...! 😨 |
||
break; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 소개해드렸던 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵!! 수정해서 반영하겠습니다.!! |
||
|
||
|
||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
package com.taegeun; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class Player { | ||
int myNum; | ||
public int inputNum(){ | ||
List<Integer> myNum = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 긴 이름에 대한 거부감이 있으시겠지만, 필드나 메서드 네이밍을 할 때는 축약 없이 하면 좋을 것 같습니다! |
||
public List<Integer> inputNum(){ | ||
int num; | ||
Scanner sc = new Scanner(System.in); | ||
System.out.print("숫자를 입력해주세요 : "); | ||
myNum = sc.nextInt(); | ||
num = sc.nextInt(); | ||
|
||
if (Integer.toString(myNum).length() != 3){ | ||
throw new IllegalArgumentException("3자리수가 아닙니다."); | ||
myNum.add((num/100)%10); | ||
myNum.add((num/10)%10); | ||
myNum.add(num%10); | ||
|
||
if (myNum.get(0) == 0) { | ||
|
||
throw new IllegalArgumentException("3자리 수를 입력하세요!"); | ||
} | ||
|
||
return myNum; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이처럼 종료 조건이 명확할 때는,
true
대신 종료 조건을 명시하는 것이 가능하지 않을까요?