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

한태영 과제 제출합니다. #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions soccer-game-middle/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions soccer-game-middle/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions soccer-game-middle/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions soccer-game-middle/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions soccer-game-middle/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Binary file added src/main/java/Attacker.class
Binary file not shown.
19 changes: 19 additions & 0 deletions src/main/java/Attacker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Random;

public class Attacker extends Player{ //Player 클래스 상속
Random random = new Random();
public Attacker(String name) {
super(name); //부모클래스로 보내짐
}
@Override //어노테이션
public boolean shoot(Goalkeeper goalkeeper) {
int shoot1 = random.nextInt(11); // 0부터 10미만
int shoot2 = random.nextInt(11); // 0부터 10미만

if(goalkeeper.block(shoot1) || goalkeeper.block(shoot2)) { //goalkeeper가 슛을 한번이라도 막았을때 true 반환
goal();
return true; //return시 종료
}
return false; //return시 종료
}
}
Binary file added src/main/java/Goalkeeper.class
Binary file not shown.
26 changes: 26 additions & 0 deletions src/main/java/Goalkeeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Random;

public class Goalkeeper extends Player { //Player 클래스 상속
private int chance;
Random random = new Random();
public Goalkeeper(String name) {
super(name); //부모클래스로 보내짐
this.chance = 3;
}

public boolean shoot(Goalkeeper goalkeeper) {
return false;
} //추상클래스이지만 골키퍼는 슛을 차지 않아 false 반환

public boolean block(int shoot) {
int keeperBlock = random.nextInt(5) + 3;
if(shoot >= 3 && shoot <= 7) {
chance --;
return keeperBlock != shoot;
} // 3이상 7이하일때 골키퍼의 block값과 같다면 false 다르다면 true 반환
if(shoot < 2 || shoot > 8 ) {
return false;
} //2보다 작거나 8보다 클때는 무조건 false 출력
return true;
}
}
Binary file added src/main/java/Midfielder.class
Binary file not shown.
20 changes: 20 additions & 0 deletions src/main/java/Midfielder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Random;

public class Midfielder extends Player{ //Player 클래스 상속
Random random = new Random();

public Midfielder(String name) {
super(name); //부모클래스로 보내짐
}

@Override //어노테이션
public boolean shoot(Goalkeeper goalkeeper) {
int shoot = random.nextInt(11); // 0부터 10미만
boolean shootBlock = goalkeeper.block(shoot);
if(shootBlock) {
goal(); //shootBlock이 true라면 골에 값 추가
return true;
}
return false;
}
}
Binary file added src/main/java/Player.class
Binary file not shown.
23 changes: 23 additions & 0 deletions src/main/java/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
abstract public class Player{
private String name;
private int score;

public Player(String name) {
this.name = name;
this.score = 0; //스코어 초기화
}

public String getName() {
return name;
}

public int getScore() {
return score;
}

public abstract boolean shoot(Goalkeeper goalkeeper); //추상클래스

public void goal() {
score ++; //스코어에 값 더함
}
}
Binary file added src/main/java/SoccerGameApplication.class
Binary file not shown.
67 changes: 66 additions & 1 deletion src/main/java/SoccerGameApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class SoccerGameApplication {
static String winner = null;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

List<String> attackers = getPlayers(scanner,"공격수");
List<String> midfielders = getPlayers(scanner,"미드필더");
List<String> goalkeepers = getPlayers(scanner,"골키퍼");
int matchPoint = getMatchPoint(scanner); //getPlayers 메소드로 이름 입력

Attacker attacker1 = new Attacker(attackers.get(0));
Attacker attacker2 = new Attacker(attackers.get(1));
Midfielder midfielder1 = new Midfielder(midfielders.get(0));
Midfielder midfielder2 = new Midfielder(midfielders.get(1));
Goalkeeper goalkeeper = new Goalkeeper(goalkeepers.get(0)); //이름 저장

shootResult(attacker1,attacker2,midfielder1,midfielder2,goalkeeper,matchPoint);
}
}

public static void shootResult(Attacker attacker1,Attacker attacker2,Midfielder midfielder1,Midfielder midfielder2,Goalkeeper goalkeeper, int matchPoint) {
int count = 1;
boolean isPlayingGame = true;

while(isPlayingGame) {
System.out.println(count + "번째 슈팅 결과");
printResult(attacker1,goalkeeper);
printResult(attacker2,goalkeeper);
printResult(midfielder1,goalkeeper);
printResult(midfielder2,goalkeeper);
System.out.println();

isPlayingGame = CheckMatchPoint(attacker1,matchPoint) &&
CheckMatchPoint(attacker2,matchPoint) &&
CheckMatchPoint(midfielder1,matchPoint) &&
CheckMatchPoint(midfielder2,matchPoint);
count ++;
}
} //슈팅 결과 출력

public static void printResult(Player players,Goalkeeper goalkeeper) {
players.shoot(goalkeeper);
System.out.println(players.getName() + ": " + "—".repeat(players.getScore()));

} //이창보: -형태로 값 출력

public static boolean CheckMatchPoint(Player player, int matchPoint) {
if(player.getScore() == matchPoint) {
System.out.println("\n\n승리자는 " + player.getName() + " 입니다.");
return false; //매치포인트값과 골 값을 비교후 승자 출력
}
return true;
}

public static List<String> getPlayers(Scanner scanner,String type) {
System.out.print(type + "의 이름을 입력하세요: ");
List<String> players = Arrays.asList(scanner.nextLine().split(", "));
return players; //이름 스캔 메소드
}

public static int getMatchPoint(Scanner scanner) {
System.out.print("매치포인트를 입력하세요: ");
return scanner.nextInt(); //매치포인트 스캔 매소드
}
}
3 changes: 3 additions & 0 deletions untitled/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions untitled/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions untitled/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions untitled/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions untitled/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Main {
public static void main(String[] args) {





}
}