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

[자동차경주] 박진영 미션 제출합니다. #1472

Open
wants to merge 5 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# java-racingcar-precourse

- [x] 입력 검증기능
- [x] 레이스 턴기능
- [x] 레이스 실행&출력기능
- [x] 선두주자 index 찾는기능
- [x] 선두주자 문자열로 표현해 가져오는기능
96 changes: 95 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,101 @@
package racingcar;

import java.util.ArrayList;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현

// 입력받기
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)");
String[] cars = camp.nextstep.edu.missionutils.Console.readLine().split(",");

System.out.println("시도할 횟수는 몇 회인가요?");
int maxTryCnt = Integer.parseInt(camp.nextstep.edu.missionutils.Console.readLine());
int currentTryCnt = 0;
int[] currentRecord = new int[]{0, 0, 0};

System.out.println();
System.out.println("실행결과");

testValidInput(cars);

while (currentTryCnt < maxTryCnt) {
raceOneTurn(cars, currentRecord);
currentTryCnt++;
System.out.println();
}

int[] maxIndices = findMaxIndices(currentRecord);

ArrayList<String> winners = findWinners(cars, currentRecord);

System.out.println("최종 우승자 : " + String.join(", ", winners));
}

public static void testValidInput(String[] cars) {

for (int i = 0; i < cars.length; i++) {
if (cars[i].length() > 5) {
throw new IllegalArgumentException("입력이올바르지 않습니다");
}
}
}

static void raceOneTurn(String[] cars, int[] currentRecord) {
for (int i = 0; i < cars.length; i++) {
moveAndPrintCar(cars[i], currentRecord, i);
}
}
}

static void moveAndPrintCar(String carName, int[] currentRecord, int index) {
int randNum = camp.nextstep.edu.missionutils.Randoms.pickNumberInRange(0, 9);
if (randNum >= 4) {
currentRecord[index]++;
}
System.out.println(carName + " : " + "-".repeat(currentRecord[index]));
}

public static int[] findMaxIndices(int[] arr) {
if (arr == null || arr.length == 0) {
return new int[0];
}

// 1. 최댓값 찾기
int maxValue = arr[0];
for (int num : arr) {
maxValue = Math.max(maxValue, num);
}

// 2. 최댓값의 개수 세기
int count = 0;
for (int num : arr) {
if (num == maxValue) {
count++;
}
}

// 3. 결과 배열 생성 및 최댓값의 인덱스 저장
int[] maxIndices = new int[count];
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == maxValue) {
maxIndices[index++] = i;
}
}

return maxIndices;
}

private static ArrayList<String> findWinners(String[] cars, int[] currentRecord) {
int[] maxIndices = findMaxIndices(currentRecord);
ArrayList<String> winners = new ArrayList<>();

for (int i = 0; i < maxIndices.length; i++) {
winners.add(cars[maxIndices[i]]);
}

return winners;
}

}
29 changes: 29 additions & 0 deletions src/test/java/racingcar/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package racingcar;

import camp.nextstep.edu.missionutils.test.NsTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest;
import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

class ApplicationTest extends NsTest {
private static final int MOVING_FORWARD = 4;
Expand All @@ -31,6 +33,33 @@ class ApplicationTest extends NsTest {
);
}

@Test
@DisplayName("findMaxIndices 테스트 - 최댓값이 하나인 경우")
void testFindMaxIndicesSingleMax() {
int[] arr = {1, 3, 2};
int[] result = Application.findMaxIndices(arr);

assertArrayEquals(new int[]{1}, result);
}

@Test
@DisplayName("findMaxIndices 테스트 - 최댓값이 여러 개인 경우")
void testFindMaxIndicesMultipleMax() {
int[] arr = {3, 3, 2, 3};
int[] result = Application.findMaxIndices(arr);

assertArrayEquals(new int[]{0, 1, 3}, result);
}

@Test
@DisplayName("findMaxIndices 테스트 - 빈 배열인 경우")
void testFindMaxIndicesEmptyArray() {
int[] arr = {};
int[] result = Application.findMaxIndices(arr);

assertArrayEquals(new int[]{}, result);
}

@Override
public void runMain() {
Application.main(new String[]{});
Expand Down