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

First impl #648

Open
wants to merge 8 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
40 changes: 40 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.HashSet;
import java.util.Set;

public class Car {
private static Set<String> nameSet = new HashSet<>();
private String name;
private int count;


public Car(String name) {
validateNameLength(name);
validateNameDuplication(name);
this.name = name;
this.count = 1;
}

private void validateNameLength(String name) {
if (name.length() > 5) {
throw new IllegalArgumentException("자동차 길이가 혀용 길이를 초과힙니다.");
}
}

private void validateNameDuplication(String name) {
if (!nameSet.add(name)) {
throw new IllegalArgumentException("이름이 중복됩니다.");
}
}

public int incrementCount() {
return this.count++;
}

public String getName() {
return name;
}

public int getCount() {
return count;
}
}
16 changes: 16 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Random;

public class Dice {
Random random = new Random();
private int dice;

public Dice() {
this.dice = random.nextInt(10);
}

public boolean validateMove() {
Dice check = new Dice();
return check.dice > 4;
}

}
67 changes: 67 additions & 0 deletions src/main/java/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class GameController {
private final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
GameView gameView = new GameView();
private List<String> nameList;
private List<Car> carList;
private Dice dice = new Dice();

public void startGame() throws IOException {

gameView.askCarNames();
String names = getCarNames();
nameList = Arrays.asList(names.split(","));

carList = new ArrayList<>();

for (String name : nameList) {
carList.add(new Car(name.trim()));
}

gameView.askTryCounts();
int times = getTryTimes();

gameView.printRacingStart();
for (int i = 0; i < times; i++) {
setProgress();
GameView.showProgress(carList);
System.out.println();
}
List<String> winners = new ArrayList<>(getWinners());
gameView.showWinner(winners);

}

public String getCarNames() throws IOException {
return input.readLine();
}

public int getTryTimes() throws IOException {
String tryTimesStr = input.readLine();
return Integer.parseInt(tryTimesStr);
}

public void setProgress() {
carList.stream()
.filter(car -> dice.validateMove())
.forEach(Car::incrementCount);
}

public List<String> getWinners() {
int maxCount = carList.stream().mapToInt(Car::getCount)
.max()
.orElse(0);
return carList.stream()
.filter(car -> car.getCount() == maxCount)
.map(Car::getName)
.collect(Collectors.toList());
}

}
27 changes: 27 additions & 0 deletions src/main/java/GameView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;

public class GameView {
public void askCarNames(){
System.out.println("경주할 자동차 이름을 입력하세요 (이름은 쉼표(,)를 기준으로 구분).");
}
public void askTryCounts(){
System.out.println("시도할 횟수는 몇회인가요?");
}
public void printRacingStart(){
System.out.println("실행 결과");
}
public static void showProgress(List<Car> carList){
for (Car car : carList) {
String s = car.getName() + " : " + "-".repeat(car.getCount());
System.out.println(s);
}
}
public static void showWinner(List<String> winner){
String s = " ";
System.out.println(winner.stream().collect(Collectors.joining(",")) + "가 최종 우승했습니다.");
}

}
8 changes: 8 additions & 0 deletions src/main/java/RacingMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.io.IOException;

public class RacingMain {
public static void main(String []args) throws IOException {
GameController gameController = new GameController();
gameController.startGame();
}
}