Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
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
21 changes: 21 additions & 0 deletions src/main/java/Attacker.java
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();
}

print(getScore());
}
}
24 changes: 24 additions & 0 deletions src/main/java/Keeper.java
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){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

keep_count가 왜 필요한가요?

keep_count -= 1;
return shoot != blockShoot;
}
if (shoot >= 2 && shoot <= 8){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

이 코드의 이유가 있을까요?

return true;
}
return false;
}


}
21 changes: 21 additions & 0 deletions src/main/java/Midfielder.java
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){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

이럴떄는 변수에 할당을 안하고 사용할 수 있어요!

goal();
}

print(getScore());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

공격수도 그렇고 print(getScore())라는 로직을 가지고 있는데, printScore()메서드를 만드는 것이 더 효율적이고, 깔끔한 것 같습니다.

}
}
72 changes: 72 additions & 0 deletions src/main/java/SoccerGameApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,77 @@
import javax.accessibility.AccessibleTable;
import java.util.Scanner;

public class SoccerGameApplication {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이 클래스의 책임이 너무 많은 것 같아요. 작게는 문자를 출력하는 것부터, 크게는 핵심 로직까지 다 이 클래스가 책임지고 있습니다.

public static void main(String[] args) {
String[] names = inputNames("공격수");
Attacker attacker1 = new Attacker(names[0]);
Attacker attacker2 = new Attacker(names[1]);

names = inputNames("미드필더");
Midfielder midfielder1 = new Midfielder(names[0]);
Midfielder midfielder2 = new Midfielder(names[1]);

names = inputNames("골키퍼");
Keeper keeper = new Keeper(names[0]);

int matchPoint = inputMatchPoint();
boolean bool = true;

int count = 1;

SoccerPlayer[] players = new SoccerPlayer[] {attacker1, attacker2, midfielder1, midfielder2};

while(bool){
System.out.printf("\n%d번째 슈팅결과", count);
printResult(players, keeper);
count++;
bool = check(attacker1, attacker2, midfielder1, midfielder2, matchPoint);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

List로 받아보세요!

}

}

static String[] inputNames(String position){
Scanner sc = new Scanner(System.in);
System.out.printf("%s의 이름을 입력하세요 : ", position);
String[] names = sc.nextLine().split(", ");

return names;
}

static int inputMatchPoint(){
Scanner sc = new Scanner(System.in);
System.out.print("매치포인트를 입력하세요 : ");

int matchPoint = sc.nextInt();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

matchPoint선언한 이유가 있나요?


return matchPoint;
}

static boolean check(Attacker attacker1, Attacker attacker2, Midfielder midfielder1, Midfielder midfielder2 ,int matchPoint){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Player배열을 사용하는 것이 어떨까요?

if (attacker1.getScore() == matchPoint){
System.out.printf("\n\n승리자는 %s 입니다.", attacker1.getName());
return false;
}
if (attacker2.getScore() == matchPoint){
System.out.printf("\n\n승리자는 %s 입니다.", attacker2.getName());
return false;
}
if (midfielder1.getScore() == matchPoint){
System.out.printf("\n\n승리자는 %s 입니다.", midfielder1.getName());
return false;
}
if (midfielder2.getScore() == matchPoint){
System.out.printf("\n\n승리자는 %s 입니다.", midfielder2.getName());
return false;
}
return true;
}

static void printResult(SoccerPlayer[] players, Keeper keeper){
for (SoccerPlayer soccer : players){
System.out.printf("\n%s: ", soccer.getName());
soccer.shoot(keeper);
}
}

}
27 changes: 27 additions & 0 deletions src/main/java/SoccerPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 print(int score){
for(int i = 0; i < score; i++){
System.out.print("-");
}
}

}