-
Notifications
You must be signed in to change notification settings - Fork 3
Jyj1289 #1
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
base: jyj1289
Are you sure you want to change the base?
Jyj1289 #1
Changes from 16 commits
d24a3ce
86c0923
35584e3
85e75c3
46577a0
90d38d5
f0e8a65
93f5be1
32c3fe1
7e0c692
599bca9
20436ef
11b202f
dc46d38
d790e2d
ebf0a55
b06610e
8a0bce7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class Attacker extends SoccerPlayer{ | ||
| Attacker(String name){ | ||
| super(name); | ||
| } | ||
|
|
||
| @Override | ||
| public void shoot(Keeper keeper){ | ||
| Random random = new Random(); | ||
|
|
||
| int shoot1 = random.nextInt(11); | ||
| int shoot2 = random.nextInt(11); | ||
|
|
||
| if (keeper.block(shoot1) || keeper.block(shoot2)){ | ||
| goal(); | ||
| } | ||
|
|
||
| printScore(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class Keeper { | ||
| private String name; | ||
| private int keep_count = 3; | ||
| Keeper(String name){ | ||
| this.name = name; | ||
| } | ||
|
|
||
| public boolean block(int shoot){ | ||
| Random random = new Random(); | ||
| int blockShoot = random.nextInt(5) + 3; | ||
| if (shoot >= 2 && shoot <= 7 && keep_count > 0){ | ||
| keep_count -= 1; | ||
| return shoot != blockShoot; | ||
| } | ||
| if (shoot >= 2 && shoot <= 8){ | ||
|
Member
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. 이 코드의 이유가 있을까요? |
||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class Midfielder extends SoccerPlayer { | ||
| Midfielder(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| public void shoot(Keeper keeper) { | ||
| Random random = new Random(); | ||
|
|
||
| int shoot = random.nextInt(11); | ||
|
|
||
| boolean shootBool = keeper.block(shoot); | ||
|
|
||
| if (shootBool){ | ||
|
Member
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. 이럴떄는 변수에 할당을 안하고 사용할 수 있어요! |
||
| goal(); | ||
| } | ||
|
|
||
| printScore(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,73 @@ | ||
| import javax.accessibility.AccessibleTable; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class SoccerGameApplication { | ||
|
Contributor
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 static void main(String[] args) { | ||
| List<String> names = inputNames("공격수"); | ||
| Attacker attacker1 = new Attacker(names.get(0)); | ||
| Attacker attacker2 = new Attacker(names.get(1)); | ||
|
|
||
| names = inputNames("미드필더"); | ||
| Midfielder midfielder1 = new Midfielder(names.get(0)); | ||
| Midfielder midfielder2 = new Midfielder(names.get(1)); | ||
|
|
||
| names = inputNames("골키퍼"); | ||
| Keeper keeper = new Keeper(names.get(0)); | ||
|
|
||
| int matchPoint = inputMatchPoint(); | ||
| boolean isNotGameDone = true; | ||
|
|
||
| int count = 1; | ||
|
|
||
| List<SoccerPlayer> players = new ArrayList<SoccerPlayer>(); | ||
| players.add(attacker1); | ||
| players.add(attacker2); | ||
| players.add(midfielder1); | ||
| players.add(midfielder2); | ||
|
|
||
| while(isNotGameDone){ | ||
| System.out.printf("\n%d번째 슈팅결과", count); | ||
| printResult(players, keeper); | ||
| count++; | ||
| isNotGameDone = check(attacker1, matchPoint) && check(attacker2, matchPoint) | ||
| && check(midfielder1, matchPoint) && check(midfielder2, matchPoint); | ||
|
Member
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. 이런걸 List로 바꿔서 메소드 분리하면 좋을 것 같습니다 |
||
| } | ||
|
|
||
| } | ||
|
|
||
| static void printResult(List<SoccerPlayer> players, Keeper keeper){ | ||
| for (SoccerPlayer soccer : players){ | ||
| System.out.printf("\n%s: ", soccer.getName()); | ||
| soccer.shoot(keeper); | ||
| } | ||
| } | ||
|
|
||
| static boolean check(SoccerPlayer players ,int matchPoint){ | ||
| if (players.getScore() == matchPoint){ | ||
| System.out.printf("\n\n승리자는 %s 입니다.", players.getName()); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static int inputMatchPoint(){ | ||
| Scanner sc = new Scanner(System.in); | ||
| System.out.print("매치포인트를 입력하세요 : "); | ||
|
|
||
| int matchPoint = sc.nextInt(); | ||
|
Member
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. matchPoint선언한 이유가 있나요? |
||
|
|
||
| return matchPoint; | ||
| } | ||
|
|
||
| static List<String> inputNames(String position){ | ||
| Scanner sc = new Scanner(System.in); | ||
| System.out.printf("%s의 이름을 입력하세요 : ", position); | ||
| List<String> names = Arrays.asList(sc.nextLine().split(", ")); | ||
|
|
||
| return names; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| public abstract class SoccerPlayer{ | ||
| private String name; | ||
| private int score; | ||
|
|
||
| SoccerPlayer(String name){ | ||
| this.name = name; | ||
| this.score = 0; | ||
| } | ||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public void goal(){ | ||
| this.score += 1; | ||
| } | ||
|
|
||
| abstract void shoot(Keeper keeper); | ||
|
|
||
| public int getScore() { return score; } | ||
|
|
||
| public void printScore(){ | ||
| for(int i = 0; i < score; i++){ | ||
| System.out.print("-"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
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.
keep_count가 왜 필요한가요?